From d365e2cc9fc997512a35e037ee6a3d30b46467f4 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 26 Mar 2025 09:24:13 -0500 Subject: [PATCH 1/2] mp3 samples are signed --- shared-module/audiomp3/MP3Decoder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-module/audiomp3/MP3Decoder.c b/shared-module/audiomp3/MP3Decoder.c index 49bd4835e8855..3710b8252164d 100644 --- a/shared-module/audiomp3/MP3Decoder.c +++ b/shared-module/audiomp3/MP3Decoder.c @@ -385,7 +385,7 @@ void common_hal_audiomp3_mp3file_set_file(audiomp3_mp3file_obj_t *self, mp_obj_t self->base.channel_count = fi.nChans; self->base.single_buffer = false; self->base.bits_per_sample = 16; - self->base.samples_signed = false; + self->base.samples_signed = true; self->base.max_buffer_length = fi.outputSamps * sizeof(int16_t); self->len = 2 * self->base.max_buffer_length; self->samples_decoded = 0; From 17497e425f2173cfb317b4ba0bda526446c991f0 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 26 Mar 2025 09:40:07 -0500 Subject: [PATCH 2/2] improve test This test was written in such a way that having a wrong data type for the mp3 samples wasn't detected. Instead of using np.frombuffer(dtype=int16), just do arithmetic directly on the samples. During testing time we don't care if it might be a little slower or use a little more RAM than ulab, and we don't care whether it's actually an RMS calculation. Just that it's consistent and shows the audio data is correct, including its defined data type. --- tests/circuitpython/issue9705.py | 12 +++--------- tests/circuitpython/issue9705.py.exp | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/tests/circuitpython/issue9705.py b/tests/circuitpython/issue9705.py index 7a59ed46f2ccb..0c694cc1b9fb1 100644 --- a/tests/circuitpython/issue9705.py +++ b/tests/circuitpython/issue9705.py @@ -1,5 +1,4 @@ import audiomp3, audiocore -import ulab.numpy as np TEST_FILE = ( __file__.rsplit("/", 1)[0] @@ -7,19 +6,14 @@ ) -def normalized_rms_ulab(values): - values = np.frombuffer(values, dtype=np.int16) - # this function works with ndarrays only - minbuf = np.mean(values) - values = values - minbuf - samples_sum = np.sum(values * values) - return (samples_sum / len(values)) ** 0.5 +def loudness(values): + return sum(abs(a) for a in values) def print_frame_loudness(decoder, n): for i in range(n): result, buf = audiocore.get_buffer(decoder) - print(f"{i} {result} {normalized_rms_ulab(buf):5.0f}") + print(f"{i} {result} {loudness(buf):5.0f}") print() diff --git a/tests/circuitpython/issue9705.py.exp b/tests/circuitpython/issue9705.py.exp index 944fb65c8103f..b57f8e63395c6 100644 --- a/tests/circuitpython/issue9705.py.exp +++ b/tests/circuitpython/issue9705.py.exp @@ -1,16 +1,16 @@ 0 1 0 -1 1 4730 -2 1 27914 -3 1 28737 -4 1 29251 -5 1 29219 -6 1 28672 -7 1 28213 +1 1 25 +2 1 830 +3 1 880 +4 1 932 +5 1 892 +6 1 869 +7 1 839 0 1 0 -1 1 4730 +1 1 25 0 1 0 -1 1 4730 -2 1 27914 +1 1 25 +2 1 830