Hy,我想在Java中实现一个Midi Synthesizer。(合成器 = 代表一个或多个乐器的新设备)
我实现的是一个javax.sound.midi.Receiver:
package at.bachmann.se.midi.smc;
import javax.sound.midi.MetaMessage;
import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.Receiver;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.SysexMessage;
public class SmcReceiver implements Receiver {
public static final int NOTE_ON = 0x90;
public static final int NOTE_OFF = 0x80;
public static final String[] NOTE_NAMES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
@Override
public void close() {
System.out.println();
}
@Override
public void send(MidiMessage message, long timeStamp) {
if(message instanceof ShortMessage) {
ShortMessage sMessage = (ShortMessage) message;
System.out.print("Channel: " + sMessage.getChannel() + " ");
int cmd = sMessage.getCommand();
int key = sMessage.getData1();
int octave = (key / 12)-1;
int note = key % 12;
String noteName = NOTE_NAMES[note];
int velocity = sMessage.getData2();
switch(cmd) {
case NOTE_ON:
System.out.println("Note on, " + noteName + octave + " key=" + key + " velocity: " + velocity);
break;
case NOTE_OFF:
System.out.println("Note off, " + noteName + octave + " key=" + key + " velocity: " + velocity);
break;
}
} else if(message instanceof MetaMessage) {
;//TODO
} else if(message instanceof SysexMessage) {
;//TODO
}
}
}
以下是使用默认值播放 midi 文件的方式:
public class MidiPlayerTest {
@Test
public void playMidiTst() throws InvalidMidiDataException, IOException, MidiUnavailableException {
Sequencer sequencer = MidiSystem.getSequencer();
System.out.println(sequencer.getClass().getName());
sequencer.open();
sequencer.setSequence(MidiSystem.getSequence(new File("tst.midi")));
sequencer.start();
while(true) {
if(sequencer.isRunning()) {
try {
Thread.sleep(200); // Check every second
} catch(InterruptedException ignore) {
break;
}
} else {
break;
}
}
System.out.println("DONE!");
}
}
所以我的问题是如何告诉音序器使用我的接收器?
我尝试/发现的事情:
- 我发现没有办法告诉音序器使用我的接收器。
- 我发现 javax.sound.midi.Synthesizer 使用了接收器,但我发现没有办法将自定义合成器类附加到音序器。
我的一个朋友给我发了我找不到的文档:S:https://docs.oracle.com/javase/tutorial/sound/MIDI-messages.html
所以这是工作代码:
public class MidiPlayerTest {
@Test
public void playMidiTst() throws InvalidMidiDataException, IOException, MidiUnavailableException {
Sequencer sequencer = MidiSystem.getSequencer();
Transmitter transmitter = sequencer.getTransmitter();
transmitter.setReceiver(new SmcReceiver());
System.out.println(sequencer.getClass().getName());
sequencer.open();
sequencer.setSequence(MidiSystem.getSequence(new File("tst.midi")));
sequencer.start();
while(true) {
if(sequencer.isRunning()) {
try {
Thread.sleep(200); // Check every second
} catch(InterruptedException ignore) {
break;
}
} else {
break;
}
}
System.out.println("DONE!");
}
}