pw-cat: handle midi EOF better

Return 0 when there are no more events, 1 when we have an event and
< 0 for errors. Use this to correctly push the last set of events on
EOS and then return 0 and stop without an error.
This commit is contained in:
Wim Taymans 2020-02-21 11:19:35 +01:00
parent d8bac82e72
commit 8b874a9514
2 changed files with 9 additions and 4 deletions

View file

@ -124,7 +124,7 @@ static int peek_event(struct midi_file *mf, struct midi_track *tr, struct midi_e
int res; int res;
if (tr_avail(tr) == 0) if (tr_avail(tr) == 0)
return -EIO; return 0;
save = tr->p; save = tr->p;
status = *tr->p; status = *tr->p;
@ -182,7 +182,7 @@ static int peek_event(struct midi_file *mf, struct midi_track *tr, struct midi_e
tr->p = save; tr->p = save;
event->size = size; event->size = size;
return 0; return 1;
} }
int midi_file_add_track(struct midi_file *mf, struct midi_track *track) int midi_file_add_track(struct midi_file *mf, struct midi_track *track)
@ -213,7 +213,7 @@ int midi_file_peek_event(struct midi_file *mf, struct midi_event *event)
found = tr; found = tr;
} }
if (found == NULL) if (found == NULL)
return -EIO; return 0;
return peek_event(mf, found, event); return peek_event(mf, found, event);
} }

View file

@ -962,6 +962,7 @@ static int midi_play(struct data *d, void *src, unsigned int n_frames)
struct spa_pod_builder b; struct spa_pod_builder b;
struct spa_pod_frame f; struct spa_pod_frame f;
uint32_t first_frame, last_frame; uint32_t first_frame, last_frame;
bool have_data = false;
spa_zero(b); spa_zero(b);
spa_pod_builder_init(&b, src, n_frames); spa_pod_builder_init(&b, src, n_frames);
@ -977,8 +978,11 @@ static int midi_play(struct data *d, void *src, unsigned int n_frames)
uint8_t *buf; uint8_t *buf;
res = midi_file_peek_event(&d->midi.mf, &ev); res = midi_file_peek_event(&d->midi.mf, &ev);
if (res < 0) if (res <= 0) {
if (have_data)
break;
return res; return res;
}
if (ev.status == 0xff) if (ev.status == 0xff)
goto next; goto next;
@ -997,6 +1001,7 @@ static int midi_play(struct data *d, void *src, unsigned int n_frames)
buf[0] = ev.status; buf[0] = ev.status;
memcpy(&buf[1], ev.data, ev.size); memcpy(&buf[1], ev.data, ev.size);
} }
have_data = true;
next: next:
midi_file_consume_event(&d->midi.mf, &ev); midi_file_consume_event(&d->midi.mf, &ev);
} }