简体   繁体   English

发送多个MIDI信息

[英]Send multiple MIDI messages

I want to be able to send multiple MIDI messages independently. 我希望能够独立发送多个MIDI消息。 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. 假设我希望能够同时演奏10个音符。 Then I would have to create 10 threads? 那我将不得不创建10个线程?

I sent my MIDI messages through javax.sound.midi 我通过javax.sound.midi发送了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: 创建MIDI事件的FIFO:

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. 播放器线程将在发送下一个事件之前休眠time_delta单位。 If time_delta is zero, then just send it right away. 如果time_delta为零,则立即发送。 When this event has been sent, the next is fetched from the FIFO. 发送此事件后,将从FIFO中提取下一个事件。

Sending a bunch of MIDI messages in a loop is "simultaneous" in the sense that the sender will send the notes fast enough. 在某种意义上说,发送方将以足够快的速度发送音符,在循环中发送一串MIDI消息是“同时的”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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