4 #include "../../Pico/sound/mix.h"
5 #include "code940/940shared.h"
6 #include "helix/pub/mp3dec.h"
8 static short mp3_out_buffer[2*1152];
9 static HMP3Decoder mp3dec = 0;
10 static int mp3_buffer_offs = 0;
12 extern _940_ctl_t *shared_ctl;
13 extern unsigned char *mp3_mem;
17 static int try_get_header(unsigned char *buff, MP3FrameInfo *fi)
19 int ret, offs1, offs = 0;
23 offs1 = MP3FindSyncWord(buff + offs, 2048 - offs);
24 if (offs1 < 0) return -2;
26 if (2048 - offs < 4) return -3;
28 // printf("trying header %08x\n", *(int *)(buff + offs));
30 ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs);
31 if (ret == 0 && fi->bitrate != 0) break;
38 int mp3_get_bitrate(FILE *f, int len)
40 unsigned char buff[2048];
44 memset(buff, 0, 2048);
46 if (!mp3dec) mp3dec = MP3InitDecoder();
48 fseek(f, 0, SEEK_SET);
49 ret = fread(buff, 1, 2048, f);
50 fseek(f, 0, SEEK_SET);
51 if (ret <= 0) return -1;
53 ret = try_get_header(buff, &fi);
54 if (ret != 0 || fi.bitrate == 0) {
55 // try to read somewhere around the middle
56 fseek(f, len>>1, SEEK_SET);
57 fread(buff, 1, 2048, f);
58 fseek(f, 0, SEEK_SET);
59 ret = try_get_header(buff, &fi);
61 if (ret != 0) return ret;
63 // printf("bitrate: %i\n", fi.bitrate / 1000);
65 return fi.bitrate / 1000;
69 static void mp3_decode(void)
71 // tried copying this to cached mem, no improvement noticed
72 int mp3_offs = shared_ctl->mp3_offs;
73 unsigned char *readPtr = mp3_mem + mp3_offs;
74 int bytesLeft = shared_ctl->mp3_len - mp3_offs;
75 int offset; // frame offset from readPtr
78 if (bytesLeft <= 0) return; // EOF, nothing to do
80 offset = MP3FindSyncWord(readPtr, bytesLeft);
82 shared_ctl->mp3_offs = shared_ctl->mp3_len;
88 err = MP3Decode(mp3dec, &readPtr, &bytesLeft, mp3_out_buffer, 0);
90 if (err == ERR_MP3_INDATA_UNDERFLOW) {
91 shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF
93 } else if (err <= -6 && err >= -12) {
94 // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
95 // just try to skip the offending frame..
98 shared_ctl->mp3_errors++;
99 shared_ctl->mp3_lasterr = err;
101 shared_ctl->mp3_offs = readPtr - mp3_mem;
105 void mp3_update_local(int *buffer, int length, int stereo)
107 int length_mp3, shr = 0;
108 void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
111 if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; length_mp3 <<= 1; shr = 1; }
112 else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; length_mp3 <<= 2; shr = 2; }
114 if (1152 - mp3_buffer_offs >= length_mp3) {
115 mix_samples(buffer, mp3_out_buffer + mp3_buffer_offs*2, length<<1);
117 mp3_buffer_offs += length_mp3;
119 int left = 1152 - mp3_buffer_offs;
121 mix_samples(buffer, mp3_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1);
123 mp3_buffer_offs = length_mp3 - left;
124 mix_samples(buffer + ((left>>shr)<<1), mp3_out_buffer, (mp3_buffer_offs>>shr)<<1);
129 void mp3_start_local(void)