Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * |
| 9 | * Copyright (C) 2002 Dave Chapman |
| 10 | * |
Dave Chapman | f622a63 | 2002-05-12 10:37:49 +0000 | [diff] [blame] | 11 | * This file contains significant code from two other projects: |
| 12 | * |
| 13 | * 1) madldd - a sample application to use libmad |
| 14 | * 2) CoolPlayer - a win32 audio player that also uses libmad |
| 15 | * |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 16 | * All files in this archive are subject to the GNU General Public License. |
| 17 | * See the file COPYING in the source tree root for full license agreement. |
| 18 | * |
| 19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 20 | * KIND, either express or implied. |
| 21 | * |
| 22 | ****************************************************************************/ |
| 23 | |
Linus Nielsen Feltzing | 4242a34 | 2004-07-08 10:12:39 +0000 | [diff] [blame] | 24 | #ifdef HAVE_MPEG_PLAY |
| 25 | #ifdef HAVE_LIBMAD |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 26 | |
| 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <file.h> |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 30 | #include <lcd.h> |
| 31 | #include <button.h> |
| 32 | #include "id3.h" |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <mad.h> |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 36 | |
Dave Chapman | f07bfd0 | 2002-05-12 14:58:41 +0000 | [diff] [blame] | 37 | #include "sound.h" |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 38 | |
Dave Chapman | f622a63 | 2002-05-12 10:37:49 +0000 | [diff] [blame] | 39 | /* The "dither" code to convert the 24-bit samples produced by libmad was |
| 40 | taken from the coolplayer project - coolplayer.sourceforge.net */ |
| 41 | |
| 42 | struct dither { |
| 43 | mad_fixed_t error[3]; |
| 44 | mad_fixed_t random; |
| 45 | }; |
| 46 | # define SAMPLE_DEPTH 16 |
| 47 | # define scale(x, y) dither((x), (y)) |
| 48 | |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 49 | struct mad_stream Stream; |
| 50 | struct mad_frame Frame; |
| 51 | struct mad_synth Synth; |
| 52 | mad_timer_t Timer; |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 53 | |
Dave Chapman | f622a63 | 2002-05-12 10:37:49 +0000 | [diff] [blame] | 54 | /* |
| 55 | * NAME: prng() |
| 56 | * DESCRIPTION: 32-bit pseudo-random number generator |
| 57 | */ |
| 58 | static __inline |
| 59 | unsigned long prng(unsigned long state) |
| 60 | { |
| 61 | return (state * 0x0019660dL + 0x3c6ef35fL) & 0xffffffffL; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * NAME: dither() |
| 66 | * DESCRIPTION: dither and scale sample |
| 67 | */ |
| 68 | static __inline |
| 69 | signed int dither(mad_fixed_t sample, struct dither *dither) |
| 70 | { |
| 71 | unsigned int scalebits; |
| 72 | mad_fixed_t output, mask, random; |
| 73 | |
| 74 | enum { |
| 75 | MIN = -MAD_F_ONE, |
| 76 | MAX = MAD_F_ONE - 1 |
| 77 | }; |
| 78 | |
| 79 | /* noise shape */ |
| 80 | sample += dither->error[0] - dither->error[1] + dither->error[2]; |
| 81 | |
| 82 | dither->error[2] = dither->error[1]; |
| 83 | dither->error[1] = dither->error[0] / 2; |
| 84 | |
| 85 | /* bias */ |
| 86 | output = sample + (1L << (MAD_F_FRACBITS + 1 - SAMPLE_DEPTH - 1)); |
| 87 | |
| 88 | scalebits = MAD_F_FRACBITS + 1 - SAMPLE_DEPTH; |
| 89 | mask = (1L << scalebits) - 1; |
| 90 | |
| 91 | /* dither */ |
| 92 | random = prng(dither->random); |
| 93 | output += (random & mask) - (dither->random & mask); |
| 94 | |
| 95 | dither->random = random; |
| 96 | |
| 97 | /* clip */ |
| 98 | if (output > MAX) { |
| 99 | output = MAX; |
| 100 | |
| 101 | if (sample > MAX) |
| 102 | sample = MAX; |
| 103 | } |
| 104 | else if (output < MIN) { |
| 105 | output = MIN; |
| 106 | |
| 107 | if (sample < MIN) |
| 108 | sample = MIN; |
| 109 | } |
| 110 | |
| 111 | /* quantize */ |
| 112 | output &= ~mask; |
| 113 | |
| 114 | /* error feedback */ |
| 115 | dither->error[0] = sample - output; |
| 116 | |
| 117 | /* scale */ |
| 118 | return output >> scalebits; |
| 119 | } |
| 120 | |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 121 | #define INPUT_BUFFER_SIZE (5*8192) |
| 122 | #define OUTPUT_BUFFER_SIZE 8192 /* Must be an integer multiple of 4. */ |
Linus Nielsen Feltzing | 4242a34 | 2004-07-08 10:12:39 +0000 | [diff] [blame] | 123 | void real_mpeg_play(char* fname) |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 124 | { |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 125 | unsigned char InputBuffer[INPUT_BUFFER_SIZE], |
| 126 | OutputBuffer[OUTPUT_BUFFER_SIZE], |
| 127 | *OutputPtr=OutputBuffer; |
| 128 | const unsigned char *OutputBufferEnd=OutputBuffer+OUTPUT_BUFFER_SIZE; |
| 129 | int Status=0, i, fd; |
| 130 | unsigned long FrameCount=0; |
| 131 | sound_t sound; |
Linus Nielsen Feltzing | 4242a34 | 2004-07-08 10:12:39 +0000 | [diff] [blame] | 132 | struct mp3entry mp3; |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 133 | static struct dither d0, d1; |
Linus Nielsen Feltzing | 4242a34 | 2004-07-08 10:12:39 +0000 | [diff] [blame] | 134 | int key=0; |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 135 | |
Linus Nielsen Feltzing | f4f4111 | 2004-07-27 14:10:48 +0000 | [diff] [blame] | 136 | mp3info(&mp3, fname, false); /* FIXME: honor the v1first setting */ |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 137 | |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 138 | init_sound(&sound); |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 139 | |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 140 | /* Configure sound device for this file - always select Stereo because |
| 141 | some sound cards don't support mono */ |
| 142 | config_sound(&sound,mp3.frequency,2); |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 143 | |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 144 | if ((fd=open(fname,O_RDONLY)) < 0) { |
| 145 | fprintf(stderr,"could not open %s\n",fname); |
| 146 | return; |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 149 | /* First the structures used by libmad must be initialized. */ |
| 150 | mad_stream_init(&Stream); |
| 151 | mad_frame_init(&Frame); |
| 152 | mad_synth_init(&Synth); |
| 153 | mad_timer_reset(&Timer); |
| 154 | |
| 155 | do { |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 156 | if (Stream.buffer==NULL || Stream.error==MAD_ERROR_BUFLEN) { |
| 157 | size_t ReadSize,Remaining; |
| 158 | unsigned char *ReadStart; |
| 159 | |
| 160 | if(Stream.next_frame!=NULL) { |
| 161 | Remaining=Stream.bufend-Stream.next_frame; |
| 162 | memmove(InputBuffer,Stream.next_frame,Remaining); |
| 163 | ReadStart=InputBuffer+Remaining; |
| 164 | ReadSize=INPUT_BUFFER_SIZE-Remaining; |
| 165 | } else { |
| 166 | ReadSize=INPUT_BUFFER_SIZE, |
| 167 | ReadStart=InputBuffer, |
| 168 | Remaining=0; |
| 169 | } |
| 170 | |
Linus Nielsen Feltzing | 4242a34 | 2004-07-08 10:12:39 +0000 | [diff] [blame] | 171 | if ((int)(ReadSize=read(fd,ReadStart,ReadSize)) < 0) { |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 172 | fprintf(stderr,"end of input stream\n"); |
| 173 | break; |
| 174 | } |
| 175 | |
| 176 | mad_stream_buffer(&Stream,InputBuffer,ReadSize+Remaining); |
| 177 | Stream.error=0; |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 178 | } |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 179 | |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 180 | if(mad_frame_decode(&Frame,&Stream)) { |
| 181 | if(MAD_RECOVERABLE(Stream.error)) { |
| 182 | fprintf(stderr,"recoverable frame level error\n"); |
| 183 | fflush(stderr); |
| 184 | continue; |
| 185 | } else { |
| 186 | if(Stream.error==MAD_ERROR_BUFLEN) { |
| 187 | continue; |
| 188 | } else { |
| 189 | fprintf(stderr,"unrecoverable frame level error\n"); |
| 190 | Status=1; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | FrameCount++; |
| 197 | mad_timer_add(&Timer,Frame.header.duration); |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 198 | |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 199 | mad_synth_frame(&Synth,&Frame); |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 200 | |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 201 | for(i=0;i<Synth.pcm.length;i++) { |
| 202 | unsigned short Sample; |
| 203 | |
| 204 | /* Left channel */ |
| 205 | Sample=scale(Synth.pcm.samples[0][i],&d0); |
| 206 | *(OutputPtr++)=Sample&0xff; |
| 207 | *(OutputPtr++)=Sample>>8; |
| 208 | |
| 209 | /* Right channel. If the decoded stream is monophonic then |
| 210 | * the right output channel is the same as the left one. |
| 211 | */ |
| 212 | if(MAD_NCHANNELS(&Frame.header)==2) { |
| 213 | Sample=scale(Synth.pcm.samples[1][i],&d1); |
| 214 | } |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 215 | |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 216 | *(OutputPtr++)=Sample&0xff; |
| 217 | *(OutputPtr++)=Sample>>8; |
| 218 | |
| 219 | /* Flush the buffer if it is full. */ |
| 220 | if (OutputPtr==OutputBufferEnd) { |
| 221 | if (output_sound(&sound, OutputBuffer, |
| 222 | OUTPUT_BUFFER_SIZE)!=OUTPUT_BUFFER_SIZE) { |
| 223 | fprintf(stderr,"PCM write error.\n"); |
| 224 | Status=2; |
| 225 | break; |
| 226 | } |
| 227 | OutputPtr=OutputBuffer; |
| 228 | } |
| 229 | } |
Linus Nielsen Feltzing | 4242a34 | 2004-07-08 10:12:39 +0000 | [diff] [blame] | 230 | |
| 231 | if ((key=button_get(0))==BUTTON_STOP) |
| 232 | { |
| 233 | break; |
| 234 | } |
| 235 | |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 236 | }while(1); |
| 237 | |
| 238 | /* Mad is no longer used, the structures that were initialized must |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 239 | * now be cleared. |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 240 | */ |
| 241 | mad_synth_finish(&Synth); |
| 242 | mad_frame_finish(&Frame); |
| 243 | mad_stream_finish(&Stream); |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 244 | |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 245 | /* If the output buffer is not empty and no error occured during |
| 246 | * the last write, then flush it. */ |
| 247 | if(OutputPtr!=OutputBuffer && Status!=2) |
| 248 | { |
| 249 | size_t BufferSize=OutputPtr-OutputBuffer; |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 250 | |
Linus Nielsen Feltzing | 4242a34 | 2004-07-08 10:12:39 +0000 | [diff] [blame] | 251 | if (output_sound(&sound, OutputPtr, BufferSize)!=(int)BufferSize) |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 252 | { |
| 253 | fprintf(stderr,"PCM write error\n"); |
| 254 | Status=2; |
| 255 | } |
| 256 | } |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 257 | |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 258 | /* Accounting report if no error occured. */ |
| 259 | if(!Status) |
| 260 | { |
| 261 | char Buffer[80]; |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 262 | |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 263 | mad_timer_string(Timer,Buffer,"%lu:%02lu.%03u", |
| 264 | MAD_UNITS_MINUTES,MAD_UNITS_MILLISECONDS,0); |
| 265 | fprintf(stderr,"%lu frames decoded (%s).\n",FrameCount,Buffer); |
| 266 | } |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 267 | |
Robert Hak | ece50cd | 2002-05-27 08:08:54 +0000 | [diff] [blame] | 268 | close_sound(&sound); |
| 269 | /* That's the end of the world (in the H. G. Wells way). */ |
| 270 | return; |
Dave Chapman | 4dbbca63 | 2002-05-09 23:54:47 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |
Linus Nielsen Feltzing | 4242a34 | 2004-07-08 10:12:39 +0000 | [diff] [blame] | 274 | #endif /* HAVE_LIBMAD */ |
| 275 | #endif /* HAVE_MPEG_PLAY */ |