summaryrefslogtreecommitdiff
path: root/shared-module/audiomp3/MP3Decoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'shared-module/audiomp3/MP3Decoder.c')
-rw-r--r--shared-module/audiomp3/MP3Decoder.c113
1 files changed, 58 insertions, 55 deletions
diff --git a/shared-module/audiomp3/MP3Decoder.c b/shared-module/audiomp3/MP3Decoder.c
index 5f3f42a51..2b623233e 100644
--- a/shared-module/audiomp3/MP3Decoder.c
+++ b/shared-module/audiomp3/MP3Decoder.c
@@ -51,7 +51,7 @@
*
* Sets self->eof if any read of the file returns 0 bytes
*/
-STATIC bool mp3file_update_inbuf_always(audiomp3_mp3file_obj_t* self) {
+STATIC bool mp3file_update_inbuf_always(audiomp3_mp3file_obj_t *self) {
// If we didn't previously reach the end of file, we can try reading now
if (!self->eof) {
@@ -90,7 +90,7 @@ STATIC bool mp3file_update_inbuf_always(audiomp3_mp3file_obj_t* self) {
* This variant is introduced so that at the site of the
* add_background_callback_core call, the prototype matches.
*/
-STATIC void mp3file_update_inbuf_cb(void* self) {
+STATIC void mp3file_update_inbuf_cb(void *self) {
mp3file_update_inbuf_always(self);
}
@@ -98,9 +98,11 @@ STATIC void mp3file_update_inbuf_cb(void* self) {
*
* Returns the same as mp3file_update_inbuf_always.
*/
-STATIC bool mp3file_update_inbuf_half(audiomp3_mp3file_obj_t* self) {
+STATIC bool mp3file_update_inbuf_half(audiomp3_mp3file_obj_t *self) {
// If buffer is over half full, do nothing
- if (self->inbuf_offset < self->inbuf_length/2) return true;
+ if (self->inbuf_offset < self->inbuf_length / 2) {
+ return true;
+ }
return mp3file_update_inbuf_always(self);
}
@@ -111,23 +113,23 @@ STATIC bool mp3file_update_inbuf_half(audiomp3_mp3file_obj_t* self) {
// http://id3.org/d3v2.3.0
// http://id3.org/id3v2.3.0
-STATIC void mp3file_skip_id3v2(audiomp3_mp3file_obj_t* self) {
+STATIC void mp3file_skip_id3v2(audiomp3_mp3file_obj_t *self) {
mp3file_update_inbuf_half(self);
if (BYTES_LEFT(self) < 10) {
return;
}
uint8_t *data = READ_PTR(self);
if (!(
- data[0] == 'I' &&
- data[1] == 'D' &&
- data[2] == '3' &&
- data[3] != 0xff &&
- data[4] != 0xff &&
- (data[5] & 0x1f) == 0 &&
- (data[6] & 0x80) == 0 &&
- (data[7] & 0x80) == 0 &&
- (data[8] & 0x80) == 0 &&
- (data[9] & 0x80) == 0)) {
+ data[0] == 'I' &&
+ data[1] == 'D' &&
+ data[2] == '3' &&
+ data[3] != 0xff &&
+ data[4] != 0xff &&
+ (data[5] & 0x1f) == 0 &&
+ (data[6] & 0x80) == 0 &&
+ (data[7] & 0x80) == 0 &&
+ (data[8] & 0x80) == 0 &&
+ (data[9] & 0x80) == 0)) {
return;
}
uint32_t size = (data[6] << 21) | (data[7] << 14) | (data[8] << 7) | (data[9]);
@@ -145,7 +147,7 @@ STATIC void mp3file_skip_id3v2(audiomp3_mp3file_obj_t* self) {
/* If a sync word can be found, advance to it and return true. Otherwise,
* return false.
*/
-STATIC bool mp3file_find_sync_word(audiomp3_mp3file_obj_t* self) {
+STATIC bool mp3file_find_sync_word(audiomp3_mp3file_obj_t *self) {
do {
mp3file_update_inbuf_half(self);
int offset = MP3FindSyncWord(READ_PTR(self), BYTES_LEFT(self));
@@ -159,7 +161,7 @@ STATIC bool mp3file_find_sync_word(audiomp3_mp3file_obj_t* self) {
return false;
}
-STATIC bool mp3file_get_next_frame_info(audiomp3_mp3file_obj_t* self, MP3FrameInfo* fi) {
+STATIC bool mp3file_get_next_frame_info(audiomp3_mp3file_obj_t *self, MP3FrameInfo *fi) {
int err;
do {
err = MP3GetNextFrameInfo(self->decoder, fi, READ_PTR(self));
@@ -172,10 +174,10 @@ STATIC bool mp3file_get_next_frame_info(audiomp3_mp3file_obj_t* self, MP3FrameIn
return err == ERR_MP3_NONE;
}
-void common_hal_audiomp3_mp3file_construct(audiomp3_mp3file_obj_t* self,
- pyb_file_obj_t* file,
- uint8_t *buffer,
- size_t buffer_size) {
+void common_hal_audiomp3_mp3file_construct(audiomp3_mp3file_obj_t *self,
+ pyb_file_obj_t *file,
+ uint8_t *buffer,
+ size_t buffer_size) {
// XXX Adafruit_MP3 uses a 2kB input buffer and two 4kB output buffers.
// for a whopping total of 10kB buffers (+mp3 decoder state and frame buffer)
// At 44kHz, that's 23ms of output audio data.
@@ -192,41 +194,42 @@ void common_hal_audiomp3_mp3file_construct(audiomp3_mp3file_obj_t* self,
if (self->inbuf == NULL) {
common_hal_audiomp3_mp3file_deinit(self);
mp_raise_msg(&mp_type_MemoryError,
- translate("Couldn't allocate input buffer"));
+ translate("Couldn't allocate input buffer"));
}
self->decoder = MP3InitDecoder();
if (self->decoder == NULL) {
common_hal_audiomp3_mp3file_deinit(self);
mp_raise_msg(&mp_type_MemoryError,
- translate("Couldn't allocate decoder"));
+ translate("Couldn't allocate decoder"));
}
if ((intptr_t)buffer & 1) {
- buffer += 1; buffer_size -= 1;
+ buffer += 1;
+ buffer_size -= 1;
}
if (buffer_size >= 2 * MAX_BUFFER_LEN) {
- self->buffers[0] = (int16_t*)(void*)buffer;
- self->buffers[1] = (int16_t*)(void*)(buffer + MAX_BUFFER_LEN);
+ self->buffers[0] = (int16_t *)(void *)buffer;
+ self->buffers[1] = (int16_t *)(void *)(buffer + MAX_BUFFER_LEN);
} else {
self->buffers[0] = m_malloc(MAX_BUFFER_LEN, false);
if (self->buffers[0] == NULL) {
common_hal_audiomp3_mp3file_deinit(self);
mp_raise_msg(&mp_type_MemoryError,
- translate("Couldn't allocate first buffer"));
+ translate("Couldn't allocate first buffer"));
}
self->buffers[1] = m_malloc(MAX_BUFFER_LEN, false);
if (self->buffers[1] == NULL) {
common_hal_audiomp3_mp3file_deinit(self);
mp_raise_msg(&mp_type_MemoryError,
- translate("Couldn't allocate second buffer"));
+ translate("Couldn't allocate second buffer"));
}
}
common_hal_audiomp3_mp3file_set_file(self, file);
}
-void common_hal_audiomp3_mp3file_set_file(audiomp3_mp3file_obj_t* self, pyb_file_obj_t* file) {
+void common_hal_audiomp3_mp3file_set_file(audiomp3_mp3file_obj_t *self, pyb_file_obj_t *file) {
background_callback_begin_critical_section();
self->file = file;
@@ -248,16 +251,16 @@ void common_hal_audiomp3_mp3file_set_file(audiomp3_mp3file_obj_t* self, pyb_file
background_callback_end_critical_section();
if (!result) {
mp_raise_msg(&mp_type_RuntimeError,
- translate("Failed to parse MP3 file"));
+ translate("Failed to parse MP3 file"));
}
self->sample_rate = fi.samprate;
self->channel_count = fi.nChans;
- self->frame_buffer_size = fi.outputSamps*sizeof(int16_t);
+ self->frame_buffer_size = fi.outputSamps * sizeof(int16_t);
self->len = 2 * self->frame_buffer_size;
}
-void common_hal_audiomp3_mp3file_deinit(audiomp3_mp3file_obj_t* self) {
+void common_hal_audiomp3_mp3file_deinit(audiomp3_mp3file_obj_t *self) {
MP3FreeDecoder(self->decoder);
self->decoder = NULL;
self->inbuf = NULL;
@@ -266,34 +269,34 @@ void common_hal_audiomp3_mp3file_deinit(audiomp3_mp3file_obj_t* self) {
self->file = NULL;
}
-bool common_hal_audiomp3_mp3file_deinited(audiomp3_mp3file_obj_t* self) {
+bool common_hal_audiomp3_mp3file_deinited(audiomp3_mp3file_obj_t *self) {
return self->buffers[0] == NULL;
}
-uint32_t common_hal_audiomp3_mp3file_get_sample_rate(audiomp3_mp3file_obj_t* self) {
+uint32_t common_hal_audiomp3_mp3file_get_sample_rate(audiomp3_mp3file_obj_t *self) {
return self->sample_rate;
}
-void common_hal_audiomp3_mp3file_set_sample_rate(audiomp3_mp3file_obj_t* self,
- uint32_t sample_rate) {
+void common_hal_audiomp3_mp3file_set_sample_rate(audiomp3_mp3file_obj_t *self,
+ uint32_t sample_rate) {
self->sample_rate = sample_rate;
}
-uint8_t common_hal_audiomp3_mp3file_get_bits_per_sample(audiomp3_mp3file_obj_t* self) {
+uint8_t common_hal_audiomp3_mp3file_get_bits_per_sample(audiomp3_mp3file_obj_t *self) {
return 16;
}
-uint8_t common_hal_audiomp3_mp3file_get_channel_count(audiomp3_mp3file_obj_t* self) {
+uint8_t common_hal_audiomp3_mp3file_get_channel_count(audiomp3_mp3file_obj_t *self) {
return self->channel_count;
}
-bool audiomp3_mp3file_samples_signed(audiomp3_mp3file_obj_t* self) {
+bool audiomp3_mp3file_samples_signed(audiomp3_mp3file_obj_t *self) {
return true;
}
-void audiomp3_mp3file_reset_buffer(audiomp3_mp3file_obj_t* self,
- bool single_channel,
- uint8_t channel) {
+void audiomp3_mp3file_reset_buffer(audiomp3_mp3file_obj_t *self,
+ bool single_channel,
+ uint8_t channel) {
if (single_channel && channel == 1) {
return;
}
@@ -310,11 +313,11 @@ void audiomp3_mp3file_reset_buffer(audiomp3_mp3file_obj_t* self,
background_callback_end_critical_section();
}
-audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t* self,
- bool single_channel,
- uint8_t channel,
- uint8_t** bufptr,
- uint32_t* buffer_length) {
+audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t *self,
+ bool single_channel,
+ uint8_t channel,
+ uint8_t **bufptr,
+ uint32_t *buffer_length) {
if (!self->inbuf) {
return GET_BUFFER_ERROR;
}
@@ -325,17 +328,17 @@ audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t*
*buffer_length = self->frame_buffer_size;
if (channel == self->other_channel) {
- *bufptr = (uint8_t*)(self->buffers[self->other_buffer_index] + channel);
+ *bufptr = (uint8_t *)(self->buffers[self->other_buffer_index] + channel);
self->other_channel = -1;
return GET_BUFFER_MORE_DATA;
}
self->buffer_index = !self->buffer_index;
- self->other_channel = 1-channel;
+ self->other_channel = 1 - channel;
self->other_buffer_index = self->buffer_index;
int16_t *buffer = (int16_t *)(void *)self->buffers[self->buffer_index];
- *bufptr = (uint8_t*)buffer;
+ *bufptr = (uint8_t *)buffer;
mp3file_skip_id3v2(self);
if (!mp3file_find_sync_word(self)) {
@@ -360,9 +363,9 @@ audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t*
return GET_BUFFER_MORE_DATA;
}
-void audiomp3_mp3file_get_buffer_structure(audiomp3_mp3file_obj_t* self, bool single_channel,
- bool* single_buffer, bool* samples_signed,
- uint32_t* max_buffer_length, uint8_t* spacing) {
+void audiomp3_mp3file_get_buffer_structure(audiomp3_mp3file_obj_t *self, bool single_channel,
+ bool *single_buffer, bool *samples_signed,
+ uint32_t *max_buffer_length, uint8_t *spacing) {
*single_buffer = false;
*samples_signed = true;
*max_buffer_length = self->frame_buffer_size;
@@ -373,11 +376,11 @@ void audiomp3_mp3file_get_buffer_structure(audiomp3_mp3file_obj_t* self, bool si
}
}
-float common_hal_audiomp3_mp3file_get_rms_level(audiomp3_mp3file_obj_t* self) {
+float common_hal_audiomp3_mp3file_get_rms_level(audiomp3_mp3file_obj_t *self) {
float sumsq = 0.f;
// Assumes no DC component to the audio. Is that a safe assumption?
int16_t *buffer = (int16_t *)(void *)self->buffers[self->buffer_index];
- for(size_t i=0; i<self->frame_buffer_size / sizeof(int16_t); i++) {
+ for (size_t i = 0; i < self->frame_buffer_size / sizeof(int16_t); i++) {
sumsq += (float)buffer[i] * buffer[i];
}
return sqrtf(sumsq) / (self->frame_buffer_size / sizeof(int16_t));