简体   繁体   English

如何截获MIDI信息?

[英]How can I intercept MIDI messages?

I'm writing an application to control Novation's launchpad devices (for those familiar). 我正在编写一个应用程序来控制Novation的启动板设备(对于那些熟悉的人)。 The launchpad installs two MIDI devices : one for input (buttons pressed), and one for output (control LEDs). 启动板安装了两个MIDI设备:一个用于输入(按下按钮),另一个用于输出(控制LED)。

The idea is that upon receiving a message from the launchpad, I want to send a sequence of other messages. 我的想法是,从启动板收到消息后,我要发送一系列其他消息。 What's the best way to do that ? 最好的方法是什么?

For now, I chain the input device's Transmitter to the output device's Receiver, so that every message received is directly sent back to the launchpad : 现在,我将输入设备的发送器链接到输出设备的接收器,以便将接收到的每条消息直接发送回启动板:

Transmitter lpTransmitter = inputDevice.getTransmitter();
lpTransmitter.setReceiver(outputDevice.getReceiver());

The javax.sound.midi package has an interface that is implemented by code that wants to receive events. javax.sound.midi包具有一个接口,该接口由要接收事件的代码实现。 It is called Receiver . 它称为Receiver

Take care to open the device before getting the transmitter . 在获取变送器之前,请小心打开设备

I managed to find the solution myself. 我设法自己找到了解决方案。 What I did is I chained the devices using my own implementation of Transmitter and Receiver. 我所做的是使用自己的Transmitter和Receiver实现将设备链接在一起。 As I understand it, the midi message goes like this : 据我了解,MIDI消息是这样的:

launchpad input -> inputDevice's receiver -> inputDevice's transmitter -> my own receiver -> (my computations) -> my own transmitter -> outputDevice's receiver -> outputDevice's transmitter -> launchpad output. 启动板输入-> inputDevice的接收器-> inputDevice的发送器->我自己的接收器->(我的计算)->我自己的发送器-> outputDevice的接收器-> outputDevice的发送器-> launchpad输出。

Now bear with me, because I don't know if this is the proper way to do it, but my code goes like this : 现在请耐心等待,因为我不知道这是否是正确的方法 ,但是我的代码如下:

// main
// get launchpad devices, open them

MyMidiDevice myDevice = new MyMidiDevice();

inputDevice.getTransmitter().setReceiver(myDevice);
myDevice.setReceiver(outputDevice.getReceiver());

Code of the MyMidiDevice class : MyMidiDevice类的代码:

public class MyMidiDevice implements Transmitter, Receiver
{

    private Receiver receiver;

    @Override
    public Receiver getReceiver()
    {
        return this.receiver;
    }

    @Override
    public void setReceiver(Receiver receiver)
    {
        this.receiver = receiver;
    }

    @Override
    public void close()
    {
    }

    @Override
    public void send(MidiMessage message, long timeStamp)
    {
        System.out.println(message); // computations
        this.getReceiver().send(message, timeStamp);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM