如何关闭midi设备?

时间:2015-04-15 19:33:16

标签: java midi javax.sound.midi

如何在java中关闭midi设备?我尝试重新初始化MidiHandler对象,但设备保持打开状态直到程序终止。此外,如果我在程序运行时拔下我的midi控制器,即使重新初始化后它也不会在重新连接后发送备注。我必须再次终止并重新启动程序,以使其工作。我对使用midi很新,所以我可能从根本上误解了整个概念。以下是我到目前为止的情况:

public class MidiHandler {
MidiDevice device;
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();

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
            List<Transmitter> transmitters = this.device.getTransmitters();
            // and for each transmitter

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

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

}

public void 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 close() {
    }
}

主要类只是实例化一个MidiHandler并且工作正常,除了上面的问题。

2 个答案:

答案 0 :(得分:1)

你签出了Oracle MIDI-messages吗?

  

关闭连接

     

完成连接后,您可以通过为您获得的每个发送器和接收器调用close方法来释放其资源。每个发送器和接收器接口都有一个紧密的方法。请注意,调用Transmitter.setReceiver并不会关闭发送器的当前接收器。接收器处于打开状态,它仍然可以从与其连接的任何其他发射器接收消息。

     

如果您还完成了这些设备,则可以通过调用MidiDevice.close()同样使其可用于其他应用程序。关闭设备会自动关闭所有发射器和接收器。

答案 1 :(得分:1)

您可以拨打MidiDevice::close()

  

通过调用close()

来完成显式关闭

但是MIDI设备扩展了Autocloseable,因此,根据API,您必须关闭TransmitterReceiver

API

  

设备在最后一个Receiver或Transmitter关闭后关闭。另一方面,直接在设备实例上调用getReceiver或getTransmitter不会隐式打开设备。关闭这些发送器和接收器不会隐式关闭设备。