control: improve UMP to Midi conversiom

Improve the spa_ump_to_midi function so that it can consume multiple UMP
messages and produce multiple midi messages.

Some UMP messages (like program changes) need to be translated into up
to 3 midi messages. Do this byt adding a state to the function and by
making it consume the input bytes, just like the spa_ump_from_midi
function.

Adapt code to this new world. This is a little API break..
This commit is contained in:
Wim Taymans 2025-08-19 17:41:03 +02:00
parent bf10458604
commit e35a8554f8
13 changed files with 307 additions and 228 deletions

View file

@ -880,34 +880,41 @@ static int process_write(struct seq_state *state)
snd_seq_event_t ev;
uint8_t data[MAX_EVENT_SIZE];
int size;
uint64_t st = 0;
if ((size = spa_ump_to_midi((uint32_t *)body, body_size, data, sizeof(data))) <= 0)
continue;
while (body_size > 0) {
if ((size = spa_ump_to_midi((const uint32_t **)&body, &body_size,
data, sizeof(data), &st)) <= 0)
break;
if (first)
snd_seq_ev_clear(&ev);
if (first)
snd_seq_ev_clear(&ev);
if ((size = snd_midi_event_encode(stream->codec, data, size, &ev)) < 0) {
spa_log_warn(state->log, "failed to encode event: %s", snd_strerror(size));
snd_midi_event_reset_encode(stream->codec);
if ((size = snd_midi_event_encode(stream->codec, data, size, &ev)) < 0) {
spa_log_warn(state->log, "failed to encode event: %s",
snd_strerror(size));
snd_midi_event_reset_encode(stream->codec);
first = true;
continue;
}
first = false;
if (ev.type == SND_SEQ_EVENT_NONE)
/* this can happen when the event is not complete yet, like
* a sysex message and we need to encode some more data. */
continue;
snd_seq_ev_set_source(&ev, state->event.addr.port);
snd_seq_ev_set_dest(&ev, port->addr.client, port->addr.port);
snd_seq_ev_schedule_real(&ev, state->event.queue_id, 0, &out_rt);
debug_event(state, "send", &ev);
if ((err = snd_seq_event_output(state->event.hndl, &ev)) < 0) {
spa_log_warn(state->log, "failed to output event: %s",
snd_strerror(err));
}
first = true;
continue;
}
first = false;
if (ev.type == SND_SEQ_EVENT_NONE)
/* this can happen when the event is not complete yet, like
* a sysex message and we need to encode some more data. */
continue;
snd_seq_ev_set_source(&ev, state->event.addr.port);
snd_seq_ev_set_dest(&ev, port->addr.client, port->addr.port);
snd_seq_ev_schedule_real(&ev, state->event.queue_id, 0, &out_rt);
if ((err = snd_seq_event_output(state->event.hndl, &ev)) < 0) {
spa_log_warn(state->log, "failed to output event: %s",
snd_strerror(err));
}
first = true;
}
}
}

View file

@ -1824,18 +1824,20 @@ static int apply_props(struct impl *this, const struct spa_pod *param)
static int apply_midi(struct impl *this, const struct spa_pod *value)
{
struct props *p = &this->props;
uint8_t data[8];
int size;
uint8_t ev[8];
int ev_size;
const uint32_t *body = SPA_POD_BODY_CONST(value);
size_t size = SPA_POD_BODY_SIZE(value);
uint64_t state = 0;
size = spa_ump_to_midi(SPA_POD_BODY(value), SPA_POD_BODY_SIZE(value),
data, sizeof(data));
if (size < 3)
ev_size = spa_ump_to_midi(&body, &size, ev, sizeof(ev), &state);
if (ev_size < 3)
return -EINVAL;
if ((data[0] & 0xf0) != 0xb0 || data[1] != 7)
if ((ev[0] & 0xf0) != 0xb0 || ev[1] != 7)
return 0;
p->volume = data[2] / 127.0f;
p->volume = ev[2] / 127.0f;
set_volume(this);
return 1;
}

View file

@ -788,30 +788,35 @@ static int write_data(struct impl *this, struct spa_data *d)
while (spa_pod_parser_get_control_body(&parser, &c, &c_body) >= 0) {
int size;
uint8_t event[32];
const uint32_t *ump = c_body;
size_t ump_size = c.value.size;
uint64_t state = 0;
if (c.type != SPA_CONTROL_UMP)
continue;
time = SPA_MAX(time, this->current_time + c.offset * SPA_NSEC_PER_SEC / this->rate);
size = spa_ump_to_midi(c_body, c.value.size, event, sizeof(event));
if (size <= 0)
continue;
while (ump_size > 0) {
size = spa_ump_to_midi(&ump, &ump_size, event, sizeof(event), &state);
if (size <= 0)
break;
spa_log_trace(this->log, "%p: output event:0x%x time:%"PRIu64, this,
(size > 0) ? event[0] : 0, time);
spa_log_trace(this->log, "%p: output event:0x%x time:%"PRIu64, this,
(size > 0) ? event[0] : 0, time);
do {
res = spa_bt_midi_writer_write(&this->writer,
time, event, size);
if (res < 0) {
return res;
} else if (res) {
int res2;
if ((res2 = flush_packet(this)) < 0)
return res2;
}
} while (res);
do {
res = spa_bt_midi_writer_write(&this->writer,
time, event, size);
if (res < 0) {
return res;
} else if (res) {
int res2;
if ((res2 = flush_packet(this)) < 0)
return res2;
}
} while (res);
}
}
if ((res = flush_packet(this)) < 0)

View file

@ -826,11 +826,15 @@ static int impl_node_process(void *object)
case SPA_CONTROL_UMP:
{
uint8_t ev[8];
int ev_size = spa_ump_to_midi((uint32_t*)body, size, ev, sizeof(ev));
if (ev_size <= 0)
break;
spa_pod_builder_control(&builder, control->offset, SPA_CONTROL_Midi);
spa_pod_builder_bytes(&builder, ev, ev_size);
const uint32_t *ump = (const uint32_t*)body;
uint64_t state = 0;
while (size > 0) {
int ev_size = spa_ump_to_midi(&ump, &size, ev, sizeof(ev), &state);
if (ev_size <= 0)
break;
spa_pod_builder_control(&builder, control->offset, SPA_CONTROL_Midi);
spa_pod_builder_bytes(&builder, ev, ev_size);
}
break;
}
}