From c7aea05b99717a8f287a4a2020fd7d9570961db3 Mon Sep 17 00:00:00 2001 From: "kyle.hu.gz" Date: Tue, 23 Mar 2010 16:13:36 +0000 Subject: [PATCH] Added sample rate and mono/stereo handling for MP3 decoder. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@527 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- bsp/stm32_radio/codec.c | 2 +- bsp/stm32_radio/mp3.c | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/bsp/stm32_radio/codec.c b/bsp/stm32_radio/codec.c index 233c577e97..4e293617c4 100644 --- a/bsp/stm32_radio/codec.c +++ b/bsp/stm32_radio/codec.c @@ -270,7 +270,7 @@ static rt_err_t codec_init(rt_device_t dev) codec_send(REG_BEEP | INVROUT2); // Set output volume. - vol(40); + vol(25); return RT_EOK; } diff --git a/bsp/stm32_radio/mp3.c b/bsp/stm32_radio/mp3.c index ad583389b4..a9ac94fef8 100644 --- a/bsp/stm32_radio/mp3.c +++ b/bsp/stm32_radio/mp3.c @@ -7,6 +7,7 @@ #include "netbuffer.h" #include "player_ui.h" #include "player_bg.h" +#include "codec.h" #define MP3_AUDIO_BUF_SZ (5 * 1024) #ifndef MIN @@ -14,6 +15,7 @@ #endif rt_uint8_t mp3_fd_buffer[MP3_AUDIO_BUF_SZ]; +int current_sample_rate = 0; struct mp3_decoder { @@ -232,15 +234,33 @@ int mp3_decoder_run(struct mp3_decoder* decoder) } else { + int outputSamps; /* no error */ MP3GetLastFrameInfo(decoder->decoder, &decoder->frame_info); /* set sample rate */ + if (decoder->frame_info.samprate != current_sample_rate) + { + current_sample_rate = decoder->frame_info.samprate; + rt_device_control(decoder->snd_device, CODEC_CMD_SAMPLERATE, ¤t_sample_rate); + } /* write to sound device */ - if (decoder->frame_info.outputSamps > 0) + outputSamps = decoder->frame_info.outputSamps; + if (outputSamps > 0) { - rt_device_write(decoder->snd_device, 0, buffer, decoder->frame_info.outputSamps * 2); + if (decoder->frame_info.nChans == 1) + { + int i; + for (i = outputSamps - 1; i >= 0; i--) + { + buffer[i * 2] = buffer[i]; + buffer[i * 2 + 1] = buffer[i]; + } + outputSamps *= 2; + } + + rt_device_write(decoder->snd_device, 0, buffer, outputSamps * sizeof(rt_uint16_t)); } else { -- GitLab