简体   繁体   中英

How can I intercept MIDI messages?

I'm writing an application to control Novation's launchpad devices (for those familiar). The launchpad installs two MIDI devices : one for input (buttons pressed), and one for output (control LEDs).

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. It is called 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. As I understand it, the midi message goes like this :

launchpad input -> inputDevice's receiver -> inputDevice's transmitter -> my own receiver -> (my computations) -> my own transmitter -> outputDevice's receiver -> outputDevice's transmitter -> launchpad output.

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 :

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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