2012-12-06 96 views
1

我有一些筆記,每個都有一個MIDI編號,持續時間和速度。最簡單的輸出簡單MIDI文件的方法?

我目前使用的合成器演奏出音符:

Synthesizer synthesizer = MidiSystem.getSynthesizer(); 
synthesizer.open(); 

MidiChannel[] channels = synthesizer.getChannels(); 

for(Note n: song) 
{ 
    n.playNote(channels[0]); 
} 

synthesizer.close(); 

Note.playNote()

public void playNote(MidiChannel c) throws InterruptedException 
{ 
    if (type == 'n') 
    c.noteOn(noteNumber, 60); 
    Thread.sleep(getLength()); 
    if (type == 'n') 
    c.noteOff(noteNumber); 
} 

不過,現在我想這個保存到一個MIDI文件。什麼是最簡單的方法來做到這一點?

+2

http://www.automatic-pilot.com/midifile.html 這可能會有幫助。 – diegoperini

回答

2

一種簡單的方法基本上如下:

  • 創建序列對象;
  • 創建一個或多個軌道(sequence.createTrack())
  • 的每一個音符,創建代表在注意到了有關MidiMessages並注意關閉的消息,並將其添加到軌道
  • 調用MidiSystem.write()寫的將序列填充到文件。