sequencer.startRecording() 没有立即开始?Java MIDI

所以我从我的键盘获取 MIDI 数据并开始使用


sequence = new Sequence(Sequence.PPQ, 24);

currentTrack = sequence.createTrack();

sequencer.setSequence(sequence);

sequencer.setTickPosition(0);

sequencer.recordEnable(currentTrack, -1);

sequencer.startRecording();

我使用一个按钮停止录制并将其保存到文件中。当我播放 MIDI 文件时,大约需要 4 秒钟才能开始录制。我什至播放它保存在外部程序中的 midifiles,它似乎在录制开始时增加了大约 4 秒的内容。有谁知道这是为什么或如何修复它,以便当我播放 midifile 时,它会立即开始?


交互式爱情
浏览 117回答 1
1回答

慕码人8056858

这是获取文件并删除第一个音符(事件)的函数。在这种情况下,系统添加了一个代码为 144 的事件,用于 Note_On,代替文件开头的一些控制命令(设置为第一个音符的持续时间的代码 171 告诉我这意味着) . 所以我们删除了第一个事件。保存后即可应用。&nbsp; public void write(Sequence s) {&nbsp; &nbsp; int i=0, j;&nbsp; &nbsp; int max=0;&nbsp; &nbsp; Vector notes=new Vector();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; Sequence s2=MidiPlayer.createSequence();&nbsp; &nbsp; &nbsp; Track tr2=s2.createTrack();&nbsp; &nbsp; &nbsp; long tlen=s.getMicrosecondLength()/s.getTickLength();&nbsp; &nbsp; &nbsp; Track[] tr=s.getTracks();//&nbsp; &nbsp; &nbsp; System.out.println("has tracks "+tr.length);&nbsp; &nbsp; &nbsp; Vector note=new Vector();&nbsp; &nbsp; &nbsp; for(i=0; i<tr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; int l=tr[i].size()-1;//&nbsp; &nbsp; &nbsp; System.out.println(i+" has events "+l);&nbsp; &nbsp; &nbsp; &nbsp; int sum=0, start=0;&nbsp; &nbsp; &nbsp; &nbsp; MidiEvent mi=null;&nbsp; &nbsp; &nbsp; &nbsp; ShortMessage sm=null;&nbsp; &nbsp; &nbsp; &nbsp; byte[] dat=null;&nbsp; &nbsp; &nbsp; &nbsp; int tick=0, tick2=0;&nbsp; &nbsp; &nbsp; &nbsp; for(j=0; j<l; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mi=tr[i].get(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(mi.getMessage().getStatus()==MetaMessage.META) { System.out.println("meta "); continue; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sm=(ShortMessage)mi.getMessage();//&nbsp; &nbsp; &nbsp; System.out.println("sm "+sm.getCommand()+" "+sm.getStatus()+" "+sm.getData1()+" "+sm.getData2());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dat=sm.getMessage();//&nbsp; &nbsp; &nbsp; &nbsp; for(int ik=0; ik<dat.length; ik++) System.out.print(dat[ik]+" ");//&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tick2=(int)mi.getTick();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int key=sm.getData1(), vel=sm.getData2();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(j+" "+(tick2-tick)+" "+tlen+" "+tick2+" "+tick+" ch "+sm.getChannel()+" "+key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(j==0) { tick=tick2; continue; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; notes.add(new int[]{key, vel, max, tick2-tick});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max+=tick2-tick;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tick=tick2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for(i=0; i<notes.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] ii=(int[])notes.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(ii==null) break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShortMessage on = new ShortMessage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; on.setMessage(ShortMessage.NOTE_ON, 0, ii[0], ii[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShortMessage off = new ShortMessage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; off.setMessage(ShortMessage.NOTE_OFF, 0, ii[0], ii[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tr2.add(new MidiEvent(on, ii[2]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tr2.add(new MidiEvent(off, ii[2]+ii[3]));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; String t=""+(System.currentTimeMillis()/1000);&nbsp; &nbsp; &nbsp; &nbsp; MidiPlayer.write(s2, "new-"+t+".mid");&nbsp; &nbsp; }&nbsp; &nbsp; catch(Exception e) { e.printStackTrace(); }&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java