2019-11-26 10:29:55 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
|
|
|
|
* Copyright (c) 2019 Jeff Epler for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2020-01-06 14:35:43 -05:00
|
|
|
#include "shared-bindings/audiomp3/MP3Decoder.h"
|
2019-11-26 10:29:55 -05:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2020-01-02 16:23:42 -05:00
|
|
|
#include <math.h>
|
2019-11-26 10:29:55 -05:00
|
|
|
|
|
|
|
#include "py/mperrno.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
2020-01-06 14:35:43 -05:00
|
|
|
#include "shared-module/audiomp3/MP3Decoder.h"
|
2020-05-11 09:51:41 -04:00
|
|
|
#include "supervisor/background_callback.h"
|
2019-11-26 10:29:55 -05:00
|
|
|
#include "lib/mp3/src/mp3common.h"
|
|
|
|
|
2019-12-23 10:36:46 -05:00
|
|
|
#define MAX_BUFFER_LEN (MAX_NSAMP * MAX_NGRAN * MAX_NCHAN * sizeof(int16_t))
|
|
|
|
|
2020-05-11 09:51:41 -04:00
|
|
|
/** Fill the input buffer unconditionally.
|
2019-11-26 10:29:55 -05:00
|
|
|
*
|
|
|
|
* Returns true if the input buffer contains any useful data,
|
|
|
|
* false otherwise. (The input buffer will be padded to the end with
|
|
|
|
* 0 bytes, which do not interfere with MP3 decoding)
|
|
|
|
*
|
|
|
|
* Raises OSError if f_read fails.
|
|
|
|
*
|
|
|
|
* Sets self->eof if any read of the file returns 0 bytes
|
|
|
|
*/
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC bool mp3file_update_inbuf_always(audiomp3_mp3file_obj_t *self) {
|
2019-11-26 10:29:55 -05:00
|
|
|
// If we didn't previously reach the end of file, we can try reading now
|
2022-04-22 14:59:42 -04:00
|
|
|
if (!self->eof && self->inbuf_offset != 0) {
|
2019-11-26 10:29:55 -05:00
|
|
|
|
|
|
|
// Move the unconsumed portion of the buffer to the start
|
|
|
|
uint8_t *end_of_buffer = self->inbuf + self->inbuf_length;
|
|
|
|
uint8_t *new_end_of_data = self->inbuf + self->inbuf_length - self->inbuf_offset;
|
|
|
|
memmove(self->inbuf, self->inbuf + self->inbuf_offset,
|
|
|
|
self->inbuf_length - self->inbuf_offset);
|
|
|
|
self->inbuf_offset = 0;
|
|
|
|
|
|
|
|
UINT to_read = end_of_buffer - new_end_of_data;
|
|
|
|
UINT bytes_read = 0;
|
|
|
|
memset(new_end_of_data, 0, to_read);
|
|
|
|
if (f_read(&self->file->fp, new_end_of_data, to_read, &bytes_read) != FR_OK) {
|
|
|
|
self->eof = true;
|
|
|
|
mp_raise_OSError(MP_EIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes_read == 0) {
|
|
|
|
self->eof = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (to_read != bytes_read) {
|
|
|
|
new_end_of_data += bytes_read;
|
|
|
|
memset(new_end_of_data, 0, end_of_buffer - new_end_of_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true iff there are at least some useful bytes in the buffer
|
|
|
|
return self->inbuf_offset < self->inbuf_length;
|
|
|
|
}
|
|
|
|
|
2020-05-11 09:51:41 -04:00
|
|
|
/** Update the inbuf from a background callback.
|
|
|
|
*
|
|
|
|
* This variant is introduced so that at the site of the
|
|
|
|
* add_background_callback_core call, the prototype matches.
|
|
|
|
*/
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC void mp3file_update_inbuf_cb(void *self) {
|
2020-05-11 09:51:41 -04:00
|
|
|
mp3file_update_inbuf_always(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Fill the input buffer if it is less than half full.
|
|
|
|
*
|
|
|
|
* Returns the same as mp3file_update_inbuf_always.
|
|
|
|
*/
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC bool mp3file_update_inbuf_half(audiomp3_mp3file_obj_t *self) {
|
2020-05-11 09:51:41 -04:00
|
|
|
// If buffer is over half full, do nothing
|
2021-03-15 09:57:36 -04:00
|
|
|
if (self->inbuf_offset < self->inbuf_length / 2) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-11 09:51:41 -04:00
|
|
|
|
|
|
|
return mp3file_update_inbuf_always(self);
|
|
|
|
}
|
|
|
|
|
2019-11-26 10:29:55 -05:00
|
|
|
#define READ_PTR(self) (self->inbuf + self->inbuf_offset)
|
|
|
|
#define BYTES_LEFT(self) (self->inbuf_length - self->inbuf_offset)
|
|
|
|
#define CONSUME(self, n) (self->inbuf_offset += n)
|
|
|
|
|
2019-12-13 15:07:00 -05:00
|
|
|
// http://id3.org/d3v2.3.0
|
|
|
|
// http://id3.org/id3v2.3.0
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC void mp3file_skip_id3v2(audiomp3_mp3file_obj_t *self) {
|
2020-05-11 09:51:41 -04:00
|
|
|
mp3file_update_inbuf_half(self);
|
2019-12-13 15:07:00 -05:00
|
|
|
if (BYTES_LEFT(self) < 10) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint8_t *data = READ_PTR(self);
|
|
|
|
if (!(
|
2021-03-15 09:57:36 -04:00
|
|
|
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)) {
|
2019-12-13 15:07:00 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t size = (data[6] << 21) | (data[7] << 14) | (data[8] << 7) | (data[9]);
|
|
|
|
size += 10; // size excludes the "header" (but not the "extended header")
|
|
|
|
// First, deduct from size whatever is left in buffer
|
|
|
|
uint32_t to_consume = MIN(size, BYTES_LEFT(self));
|
|
|
|
CONSUME(self, to_consume);
|
|
|
|
size -= to_consume;
|
|
|
|
|
|
|
|
// Next, seek in the file after the header
|
|
|
|
f_lseek(&self->file->fp, f_tell(&self->file->fp) + size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-26 10:29:55 -05:00
|
|
|
/* If a sync word can be found, advance to it and return true. Otherwise,
|
|
|
|
* return false.
|
|
|
|
*/
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC bool mp3file_find_sync_word(audiomp3_mp3file_obj_t *self) {
|
2019-11-26 10:29:55 -05:00
|
|
|
do {
|
2020-05-11 09:51:41 -04:00
|
|
|
mp3file_update_inbuf_half(self);
|
2019-11-26 10:29:55 -05:00
|
|
|
int offset = MP3FindSyncWord(READ_PTR(self), BYTES_LEFT(self));
|
|
|
|
if (offset >= 0) {
|
|
|
|
CONSUME(self, offset);
|
2020-05-11 09:51:41 -04:00
|
|
|
mp3file_update_inbuf_half(self);
|
2019-11-26 10:29:55 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
CONSUME(self, MAX(0, BYTES_LEFT(self) - 16));
|
|
|
|
} while (!self->eof);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC bool mp3file_get_next_frame_info(audiomp3_mp3file_obj_t *self, MP3FrameInfo *fi) {
|
2019-12-13 16:20:48 -05:00
|
|
|
int err;
|
|
|
|
do {
|
|
|
|
err = MP3GetNextFrameInfo(self->decoder, fi, READ_PTR(self));
|
|
|
|
if (err == ERR_MP3_NONE) {
|
2019-12-23 10:36:50 -05:00
|
|
|
break;
|
2019-12-13 16:20:48 -05:00
|
|
|
}
|
|
|
|
CONSUME(self, 1);
|
|
|
|
mp3file_find_sync_word(self);
|
|
|
|
} while (!self->eof);
|
2019-11-26 10:29:55 -05:00
|
|
|
return err == ERR_MP3_NONE;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_audiomp3_mp3file_construct(audiomp3_mp3file_obj_t *self,
|
|
|
|
pyb_file_obj_t *file,
|
|
|
|
uint8_t *buffer,
|
|
|
|
size_t buffer_size) {
|
2019-11-26 10:29:55 -05:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// We will choose a slightly different allocation strategy for the output:
|
|
|
|
// Make sure the buffers are sized exactly to match (a multiple of) the
|
|
|
|
// frame size; this is typically 2304 * 2 bytes, so a little bit bigger
|
|
|
|
// than the two 4kB output buffers, except that the alignment allows to
|
|
|
|
// never allocate that extra frame buffer.
|
|
|
|
|
|
|
|
self->inbuf_length = 2048;
|
|
|
|
self->inbuf_offset = self->inbuf_length;
|
2023-08-10 20:06:32 -04:00
|
|
|
self->inbuf = m_malloc(self->inbuf_length);
|
2019-11-26 10:29:55 -05:00
|
|
|
if (self->inbuf == NULL) {
|
|
|
|
common_hal_audiomp3_mp3file_deinit(self);
|
2022-05-13 15:33:43 -04:00
|
|
|
m_malloc_fail(self->inbuf_length);
|
2019-11-26 10:29:55 -05:00
|
|
|
}
|
|
|
|
self->decoder = MP3InitDecoder();
|
|
|
|
if (self->decoder == NULL) {
|
|
|
|
common_hal_audiomp3_mp3file_deinit(self);
|
|
|
|
mp_raise_msg(&mp_type_MemoryError,
|
2023-10-30 04:49:06 -04:00
|
|
|
MP_ERROR_TEXT("Couldn't allocate decoder"));
|
2019-11-26 10:29:55 -05:00
|
|
|
}
|
|
|
|
|
2019-12-12 09:44:15 -05:00
|
|
|
if ((intptr_t)buffer & 1) {
|
2021-03-15 09:57:36 -04:00
|
|
|
buffer += 1;
|
|
|
|
buffer_size -= 1;
|
2019-12-12 09:44:15 -05:00
|
|
|
}
|
2019-12-23 10:36:46 -05:00
|
|
|
if (buffer_size >= 2 * MAX_BUFFER_LEN) {
|
2021-03-15 09:57:36 -04:00
|
|
|
self->buffers[0] = (int16_t *)(void *)buffer;
|
|
|
|
self->buffers[1] = (int16_t *)(void *)(buffer + MAX_BUFFER_LEN);
|
2019-11-26 10:29:55 -05:00
|
|
|
} else {
|
2023-08-10 20:06:32 -04:00
|
|
|
self->buffers[0] = m_malloc(MAX_BUFFER_LEN);
|
2019-11-26 10:29:55 -05:00
|
|
|
if (self->buffers[0] == NULL) {
|
|
|
|
common_hal_audiomp3_mp3file_deinit(self);
|
2022-05-13 15:33:43 -04:00
|
|
|
m_malloc_fail(MAX_BUFFER_LEN);
|
2019-11-26 10:29:55 -05:00
|
|
|
}
|
|
|
|
|
2023-08-10 20:06:32 -04:00
|
|
|
self->buffers[1] = m_malloc(MAX_BUFFER_LEN);
|
2019-11-26 10:29:55 -05:00
|
|
|
if (self->buffers[1] == NULL) {
|
|
|
|
common_hal_audiomp3_mp3file_deinit(self);
|
2022-05-13 15:33:43 -04:00
|
|
|
m_malloc_fail(MAX_BUFFER_LEN);
|
2019-11-26 10:29:55 -05:00
|
|
|
}
|
|
|
|
}
|
2019-12-23 10:36:46 -05:00
|
|
|
|
|
|
|
common_hal_audiomp3_mp3file_set_file(self, file);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_audiomp3_mp3file_set_file(audiomp3_mp3file_obj_t *self, pyb_file_obj_t *file) {
|
2020-05-11 09:51:41 -04:00
|
|
|
background_callback_begin_critical_section();
|
|
|
|
|
2019-12-23 10:36:46 -05:00
|
|
|
self->file = file;
|
|
|
|
f_lseek(&self->file->fp, 0);
|
|
|
|
self->inbuf_offset = self->inbuf_length;
|
|
|
|
self->eof = 0;
|
|
|
|
self->other_channel = -1;
|
2020-05-11 09:51:41 -04:00
|
|
|
mp3file_update_inbuf_half(self);
|
2019-12-23 10:36:46 -05:00
|
|
|
mp3file_find_sync_word(self);
|
2019-12-24 10:43:15 -05:00
|
|
|
// It **SHOULD** not be necessary to do this; the buffer should be filled
|
|
|
|
// with fresh content before it is returned by get_buffer(). The fact that
|
|
|
|
// this is necessary to avoid a glitch at the start of playback of a second
|
|
|
|
// track using the same decoder object means there's still a bug in
|
|
|
|
// get_buffer() that I didn't understand.
|
|
|
|
memset(self->buffers[0], 0, MAX_BUFFER_LEN);
|
|
|
|
memset(self->buffers[1], 0, MAX_BUFFER_LEN);
|
2019-12-23 10:36:46 -05:00
|
|
|
MP3FrameInfo fi;
|
2020-05-11 09:51:41 -04:00
|
|
|
bool result = mp3file_get_next_frame_info(self, &fi);
|
|
|
|
background_callback_end_critical_section();
|
|
|
|
if (!result) {
|
2019-12-23 10:36:46 -05:00
|
|
|
mp_raise_msg(&mp_type_RuntimeError,
|
2023-10-30 04:49:06 -04:00
|
|
|
MP_ERROR_TEXT("Failed to parse MP3 file"));
|
2019-12-23 10:36:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
self->sample_rate = fi.samprate;
|
|
|
|
self->channel_count = fi.nChans;
|
2021-03-15 09:57:36 -04:00
|
|
|
self->frame_buffer_size = fi.outputSamps * sizeof(int16_t);
|
2019-12-23 10:36:46 -05:00
|
|
|
self->len = 2 * self->frame_buffer_size;
|
2022-01-02 00:28:43 -05:00
|
|
|
self->samples_decoded = 0;
|
2019-11-26 10:29:55 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_audiomp3_mp3file_deinit(audiomp3_mp3file_obj_t *self) {
|
2019-11-26 10:29:55 -05:00
|
|
|
MP3FreeDecoder(self->decoder);
|
|
|
|
self->decoder = NULL;
|
|
|
|
self->inbuf = NULL;
|
|
|
|
self->buffers[0] = NULL;
|
|
|
|
self->buffers[1] = NULL;
|
|
|
|
self->file = NULL;
|
2022-01-02 00:28:43 -05:00
|
|
|
self->samples_decoded = 0;
|
2019-11-26 10:29:55 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_audiomp3_mp3file_deinited(audiomp3_mp3file_obj_t *self) {
|
2019-11-26 10:29:55 -05:00
|
|
|
return self->buffers[0] == NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t common_hal_audiomp3_mp3file_get_sample_rate(audiomp3_mp3file_obj_t *self) {
|
2019-11-26 10:29:55 -05:00
|
|
|
return self->sample_rate;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_audiomp3_mp3file_set_sample_rate(audiomp3_mp3file_obj_t *self,
|
|
|
|
uint32_t sample_rate) {
|
2019-11-26 10:29:55 -05:00
|
|
|
self->sample_rate = sample_rate;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t common_hal_audiomp3_mp3file_get_bits_per_sample(audiomp3_mp3file_obj_t *self) {
|
2019-11-26 10:29:55 -05:00
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t common_hal_audiomp3_mp3file_get_channel_count(audiomp3_mp3file_obj_t *self) {
|
2019-11-26 10:29:55 -05:00
|
|
|
return self->channel_count;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void audiomp3_mp3file_reset_buffer(audiomp3_mp3file_obj_t *self,
|
2021-06-30 13:11:54 -04:00
|
|
|
bool single_channel_output,
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t channel) {
|
2021-06-30 13:11:54 -04:00
|
|
|
if (single_channel_output && channel == 1) {
|
2019-11-26 10:29:55 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We don't reset the buffer index in case we're looping and we have an odd number of buffer
|
|
|
|
// loads
|
2020-05-11 09:51:41 -04:00
|
|
|
background_callback_begin_critical_section();
|
2019-11-26 10:29:55 -05:00
|
|
|
f_lseek(&self->file->fp, 0);
|
|
|
|
self->inbuf_offset = self->inbuf_length;
|
|
|
|
self->eof = 0;
|
2022-01-02 16:19:25 -05:00
|
|
|
self->samples_decoded = 0;
|
2019-12-12 09:44:15 -05:00
|
|
|
self->other_channel = -1;
|
2020-05-11 09:51:41 -04:00
|
|
|
mp3file_update_inbuf_half(self);
|
2019-12-13 15:07:00 -05:00
|
|
|
mp3file_skip_id3v2(self);
|
2019-11-26 10:29:55 -05:00
|
|
|
mp3file_find_sync_word(self);
|
2020-05-11 09:51:41 -04:00
|
|
|
background_callback_end_critical_section();
|
2019-11-26 10:29:55 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t *self,
|
2021-06-30 13:11:54 -04:00
|
|
|
bool single_channel_output,
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t channel,
|
|
|
|
uint8_t **bufptr,
|
|
|
|
uint32_t *buffer_length) {
|
2019-12-11 23:04:10 -05:00
|
|
|
if (!self->inbuf) {
|
2021-08-17 11:00:19 -04:00
|
|
|
*buffer_length = 0;
|
2019-12-11 23:04:10 -05:00
|
|
|
return GET_BUFFER_ERROR;
|
|
|
|
}
|
2021-06-30 13:11:54 -04:00
|
|
|
if (!single_channel_output) {
|
2019-11-26 10:29:55 -05:00
|
|
|
channel = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*buffer_length = self->frame_buffer_size;
|
|
|
|
|
2019-12-12 09:44:15 -05:00
|
|
|
if (channel == self->other_channel) {
|
2021-03-15 09:57:36 -04:00
|
|
|
*bufptr = (uint8_t *)(self->buffers[self->other_buffer_index] + channel);
|
2019-12-12 09:44:15 -05:00
|
|
|
self->other_channel = -1;
|
2022-01-02 00:28:43 -05:00
|
|
|
self->samples_decoded += *buffer_length / sizeof(int16_t);
|
2019-12-12 09:44:15 -05:00
|
|
|
return GET_BUFFER_MORE_DATA;
|
|
|
|
}
|
2019-11-26 10:29:55 -05:00
|
|
|
|
2019-12-12 09:44:15 -05:00
|
|
|
|
|
|
|
self->buffer_index = !self->buffer_index;
|
2021-03-15 09:57:36 -04:00
|
|
|
self->other_channel = 1 - channel;
|
2019-12-24 10:43:15 -05:00
|
|
|
self->other_buffer_index = self->buffer_index;
|
2019-12-12 09:44:15 -05:00
|
|
|
int16_t *buffer = (int16_t *)(void *)self->buffers[self->buffer_index];
|
2021-03-15 09:57:36 -04:00
|
|
|
*bufptr = (uint8_t *)buffer;
|
2019-12-12 09:44:15 -05:00
|
|
|
|
2019-12-13 15:07:00 -05:00
|
|
|
mp3file_skip_id3v2(self);
|
2019-12-12 09:44:15 -05:00
|
|
|
if (!mp3file_find_sync_word(self)) {
|
2021-08-17 11:00:19 -04:00
|
|
|
*buffer_length = 0;
|
2019-12-12 09:44:15 -05:00
|
|
|
return self->eof ? GET_BUFFER_DONE : GET_BUFFER_ERROR;
|
|
|
|
}
|
|
|
|
int bytes_left = BYTES_LEFT(self);
|
|
|
|
uint8_t *inbuf = READ_PTR(self);
|
|
|
|
int err = MP3Decode(self->decoder, &inbuf, &bytes_left, buffer, 0);
|
|
|
|
CONSUME(self, BYTES_LEFT(self) - bytes_left);
|
2020-05-11 09:51:41 -04:00
|
|
|
|
2021-08-17 11:00:19 -04:00
|
|
|
if (err) {
|
|
|
|
*buffer_length = 0;
|
|
|
|
return GET_BUFFER_DONE;
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:10:38 -04:00
|
|
|
self->samples_decoded += *buffer_length / sizeof(int16_t);
|
|
|
|
|
|
|
|
mp3file_skip_id3v2(self);
|
|
|
|
int result = mp3file_find_sync_word(self) ? GET_BUFFER_MORE_DATA : GET_BUFFER_DONE;
|
|
|
|
|
2020-05-11 09:51:41 -04:00
|
|
|
if (self->inbuf_offset >= 512) {
|
|
|
|
background_callback_add(
|
|
|
|
&self->inbuf_fill_cb,
|
|
|
|
mp3file_update_inbuf_cb,
|
|
|
|
self);
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:10:38 -04:00
|
|
|
return result;
|
2019-11-26 10:29:55 -05:00
|
|
|
}
|
|
|
|
|
2021-06-30 13:11:54 -04:00
|
|
|
void audiomp3_mp3file_get_buffer_structure(audiomp3_mp3file_obj_t *self, bool single_channel_output,
|
2021-03-15 09:57:36 -04:00
|
|
|
bool *single_buffer, bool *samples_signed,
|
|
|
|
uint32_t *max_buffer_length, uint8_t *spacing) {
|
2019-11-26 10:29:55 -05:00
|
|
|
*single_buffer = false;
|
|
|
|
*samples_signed = true;
|
|
|
|
*max_buffer_length = self->frame_buffer_size;
|
2021-06-30 13:11:54 -04:00
|
|
|
if (single_channel_output) {
|
2019-11-26 10:29:55 -05:00
|
|
|
*spacing = self->channel_count;
|
|
|
|
} else {
|
|
|
|
*spacing = 1;
|
|
|
|
}
|
|
|
|
}
|
2020-01-02 16:23:42 -05:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
float common_hal_audiomp3_mp3file_get_rms_level(audiomp3_mp3file_obj_t *self) {
|
2020-01-02 16:23:42 -05:00
|
|
|
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];
|
2021-03-15 09:57:36 -04:00
|
|
|
for (size_t i = 0; i < self->frame_buffer_size / sizeof(int16_t); i++) {
|
2020-01-02 16:23:42 -05:00
|
|
|
sumsq += (float)buffer[i] * buffer[i];
|
|
|
|
}
|
|
|
|
return sqrtf(sumsq) / (self->frame_buffer_size / sizeof(int16_t));
|
|
|
|
}
|
2022-01-02 00:28:43 -05:00
|
|
|
|
|
|
|
uint32_t common_hal_audiomp3_mp3file_get_samples_decoded(audiomp3_mp3file_obj_t *self) {
|
|
|
|
return self->samples_decoded;
|
|
|
|
}
|