0

I want to be able to send multiple MIDI messages independently. But the problem is that I have to wait until the previous note has ended. Do I have to create a thread for all my voices? Let's say I want to be able to play 10 notes at the same time. Then I would have to create 10 threads?

I sent my MIDI messages through javax.sound.midi

public void playNote(int pitch, int length, int velocity) {

    try {

        msg.setMessage(ShortMessage.NOTE_ON, 0, pitch, velocity);
        rcvr.send(msg, timeStamp);

        Thread.sleep(length);

        msg.setMessage(ShortMessage.NOTE_OFF, 0, pitch, 0);
        rcvr.send(msg, timeStamp);

    } catch (Exception e) {
        e.printStackTrace();
    }

}
TomTom
  • 2,820
  • 4
  • 28
  • 46
  • "A device that sends its MIDI messages to multiple other devices simultaneously does so by having multiple transmitters, each connected to a receiver of a different device" - http://docs.oracle.com/javase/tutorial/sound/overview-MIDI.html – Rachel Gallen Jul 01 '14 at 09:56

1 Answers1

1

You do not need to wait for the note before sending next. Create a FIFO of MIDI events:

public class MidiEvent
    {
    /**Number of time units to wait until this message should be sent.
    */
    public int time_delta;

    /**First status byte.
    */
    public byte byte_0;

    /**Second status byte.
    */
    public byte byte_1;

    /**Third status byte.
    */
    public byte byte_2;
    }

Then add such objects to a queue. The player thread will sleep time_delta units before sending next event. If time_delta is zero, then just send it right away. When this event has been sent, the next is fetched from the FIFO.

Sending a bunch of MIDI messages in a loop is "simultaneous" in the sense that the sender will send the notes fast enough.

user877329
  • 6,717
  • 8
  • 46
  • 88