1
|
1 // #define NEWBUFFERING
|
|
2 //#define DEBUG_RESYNC
|
|
3
|
|
4 /* 1 frame = 4608 byte PCM */
|
|
5
|
|
6 #ifdef __GNUC__
|
|
7 #define LOCAL static inline
|
|
8 #else
|
|
9 #define LOCAL static _inline
|
|
10 #endif
|
|
11
|
|
12 //#undef LOCAL
|
|
13 //#define LOCAL
|
|
14
|
|
15 #include <stdlib.h>
|
|
16 #include <stdio.h>
|
|
17 #include <string.h>
|
|
18 #include <signal.h>
|
|
19 #include <math.h>
|
|
20
|
|
21 #define real float
|
|
22 // #define int long
|
|
23
|
|
24 #include "mpg123.h"
|
|
25 #include "huffman.h"
|
|
26 #include "mp3.h"
|
1040
|
27 #include "bswap.h"
|
1
|
28 #include "d_cpu.h"
|
|
29
|
1045
|
30 #include "fastmemcpy.h"
|
1
|
31 //static FILE* mp3_file=NULL;
|
|
32
|
|
33 int MP3_frames=0;
|
|
34 int MP3_eof=0;
|
|
35 int MP3_pause=0;
|
|
36 int MP3_filesize=0;
|
|
37 int MP3_fpos=0; // current file position
|
|
38 int MP3_framesize=0; // current framesize
|
|
39 int MP3_bitrate=0; // current bitrate
|
|
40 int MP3_samplerate=0; // current samplerate
|
|
41 int MP3_resync=0;
|
|
42 int MP3_channels=0;
|
|
43 int MP3_bps=2;
|
|
44
|
|
45 static long outscale = 32768;
|
|
46 #include "tabinit.c"
|
|
47
|
|
48 extern int mplayer_audio_read(char *buf,int size);
|
|
49
|
|
50 #if 1
|
|
51 LOCAL int mp3_read(char *buf,int size){
|
|
52 // int len=fread(buf,1,size,mp3_file);
|
|
53 int len=mplayer_audio_read(buf,size);
|
|
54 if(len>0) MP3_fpos+=len;
|
|
55 // if(len!=size) MP3_eof=1;
|
|
56 return len;
|
|
57 }
|
|
58 #else
|
|
59 extern int mp3_read(char *buf,int size);
|
|
60 #endif
|
|
61
|
|
62 //void mp3_seek(int pos){
|
|
63 // fseek(mp3_file,pos,SEEK_SET);
|
|
64 // return (MP3_fpos=ftell(mp3_file));
|
|
65 //}
|
|
66
|
|
67 /* Frame reader */
|
|
68
|
|
69 #define MAXFRAMESIZE 1280
|
|
70 #define MAXFRAMESIZE2 (512+MAXFRAMESIZE)
|
|
71
|
|
72 static int fsizeold=0,ssize=0;
|
|
73 static unsigned char bsspace[2][MAXFRAMESIZE2]; /* !!!!! */
|
|
74 static unsigned char *bsbufold=bsspace[0]+512;
|
|
75 static unsigned char *bsbuf=bsspace[1]+512;
|
|
76 static int bsnum=0;
|
|
77
|
|
78 static int bitindex;
|
|
79 static unsigned char *wordpointer;
|
|
80 static int bitsleft;
|
|
81
|
|
82 unsigned char *pcm_sample; /* az outbuffer CIME */
|
|
83 int pcm_point = 0; /* ez az outbuffer pozicioja */
|
|
84
|
|
85 static struct frame fr;
|
|
86
|
|
87 static int tabsel_123[2][3][16] = {
|
|
88 { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
|
|
89 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
|
|
90 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
|
|
91
|
|
92 { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
|
|
93 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
|
|
94 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} }
|
|
95 };
|
|
96
|
|
97 static long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
|
|
98
|
|
99 LOCAL unsigned int getbits(short number_of_bits)
|
|
100 {
|
|
101 unsigned long rval;
|
|
102 // if(MP3_frames>=7741) printf("getbits: bits=%d bitsleft=%d wordptr=%x\n",number_of_bits,bitsleft,wordpointer);
|
|
103 if((bitsleft-=number_of_bits)<0) return 0;
|
|
104 if(!number_of_bits) return 0;
|
|
105 rval = wordpointer[0];
|
|
106 rval <<= 8;
|
|
107 rval |= wordpointer[1];
|
|
108 rval <<= 8;
|
|
109 rval |= wordpointer[2];
|
|
110 rval <<= bitindex;
|
|
111 rval &= 0xffffff;
|
|
112 bitindex += number_of_bits;
|
|
113 rval >>= (24-number_of_bits);
|
|
114 wordpointer += (bitindex>>3);
|
|
115 bitindex &= 7;
|
|
116 return rval;
|
|
117 }
|
|
118
|
|
119
|
|
120 LOCAL unsigned int getbits_fast(short number_of_bits)
|
|
121 {
|
|
122 unsigned long rval;
|
|
123 // if(MP3_frames>=7741) printf("getbits_fast: bits=%d bitsleft=%d wordptr=%x\n",number_of_bits,bitsleft,wordpointer);
|
|
124 if((bitsleft-=number_of_bits)<0) return 0;
|
|
125 if(!number_of_bits) return 0;
|
1040
|
126 rval = bswap_16(*((unsigned short *)wordpointer));
|
1
|
127 rval <<= bitindex;
|
|
128 rval &= 0xffff;
|
|
129 bitindex += number_of_bits;
|
|
130 rval >>= (16-number_of_bits);
|
|
131 wordpointer += (bitindex>>3);
|
|
132 bitindex &= 7;
|
|
133 return rval;
|
|
134 }
|
|
135
|
|
136 LOCAL unsigned int get1bit(void)
|
|
137 {
|
|
138 unsigned char rval;
|
|
139 // if(MP3_frames>=7741) printf("get1bit: bitsleft=%d wordptr=%x\n",bitsleft,wordpointer);
|
|
140 if((--bitsleft)<0) return 0;
|
|
141 rval = *wordpointer << bitindex;
|
|
142 bitindex++;
|
|
143 wordpointer += (bitindex>>3);
|
|
144 bitindex &= 7;
|
|
145 return ((rval>>7)&1);
|
|
146 }
|
|
147
|
|
148 LOCAL void set_pointer(long backstep)
|
|
149 {
|
|
150 // if(backstep!=512 && backstep>fsizeold)
|
|
151 // printf("\rWarning! backstep (%d>%d) \n",backstep,fsizeold);
|
|
152 wordpointer = bsbuf + ssize - backstep;
|
|
153 if (backstep) memcpy(wordpointer,bsbufold+fsizeold-backstep,backstep);
|
|
154 bitindex = 0;
|
|
155 bitsleft+=8*backstep;
|
|
156 // printf("Backstep %d (bitsleft=%d)\n",backstep,bitsleft);
|
|
157 }
|
|
158
|
|
159 LOCAL int stream_head_read(unsigned char *hbuf,unsigned long *newhead){
|
|
160 if(mp3_read(hbuf,4) != 4) return FALSE;
|
1040
|
161 *newhead = bswap_32(*((unsigned long *)hbuf));
|
|
162 return TRUE;
|
1
|
163 }
|
|
164
|
|
165 LOCAL int stream_head_shift(unsigned char *hbuf,unsigned long *head){
|
1040
|
166 *((unsigned long *)hbuf) >>= 8;
|
1
|
167 if(mp3_read(hbuf+3,1) != 1) return 0;
|
|
168 *head <<= 8;
|
|
169 *head |= hbuf[3];
|
|
170 return 1;
|
|
171 }
|
|
172
|
|
173 /*
|
|
174 * decode a header and write the information
|
|
175 * into the frame structure
|
|
176 */
|
|
177 LOCAL int decode_header(struct frame *fr,unsigned long newhead){
|
|
178
|
|
179 // head_check:
|
1040
|
180 if( (newhead & 0xffe00000) != 0xffe00000 ||
|
1045
|
181 (newhead & 0x0000fc00) == 0x0000fc00) return FALSE;
|
1
|
182
|
|
183 fr->lay = 4-((newhead>>17)&3);
|
|
184 // if(fr->lay!=3) return FALSE;
|
|
185
|
|
186 if( newhead & ((long)1<<20) ) {
|
|
187 fr->lsf = (newhead & ((long)1<<19)) ? 0x0 : 0x1;
|
|
188 fr->mpeg25 = 0;
|
|
189 } else {
|
|
190 fr->lsf = 1;
|
|
191 fr->mpeg25 = 1;
|
|
192 }
|
|
193
|
|
194 if(fr->mpeg25)
|
|
195 fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
|
|
196 else
|
|
197 fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3);
|
|
198
|
1166
|
199 if(fr->sampling_frequency>8) return FALSE; // valid: 0..8
|
|
200
|
1
|
201 fr->error_protection = ((newhead>>16)&0x1)^0x1;
|
|
202 fr->bitrate_index = ((newhead>>12)&0xf);
|
|
203 fr->padding = ((newhead>>9)&0x1);
|
|
204 fr->extension = ((newhead>>8)&0x1);
|
|
205 fr->mode = ((newhead>>6)&0x3);
|
|
206 fr->mode_ext = ((newhead>>4)&0x3);
|
|
207 fr->copyright = ((newhead>>3)&0x1);
|
|
208 fr->original = ((newhead>>2)&0x1);
|
|
209 fr->emphasis = newhead & 0x3;
|
|
210
|
|
211 MP3_channels = fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
|
|
212
|
|
213 if(!fr->bitrate_index){
|
|
214 // fprintf(stderr,"Free format not supported.\n");
|
|
215 return FALSE;
|
|
216 }
|
|
217
|
|
218 switch(fr->lay){
|
|
219 case 2:
|
|
220 MP3_bitrate=tabsel_123[fr->lsf][1][fr->bitrate_index];
|
|
221 MP3_samplerate=freqs[fr->sampling_frequency];
|
|
222 fr->framesize = (long) MP3_bitrate * 144000;
|
|
223 fr->framesize /= MP3_samplerate;
|
|
224 MP3_framesize=fr->framesize;
|
|
225 fr->framesize += fr->padding - 4;
|
|
226 break;
|
|
227 case 3:
|
|
228 if(fr->lsf)
|
|
229 ssize = (fr->stereo == 1) ? 9 : 17;
|
|
230 else
|
|
231 ssize = (fr->stereo == 1) ? 17 : 32;
|
|
232 if(fr->error_protection) ssize += 2;
|
|
233
|
|
234 MP3_bitrate=tabsel_123[fr->lsf][2][fr->bitrate_index];
|
|
235 MP3_samplerate=freqs[fr->sampling_frequency];
|
|
236 fr->framesize = (long) MP3_bitrate * 144000;
|
|
237 fr->framesize /= MP3_samplerate<<(fr->lsf);
|
|
238 MP3_framesize=fr->framesize;
|
|
239 fr->framesize += fr->padding - 4;
|
|
240 break;
|
|
241 default:
|
|
242 // fprintf(stderr,"Sorry, unsupported layer type.\n");
|
|
243 return 0;
|
|
244 }
|
|
245 if(fr->framesize<=0 || fr->framesize>MAXFRAMESIZE) return FALSE;
|
|
246
|
|
247 return 1;
|
|
248 }
|
|
249
|
|
250
|
|
251 LOCAL int stream_read_frame_body(int size){
|
|
252
|
|
253 /* flip/init buffer for Layer 3 */
|
|
254 bsbufold = bsbuf;
|
|
255 bsbuf = bsspace[bsnum]+512;
|
|
256 bsnum = (bsnum + 1) & 1;
|
|
257
|
|
258 if( mp3_read(bsbuf,size) != size) return 0; // broken frame
|
|
259
|
|
260 bitindex = 0;
|
|
261 wordpointer = (unsigned char *) bsbuf;
|
|
262 bitsleft=8*size;
|
|
263
|
|
264 return 1;
|
|
265 }
|
|
266
|
|
267
|
|
268 /*****************************************************************
|
|
269 * read next frame return number of frames read.
|
|
270 */
|
|
271 LOCAL int read_frame(struct frame *fr){
|
|
272 unsigned long newhead;
|
|
273 unsigned char hbuf[8];
|
|
274 int skipped,resyncpos;
|
|
275 int frames=0;
|
|
276
|
|
277 resync:
|
|
278 skipped=MP3_fpos;
|
|
279 resyncpos=MP3_fpos;
|
|
280
|
|
281 set_pointer(512);
|
|
282 fsizeold=fr->framesize; /* for Layer3 */
|
|
283 if(!stream_head_read(hbuf,&newhead)) return 0;
|
|
284 if(!decode_header(fr,newhead)){
|
|
285 // invalid header! try to resync stream!
|
|
286 #ifdef DEBUG_RESYNC
|
|
287 printf("ReSync: searching for a valid header... (pos=%X)\n",MP3_fpos);
|
|
288 #endif
|
|
289 retry1:
|
|
290 while(!decode_header(fr,newhead)){
|
|
291 if(!stream_head_shift(hbuf,&newhead)) return 0;
|
|
292 }
|
|
293 resyncpos=MP3_fpos-4;
|
|
294 // found valid header
|
|
295 #ifdef DEBUG_RESYNC
|
|
296 printf("ReSync: found valid hdr at %X fsize=%ld ",resyncpos,fr->framesize);
|
|
297 #endif
|
|
298 if(!stream_read_frame_body(fr->framesize)) return 0; // read body
|
|
299 set_pointer(512);
|
|
300 fsizeold=fr->framesize; /* for Layer3 */
|
|
301 if(!stream_head_read(hbuf,&newhead)) return 0;
|
|
302 if(!decode_header(fr,newhead)){
|
|
303 // invalid hdr! go back...
|
|
304 #ifdef DEBUG_RESYNC
|
|
305 printf("INVALID\n");
|
|
306 #endif
|
|
307 // mp3_seek(resyncpos+1);
|
|
308 if(!stream_head_read(hbuf,&newhead)) return 0;
|
|
309 goto retry1;
|
|
310 }
|
|
311 #ifdef DEBUG_RESYNC
|
|
312 printf("OK!\n");
|
|
313 ++frames;
|
|
314 #endif
|
|
315 }
|
|
316
|
|
317 skipped=resyncpos-skipped;
|
|
318 // if(skipped && !MP3_resync) printf("\r%d bad bytes skipped (resync at 0x%X) \n",skipped,resyncpos);
|
|
319
|
|
320 // printf("%8X [%08X] %d %d (%d)%s%s\n",MP3_fpos-4,newhead,fr->framesize,fr->mode,fr->mode_ext,fr->error_protection?" CRC":"",fr->padding?" PAD":"");
|
|
321
|
|
322 /* read main data into memory */
|
|
323 if(!stream_read_frame_body(fr->framesize)){
|
|
324 printf("\nBroken frame at 0x%X \n",resyncpos);
|
|
325 return 0;
|
|
326 }
|
|
327 ++frames;
|
|
328
|
|
329 if(MP3_resync){
|
|
330 MP3_resync=0;
|
|
331 if(frames==1) goto resync;
|
|
332 }
|
|
333
|
|
334 return frames;
|
|
335 }
|
|
336
|
|
337 #include "layer2.c"
|
|
338 #include "layer3.c"
|
|
339
|
|
340 /******************************************************************************/
|
|
341 /* PUBLIC FUNCTIONS */
|
|
342 /******************************************************************************/
|
|
343
|
|
344 static int tables_done_flag=0;
|
|
345
|
1245
|
346 /* It's hidden from gcc in assembler */
|
|
347 extern void dct64_MMX( void );
|
|
348 extern void dct64_MMX_3dnow( void );
|
|
349 extern void dct64_MMX_3dnowex( void );
|
|
350 void (*dct64_MMX_func)( void );
|
|
351
|
1
|
352 // Init decoder tables. Call first, once!
|
732
|
353 #ifdef USE_FAKE_MONO
|
|
354 void MP3_Init(int fakemono){
|
|
355 #else
|
1
|
356 void MP3_Init(){
|
732
|
357 #endif
|
1253
|
358 #ifdef ARCH_X86
|
1
|
359 _CpuID=CpuDetect();
|
|
360 _i586=ipentium();
|
1245
|
361 #ifndef HAVE_MMX
|
|
362 _i586 &= 1;
|
|
363 #endif
|
1
|
364 _3dnow=a3dnow();
|
1245
|
365 #ifndef HAVE_3DNOW
|
|
366 _3dnow = 0;
|
1
|
367 #endif
|
1245
|
368 #ifndef HAVE_3DNOWEX
|
|
369 _3dnow &= 1;
|
|
370 #endif
|
|
371 _isse=isse();
|
|
372 #ifndef HAVE_SSE
|
|
373 _isse = 0;
|
|
374 #endif
|
|
375 #ifndef HAVE_SSE2
|
|
376 _isse &= 1;
|
|
377 #endif
|
|
378 _has_mmx=_i586>1||_3dnow||_isse;
|
1
|
379 printf( "mp3lib: Processor ID: %x\n",_CpuID );
|
1245
|
380 if(_i586&&!_3dnow&&!_isse)
|
|
381 printf( "mp3lib: Using Pentium%s optimized decore.\n",(_i586>1?"-MMX":""));
|
|
382 else
|
|
383 if(_isse)
|
|
384 /*
|
|
385 Note: It's ok, Since K8 will have SSE2 support and will much faster
|
|
386 of P4 ;)
|
|
387 */
|
1253
|
388 // printf( "mp3lib: Using SSE%s! optimized decore.\n",(_isse>1?"2":""));
|
|
389 printf( "mp3lib: Using Pentium%s optimized decore.\n",(_i586>1?"-MMX":""));
|
1245
|
390 else
|
|
391 if(_3dnow)
|
|
392 printf( "mp3lib: Using AMD 3dnow%s! optimized decore.\n",(_3dnow>1?"-dsp(k7)":""));
|
1253
|
393 #else
|
|
394 _CpuID = _i586 = _3dnow = _isse = _has_mmx = 0;
|
|
395 printf( "mp3lib: Using generic decore.\n");
|
|
396 #endif
|
1276
|
397 #ifdef HAVE_MMX
|
1245
|
398 /* Use it for any MMX cpu */
|
1276
|
399 if(_has_mmx)
|
|
400 make_decode_tables_MMX(outscale);
|
|
401 else
|
|
402 #endif
|
|
403 make_decode_tables(outscale);
|
732
|
404 #ifdef USE_FAKE_MONO
|
|
405 if (fakemono == 1)
|
|
406 fr.synth=synth_1to1_l;
|
|
407 else if (fakemono == 2)
|
|
408 fr.synth=synth_1to1_r;
|
|
409 else
|
|
410 fr.synth=synth_1to1;
|
|
411 #else
|
1
|
412 fr.synth=synth_1to1;
|
732
|
413 #endif
|
1
|
414 fr.synth_mono=synth_1to1_mono2stereo;
|
|
415 fr.down_sample=0;
|
|
416 fr.down_sample_sblimit = SBLIMIT>>(fr.down_sample);
|
|
417 init_layer2();
|
|
418 init_layer3(fr.down_sample_sblimit);
|
|
419 tables_done_flag=1;
|
1245
|
420
|
|
421 dct36_func=dct36;
|
1258
|
422 #ifdef HAVE_SSE
|
1245
|
423 if(_isse)
|
|
424 {
|
|
425 synth_func=synth_1to1_MMX;
|
|
426 dct64_MMX_func=dct64_MMX;
|
|
427 }
|
|
428 else
|
1258
|
429 #endif
|
|
430 #ifdef HAVE_3DNOWEX
|
1245
|
431 if ( _3dnow > 1 )
|
|
432 {
|
|
433 synth_func=synth_1to1_MMX;
|
|
434 dct36_func=dct36_3dnowex;
|
|
435 dct64_MMX_func=dct64_MMX_3dnowex;
|
|
436 }
|
|
437 else
|
1258
|
438 #endif
|
|
439 #ifdef HAVE_3DNOW
|
1245
|
440 if ( _3dnow )
|
|
441 {
|
|
442 synth_func=synth_1to1_MMX;
|
|
443 dct36_func=dct36_3dnow;
|
|
444 dct64_MMX_func=dct64_MMX_3dnow;
|
|
445 }
|
|
446 else
|
1258
|
447 #endif
|
|
448 #ifdef HAVE_MMX
|
1245
|
449 if ( _i586 > 1)
|
|
450 {
|
|
451 synth_func=synth_1to1_MMX;
|
|
452 dct64_MMX_func=dct64_MMX;
|
|
453 }
|
|
454 else
|
1258
|
455 #endif
|
|
456 #ifdef ARCH_X86
|
1245
|
457 if ( _i586 )
|
|
458 {
|
|
459 synth_func=synth_1to1_pent;
|
|
460 }
|
|
461 else
|
1258
|
462 #endif
|
1245
|
463 {
|
|
464 synth_func = NULL;
|
|
465 }
|
1
|
466 }
|
|
467
|
|
468 #if 0
|
|
469
|
|
470 void MP3_Close(){
|
|
471 MP3_eof=1;
|
|
472 if(mp3_file) fclose(mp3_file);
|
|
473 mp3_file=NULL;
|
|
474 }
|
|
475
|
|
476 // Open a file, init buffers. Call once per file!
|
|
477 int MP3_Open(char *filename,int buffsize){
|
|
478 MP3_eof=1; // lock decoding
|
|
479 MP3_pause=1; // lock playing
|
|
480 if(mp3_file) MP3_Close(); // close prev. file
|
|
481 MP3_frames=0;
|
|
482
|
|
483 mp3_file=fopen(filename,"rb");
|
|
484 // printf("MP3_Open: file='%s'",filename);
|
|
485 // if(!mp3_file){ printf(" not found!\n"); return 0;} else printf("Ok!\n");
|
|
486 if(!mp3_file) return 0;
|
|
487
|
|
488 MP3_filesize=MP3_PrintTAG();
|
|
489 fseek(mp3_file,0,SEEK_SET);
|
|
490
|
|
491 MP3_InitBuffers(buffsize);
|
|
492 if(!tables_done_flag) MP3_Init();
|
|
493 MP3_eof=0; // allow decoding
|
|
494 MP3_pause=0; // allow playing
|
|
495 return MP3_filesize;
|
|
496 }
|
|
497
|
|
498 #endif
|
|
499
|
|
500 // Read & decode a single frame. Called by sound driver.
|
|
501 int MP3_DecodeFrame(unsigned char *hova,short single){
|
|
502 pcm_sample = hova;
|
|
503 pcm_point = 0;
|
|
504 if(!read_frame(&fr))return(0);
|
|
505 if(single==-2){ set_pointer(512); return(1); }
|
|
506 if(fr.error_protection) getbits(16); /* skip crc */
|
|
507 fr.single=single;
|
|
508 switch(fr.lay){
|
|
509 case 2: do_layer2(&fr,single);break;
|
|
510 case 3: do_layer3(&fr,single);break;
|
|
511 }
|
|
512 // ++MP3_frames;
|
|
513 return(pcm_point?pcm_point:2);
|
|
514 }
|
|
515
|
|
516 #if 0
|
|
517
|
|
518 // Prints last frame header in ascii.
|
|
519 void MP3_PrintHeader(){
|
|
520 static char *modes[4] = { "Stereo", "Joint-Stereo", "Dual-Channel", "Single-Channel" };
|
|
521 static char *layers[4] = { "???" , "I", "II", "III" };
|
|
522
|
|
523 printf("\rMPEG %s, Layer %s, %ld Hz %d kbit %s, BPF : %ld\n",
|
|
524 fr.mpeg25 ? "2.5" : (fr.lsf ? "2.0" : "1.0"),
|
|
525 layers[fr.lay],freqs[fr.sampling_frequency],
|
|
526 tabsel_123[fr.lsf][fr.lay-1][fr.bitrate_index],
|
|
527 modes[fr.mode],fr.framesize+4);
|
|
528 printf("Channels: %d, copyright: %s, original: %s, CRC: %s, emphasis: %d.\n\n",
|
|
529 fr.stereo,fr.copyright?"Yes":"No",
|
|
530 fr.original?"Yes":"No",fr.error_protection?"Yes":"No",
|
|
531 fr.emphasis);
|
|
532 }
|
|
533
|
|
534 #include "genre.h"
|
|
535
|
|
536 // Read & print ID3 TAG. Do not call when playing!!! returns filesize.
|
|
537 int MP3_PrintTAG(){
|
|
538 struct id3tag {
|
|
539 char tag[3];
|
|
540 char title[30];
|
|
541 char artist[30];
|
|
542 char album[30];
|
|
543 char year[4];
|
|
544 char comment[30];
|
|
545 unsigned char genre;
|
|
546 };
|
|
547 struct id3tag tag;
|
|
548 char title[31]={0,};
|
|
549 char artist[31]={0,};
|
|
550 char album[31]={0,};
|
|
551 char year[5]={0,};
|
|
552 char comment[31]={0,};
|
|
553 char genre[31]={0,};
|
|
554 int fsize;
|
|
555 int ret;
|
|
556
|
|
557 fseek(mp3_file,0,SEEK_END);
|
|
558 fsize=ftell(mp3_file);
|
|
559 if(fseek(mp3_file,-128,SEEK_END)) return fsize;
|
|
560 ret=fread(&tag,128,1,mp3_file);
|
|
561 if(ret!=1 || tag.tag[0]!='T' || tag.tag[1]!='A' || tag.tag[2]!='G') return fsize;
|
|
562
|
|
563 strncpy(title,tag.title,30);
|
|
564 strncpy(artist,tag.artist,30);
|
|
565 strncpy(album,tag.album,30);
|
|
566 strncpy(year,tag.year,4);
|
|
567 strncpy(comment,tag.comment,30);
|
|
568
|
|
569 if ( tag.genre <= sizeof(genre_table)/sizeof(*genre_table) ) {
|
|
570 strncpy(genre, genre_table[tag.genre], 30);
|
|
571 } else {
|
|
572 strncpy(genre,"Unknown",30);
|
|
573 }
|
|
574
|
|
575 // printf("\n");
|
|
576 printf("Title : %30s Artist: %s\n",title,artist);
|
|
577 printf("Album : %30s Year : %4s\n",album,year);
|
|
578 printf("Comment: %30s Genre : %s\n",comment,genre);
|
|
579 printf("\n");
|
|
580 return fsize-128;
|
|
581 }
|
|
582
|
|
583 #endif
|