This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmp3.c
399 lines (330 loc) · 10.1 KB
/
mp3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <mad.h>
#include "fat.h"
#include "mp3.h"
#include <libdragon.h>
static struct mad_stream Stream;
static struct mad_header Header;
static struct mad_frame Frame;
static struct mad_synth Synth;
static mad_timer_t Timer;
typedef struct {
short left;
short right;
} Sample;
static int eos;
#define INPUT_BUFFER_SIZE 2048
static unsigned char fileBuffer[INPUT_BUFFER_SIZE];
static unsigned char readBuffer[INPUT_BUFFER_SIZE];
static int useReadBuffer;
static int readPos;
static unsigned int readLen;
static int samplesRead;
char * mp3File;
char * mp3Fd;
long mp3File_fptr=0;
int mp3File_fsize;
extern int gBrowser;
extern char path[1024];
extern void c2wstrcpy(void *dst, void *src);
extern void c2wstrcat(void *dst, void *src);
static int mp3_seek(char* fd, int offset, int whence) {
//todo filesize and mp3File_fptr;
long offs = 0;
// libff routine
switch (whence)
{
case SEEK_SET:
offs = offset;
break;
case SEEK_CUR:
offs = mp3File_fptr + offset;
break;
case SEEK_END:
offs = mp3File_fsize + offset;
break;
}
//f_lseek(&mp3File, offs);
mp3File_fptr=offs;
return offs;
}
static int mp3_size(char* fd) {
FatRecord rec_tmpf;
u8 resp=0;
resp = fatOpenFileByeName(fd, 0); //err if not found ^^
int fsize = file.sec_available*512; //fsize in bytes
mp3File_fsize = fsize;
//todo filesize
return mp3File_fsize;
}
static void _f_read(char* fname, unsigned char *readBuffer, int size){
/*
FatRecord rec_tmpf;
u8 resp=0;
resp = fatOpenFileByeName(fname, 0); //err if not found ^^
int fsize = file.sec_available*512; //fsize in bytes
mp3File_fsize = fsize;
//injecting in buffer... slow but working :/
if(file.sec_available*512>=size){
resp = fatReadPartialFile(readBuffer, size/512, mp3File_fptr);
//resp = fatReadFile(readBuffer+mp3File_fptr, size/512);//file.sec_available);
mp3File_fptr+=size;
}
//dma_write_s(buffer, 0xb0000000, fsize);
*/
}
static int mp3_read(char* fd, unsigned char *ptr, int size)
{
int ts=size;
_f_read(fd, ptr, size);
return ts;
}
static int id3_tag_size(unsigned char const *buf, int remaining) {
int size;
if (remaining < 10)
return 0;
if (!strncmp((char*)buf, "ID3", 3) || !strncmp((char*)buf, "ea3", 3)) //skip past id3v2 header, which can cause a false sync to be found
{
//get the real size from the syncsafe int
size = buf[6];
size = (size<<7) | buf[7];
size = (size<<7) | buf[8];
size = (size<<7) | buf[9];
size += 10;
if (buf[5] & 0x10) //has footer
size += 10;
}
return size;
}
//Seek next valid frame after ID3/EA3 header
//NOTE: adapted from Music prx 0.55 source
// credit goes to joek2100.
static int MP3_SkipHdr(char* fd)
{
int offset = 0;
unsigned char buf[1024];
unsigned char *pBuffer;
int i;
int size = 0;
offset = mp3_seek(fd, 0, SEEK_CUR);
mp3_read(fd, buf, sizeof(buf));
if (!strncmp((char*)buf, "ID3", 3) || !strncmp((char*)buf, "ea3", 3)) //skip past id3v2 header, which can cause a false sync to be found
{
//get the real size from the syncsafe int
size = buf[6];
size = (size<<7) | buf[7];
size = (size<<7) | buf[8];
size = (size<<7) | buf[9];
size += 10;
if (buf[5] & 0x10) //has footer
size += 10;
offset += size;
}
mp3_seek(fd, offset, SEEK_SET);
//now seek for a sync
while(1) {
offset = mp3_seek(fd, 0, SEEK_CUR);
size = mp3_read(fd, buf, sizeof(buf));
if (size <= 2)//at end of file
return -1;
if (!strncmp((char*)buf, "EA3", 3)) //oma mp3 files have non-safe ints in the EA3 header
{
mp3_seek(fd, (buf[4]<<8)+buf[5], SEEK_CUR);
continue;
}
pBuffer = buf;
for( i = 0; i < size; i++) {
//if this is a valid frame sync (0xe0 is for mpeg version 2.5,2+1)
if ( (pBuffer[i] == 0xff) && ((pBuffer[i+1] & 0xE0) == 0xE0) ) {
offset += i;
mp3_seek(fd, offset, SEEK_SET);
return offset;
}
}
//go back two bytes to catch any syncs that on the boundary
mp3_seek(fd, -2, SEEK_CUR);
}
}
static short convertSample(mad_fixed_t Fixed) {
/* Clipping */
if (Fixed >= MAD_F_ONE)
return (32767);
if (Fixed <= -MAD_F_ONE)
return (-32768);
/* Conversion. */
Fixed = Fixed >> (MAD_F_FRACBITS - 15);
return ((short)Fixed);
}
static int fillFileBuffer() {
int leftOver = Stream.bufend - Stream.next_frame;
int want = INPUT_BUFFER_SIZE - leftOver;
// move left-over bytes
if (leftOver > 0)
memmove(fileBuffer, fileBuffer + want, leftOver);
// fill remainder of buffer
unsigned char* bufferPos = fileBuffer + leftOver;
while (want > 0) {
int got = mp3_read(mp3Fd, bufferPos, want);
if (got <= 0)
return 1; // EOF
want -= got;
bufferPos += got;
}
return 0;
}
static void decode() {
while (mad_frame_decode(&Frame, &Stream) == -1) {
if ((Stream.error == MAD_ERROR_BUFLEN) || (Stream.error == MAD_ERROR_BUFPTR)) {
if (fillFileBuffer()) {
eos = 1;
break;
}
mad_stream_buffer(&Stream, fileBuffer, INPUT_BUFFER_SIZE);
}
else if (Stream.error == MAD_ERROR_LOSTSYNC) {
/* LOSTSYNC - due to ID3 tags? */
int tagsize = id3_tag_size(Stream.this_frame, Stream.bufend - Stream.this_frame);
if (tagsize > 0) {
mad_stream_skip (&Stream, tagsize);
continue;
}
}
}
mad_timer_add(&Timer, Frame.header.duration);
mad_synth_frame(&Synth, &Frame);
}
static void convertLeftSamples(Sample* first, Sample* last, const mad_fixed_t* src) {
for (Sample *dst = first; dst != last; ++dst)
dst->left = convertSample(*src++);
}
static void convertRightSamples(Sample* first, Sample* last, const mad_fixed_t* src) {
for (Sample *dst = first; dst != last; ++dst)
dst->right = convertSample(*src++);
}
static void MP3_Callback(void *buffer, unsigned int samplesToWrite) {
Sample *destination = (Sample*)buffer;
while (samplesToWrite > 0) {
while (!eos && (Synth.pcm.length == 0))
decode();
if (eos) {
// done
memset(destination, 0, samplesToWrite*4);
break;
}
unsigned int samplesAvailable = Synth.pcm.length - samplesRead;
if (samplesAvailable > samplesToWrite) {
convertLeftSamples(destination, destination + samplesToWrite, &Synth.pcm.samples[0][samplesRead]);
convertRightSamples(destination, destination + samplesToWrite, &Synth.pcm.samples[1][samplesRead]);
samplesRead += samplesToWrite;
samplesToWrite = 0;
}
else {
convertLeftSamples(destination, destination + samplesAvailable, &Synth.pcm.samples[0][samplesRead]);
convertRightSamples(destination, destination + samplesAvailable, &Synth.pcm.samples[1][samplesRead]);
destination += samplesAvailable;
samplesToWrite -= samplesAvailable;
samplesRead = 0;
decode();
}
}
}
static void MP3_Init() {
/* First the structures used by libmad must be initialized. */
mad_stream_init(&Stream);
mad_header_init(&Header);
mad_frame_init(&Frame);
mad_synth_init(&Synth);
mad_timer_reset(&Timer);
}
static void MP3_Exit() {
mad_synth_finish(&Synth);
mad_header_finish(&Header);
mad_frame_finish(&Frame);
mad_stream_finish(&Stream);
}
static void MP3_GetInfo(long long *samples, int *rate) {
unsigned long FrameCount = 0;
int bufferSize = 1024*512;
unsigned char *localBuffer;
long red = 0;
double totalBitrate = 0.0;
double mediumBitrate = 0.0;
struct mad_stream stream;
struct mad_header header;
int size = mp3_size(mp3Fd);
long count = size;
mad_stream_init (&stream);
mad_header_init (&header);
localBuffer = (unsigned char *)malloc(bufferSize);
for (int i=0; i<3; i++) {
memset(localBuffer, 0, bufferSize);
if (count > bufferSize)
red = mp3_read(mp3Fd, localBuffer, bufferSize);
else
red = mp3_read(mp3Fd, localBuffer, count);
count -= red;
if (!red)
break; // ran out of data
mad_stream_buffer (&stream, localBuffer, red);
while (1) {
if (mad_header_decode(&header, &stream) == -1) {
if (stream.buffer == NULL || stream.error == MAD_ERROR_BUFLEN) {
break;
}
else if (MAD_RECOVERABLE(stream.error)) {
continue;
}
else {
break;
}
}
if (FrameCount++ == 0)
*rate = header.samplerate;
totalBitrate += header.bitrate;
}
}
mediumBitrate = totalBitrate / (double)FrameCount;
int secs = size * 8 / mediumBitrate;
*samples = *rate * secs;
mad_header_finish (&header);
mad_stream_finish (&stream);
if (localBuffer)
free(localBuffer);
mp3_seek(mp3Fd, 0, SEEK_SET);
}
void start_mp3(char *fname, long long *samples, int *rate, int *channels) {
sprintf(mp3Fd, "%s", fname);
//if (mp3Fd[0]!=0)
//{
useReadBuffer = 0;
MP3_GetInfo(samples, rate);
*channels = 2;
MP3_Init();
MP3_SkipHdr(mp3Fd);
eos = readLen = readPos = 0;
useReadBuffer = 1;
return;
//}
//*samples = 0;
}
void stop_mp3(void) {
MP3_Exit();
mp3File_fptr=0;
/*
if (mp3Fd > 0)
{
if (gBrowser)
f_close(&mp3File);
else
dfs_close(mp3Fd);
}
mp3Fd = -1;
*/
}
int update_mp3(char *buf, int bytes) {
MP3_Callback(buf, bytes/4);
return eos ? 0 : 1;
}