mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2026-06-04 03:03:00 -04:00
midi: don't write trailing continuation 0xf0 for SysEx
Because our midi messages already have a size, we don't need the 0xf0 continuation terminator. Also having the terminator optionally requires you to check and strip it if it's there. The easiest algorithm is to check the first byte for start (0xf0) or continuation (0xf7) and the last byte for end (0xf7) and that should be enough to process the messages without having to ever stip the last byte.
This commit is contained in:
parent
b41d117609
commit
350eb9a041
10 changed files with 52 additions and 35 deletions
|
|
@ -76,23 +76,25 @@ F0 byte and end with a F7 byte. Because of the buffer data length limitations,
|
||||||
it might be necessary to split a MIDI1 SysEx message accross multiple
|
it might be necessary to split a MIDI1 SysEx message accross multiple
|
||||||
buffers.
|
buffers.
|
||||||
|
|
||||||
The stategy to implement this is specified in RFC 6295 (RTP Midi) section
|
The stategy to implement this is inspired by RFC 6295 (RTP Midi) section
|
||||||
3.2. Long SysEx messages can be split up into parts by using the following
|
3.2. Long SysEx messages can be split up into parts by using the following
|
||||||
start/end bytes combinations:
|
start/end bytes combinations:
|
||||||
|
|
||||||
|-----------------------------------------------------------|
|
|-----------------------------------------------------------|
|
||||||
| Sublist Position | Head Status Octet | Tail Status Octet |
|
| Sublist Position | Head Status Octet | Tail Status Octet |
|
||||||
|-----------------------------------------------------------|
|
|-----------------------------------------------------------|
|
||||||
| first | 0xF0 | (0xF0) |
|
| first | 0xF0 | |
|
||||||
|-----------------------------------------------------------|
|
|-----------------------------------------------------------|
|
||||||
| middle | 0xF7 | (0xF0) |
|
| middle | 0xF7 | |
|
||||||
|-----------------------------------------------------------|
|
|-----------------------------------------------------------|
|
||||||
| last | 0xF7 | 0xF7 |
|
| last | 0xF7 | 0xF7 |
|
||||||
|-----------------------------------------------------------|
|
|-----------------------------------------------------------|
|
||||||
| cancel | 0xF7 | 0xF4 |
|
| cancel | 0xF7 | 0xF4 |
|
||||||
-----------------------------------------------------------
|
-----------------------------------------------------------
|
||||||
|
|
||||||
The trailing 0xf0 byte can be omitted at the end of continuation packets.
|
Because control packets have a size, there is no need for a tail
|
||||||
|
status byte for incomplete first and middle/continuation packets.
|
||||||
|
They have a regular data byte as the last octet (contrary to RFC 6295).
|
||||||
|
|
||||||
Nodes that require a complete SysEx message must be able to assemble the
|
Nodes that require a complete SysEx message must be able to assemble the
|
||||||
complete message from the parts before processing the message.
|
complete message from the parts before processing the message.
|
||||||
|
|
|
||||||
|
|
@ -1647,6 +1647,10 @@ static void convert_to_event(struct mix_info **mix, uint32_t n_mix, void *midi,
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (size > 1 && data[0] == 0xf7) {
|
||||||
|
data++;
|
||||||
|
size--;
|
||||||
|
}
|
||||||
res = midi_event_write(midi, control->offset, data, size, fix);
|
res = midi_event_write(midi, control->offset, data, size, fix);
|
||||||
}
|
}
|
||||||
if (res < 0)
|
if (res < 0)
|
||||||
|
|
|
||||||
|
|
@ -886,8 +886,6 @@ static int process_write(struct seq_state *state)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (body[0] == 0xf0 || body[0] == 0xf7) {
|
if (body[0] == 0xf0 || body[0] == 0xf7) {
|
||||||
if (body[body_size-1] == 0xf0)
|
|
||||||
body_size -= 1;
|
|
||||||
if (body[0] == 0xf7) {
|
if (body[0] == 0xf7) {
|
||||||
body += 1;
|
body += 1;
|
||||||
body_size -= 1;
|
body_size -= 1;
|
||||||
|
|
|
||||||
|
|
@ -354,6 +354,10 @@ static void midi_to_ffado(struct port *p, float *src, uint32_t n_samples)
|
||||||
if (index < c.offset)
|
if (index < c.offset)
|
||||||
index = SPA_ROUND_UP_N(c.offset, 8);
|
index = SPA_ROUND_UP_N(c.offset, 8);
|
||||||
|
|
||||||
|
if (size > 1 && data[0] == 0xf7) {
|
||||||
|
data++;
|
||||||
|
size--;
|
||||||
|
}
|
||||||
for (j = 0; j < size; j++) {
|
for (j = 0; j < size; j++) {
|
||||||
if (index >= n_samples) {
|
if (index >= n_samples) {
|
||||||
/* keep events that don't fit for the next cycle */
|
/* keep events that don't fit for the next cycle */
|
||||||
|
|
|
||||||
|
|
@ -284,6 +284,10 @@ static void midi_to_jack(struct impl *impl, float *dst, float *src, uint32_t n_s
|
||||||
data = tmp;
|
data = tmp;
|
||||||
size = 3;
|
size = 3;
|
||||||
}
|
}
|
||||||
|
if (size > 1 && data[0] == 0xf7) {
|
||||||
|
data++;
|
||||||
|
size--;
|
||||||
|
}
|
||||||
if ((res = jack.midi_event_write(dst, c.offset, data, size)) < 0)
|
if ((res = jack.midi_event_write(dst, c.offset, data, size)) < 0)
|
||||||
pw_log_warn("midi %p: can't write event: %s", dst,
|
pw_log_warn("midi %p: can't write event: %s", dst,
|
||||||
spa_strerror(res));
|
spa_strerror(res));
|
||||||
|
|
|
||||||
|
|
@ -351,6 +351,10 @@ static void midi_to_netjack2(struct netjack2_peer *peer,
|
||||||
buf->lost_events++;
|
buf->lost_events++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (size > 1 && data[0] == 0xf7) {
|
||||||
|
data++;
|
||||||
|
size--;
|
||||||
|
}
|
||||||
n2j_midi_buffer_write(buf, c.offset, data, size, peer->fix_midi);
|
n2j_midi_buffer_write(buf, c.offset, data, size, peer->fix_midi);
|
||||||
}
|
}
|
||||||
if (buf->write_pos > 0)
|
if (buf->write_pos > 0)
|
||||||
|
|
|
||||||
|
|
@ -270,7 +270,7 @@ static int rtp_midi_receive_midi(struct impl *impl, uint8_t *packet, uint32_t ti
|
||||||
|
|
||||||
while (offs < end) {
|
while (offs < end) {
|
||||||
uint32_t delta;
|
uint32_t delta;
|
||||||
int size;
|
int size, tail_trim = 0;
|
||||||
|
|
||||||
if (first && !hdr.z)
|
if (first && !hdr.z)
|
||||||
delta = 0;
|
delta = 0;
|
||||||
|
|
@ -290,9 +290,11 @@ static int rtp_midi_receive_midi(struct impl *impl, uint8_t *packet, uint32_t ti
|
||||||
packet[offs], size, offs, end);
|
packet[offs], size, offs, end);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
if (packet[offs + size-1] == 0xf0)
|
||||||
|
tail_trim++;
|
||||||
|
|
||||||
spa_pod_builder_control(&b, timestamp, SPA_CONTROL_Midi);
|
spa_pod_builder_control(&b, timestamp, SPA_CONTROL_Midi);
|
||||||
spa_pod_builder_bytes(&b, &packet[offs], size);
|
spa_pod_builder_bytes(&b, &packet[offs], size - tail_trim);
|
||||||
|
|
||||||
offs += size;
|
offs += size;
|
||||||
first = false;
|
first = false;
|
||||||
|
|
@ -372,34 +374,41 @@ unexpected_ssrc:
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int write_event(uint8_t *p, uint32_t buffer_size, uint32_t value, const void *ev, uint32_t size)
|
static int write_event(uint8_t *p, uint32_t buffer_size, uint32_t delta, const uint8_t *ev, uint32_t size)
|
||||||
{
|
{
|
||||||
uint64_t buffer;
|
uint64_t buffer;
|
||||||
uint8_t b;
|
uint8_t b;
|
||||||
unsigned int count = 0;
|
unsigned int count = 0;
|
||||||
|
uint32_t total;
|
||||||
|
|
||||||
if (buffer_size <= size)
|
total = size;
|
||||||
|
if (ev[size-1] != 0xf7)
|
||||||
|
total++;
|
||||||
|
|
||||||
|
if (buffer_size <= total)
|
||||||
return -ENOSPC;
|
return -ENOSPC;
|
||||||
buffer = value & 0x7f;
|
buffer = delta & 0x7f;
|
||||||
while ((value >>= 7)) {
|
while ((delta >>= 7)) {
|
||||||
if (buffer > (UINT64_MAX >> 8))
|
if (buffer > (UINT64_MAX >> 8))
|
||||||
return -ERANGE;
|
return -ERANGE;
|
||||||
buffer <<= 8;
|
buffer <<= 8;
|
||||||
buffer |= ((value & 0x7f) | 0x80);
|
buffer |= ((delta & 0x7f) | 0x80);
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
if (count >= buffer_size)
|
if (count >= buffer_size)
|
||||||
return -ENOSPC;
|
return -ENOSPC;
|
||||||
b = buffer & 0xff;
|
b = buffer & 0xff;
|
||||||
p[count++] = b;
|
p[count++] = b;
|
||||||
buffer >>= 8;
|
buffer >>= 8;
|
||||||
} while (b & 0x80);
|
} while (b & 0x80);
|
||||||
|
|
||||||
if (buffer_size - size < count ||
|
if (buffer_size - total < count ||
|
||||||
count + size > (unsigned int)INT_MAX)
|
count + total > (unsigned int)INT_MAX)
|
||||||
return -ENOSPC;
|
return -ENOSPC;
|
||||||
memcpy(&p[count], ev, size);
|
memcpy(&p[count], ev, size);
|
||||||
return (int)(count + size);
|
if (size < total)
|
||||||
|
p[count+size] = 0xf0;
|
||||||
|
return (int)(count + total);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rtp_midi_flush_packets(struct impl *impl,
|
static void rtp_midi_flush_packets(struct impl *impl,
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ static void vban_midi_process_playback(void *data)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
/* the ringbuffer contains series of sequences, one for each
|
/* the ringbuffer contains series of sequences, one for each
|
||||||
* received packet. This is not share mem so we can use the
|
* received packet. This is not shared mem so we can use the
|
||||||
* iterator. */
|
* iterator. */
|
||||||
SPA_POD_SEQUENCE_FOREACH((struct spa_pod_sequence*)pod, c) {
|
SPA_POD_SEQUENCE_FOREACH((struct spa_pod_sequence*)pod, c) {
|
||||||
#if 0
|
#if 0
|
||||||
|
|
|
||||||
|
|
@ -560,9 +560,6 @@ int midi_file_write_event(struct midi_file *mf, const struct midi_event *event)
|
||||||
ev_size--;
|
ev_size--;
|
||||||
ev_data++;
|
ev_data++;
|
||||||
|
|
||||||
if (ev_data[ev_size-1] == 0xf0)
|
|
||||||
ev_size--;
|
|
||||||
|
|
||||||
CHECK_RES(write_varlen(mf, tr, ev_size));
|
CHECK_RES(write_varlen(mf, tr, ev_size));
|
||||||
|
|
||||||
tr->size += 1;
|
tr->size += 1;
|
||||||
|
|
|
||||||
|
|
@ -1569,11 +1569,6 @@ static int sysex_play(struct data *d, void *dst, uint32_t maxsize, unsigned int
|
||||||
bytes[size] = 0xf7;
|
bytes[size] = 0xf7;
|
||||||
size += 1;
|
size += 1;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (bytes[size-1] != 0xf0) {
|
|
||||||
bytes[size] = 0xf0;
|
|
||||||
size += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (d->sysex.first) {
|
if (d->sysex.first) {
|
||||||
if (bytes[0] != 0xf0) {
|
if (bytes[0] != 0xf0) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue