From dd262258e0889cb1e18de0fa4ef4e631feb53b9e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 3 May 2023 08:45:37 -0500 Subject: [PATCH] synthio: Add `MidiTrack.error_location` to record errors .. instead of just printing the unraiseable error on the repl --- shared-bindings/synthio/MidiTrack.c | 18 ++++++++++++++++++ shared-bindings/synthio/MidiTrack.h | 1 + shared-module/synthio/MidiTrack.c | 7 ++++++- shared-module/synthio/MidiTrack.h | 1 + 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/shared-bindings/synthio/MidiTrack.c b/shared-bindings/synthio/MidiTrack.c index 6916a21689..084bad204c 100644 --- a/shared-bindings/synthio/MidiTrack.c +++ b/shared-bindings/synthio/MidiTrack.c @@ -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); diff --git a/shared-bindings/synthio/MidiTrack.h b/shared-bindings/synthio/MidiTrack.h index 046163c2de..1a76ed36f4 100644 --- a/shared-bindings/synthio/MidiTrack.h +++ b/shared-bindings/synthio/MidiTrack.h @@ -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); diff --git a/shared-module/synthio/MidiTrack.c b/shared-module/synthio/MidiTrack.c index 6a021af85c..c59fa23bef 100644 --- a/shared-module/synthio/MidiTrack.c +++ b/shared-module/synthio/MidiTrack.c @@ -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; } diff --git a/shared-module/synthio/MidiTrack.h b/shared-module/synthio/MidiTrack.h index 549e72fb25..2f843dcc3d 100644 --- a/shared-module/synthio/MidiTrack.h +++ b/shared-module/synthio/MidiTrack.h @@ -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;