synthio: Add `MidiTrack.error_location` to record errors

.. instead of just printing the unraiseable error on the repl
This commit is contained in:
Jeff Epler 2023-05-03 08:45:37 -05:00
parent d8550f4e7c
commit dd262258e0
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
4 changed files with 26 additions and 1 deletions

View File

@ -151,6 +151,23 @@ MP_DEFINE_CONST_FUN_OBJ_1(synthio_miditrack_get_sample_rate_obj, synthio_miditra
MP_PROPERTY_GETTER(synthio_miditrack_sample_rate_obj,
(mp_obj_t)&synthio_miditrack_get_sample_rate_obj);
//| error_location: Optional[int]
//| """Offset, in bytes within the midi data, of a decoding error"""
//|
STATIC mp_obj_t synthio_miditrack_obj_get_error_location(mp_obj_t self_in) {
synthio_miditrack_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
mp_int_t location = common_hal_synthio_miditrack_get_error_location(self);
if (location >= 0) {
return MP_OBJ_NEW_SMALL_INT(location);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(synthio_miditrack_get_error_location_obj, synthio_miditrack_obj_get_error_location);
MP_PROPERTY_GETTER(synthio_miditrack_error_location_obj,
(mp_obj_t)&synthio_miditrack_get_error_location_obj);
STATIC const mp_rom_map_elem_t synthio_miditrack_locals_dict_table[] = {
// Methods
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&synthio_miditrack_deinit_obj) },
@ -159,6 +176,7 @@ STATIC const mp_rom_map_elem_t synthio_miditrack_locals_dict_table[] = {
// Properties
{ MP_ROM_QSTR(MP_QSTR_sample_rate), MP_ROM_PTR(&synthio_miditrack_sample_rate_obj) },
{ MP_ROM_QSTR(MP_QSTR_error_location), MP_ROM_PTR(&synthio_miditrack_error_location_obj) },
};
STATIC MP_DEFINE_CONST_DICT(synthio_miditrack_locals_dict, synthio_miditrack_locals_dict_table);

View File

@ -39,3 +39,4 @@ bool common_hal_synthio_miditrack_deinited(synthio_miditrack_obj_t *self);
uint32_t common_hal_synthio_miditrack_get_sample_rate(synthio_miditrack_obj_t *self);
uint8_t common_hal_synthio_miditrack_get_bits_per_sample(synthio_miditrack_obj_t *self);
uint8_t common_hal_synthio_miditrack_get_channel_count(synthio_miditrack_obj_t *self);
mp_int_t common_hal_synthio_miditrack_get_error_location(synthio_miditrack_obj_t *self);

View File

@ -29,7 +29,7 @@
STATIC void print_midi_stream_error(synthio_miditrack_obj_t *self) {
mp_cprintf(&mp_plat_print, translate("Error in MIDI stream at position %d"), self->pos);
self->error_location = self->pos;
self->pos = self->track.len;
}
@ -106,6 +106,7 @@ static void decode_until_pause(synthio_miditrack_obj_t *self) {
STATIC void start_parse(synthio_miditrack_obj_t *self) {
self->pos = 0;
self->error_location = -1;
self->synth.span.dur = decode_duration(self);
if (self->synth.span.dur == 0) {
// the usual case: the file starts with some MIDI event, not a delay
@ -135,6 +136,10 @@ bool common_hal_synthio_miditrack_deinited(synthio_miditrack_obj_t *self) {
return synthio_synth_deinited(&self->synth);
}
mp_int_t common_hal_synthio_miditrack_get_error_location(synthio_miditrack_obj_t *self) {
return self->error_location;
}
uint32_t common_hal_synthio_miditrack_get_sample_rate(synthio_miditrack_obj_t *self) {
return self->synth.sample_rate;
}

View File

@ -37,6 +37,7 @@ typedef struct {
mp_buffer_info_t track;
// invariant: after initial startup, pos always points just after an encoded duration, i.e., at a midi message (or at EOF)
size_t pos;
mp_int_t error_location;
uint32_t tempo;
} synthio_miditrack_obj_t;