audiomp3: add decoded_samples property · MicroPythonNexus/circuitpython@98b0029 · GitHub
Skip to content

Commit 98b0029

Browse files
committed
audiomp3: add decoded_samples property
In my testing, there is no way to accurately know how far into a MP3 file you're currently playing. You can use monotonic time, but that can have drift versus the audio playback system, which may not be running at exactly the expected sample rate. To allow syncing animation with timestamps in a MP3 file, this presents a new property, decoded_samples, that records the number of audio samples sent out of the decoder. While this may not be a completely accurate time, due to mixer delays, it's much better position that the monotonic clock difference. Implementation is keeping track of this value in the mp3file structure and adding to it whenever data is sent out of the decoder. The property implementation was a copy/paste from current properties in the audiomp3 files.
1 parent 6f8c9de commit 98b0029

4 files changed

Lines changed: 30 additions & 0 deletions

File tree

shared-bindings/audiomp3/MP3Decoder.c

Lines changed: 17 additions & 0 deletions

shared-bindings/audiomp3/MP3Decoder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ void common_hal_audiomp3_mp3file_set_sample_rate(audiomp3_mp3file_obj_t *self, u
4646
uint8_t common_hal_audiomp3_mp3file_get_bits_per_sample(audiomp3_mp3file_obj_t *self);
4747
uint8_t common_hal_audiomp3_mp3file_get_channel_count(audiomp3_mp3file_obj_t *self);
4848
float common_hal_audiomp3_mp3file_get_rms_level(audiomp3_mp3file_obj_t *self);
49+
uint32_t common_hal_audiomp3_mp3file_get_samples_decoded(audiomp3_mp3file_obj_t *self);
4950

5051
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOIO_MP3FILE_H

shared-module/audiomp3/MP3Decoder.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ void common_hal_audiomp3_mp3file_set_file(audiomp3_mp3file_obj_t *self, pyb_file
258258
self->channel_count = fi.nChans;
259259
self->frame_buffer_size = fi.outputSamps * sizeof(int16_t);
260260
self->len = 2 * self->frame_buffer_size;
261+
self->samples_decoded = 0;
261262
}
262263

263264
void common_hal_audiomp3_mp3file_deinit(audiomp3_mp3file_obj_t *self) {
@@ -267,6 +268,7 @@ void common_hal_audiomp3_mp3file_deinit(audiomp3_mp3file_obj_t *self) {
267268
self->buffers[0] = NULL;
268269
self->buffers[1] = NULL;
269270
self->file = NULL;
271+
self->samples_decoded = 0;
270272
}
271273

272274
bool common_hal_audiomp3_mp3file_deinited(audiomp3_mp3file_obj_t *self) {
@@ -327,6 +329,7 @@ audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t *
327329
if (channel == self->other_channel) {
328330
*bufptr = (uint8_t *)(self->buffers[self->other_buffer_index] + channel);
329331
self->other_channel = -1;
332+
self->samples_decoded += *buffer_length / sizeof(int16_t);
330333
return GET_BUFFER_MORE_DATA;
331334
}
332335

@@ -359,6 +362,7 @@ audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t *
359362
self);
360363
}
361364

365+
self->samples_decoded += *buffer_length / sizeof(int16_t);
362366
return GET_BUFFER_MORE_DATA;
363367
}
364368

@@ -384,3 +388,7 @@ float common_hal_audiomp3_mp3file_get_rms_level(audiomp3_mp3file_obj_t *self) {
384388
}
385389
return sqrtf(sumsq) / (self->frame_buffer_size / sizeof(int16_t));
386390
}
391+
392+
uint32_t common_hal_audiomp3_mp3file_get_samples_decoded(audiomp3_mp3file_obj_t *self) {
393+
return self->samples_decoded;
394+
}

shared-module/audiomp3/MP3Decoder.h

Lines changed: 4 additions & 0 deletions

0 commit comments

Comments
 (0)