如何在 Java 中实现 Midi 合成器

How to implement a Midi Synthesizer in Java

嗨,我想在 Java 中实现一个 Midi 合成器。 (合成器 = 代表一种或多种乐器的新设备)

我实现的是 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!");
    }
}

所以我的问题是如何告诉 Sequencer 使用我的 Receiver?

事情我 tried/found:

我的一个朋友把我找不到的文件发给我 :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!");
    }
}