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
This commit is contained in:
kyle.hu.gz 2010-03-23 16:13:36 +00:00
parent f5f4c7e97d
commit c7aea05b99
2 changed files with 23 additions and 3 deletions

View File

@ -270,7 +270,7 @@ static rt_err_t codec_init(rt_device_t dev)
codec_send(REG_BEEP | INVROUT2); codec_send(REG_BEEP | INVROUT2);
// Set output volume. // Set output volume.
vol(40); vol(25);
return RT_EOK; return RT_EOK;
} }

View File

@ -7,6 +7,7 @@
#include "netbuffer.h" #include "netbuffer.h"
#include "player_ui.h" #include "player_ui.h"
#include "player_bg.h" #include "player_bg.h"
#include "codec.h"
#define MP3_AUDIO_BUF_SZ (5 * 1024) #define MP3_AUDIO_BUF_SZ (5 * 1024)
#ifndef MIN #ifndef MIN
@ -14,6 +15,7 @@
#endif #endif
rt_uint8_t mp3_fd_buffer[MP3_AUDIO_BUF_SZ]; rt_uint8_t mp3_fd_buffer[MP3_AUDIO_BUF_SZ];
int current_sample_rate = 0;
struct mp3_decoder struct mp3_decoder
{ {
@ -232,15 +234,33 @@ int mp3_decoder_run(struct mp3_decoder* decoder)
} }
else else
{ {
int outputSamps;
/* no error */ /* no error */
MP3GetLastFrameInfo(decoder->decoder, &decoder->frame_info); MP3GetLastFrameInfo(decoder->decoder, &decoder->frame_info);
/* set sample rate */ /* 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, &current_sample_rate);
}
/* write to sound device */ /* 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 else
{ {