Skip to content

Commit

Permalink
Merge pull request #9866 from jepler/mp3decoder-reopen-glitch
Browse files Browse the repository at this point in the history
  • Loading branch information
jepler authored Dec 5, 2024
2 parents 2bb9fc4 + 5e204c9 commit b481f1f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
12 changes: 12 additions & 0 deletions shared-module/audiomp3/MP3Decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "shared-module/audiomp3/MP3Decoder.h"
#include "supervisor/background_callback.h"
#include "lib/mp3/src/mp3common.h"
#include "lib/mp3/src/coder.h"

#define MAX_BUFFER_LEN (MAX_NSAMP * MAX_NGRAN * MAX_NCHAN * sizeof(int16_t))

Expand Down Expand Up @@ -360,6 +361,17 @@ void common_hal_audiomp3_mp3file_set_file(audiomp3_mp3file_obj_t *self, mp_obj_t
// get_buffer() that I didn't understand.
memset(self->pcm_buffer[0], 0, MAX_BUFFER_LEN);
memset(self->pcm_buffer[1], 0, MAX_BUFFER_LEN);

/* important to do this - DSP primitives assume a bunch of state variables are 0 on first use */
struct _MP3DecInfo *decoder = self->decoder;
memset(decoder->FrameHeaderPS, 0, sizeof(FrameHeader));
memset(decoder->SideInfoPS, 0, sizeof(SideInfo));
memset(decoder->ScaleFactorInfoPS, 0, sizeof(ScaleFactorInfo));
memset(decoder->HuffmanInfoPS, 0, sizeof(HuffmanInfo));
memset(decoder->DequantInfoPS, 0, sizeof(DequantInfo));
memset(decoder->IMDCTInfoPS, 0, sizeof(IMDCTInfo));
memset(decoder->SubbandInfoPS, 0, sizeof(SubbandInfo));

MP3FrameInfo fi;
bool result = mp3file_get_next_frame_info(self, &fi, true);
background_callback_allow();
Expand Down
30 changes: 30 additions & 0 deletions tests/circuitpython/issue9705.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import audiomp3, audiocore
import ulab.numpy as np

TEST_FILE = __file__.rsplit("/", 1)[0] + "/../circuitpython-manual/audiocore/jeplayer-splash-44100-stereo.mp3"

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))**.5

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()

# First frames
decoder = audiomp3.MP3Decoder(TEST_FILE)
print_frame_loudness(decoder, 8)

# First frames (fresh decoder)
decoder = audiomp3.MP3Decoder(TEST_FILE)
print_frame_loudness(decoder, 2)

# First frames (reopen)
decoder.open(TEST_FILE)
print_frame_loudness(decoder, 3)
16 changes: 16 additions & 0 deletions tests/circuitpython/issue9705.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +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

0 1 0
1 1 4730

0 1 0
1 1 4730
2 1 27914

0 comments on commit b481f1f

Please sign in to comment.