使用Jack和Java

时间:2015-04-21 16:47:31

标签: java audio jna midi jack

我正在开发一个相当简单的程序,它从midi设备接收输入并使用javax合成器输出相应的声音。它运行良好,然而,有相当多的延迟。为了避免这种情况,我想使用JNA音频连接套件和我的应用程序使用JNAJack包装器和AudioServers API。但是我现在应该做的事情让我感到非常不知所措...... 这是我的MidiHandler类,它打开设备/接收器/发送器并包含我的MidiInputReciever实现:

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()方法,但我不知道从哪里开始或如何使其工作。 有没有人有过这个主题的经验,可以指出我正确的方向?

0 个答案:

没有答案