简体   繁体   中英

Send multiple MIDI messages

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

}

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.

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