mp3_demo: Fix support for mono mp3s by performing an in-place conversion from mono to stereo to match the format expected by es8311. Closes https://github.com/espressif/esp-box/pull/22

This commit is contained in:
Chris Morgan
2022-04-27 22:13:22 +08:00
committed by TDA2030
parent c46079095e
commit c29f8a222a
5 changed files with 33 additions and 4 deletions
+32 -3
View File
@@ -235,6 +235,36 @@ static esp_err_t aplay_mp3(const char *path)
/* Get MP3 frame info and configure I2S clock */
MP3GetLastFrameInfo(mp3_decoder, &frame_info);
// if mono, convert to stereo as es8311 requires stereo input
// even though it is mono output
if(frame_info.nChans == 1) {
size_t new_output_sample_count = frame_info.outputSamps * 2;
// convert from back to front to allow conversion in-place
int16_t *out = (int16_t*)output + new_output_sample_count;
int16_t *in = (int16_t*)output + frame_info.outputSamps;
size_t samples = frame_info.outputSamps;
while(samples) {
// write right channel
*out = *in;
out--;
// write left channel
*out = *in;
out--;
// move input buffer back and decrement samples
in--;
samples--;
}
// adjust channels to 2
frame_info.nChans = 2;
// recalculate output sample count as its calculated by (channels * samples per frame)
frame_info.outputSamps = new_output_sample_count;
}
/* Configure I2S clock if sample rate changed. Always reconfigure at first frame */
if (sample_rate != frame_info.samprate) {
sample_rate = frame_info.samprate;
@@ -289,9 +319,8 @@ static void audio_task(void *pvParam)
/**
* @brief Initialize I2S and audio codec
*
* @note Actually the sampling rate can be reconfigured.
* `MP3GetLastFrameInfo` can fill the `MP3FrameInfo`, which includes `samprate`.
* So theoretically, the sampling rate can be dynamically changed according to the MP3 frame information.
* @note `MP3GetLastFrameInfo` is used to fill the `MP3FrameInfo`, which includes `samprate`,
* and the sampling rate is updated during playback using this value.
*/
bsp_codec_init(AUDIO_HAL_44K_SAMPLES);
bsp_i2s_init(I2S_NUM_0, 44100);
+1 -1
View File
@@ -3,4 +3,4 @@
nvs, data, nvs, 0x9000, 24k
phy_init, data, phy, 0xf000, 4k
factory, app, factory, , 1536k
storage, data, spiffs, , 14M,
storage, data, spiffs, , 7M,
1 # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
3 nvs, data, nvs, 0x9000, 24k
4 phy_init, data, phy, 0xf000, 4k
5 factory, app, factory, , 1536k
6 storage, data, spiffs, , 14M, storage, data, spiffs, , 7M,
Binary file not shown.
Binary file not shown.
Binary file not shown.