2009-07-28 07:28:26 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
#include <dfs_posix.h>
|
|
|
|
#include <mp3/pub/mp3dec.h>
|
2009-12-01 23:31:48 +08:00
|
|
|
#include <string.h>
|
2009-08-19 21:14:50 +08:00
|
|
|
|
2009-10-14 23:41:47 +08:00
|
|
|
#include "board.h"
|
|
|
|
#include "netbuffer.h"
|
2009-12-01 23:31:48 +08:00
|
|
|
#include "player_ui.h"
|
|
|
|
#include "player_bg.h"
|
2009-10-14 23:41:47 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
#define MP3_AUDIO_BUF_SZ 4096
|
2009-12-01 23:31:48 +08:00
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(x, y) ((x) < (y)? (x) : (y))
|
|
|
|
#endif
|
2009-08-19 21:14:50 +08:00
|
|
|
|
2009-10-14 23:41:47 +08:00
|
|
|
rt_uint8_t mp3_fd_buffer[MP3_AUDIO_BUF_SZ];
|
2009-08-19 21:14:50 +08:00
|
|
|
|
|
|
|
struct mp3_decoder
|
|
|
|
{
|
|
|
|
/* mp3 information */
|
|
|
|
HMP3Decoder decoder;
|
|
|
|
MP3FrameInfo frame_info;
|
|
|
|
rt_uint32_t frames;
|
|
|
|
|
|
|
|
/* mp3 file descriptor */
|
2009-10-14 23:41:47 +08:00
|
|
|
rt_size_t (*fetch_data)(void* parameter, rt_uint8_t *buffer, rt_size_t length);
|
|
|
|
void* fetch_parameter;
|
2009-08-19 21:14:50 +08:00
|
|
|
|
|
|
|
/* mp3 read session */
|
2009-12-01 23:31:48 +08:00
|
|
|
rt_uint8_t *read_buffer, *read_ptr;
|
|
|
|
rt_int32_t read_offset;
|
2009-08-19 21:14:50 +08:00
|
|
|
rt_uint32_t bytes_left, bytes_left_before_decoding;
|
|
|
|
|
|
|
|
/* audio device */
|
|
|
|
rt_device_t snd_device;
|
|
|
|
};
|
|
|
|
|
|
|
|
static rt_err_t mp3_decoder_tx_done(rt_device_t dev, void *buffer)
|
2009-07-28 07:28:26 +08:00
|
|
|
{
|
2009-10-14 23:41:47 +08:00
|
|
|
/* release memory block */
|
|
|
|
sbuf_release(buffer);
|
2009-08-19 21:14:50 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
2009-07-28 07:28:26 +08:00
|
|
|
}
|
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
void mp3_decoder_init(struct mp3_decoder* decoder)
|
2009-07-28 07:28:26 +08:00
|
|
|
{
|
2009-08-19 21:14:50 +08:00
|
|
|
RT_ASSERT(decoder != RT_NULL);
|
|
|
|
|
|
|
|
/* init read session */
|
|
|
|
decoder->read_ptr = RT_NULL;
|
|
|
|
decoder->bytes_left_before_decoding = decoder->bytes_left = 0;
|
|
|
|
decoder->frames = 0;
|
|
|
|
|
|
|
|
// decoder->read_buffer = rt_malloc(MP3_AUDIO_BUF_SZ);
|
|
|
|
decoder->read_buffer = &mp3_fd_buffer[0];
|
|
|
|
if (decoder->read_buffer == RT_NULL) return;
|
|
|
|
|
|
|
|
decoder->decoder = MP3InitDecoder();
|
|
|
|
|
|
|
|
/* open audio device */
|
|
|
|
decoder->snd_device = rt_device_find("snd");
|
|
|
|
if (decoder->snd_device != RT_NULL)
|
|
|
|
{
|
|
|
|
/* set tx complete call back function */
|
|
|
|
rt_device_set_tx_complete(decoder->snd_device, mp3_decoder_tx_done);
|
|
|
|
rt_device_open(decoder->snd_device, RT_DEVICE_OFLAG_WRONLY);
|
|
|
|
}
|
2009-07-28 07:28:26 +08:00
|
|
|
}
|
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
void mp3_decoder_detach(struct mp3_decoder* decoder)
|
2009-07-28 07:28:26 +08:00
|
|
|
{
|
2009-08-19 21:14:50 +08:00
|
|
|
RT_ASSERT(decoder != RT_NULL);
|
|
|
|
|
|
|
|
/* close audio device */
|
|
|
|
if (decoder->snd_device != RT_NULL)
|
|
|
|
rt_device_close(decoder->snd_device);
|
2009-10-14 23:41:47 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
/* release mp3 decoder */
|
|
|
|
MP3FreeDecoder(decoder->decoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mp3_decoder* mp3_decoder_create()
|
|
|
|
{
|
|
|
|
struct mp3_decoder* decoder;
|
|
|
|
|
|
|
|
/* allocate object */
|
|
|
|
decoder = (struct mp3_decoder*) rt_malloc (sizeof(struct mp3_decoder));
|
|
|
|
if (decoder != RT_NULL)
|
|
|
|
{
|
|
|
|
mp3_decoder_init(decoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
return decoder;
|
2009-07-28 07:28:26 +08:00
|
|
|
}
|
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
void mp3_decoder_delete(struct mp3_decoder* decoder)
|
2009-07-28 07:28:26 +08:00
|
|
|
{
|
2009-08-19 21:14:50 +08:00
|
|
|
RT_ASSERT(decoder != RT_NULL);
|
|
|
|
|
|
|
|
/* de-init mp3 decoder object */
|
|
|
|
mp3_decoder_detach(decoder);
|
|
|
|
/* release this object */
|
|
|
|
rt_free(decoder);
|
2009-07-28 07:28:26 +08:00
|
|
|
}
|
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
rt_uint32_t current_offset = 0;
|
|
|
|
static rt_int32_t mp3_decoder_fill_buffer(struct mp3_decoder* decoder)
|
2009-07-28 07:28:26 +08:00
|
|
|
{
|
2009-08-19 21:14:50 +08:00
|
|
|
rt_size_t bytes_read;
|
|
|
|
rt_size_t bytes_to_read;
|
|
|
|
|
|
|
|
// rt_kprintf("left: %d. refilling inbuffer...\n", decoder->bytes_left);
|
|
|
|
if (decoder->bytes_left > 0)
|
|
|
|
{
|
2009-07-28 07:28:26 +08:00
|
|
|
// better: move unused rest of buffer to the start
|
2009-08-19 21:14:50 +08:00
|
|
|
rt_memmove(decoder->read_buffer, decoder->read_ptr, decoder->bytes_left);
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes_to_read = (MP3_AUDIO_BUF_SZ - decoder->bytes_left) & ~(512 - 1);
|
|
|
|
// rt_kprintf("read bytes: %d\n", bytes_to_read);
|
2009-10-14 23:41:47 +08:00
|
|
|
|
|
|
|
bytes_read = decoder->fetch_data(decoder->fetch_parameter,
|
|
|
|
(rt_uint8_t *)(decoder->read_buffer + decoder->bytes_left),
|
2009-08-19 21:14:50 +08:00
|
|
|
bytes_to_read);
|
|
|
|
|
2009-07-28 07:28:26 +08:00
|
|
|
if (bytes_read == bytes_to_read)
|
|
|
|
{
|
2009-08-19 21:14:50 +08:00
|
|
|
decoder->read_ptr = decoder->read_buffer;
|
|
|
|
decoder->read_offset = 0;
|
|
|
|
decoder->bytes_left = decoder->bytes_left + bytes_to_read;
|
2009-07-28 07:28:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_kprintf("can't read more data");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
int mp3_decoder_run(struct mp3_decoder* decoder)
|
2009-07-28 07:28:26 +08:00
|
|
|
{
|
2009-08-19 21:14:50 +08:00
|
|
|
int err;
|
|
|
|
rt_uint16_t* buffer;
|
2009-12-01 23:31:48 +08:00
|
|
|
rt_uint32_t delta;
|
2009-08-19 21:14:50 +08:00
|
|
|
|
|
|
|
RT_ASSERT(decoder != RT_NULL);
|
|
|
|
|
2009-12-01 23:31:48 +08:00
|
|
|
if (player_is_playing() != RT_TRUE) return -1;
|
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
if ((decoder->read_ptr == RT_NULL) || decoder->bytes_left < 2*MAINBUF_SIZE)
|
2009-07-28 07:28:26 +08:00
|
|
|
{
|
2009-08-19 21:14:50 +08:00
|
|
|
if(mp3_decoder_fill_buffer(decoder) != 0)
|
|
|
|
return -1;
|
2009-07-28 07:28:26 +08:00
|
|
|
}
|
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
// rt_kprintf("read offset: 0x%08x\n", decoder->read_ptr - decoder->read_buffer);
|
|
|
|
decoder->read_offset = MP3FindSyncWord(decoder->read_ptr, decoder->bytes_left);
|
|
|
|
if (decoder->read_offset < 0)
|
2009-07-28 07:28:26 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("Error: MP3FindSyncWord returned <0");
|
2009-08-19 21:14:50 +08:00
|
|
|
|
|
|
|
if(mp3_decoder_fill_buffer(decoder) != 0)
|
|
|
|
return -1;
|
2009-07-28 07:28:26 +08:00
|
|
|
}
|
2009-08-19 21:14:50 +08:00
|
|
|
// rt_kprintf("sync position: %x\n", decoder->read_offset);
|
2009-07-28 07:28:26 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
decoder->read_ptr += decoder->read_offset;
|
2009-12-01 23:31:48 +08:00
|
|
|
delta = decoder->read_offset;
|
2009-08-19 21:14:50 +08:00
|
|
|
decoder->bytes_left -= decoder->read_offset;
|
|
|
|
decoder->bytes_left_before_decoding = decoder->bytes_left;
|
2009-07-28 07:28:26 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
#if 0
|
2009-07-28 07:28:26 +08:00
|
|
|
// check if this is really a valid frame
|
|
|
|
// (the decoder does not seem to calculate CRC, so make some plausibility checks)
|
2009-08-19 21:14:50 +08:00
|
|
|
if (!(MP3GetNextFrameInfo(decoder->decoder, &decoder->frame_info, decoder->read_ptr) == 0 &&
|
|
|
|
decoder->frame_info.nChans == 2 &&
|
|
|
|
decoder->frame_info.version == 0))
|
2009-07-28 07:28:26 +08:00
|
|
|
{
|
2009-08-19 21:14:50 +08:00
|
|
|
rt_kprintf("this is an invalid frame\n");
|
2009-07-28 07:28:26 +08:00
|
|
|
// advance data pointer
|
|
|
|
// TODO: handle bytes_left == 0
|
2009-08-19 21:14:50 +08:00
|
|
|
RT_ASSERT(decoder->bytes_left > 0);
|
|
|
|
|
|
|
|
decoder->bytes_left --;
|
|
|
|
decoder->read_ptr ++;
|
2009-07-28 07:28:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
if (decoder->bytes_left < 1024)
|
|
|
|
{
|
|
|
|
if(mp3_decoder_fill_buffer(decoder) != 0)
|
|
|
|
return -1;
|
2009-07-28 07:28:26 +08:00
|
|
|
}
|
2009-08-19 21:14:50 +08:00
|
|
|
#endif
|
2009-07-28 07:28:26 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
/* get a decoder buffer */
|
2009-10-14 23:41:47 +08:00
|
|
|
buffer = (rt_uint16_t*)sbuf_alloc();
|
2009-07-28 07:28:26 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
// rt_kprintf("bytes left before decode: %d\n", decoder->bytes_left);
|
2009-07-28 07:28:26 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
err = MP3Decode(decoder->decoder, &decoder->read_ptr,
|
|
|
|
(int*)&decoder->bytes_left, (short*)buffer, 0);
|
2009-12-01 23:31:48 +08:00
|
|
|
delta += (decoder->bytes_left_before_decoding - decoder->bytes_left);
|
|
|
|
|
|
|
|
current_offset += delta;
|
|
|
|
player_set_position(current_offset);
|
2009-07-28 07:28:26 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
// rt_kprintf("bytes left after decode: %d\n", decoder->bytes_left);
|
2009-10-14 23:41:47 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
decoder->frames++;
|
|
|
|
|
|
|
|
if (err != ERR_MP3_NONE)
|
2009-07-28 07:28:26 +08:00
|
|
|
{
|
2009-08-19 21:14:50 +08:00
|
|
|
switch (err)
|
2009-07-28 07:28:26 +08:00
|
|
|
{
|
|
|
|
case ERR_MP3_INDATA_UNDERFLOW:
|
2009-08-19 21:14:50 +08:00
|
|
|
rt_kprintf("ERR_MP3_INDATA_UNDERFLOW\n");
|
|
|
|
decoder->bytes_left = 0;
|
|
|
|
if(mp3_decoder_fill_buffer(decoder) != 0)
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ERR_MP3_MAINDATA_UNDERFLOW:
|
|
|
|
/* do nothing - next call to decode will provide more mainData */
|
|
|
|
rt_kprintf("ERR_MP3_MAINDATA_UNDERFLOW\n");
|
2009-07-28 07:28:26 +08:00
|
|
|
break;
|
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
case ERR_MP3_INVALID_FRAMEHEADER:
|
|
|
|
rt_kprintf("ERR_MP3_INVALID_FRAMEHEADER\n");
|
|
|
|
rt_kprintf("current offset: 0x%08x, frames: %d\n", current_offset, decoder->frames);
|
|
|
|
/* dump sector */
|
|
|
|
{
|
|
|
|
rt_uint8_t *ptr;
|
|
|
|
rt_size_t size = 0, col = 0;
|
|
|
|
|
|
|
|
ptr = decoder->read_buffer;
|
|
|
|
rt_kprintf(" 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f\n");
|
|
|
|
rt_kprintf("00 ");
|
|
|
|
while (size ++ < 512)
|
|
|
|
{
|
|
|
|
rt_kprintf("%02x ", *ptr ++);
|
|
|
|
if (size % 16 == 0) rt_kprintf("\n%02x ", ++col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RT_ASSERT(0);
|
|
|
|
// break;
|
|
|
|
|
|
|
|
case ERR_MP3_INVALID_HUFFCODES:
|
|
|
|
rt_kprintf("ERR_MP3_INVALID_HUFFCODES\n");
|
|
|
|
break;
|
2009-07-28 07:28:26 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
default:
|
|
|
|
rt_kprintf("unknown error: %i\n", err);
|
|
|
|
// skip this frame
|
|
|
|
if (decoder->bytes_left > 0)
|
2009-07-28 07:28:26 +08:00
|
|
|
{
|
2009-08-19 21:14:50 +08:00
|
|
|
decoder->bytes_left --;
|
|
|
|
decoder->read_ptr ++;
|
|
|
|
}
|
2009-07-28 07:28:26 +08:00
|
|
|
else
|
|
|
|
{
|
2009-08-19 21:14:50 +08:00
|
|
|
// TODO
|
|
|
|
RT_ASSERT(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-07-28 07:28:26 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
/* release this memory block */
|
2009-10-14 23:41:47 +08:00
|
|
|
sbuf_release(buffer);
|
2009-07-28 07:28:26 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no error */
|
2009-08-19 21:14:50 +08:00
|
|
|
MP3GetLastFrameInfo(decoder->decoder, &decoder->frame_info);
|
2009-07-28 07:28:26 +08:00
|
|
|
|
2009-10-08 21:47:58 +08:00
|
|
|
#ifdef MP3_DECODER_TRACE
|
2009-08-19 21:14:50 +08:00
|
|
|
rt_kprintf("Bitrate: %i\n", decoder->frame_info.bitrate);
|
|
|
|
rt_kprintf("%i samples\n", decoder->frame_info.outputSamps);
|
2009-07-28 07:28:26 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
rt_kprintf("%lu Hz, %i kbps\n", decoder->frame_info.samprate,
|
|
|
|
decoder->frame_info.bitrate/1000);
|
2009-10-08 21:47:58 +08:00
|
|
|
#endif
|
2009-07-28 07:28:26 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
/* set sample rate */
|
|
|
|
|
|
|
|
/* write to sound device */
|
|
|
|
rt_device_write(decoder->snd_device, 0, buffer, decoder->frame_info.outputSamps * 2);
|
2009-07-28 07:28:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-01 23:31:48 +08:00
|
|
|
/* get mp3 information */
|
|
|
|
void mp3_get_info(const char* filename, struct tag_info* info)
|
2009-11-25 07:58:47 +08:00
|
|
|
{
|
2009-12-01 23:31:48 +08:00
|
|
|
int fd;
|
|
|
|
char* id3buffer;
|
|
|
|
rt_size_t bytes_read;
|
|
|
|
int sync_word;
|
|
|
|
HMP3Decoder decoder;
|
|
|
|
MP3FrameInfo frame_info;
|
2009-11-25 07:58:47 +08:00
|
|
|
|
2009-12-01 23:31:48 +08:00
|
|
|
id3buffer = (char*)&mp3_fd_buffer[0];
|
|
|
|
if (filename == RT_NULL || info == RT_NULL) return;
|
2009-11-25 07:58:47 +08:00
|
|
|
|
2009-12-01 23:31:48 +08:00
|
|
|
fd = open(filename, O_RDONLY, 0);
|
|
|
|
if (fd < 0) return; /* can't read file */
|
|
|
|
|
|
|
|
/* init decoder */
|
|
|
|
decoder = MP3InitDecoder();
|
|
|
|
|
|
|
|
/* read data */
|
|
|
|
bytes_read = read(fd, id3buffer, sizeof(mp3_fd_buffer));
|
|
|
|
|
|
|
|
/* get frame information */
|
|
|
|
sync_word = MP3FindSyncWord(id3buffer, bytes_read);
|
|
|
|
if (sync_word < 0)
|
|
|
|
{
|
|
|
|
/* can't get sync word */
|
|
|
|
close(fd);
|
|
|
|
mp3_decoder_detach(decoder);
|
|
|
|
}
|
|
|
|
/* get frame information */
|
|
|
|
MP3GetNextFrameInfo(decoder, &frame_info, &id3buffer[sync_word]);
|
|
|
|
info->bit_rate = frame_info.bitrate;
|
|
|
|
info->sampling = frame_info.samprate;
|
|
|
|
info->duration = lseek(fd, 0, SEEK_END)/ (info->bit_rate / 8); /* second */
|
|
|
|
|
|
|
|
if (strncmp("ID3", id3buffer, 4) == 0)
|
|
|
|
{
|
|
|
|
rt_uint32_t tag_size, frame_size, i;
|
|
|
|
rt_uint8_t version_major;
|
|
|
|
int frame_header_size;
|
|
|
|
|
|
|
|
tag_size = ((rt_uint32_t)id3buffer[6] << 21)|((rt_uint32_t)id3buffer[7] << 14)|((rt_uint16_t)id3buffer[8] << 7)|id3buffer[9];
|
|
|
|
info->data_start = tag_size;
|
|
|
|
version_major = id3buffer[3];
|
|
|
|
if (version_major >= 3)
|
|
|
|
{
|
|
|
|
frame_header_size = 10;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
frame_header_size = 6;
|
|
|
|
}
|
|
|
|
i = 10;
|
|
|
|
|
|
|
|
// iterate through frames
|
|
|
|
while (i < MIN(tag_size, sizeof(id3buffer)))
|
|
|
|
{
|
|
|
|
if (version_major >= 3)
|
|
|
|
{
|
|
|
|
frame_size = ((rt_uint32_t)id3buffer[i + 4] << 24)|((rt_uint32_t)id3buffer[i + 5] << 16)|((rt_uint16_t)id3buffer[i + 6] << 8)|id3buffer[i + 7];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
frame_size = ((rt_uint32_t)id3buffer[i + 3] << 14)|((rt_uint16_t)id3buffer[i + 4] << 7)|id3buffer[i + 5];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp("TT2", id3buffer + i, 3) == 0 || strncmp("TIT2", id3buffer + i, 4) == 0)
|
|
|
|
{
|
|
|
|
strncpy(info->title, id3buffer + i + frame_header_size + 1, MIN(frame_size - 1, sizeof(info->title) - 1));
|
|
|
|
}
|
|
|
|
else if (strncmp("TP1", id3buffer + i, 3) == 0 || strncmp("TPE1", id3buffer + i, 4) == 0)
|
|
|
|
{
|
|
|
|
strncpy(info->artist, id3buffer + i + frame_header_size + 1, MIN(frame_size - 1, sizeof(info->artist) - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
i += frame_size + frame_header_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lseek(fd, -128, SEEK_END);
|
|
|
|
bytes_read = read(fd, id3buffer, 128);
|
|
|
|
|
|
|
|
/* ID3v1 */
|
|
|
|
if (strncmp("TAG", id3buffer, 3) == 0)
|
|
|
|
{
|
|
|
|
strncpy(info->title, id3buffer + 3, MIN(30, sizeof(info->title) - 1));
|
|
|
|
strncpy(info->artist, id3buffer + 3 + 30, MIN(30, sizeof(info->artist) - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set data start position */
|
|
|
|
info->data_start = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set current position */
|
|
|
|
info->position = 0;
|
|
|
|
|
|
|
|
/* release mp3 decoder */
|
|
|
|
MP3FreeDecoder(decoder);
|
|
|
|
|
|
|
|
/* close file */
|
|
|
|
close(fd);
|
2009-11-25 07:58:47 +08:00
|
|
|
}
|
|
|
|
|
2009-07-28 07:28:26 +08:00
|
|
|
#include <finsh.h>
|
2009-10-14 23:41:47 +08:00
|
|
|
rt_size_t fd_fetch(void* parameter, rt_uint8_t *buffer, rt_size_t length)
|
|
|
|
{
|
|
|
|
int fd = (int)parameter;
|
|
|
|
return read(fd, (char*)buffer, length);
|
|
|
|
}
|
2009-07-28 07:28:26 +08:00
|
|
|
void mp3(char* filename)
|
|
|
|
{
|
|
|
|
int fd;
|
2009-08-19 21:14:50 +08:00
|
|
|
struct mp3_decoder* decoder;
|
2009-12-01 23:31:48 +08:00
|
|
|
extern rt_bool_t is_playing;
|
|
|
|
|
|
|
|
is_playing = RT_TRUE;
|
2009-07-28 07:28:26 +08:00
|
|
|
fd = open(filename, O_RDONLY, 0);
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
2009-08-19 21:14:50 +08:00
|
|
|
decoder = mp3_decoder_create();
|
|
|
|
if (decoder != RT_NULL)
|
|
|
|
{
|
2009-10-14 23:41:47 +08:00
|
|
|
decoder->fetch_data = fd_fetch;
|
|
|
|
decoder->fetch_parameter = (void*)fd;
|
|
|
|
|
2009-12-01 23:31:48 +08:00
|
|
|
current_offset = 0;
|
2009-08-19 21:14:50 +08:00
|
|
|
while (mp3_decoder_run(decoder) != -1);
|
|
|
|
close(fd);
|
2009-10-14 23:41:47 +08:00
|
|
|
|
2009-08-19 21:14:50 +08:00
|
|
|
/* delete decoder object */
|
|
|
|
mp3_decoder_delete(decoder);
|
|
|
|
}
|
2009-07-28 07:28:26 +08:00
|
|
|
}
|
2009-12-01 23:31:48 +08:00
|
|
|
is_playing = RT_FALSE;
|
2009-07-28 07:28:26 +08:00
|
|
|
}
|
2009-10-14 23:41:47 +08:00
|
|
|
FINSH_FUNCTION_EXPORT(mp3, mp3 decode test);
|
|
|
|
|
|
|
|
#if STM32_EXT_SRAM
|
|
|
|
/* http mp3 */
|
|
|
|
#include "http.h"
|
|
|
|
static rt_size_t http_fetch(rt_uint8_t* ptr, rt_size_t len, void* parameter)
|
|
|
|
{
|
|
|
|
struct http_session* session = (struct http_session*)parameter;
|
|
|
|
RT_ASSERT(session != RT_NULL);
|
|
|
|
|
|
|
|
return http_session_read(session, ptr, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void http_close(void* parameter)
|
|
|
|
{
|
|
|
|
struct http_session* session = (struct http_session*)parameter;
|
|
|
|
RT_ASSERT(session != RT_NULL);
|
|
|
|
|
|
|
|
http_session_close(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_size_t http_data_fetch(void* parameter, rt_uint8_t *buffer, rt_size_t length)
|
|
|
|
{
|
|
|
|
return net_buf_read(buffer, length);
|
|
|
|
}
|
|
|
|
void http_mp3(char* url)
|
|
|
|
{
|
|
|
|
struct http_session* session;
|
|
|
|
struct mp3_decoder* decoder;
|
|
|
|
|
|
|
|
session = http_session_open(url);
|
|
|
|
if (session != RT_NULL)
|
|
|
|
{
|
|
|
|
/* add a job to netbuf worker */
|
|
|
|
net_buf_add_job(http_fetch, http_close, (void*)session);
|
|
|
|
|
|
|
|
decoder = mp3_decoder_create();
|
|
|
|
if (decoder != RT_NULL)
|
|
|
|
{
|
|
|
|
decoder->fetch_data = http_data_fetch;
|
|
|
|
decoder->fetch_parameter = RT_NULL;
|
|
|
|
|
2009-12-01 23:31:48 +08:00
|
|
|
current_offset = 0;
|
2009-10-14 23:41:47 +08:00
|
|
|
while (mp3_decoder_run(decoder) != -1);
|
|
|
|
|
|
|
|
/* delete decoder object */
|
|
|
|
mp3_decoder_delete(decoder);
|
|
|
|
}
|
|
|
|
session = RT_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FINSH_FUNCTION_EXPORT(http_mp3, http mp3 decode test);
|
|
|
|
#endif
|