MP3 Player without external hardware MP3 Player without external hardware. A software based MP3 player based on a modified version of libmad. Mono output (at the moment) via AnalogOut. Files are read from an USB drive. This is a demo program, it plays only one file at the moment. Documentation is in "main.cpp" and "config.h"

Dependencies:   mbed

Committer:
Gruenfrosch
Date:
Sat Nov 27 17:27:33 2010 +0000
Revision:
2:f28cf0afd021
Parent:
0:7627c79db971
Version 3:
* moved another memory block into AHB RAM, giving more room for
* stereo buffer.
* moved content of decode() to main()
* decoding is now safe to be called multiple times (bug in older versions)
* Output routine now fills stereo buffer, DAC output sums channels,
* just for demonstration that stereo output could go here

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Gruenfrosch 0:7627c79db971 1 /*
Gruenfrosch 0:7627c79db971 2 * libmad - MPEG audio decoder library
Gruenfrosch 0:7627c79db971 3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
Gruenfrosch 0:7627c79db971 4 *
Gruenfrosch 0:7627c79db971 5 * This program is free software; you can redistribute it and/or modify
Gruenfrosch 0:7627c79db971 6 * it under the terms of the GNU General Public License as published by
Gruenfrosch 0:7627c79db971 7 * the Free Software Foundation; either version 2 of the License, or
Gruenfrosch 0:7627c79db971 8 * (at your option) any later version.
Gruenfrosch 0:7627c79db971 9 *
Gruenfrosch 0:7627c79db971 10 * This program is distributed in the hope that it will be useful,
Gruenfrosch 0:7627c79db971 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Gruenfrosch 0:7627c79db971 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Gruenfrosch 0:7627c79db971 13 * GNU General Public License for more details.
Gruenfrosch 0:7627c79db971 14 *
Gruenfrosch 0:7627c79db971 15 * You should have received a copy of the GNU General Public License
Gruenfrosch 0:7627c79db971 16 * along with this program; if not, write to the Free Software
Gruenfrosch 0:7627c79db971 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Gruenfrosch 0:7627c79db971 18 *
Gruenfrosch 0:7627c79db971 19 * $Id: stream.c,v 1.1 2010/11/23 20:12:57 andy Exp $
Gruenfrosch 0:7627c79db971 20 */
Gruenfrosch 0:7627c79db971 21
Gruenfrosch 0:7627c79db971 22 # include "config.h"
Gruenfrosch 0:7627c79db971 23
Gruenfrosch 0:7627c79db971 24 # include "global.h"
Gruenfrosch 0:7627c79db971 25
Gruenfrosch 0:7627c79db971 26 # include <stdlib.h>
Gruenfrosch 0:7627c79db971 27
Gruenfrosch 0:7627c79db971 28 # include "bit.h"
Gruenfrosch 0:7627c79db971 29 # include "stream.h"
Gruenfrosch 0:7627c79db971 30
Gruenfrosch 0:7627c79db971 31 /*
Gruenfrosch 0:7627c79db971 32 * NAME: stream->init()
Gruenfrosch 0:7627c79db971 33 * DESCRIPTION: initialize stream struct
Gruenfrosch 0:7627c79db971 34 */
Gruenfrosch 0:7627c79db971 35 void mad_stream_init(struct mad_stream *stream)
Gruenfrosch 0:7627c79db971 36 {
Gruenfrosch 0:7627c79db971 37 stream->buffer = 0;
Gruenfrosch 0:7627c79db971 38 stream->bufend = 0;
Gruenfrosch 0:7627c79db971 39 stream->skiplen = 0;
Gruenfrosch 0:7627c79db971 40
Gruenfrosch 0:7627c79db971 41 stream->sync = 0;
Gruenfrosch 0:7627c79db971 42 stream->freerate = 0;
Gruenfrosch 0:7627c79db971 43
Gruenfrosch 0:7627c79db971 44 stream->this_frame = 0;
Gruenfrosch 0:7627c79db971 45 stream->next_frame = 0;
Gruenfrosch 0:7627c79db971 46 mad_bit_init(&stream->ptr, 0);
Gruenfrosch 0:7627c79db971 47
Gruenfrosch 0:7627c79db971 48 mad_bit_init(&stream->anc_ptr, 0);
Gruenfrosch 0:7627c79db971 49 stream->anc_bitlen = 0;
Gruenfrosch 0:7627c79db971 50
Gruenfrosch 0:7627c79db971 51 stream->main_data = 0;
Gruenfrosch 0:7627c79db971 52 stream->md_len = 0;
Gruenfrosch 0:7627c79db971 53
Gruenfrosch 0:7627c79db971 54 stream->options = 0;
Gruenfrosch 0:7627c79db971 55 stream->error = MAD_ERROR_NONE;
Gruenfrosch 0:7627c79db971 56 }
Gruenfrosch 0:7627c79db971 57
Gruenfrosch 0:7627c79db971 58 /*
Gruenfrosch 0:7627c79db971 59 * NAME: stream->finish()
Gruenfrosch 0:7627c79db971 60 * DESCRIPTION: deallocate any dynamic memory associated with stream
Gruenfrosch 0:7627c79db971 61 */
Gruenfrosch 0:7627c79db971 62 void mad_stream_finish(struct mad_stream *stream)
Gruenfrosch 0:7627c79db971 63 {
Gruenfrosch 0:7627c79db971 64 if (stream->main_data) {
Gruenfrosch 2:f28cf0afd021 65 #if !defined(TARGET_LPC1768)
Gruenfrosch 0:7627c79db971 66 free(stream->main_data);
Gruenfrosch 2:f28cf0afd021 67 #endif
Gruenfrosch 0:7627c79db971 68 stream->main_data = 0;
Gruenfrosch 0:7627c79db971 69 }
Gruenfrosch 0:7627c79db971 70
Gruenfrosch 0:7627c79db971 71 mad_bit_finish(&stream->anc_ptr);
Gruenfrosch 0:7627c79db971 72 mad_bit_finish(&stream->ptr);
Gruenfrosch 0:7627c79db971 73 }
Gruenfrosch 0:7627c79db971 74
Gruenfrosch 0:7627c79db971 75 /*
Gruenfrosch 0:7627c79db971 76 * NAME: stream->buffer()
Gruenfrosch 0:7627c79db971 77 * DESCRIPTION: set stream buffer pointers
Gruenfrosch 0:7627c79db971 78 */
Gruenfrosch 0:7627c79db971 79 void mad_stream_buffer(struct mad_stream *stream,
Gruenfrosch 0:7627c79db971 80 unsigned char const *buffer, unsigned long length)
Gruenfrosch 0:7627c79db971 81 {
Gruenfrosch 0:7627c79db971 82 stream->buffer = buffer;
Gruenfrosch 0:7627c79db971 83 stream->bufend = buffer + length;
Gruenfrosch 0:7627c79db971 84
Gruenfrosch 0:7627c79db971 85 stream->this_frame = buffer;
Gruenfrosch 0:7627c79db971 86 stream->next_frame = buffer;
Gruenfrosch 0:7627c79db971 87
Gruenfrosch 0:7627c79db971 88 stream->sync = 1;
Gruenfrosch 0:7627c79db971 89
Gruenfrosch 0:7627c79db971 90 mad_bit_init(&stream->ptr, buffer);
Gruenfrosch 0:7627c79db971 91 }
Gruenfrosch 0:7627c79db971 92
Gruenfrosch 0:7627c79db971 93 /*
Gruenfrosch 0:7627c79db971 94 * NAME: stream->skip()
Gruenfrosch 0:7627c79db971 95 * DESCRIPTION: arrange to skip bytes before the next frame
Gruenfrosch 0:7627c79db971 96 */
Gruenfrosch 0:7627c79db971 97 void mad_stream_skip(struct mad_stream *stream, unsigned long length)
Gruenfrosch 0:7627c79db971 98 {
Gruenfrosch 0:7627c79db971 99 stream->skiplen += length;
Gruenfrosch 0:7627c79db971 100 }
Gruenfrosch 0:7627c79db971 101
Gruenfrosch 0:7627c79db971 102 /*
Gruenfrosch 0:7627c79db971 103 * NAME: stream->sync()
Gruenfrosch 0:7627c79db971 104 * DESCRIPTION: locate the next stream sync word
Gruenfrosch 0:7627c79db971 105 */
Gruenfrosch 0:7627c79db971 106 int mad_stream_sync(struct mad_stream *stream)
Gruenfrosch 0:7627c79db971 107 {
Gruenfrosch 0:7627c79db971 108 register unsigned char const *ptr, *end;
Gruenfrosch 0:7627c79db971 109
Gruenfrosch 0:7627c79db971 110 ptr = mad_bit_nextbyte(&stream->ptr);
Gruenfrosch 0:7627c79db971 111 end = stream->bufend;
Gruenfrosch 0:7627c79db971 112
Gruenfrosch 0:7627c79db971 113 while (ptr < end - 1 &&
Gruenfrosch 0:7627c79db971 114 !(ptr[0] == 0xff && (ptr[1] & 0xe0) == 0xe0))
Gruenfrosch 0:7627c79db971 115 ++ptr;
Gruenfrosch 0:7627c79db971 116
Gruenfrosch 0:7627c79db971 117 if (end - ptr < MAD_BUFFER_GUARD)
Gruenfrosch 0:7627c79db971 118 return -1;
Gruenfrosch 0:7627c79db971 119
Gruenfrosch 0:7627c79db971 120 mad_bit_init(&stream->ptr, ptr);
Gruenfrosch 0:7627c79db971 121
Gruenfrosch 0:7627c79db971 122 return 0;
Gruenfrosch 0:7627c79db971 123 }
Gruenfrosch 0:7627c79db971 124
Gruenfrosch 0:7627c79db971 125 /*
Gruenfrosch 0:7627c79db971 126 * NAME: stream->errorstr()
Gruenfrosch 0:7627c79db971 127 * DESCRIPTION: return a string description of the current error condition
Gruenfrosch 0:7627c79db971 128 */
Gruenfrosch 0:7627c79db971 129 char const *mad_stream_errorstr(struct mad_stream const *stream)
Gruenfrosch 0:7627c79db971 130 {
Gruenfrosch 0:7627c79db971 131 switch (stream->error) {
Gruenfrosch 0:7627c79db971 132 case MAD_ERROR_NONE: return "no error";
Gruenfrosch 0:7627c79db971 133
Gruenfrosch 0:7627c79db971 134 case MAD_ERROR_BUFLEN: return "input buffer too small (or EOF)";
Gruenfrosch 0:7627c79db971 135 case MAD_ERROR_BUFPTR: return "invalid (null) buffer pointer";
Gruenfrosch 0:7627c79db971 136
Gruenfrosch 0:7627c79db971 137 case MAD_ERROR_NOMEM: return "not enough memory";
Gruenfrosch 0:7627c79db971 138
Gruenfrosch 0:7627c79db971 139 case MAD_ERROR_LOSTSYNC: return "lost synchronization";
Gruenfrosch 0:7627c79db971 140 case MAD_ERROR_BADLAYER: return "reserved header layer value";
Gruenfrosch 0:7627c79db971 141 case MAD_ERROR_BADBITRATE: return "forbidden bitrate value";
Gruenfrosch 0:7627c79db971 142 case MAD_ERROR_BADSAMPLERATE: return "reserved sample frequency value";
Gruenfrosch 0:7627c79db971 143 case MAD_ERROR_BADEMPHASIS: return "reserved emphasis value";
Gruenfrosch 0:7627c79db971 144
Gruenfrosch 0:7627c79db971 145 case MAD_ERROR_BADCRC: return "CRC check failed";
Gruenfrosch 0:7627c79db971 146 case MAD_ERROR_BADBITALLOC: return "forbidden bit allocation value";
Gruenfrosch 0:7627c79db971 147 case MAD_ERROR_BADSCALEFACTOR: return "bad scalefactor index";
Gruenfrosch 0:7627c79db971 148 case MAD_ERROR_BADMODE: return "bad bitrate/mode combination";
Gruenfrosch 0:7627c79db971 149 case MAD_ERROR_BADFRAMELEN: return "bad frame length";
Gruenfrosch 0:7627c79db971 150 case MAD_ERROR_BADBIGVALUES: return "bad big_values count";
Gruenfrosch 0:7627c79db971 151 case MAD_ERROR_BADBLOCKTYPE: return "reserved block_type";
Gruenfrosch 0:7627c79db971 152 case MAD_ERROR_BADSCFSI: return "bad scalefactor selection info";
Gruenfrosch 0:7627c79db971 153 case MAD_ERROR_BADDATAPTR: return "bad main_data_begin pointer";
Gruenfrosch 0:7627c79db971 154 case MAD_ERROR_BADPART3LEN: return "bad audio data length";
Gruenfrosch 0:7627c79db971 155 case MAD_ERROR_BADHUFFTABLE: return "bad Huffman table select";
Gruenfrosch 0:7627c79db971 156 case MAD_ERROR_BADHUFFDATA: return "Huffman data overrun";
Gruenfrosch 0:7627c79db971 157 case MAD_ERROR_BADSTEREO: return "incompatible block_type for JS";
Gruenfrosch 0:7627c79db971 158 }
Gruenfrosch 0:7627c79db971 159
Gruenfrosch 0:7627c79db971 160 return 0;
Gruenfrosch 0:7627c79db971 161 }