1

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());
Rosh Donniet
  • 418
  • 2
  • 10
  • 1
    "upon receiving a message from the launchpad, send a sequence of other messages." You have correctly described how it works. What exactly is the problem? – CL. May 22 '16 at 18:54
  • Well, how do I know that I have received a message ? Is there some kind of event listener ? – Rosh Donniet May 23 '16 at 06:42

2 Answers2

0

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.

Community
  • 1
  • 1
CL.
  • 173,858
  • 17
  • 217
  • 259
  • I know about Receiver, but how do I use it to receive messages ? Do I create my own class that implements it ? The javadoc tells me I have to define `send()`, but will that method be automatically called if I link a transmitter ? Pardon the ignorance, I'm a beginner in Java – Rosh Donniet May 23 '16 at 07:47
0

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);
    }
}
Rosh Donniet
  • 418
  • 2
  • 10