2

我安装了 snd-virmidi 内核模块,以便在 java 中拥有虚拟 MIDI 端口。多亏了这些 MIDI 端口,java 可以在 Linux 上连接非 java MIDI 软件合成器。

但是,openjdk 需要很长时间才能检测到这些虚拟 MIDI 端口……而且,它检测到一长串虚拟端口,而“aconnect”命令行告诉我只有 4 个虚拟端口……

我在 impro-visor(java 软件)和我当前的开发(一个 MIDI 应用程序)中观察到了这种行为。

我在 Ubuntu 12.04 上使用 openjdk 7。

这是我用来测试不同关键 MIDI 方法的时间响应的代码:

public static void main(String[] args) throws Exception {
    long start = new Date().getTime();
    Sequencer sequencer = MidiSystem.getSequencer(true);
    long end = new Date().getTime();
    System.out.println("getSequencer true end : " + (end - start) + " ms");

    start = new Date().getTime();
    sequencer = MidiSystem.getSequencer(false);
    end = new Date().getTime();
    System.out.println("getSequencer false end : " + (end - start) + " ms");

    start = new Date().getTime();
    List<String> outputDeviceList = new ArrayList<String>();

    // Obtain information about all the installed synthesizers.
    MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();

    // Scan all found devices and check to see what type they are
    MidiDevice device = null;
    for (MidiDevice.Info info : infos) {
        try {
            device = MidiSystem.getMidiDevice(info);
        } catch (MidiUnavailableException e) {
            continue;
        }
        if (device instanceof Synthesizer) {
            outputDeviceList.add(info.getName());
            continue;
        }
        if (!(device instanceof Synthesizer)
                && !(device instanceof Sequencer)) {
            // The device is an instance of a hardware Midi port.
            int numReceivers = device.getMaxReceivers();

            if (numReceivers != 0) {
                outputDeviceList.add(info.getName());
            }
        }
    }
    end = new Date().getTime();
    System.out.println("getAllOutputDevice end : " + (end - start) + " ms");
}

上面的代码已经在带有 openjdk 的 i3 intel 处理器(Ubuntu 12.04)上运行:

java version "1.7.0_15"
OpenJDK Runtime Environment (IcedTea7 2.3.7) (7u15-2.3.7-0ubuntu1~12.04.1)
OpenJDK Server VM (build 23.7-b01, mixed mode)

并给出以下输出:

getSequencer true end : 20811 ms
getSequencer false end : 6945 ms
getAllOutputDevice end : 13933 ms

卸载 snd-virmidi 内核模块后,结果是:

getSequencer true end : 412 ms
getSequencer false end : 5 ms
getAllOutputDevice end : 11 ms

有没有办法加快 java 虚拟 MIDI 端口检测和音序器初始化过程?

4

0 回答 0