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:
@@ -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);
|
||||
|
||||
@@ -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,
|
||||
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user