问题
I want to send large SysEx messages (which can be several Megabytes) through the ALSA sequencer: Currently I split them into several ALSA events and send each of them in a loop like that:
while (1) {
result = snd_seq_event_output_direct( seq, const_cast<snd_seq_event_t*>(&ev) );
if (result != -EAGAIN && result != -EWOULDBLOCK)
break;
snd_seq_sync_output_queue( seq );
//std::this_thread::sleep_for(sleeptime);
}
This loop ensures that all events get sent from the sending client/port. But as soon as snd_seq_event_output_direct returns -EAGAIN, the input pool of the receiving client gets emptied, throwing unconditionally about 70 events away, not only the rejected packet. As the sending client has no information about the events that got sent to the receiving client, it can't know how many packets got lost and retransmit.
Some people suggest to limit the transmission rate to 31.25 kbit/s defined by the MIDI 1.0 hardware specification. But this is not necessarily a good idea as the MIDI protocol and the transmission speed have practically been split by the support of Ethernet, Bluetooth and USB hardware layers, which are much faster. The MIDI 2.0 specification is explicitly independent of the transmission rate, and suggests to develop/use hardware layers with higher transmission rates.
So my question is: How can I reliably deliver large amounts of MIDI data at least to software clients without artificially throttling the transmission?
I thought of peeking at the input pool of the receiving client, but I don't know how to do that. So far I can find information about the input queue of the receiving port, but I don't know how to deal with it.
回答1:
The ALSA sequencer was designed for sending messages in real time. It is not possible to throttle transmissions based on the receiver's buffer state.
If you do not want to limit the speed to that of MIDI 1.0, then your only choice is to use the ALSA RawMIDI interface instead.
来源:https://stackoverflow.com/questions/60950252/alsa-sequencer-avoid-input-buffer-overrun-with-high-speed-midi