3

I am building a Java application that programatically generates a MIDI Sequence that is then sent over the LoopBe Internal Midi Port so that I can use Ableton Live instruments for better sound playback quality.

Please correct me if I am wrong. What I need is to generate a Sequence, that will contain Tracks that will contains MidiEvents, that will contain MIDI messages with time information. That I think I got down.

The real problem is how to send it over the LoopBe MIDI Port. For that I supposedly need a Sequencer, but I don't know how I can get one rather than the default one, and I don't want that.

I guess a workaround would be to write the Sequence to a .mid file and then programatically play it back on the LoopBe Port.

So my question is: How can I obtain a non-default Sequencer?

nunos
  • 20,479
  • 50
  • 119
  • 154
  • 1
    Possibly related: http://stackoverflow.com/questions/6038917/how-to-play-a-midi-file-in-a-new-thread-in-java – finnw Jun 11 '12 at 23:00

2 Answers2

2

You need method MidiSystem.getSequencer(boolean). When you call it with false parameter, it gives you unconnected sequencer.

Get Receiver instance from your target MIDI device and set it to sequencer with seq.getTransmitter().setReceiver(rec) call.

Example snippet:

MIDIDevice device = ... // obtain the MIDIDevice instance
Sequencer seq = MidiSystem.getSequencer(false);
Receiver rec = device.getReceiver();
seq.getTransmitter().setReceiver(rec)

For examples on Sequencer use, see tutorial on http://docs.oracle.com/javase/tutorial/sound/MIDI-seq-methods.html

Aries
  • 353
  • 1
  • 10
  • Perfect. On mac one must use an external library such as 'coreMidi4j' to see all available external MIDI devices. – chikitin Mar 18 '19 at 15:35
1

For my own project I use LoopBe1 to send MIDI signals to REAPER. Of course, LoopBe1 should already be installed.

In this example I iterate through the system's MIDI devices for the external MIDI port of LoopBe and then send the note C 10 times.

import javax.sound.midi.*;

public class Main {
    public static void main(String[] args) throws MidiUnavailableException, InvalidMidiDataException, InterruptedException {
        MidiDevice external;

        MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();

        //Iterate through the devices to get the External LoopBe MIDI port

        for (MidiDevice.Info deviceInfo : devices) {

            if(deviceInfo.getName().equals("LoopBe Internal MIDI")){
                if(deviceInfo.getDescription().equals("External MIDI Port")){
                    external = MidiSystem.getMidiDevice(deviceInfo);
                    System.out.println("Device Name : " + deviceInfo.getName());
                    System.out.println("Device Description : " + deviceInfo.getDescription() + "\n");

                    external.open();
                    Receiver receiver = external.getReceiver();
                    ShortMessage message = new ShortMessage();


                    for (int i = 0; i < 10; i++) {
                        // Start playing the note Middle C (60),
                        // moderately loud (velocity = 93).
                        message.setMessage(ShortMessage.NOTE_ON, 0, 60, 93);
                        long timeStamp = -1;
                        receiver.send(message, timeStamp);
                        Thread.sleep(1000);
                    }

                    external.close();
                }
            }
        }
    }
}

For further information about the sending a MIDI signal, refer to this link:

https://docs.oracle.com/javase/tutorial/sound/MIDI-messages.html

I hope this helps!

graves501
  • 41
  • 1
  • 2