3

私は、MIDI デバイスから入力を受け取り、javax シンセサイザーを使用して対応するサウンドを出力する、かなり単純なプログラムに取り組んでいます。うまく機能していますが、かなりの遅延があります。これを避けるために、JNAJack ラッパーと AudioServers API を使用して、アプリケーションで JACK Audio Connection Kit を使用したいと考えています。しかし、私は今実際にやるべきことにかなり圧倒されています...これは、デバイス/レシーバー/トランスミッターを開き、MidiInputReciever 実装を含む私の MidiHandler クラスです。

public class MidiHandler {
MidiDevice device;
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
List<Transmitter> transmitters;
MidiInputReceiver reciever;

public MidiHandler() {

    for (int i = 0; i < infos.length; i++) {
        try {
            this.device = MidiSystem.getMidiDevice(this.infos[i]);
            // does the device have any transmitters?
            // if it does, add it to the device list
            System.out.println(this.infos[i]);

            // get all transmitters
            this.transmitters = this.device.getTransmitters();

            // using my own MidiInputReceiver
            this.reciever = new MidiInputReceiver(this.device
                    .getDeviceInfo().toString());
            // and for each transmitter
            for (int j = 0; j < this.transmitters.size(); j++) {
                // create a new receiver
                this.transmitters.get(j).setReceiver(this.reciever);
            }

            Transmitter trans = this.device.getTransmitter();
            trans.setReceiver(new MidiInputReceiver(this.device
                    .getDeviceInfo().toString()));
            this.device.open();
        } catch (MidiUnavailableException e) {
        }
    }

}

public void playNote(byte b) {
    reciever.playNote(b);
}

public void stopNote(byte b) {
    reciever.stopNote(b);
}

public void close() {
    for (int i = 0; i < this.transmitters.size(); ++i) {
        this.transmitters.get(i).close();
    }
    this.reciever.close();
    this.device.close();
}

public String getInfos() {
    String infos = "";
    for (int i = 0; i < this.infos.length; i++) {
        infos += "\n" + this.infos[i] + " ";
    }
    return infos;
}

// tried to write my own class. I thought the send method handles an
// MidiEvents sent to it
public class MidiInputReceiver implements Receiver {
    Synthesizer synth;
    MidiChannel[] mc;
    Instrument[] instr;
    int instrument;
    int channel;

    public MidiInputReceiver(String name) {
        try {
            patcher p = new patcher();
            this.instrument = p.getInstrument();
            this.channel = p.getChannel();
            this.synth = MidiSystem.getSynthesizer();
            this.synth.open();
            this.mc = synth.getChannels();
            instr = synth.getDefaultSoundbank().getInstruments();
            this.synth.loadInstrument(instr[1]);
            mc[this.channel].programChange(0, this.instrument);
        } catch (MidiUnavailableException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    public void send(MidiMessage msg, long timeStamp) {
        /*
         * Use to display midi message
         */
        for (int i = 0; i < msg.getMessage().length; i++) {
            System.out.print("[" + msg.getMessage()[i] + "] ");

        }
        System.out.println();

        if (msg.getMessage()[0] == -112) {
            mc[this.channel].noteOn(msg.getMessage()[1],
                    msg.getMessage()[2] + 1000);
        }

        if (msg.getMessage()[0] == -128) {
            mc[this.channel].noteOff(msg.getMessage()[1],
                    msg.getMessage()[2] + 1000);
        }

    }

    public void playNote(byte b) {
        mc[this.channel].noteOn(b, 1000);
    }

    public void stopNote(byte b) {
        mc[this.channel].noteOff(b);
    }

    public void close() {
    }

}

AudioClient インターフェイスとその中に process() メソッドを実装することになっていることはわかっていますが、どこから始めればよいのか、どのように機能させるのかわかりません。このトピックに関する以前の経験があり、正しい方向に私を向けることができる人はいますか?

4

0 に答える 0