13

我用 java 设计了自己的合成器,现在我想用 MIDI 键盘连接它。我下面的班级搜索所有具有发射器的 MIDI 设备。它成功地找到了我的 MIDI 键盘。我将自己的接收器添加到每个设备的每个发射器,以便它可以接收所有可能的信息。通过阅读所有帮助文档和 java 文档,我知道 Transmitter 将 MidiEvents 发送到 Receiver,然后使用 send 方法处理它们。所以我编写了自己的内部类来实现 Receiver 并使用 println 语句来检查 send 方法中是否检测到任何东西。然而,什么都没有被捡起。做这么简单的事情似乎没有什么帮助,我查看了每个帮助文件、javadoc 和论坛。我敢肯定,这一定是我以某种方式错过的非常明显的事情。

我的合成器不应该与界面合成器混淆,它不是一个 MIDI 乐器。它使用合成算法并具有播放方法。基本上我只需要让midi键盘发送一个将调用播放方法的事件的注释。

import javax.sound.midi.*;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
public class MidiHandler
{
    //ArrayList of MidiDevices
    private ArrayList<MidiDevice> devices = new ArrayList<MidiDevice>();

    public MidiHandler()
    {
        MidiDevice device;
        MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
        for (int i = 0; i < infos.length; i++) {
            try {
                device = MidiSystem.getMidiDevice(infos[i]);
                //does the device have any transmitters?
                if (device.getTransmitters().size() > 0) {
                    //if it does, add it to the device list
                    System.out.println(infos[i] + ": " + device.getTransmitters().size());
                    devices.add(device);
                }
            } catch (MidiUnavailableException e) {}
        }
        //if any transmitting devices were found
        if(devices.size()>0) {
            //for each device
            for(int i = 0; i<devices.size(); i++) {
                try {
                    //get all transmitters
                    List<Transmitter> transmitters = devices.get(i).getTransmitters();
                    //and for each transmitter
                    for(int j = 0; j<transmitters.size();j++) {
                        //create a new receiver
                        transmitters.get(i).setReceiver(
                            //using my own MidiInputReceiver
                            new MidiInputReceiver(devices.get(i).getDeviceInfo().toString())
                        );
                    }
                    //open each device
                    devices.get(i).open();
                    //if code gets this far without throwing an exception
                    //print a success message
                    System.out.println(devices.get(i).getDeviceInfo()+" Was Opened");
                } catch (MidiUnavailableException e) {}
            }
        }
    }
    //tried to write my own class. I thought the send method handles an MidiEvents sent to it
    public class MidiInputReceiver implements Receiver {
        public String name;
        public MidiInputReceiver(String name) {
            this.name = name;
        }
        public void send(MidiMessage msg, long timeStamp) {
            System.out.println("midi received");
        }
        public void close() {}
    }
}

注意:我已经看过这个:Java MIDI - 从钢琴中获取数据?.

这个: http: //www.jsresources.org/examples/MidiInDump.html

界面 Sequencer 对于我想要的东西看起来也很复杂。

4

2 回答 2

16

我发现 MidiDevice getTransmitters() 似乎返回当前已打开的发射器列表,而不是打开的发射器。我相信打开一个新发射器的方法是通过 getTransmitter() 方法。我已经修改了你的代码来做到这一点:

import javax.sound.midi.*;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
    public class MidiHandler
{

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

            //get all transmitters
            List<Transmitter> transmitters = 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(device.getDeviceInfo().toString())
                );
            }

            Transmitter trans = device.getTransmitter();
            trans.setReceiver(new MidiInputReceiver(device.getDeviceInfo().toString()));

            //open each device
            device.open();
            //if code gets this far without throwing an exception
            //print a success message
            System.out.println(device.getDeviceInfo()+" Was Opened");


        } catch (MidiUnavailableException e) {}
    }


}
//tried to write my own class. I thought the send method handles an MidiEvents sent to it
public class MidiInputReceiver implements Receiver {
    public String name;
    public MidiInputReceiver(String name) {
        this.name = name;
    }
    public void send(MidiMessage msg, long timeStamp) {
        System.out.println("midi received");
    }
    public void close() {}
    }
}

在我的硬件上(我插入了一个简单的 USB MIDI 控制器),代码在创建 MidiHandler 实例后正确打印出“midi received”。

希望这可以帮助!

于 2011-08-28T04:45:37.880 回答
3

我发现你必须在调用 setRecceiver() 之前打开设备,否则接收器的 send() 方法会被你上次运行应用程序时的任何垃圾 MIDI 数据调用。

device.open();

Transmitter trans = device.getTransmitter();    
// set new receiver after opening so that the input buffer will be flushed
trans.setReceiver(new MidiInputReceiver(device.getDeviceInfo().toString()));  
于 2015-03-18T14:15:12.360 回答