forked from FrankBoesing/Arduino-Teensy-Codec-lib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
play_sd_mp3.cpp
516 lines (423 loc) · 14.4 KB
/
play_sd_mp3.cpp
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
Arduino Audiocodecs
Copyright (c) 2014 Frank Bösing
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library. If not, see <http://www.gnu.org/licenses/>.
The helix decoder itself as a different license, look at the subdirectories for more info.
Diese Bibliothek ist freie Software: Sie können es unter den Bedingungen
der GNU General Public License, wie von der Free Software Foundation,
Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
veröffentlichten Version, weiterverbreiten und/oder modifizieren.
Diese Bibliothek wird in der Hoffnung, dass es nützlich sein wird, aber
OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
Siehe die GNU General Public License für weitere Details.
Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
Der Helixdecoder selbst hat eine eigene Lizenz, bitte für mehr Informationen
in den Unterverzeichnissen nachsehen.
*/
/* The Helix-Library is modified for Teensy 3.1 */
// Total RAM Usage: 35 KB
#include "play_sd_mp3.h"
#define MP3_SD_BUF_SIZE 4096 //Enough space for 2 complete stereo frames. If not using seeking, then only half of this is needed
#define MP3_BUF_SIZE (MAX_NCHAN * MAX_NGRAN * MAX_NSAMP) //MP3 output buffer
#define DECODE_NUM_STATES 2 //How many steps in decode() ?
#define MIN_FREE_RAM (35+1)*1024 // 1KB Reserve
//#define CODEC_DEBUG
static AudioPlaySdMp3 *mp3objptr;
void decodeMp3(void);
void AudioPlaySdMp3::stop(void)
{
NVIC_DISABLE_IRQ(IRQ_AUDIOCODEC);
playing = codec_stopped;
if (buf[1]) {free(buf[1]);buf[1] = NULL;}
if (buf[0]) {free(buf[0]);buf[0] = NULL;}
freeBuffer();
if (hMP3Decoder) {MP3FreeDecoder(hMP3Decoder);hMP3Decoder=NULL;};
}
/*
float AudioPlaySdMp3::processorUsageMaxDecoder(void){
//this is somewhat incorrect, it does not take the interruptions of update() into account -
//therefore the returned number is too high.
//Todo: better solution
return (decode_cycles_max / (0.026*F_CPU)) * 100;
};
float AudioPlaySdMp3::processorUsageMaxSD(void){
//this is somewhat incorrect, it does not take the interruptions of update() into account -
//therefore the returned number is too high.
//Todo: better solution
return (decode_cycles_max_sd / (0.026*F_CPU)) * 100;
};
*/
int AudioPlaySdMp3::play(void)
{
lastError = ERR_CODEC_NONE;
initVars();
sd_buf = allocBuffer(MP3_SD_BUF_SIZE);
if (!sd_buf) return ERR_CODEC_OUT_OF_MEMORY;
mp3objptr = this;
buf[0] = (short *) malloc(MP3_BUF_SIZE * sizeof(int16_t));
buf[1] = (short *) malloc(MP3_BUF_SIZE * sizeof(int16_t));
hMP3Decoder = MP3InitDecoder();
if (!buf[0] || !buf[1] || !hMP3Decoder)
{
lastError = ERR_CODEC_OUT_OF_MEMORY;
stop();
return lastError;
}
// parse and skip ID3
size_id3 = parseID3();
fseek(size_id3);
//Fill buffer from the beginning with fresh data
sd_left = fillReadBuffer(sd_buf, sd_buf, 0, MP3_SD_BUF_SIZE);
if (!sd_left) {
lastError = ERR_CODEC_FILE_NOT_FOUND;
stop();
return lastError;
}
// check and parse Xing header for VBR file duration
vbr_total_frames = 0;
int syncOffset = MP3FindSyncWord(sd_buf, sd_left);
if (syncOffset <= sd_left - 160) { // xing header is less than 160 bytes
XHEADDATA xingHeaderData;
xingHeaderData.toc = NULL; // not reading TOC right now
if(GetXingHeader(&xingHeaderData, sd_buf + syncOffset)){
Serial.println("found xing VBR header");
vbr_total_frames = xingHeaderData.frames;
}
}
_VectorsRam[IRQ_AUDIOCODEC + 16] = &decodeMp3;
initSwi();
decoded_length[0] = 0;
decoded_length[1] = 0;
decoding_block = 0;
decoding_state = 0;
play_pos = 0;
sd_p = sd_buf;
for (size_t i=0; i< DECODE_NUM_STATES * 2; i++) decodeMp3();
samplerate = mp3FrameInfo.samprate;
// only MPEG-1 layer 3 (16k-24k) and MPEG-2 layer 3 (32k-48k) are supported
if((samplerate < 16000) || (mp3FrameInfo.bitsPerSample != 16) || (_channels > 2)) {
//Serial.println("incompatible MP3 file.");
lastError = ERR_CODEC_FORMAT;
stop();
return lastError;
}
decoding_block = 1;
playing = codec_playing;
#ifdef CODEC_DEBUG
// Serial.printf("RAM: %d\r\n",ram-freeRam());
#endif
return lastError;
}
uint32_t AudioPlaySdMp3::timeMsToOffset(uint32_t timeMs, uint8_t *toc)
{
uint64_t sizeWithoutID3 = fsize() - size_id3;
if (vbr_total_frames && toc) {
// use Xing DXHEAD for VBR
return size_id3 + SeekPoint(toc, sizeWithoutID3, 100.0 * timeMs / lengthMillis());
} else {
// offset calculation for CBR
return size_id3 + sizeWithoutID3 * timeMs / lengthMillis();
}
}
uint32_t AudioPlaySdMp3::offsetToTimeMs(uint32_t offset, uint8_t *toc)
{
uint32_t sizeWithoutID3 = fsize() - size_id3;
if (vbr_total_frames && toc) {
// use Xing DXHEAD for VBR
float tocValue = 256.0 * (offset - size_id3) / sizeWithoutID3;
int l = 0, h = 99, loffset = 0, hoffset = 255;
if (tocValue > toc[99]) {
// special case near end of file
l = 99;
loffset = toc[99];
h = 100;
hoffset = 256;
} else {
// do a binary search to find the toc entry that's just before the offset
while (h - l >= 2){
int mid = (h + l) / 2;
if (toc[mid] < tocValue) {
l = mid;
loffset = toc[mid];
} else if (toc[mid] > tocValue) {
h = mid;
hoffset = toc[mid];
} else {
// highly unlikely
return lengthMillis() * mid / 100;
}
}
}
Serial.print("l="); Serial.print(l); Serial.print(", h="); Serial.println(h);
return lengthMillis() * (l + (tocValue - loffset) / (hoffset - loffset) * (h - l)) / 100;
} else {
// calculation for CBR
return (uint64_t)(offset - size_id3) * lengthMillis() / sizeWithoutID3;
}
}
// MPEG-2 layer 3 (16k-24k sample rates) has only 576 samples per frame as opposed to 1152.
// 576 = 128 * 4.5, so for these files we need to decode 2 frames to get a multiple of 128.
uint32_t AudioPlaySdMp3::requiredBufSamples(void)
{
if (_channels == 0) {
return MP3_BUF_SIZE;
} else {
return MAX_NGRAN * MAX_NSAMP * _channels;
}
}
bool AudioPlaySdMp3::seek(uint32_t timesec)
{
if (!isPlaying()) {
return false;
}
pause(true);
uint8_t toc[100];
bool tocValid = false;
if (vbr_total_frames) {
// use TOC in VBR header for offset
fseek(size_id3);
fillReadBuffer(sd_buf, sd_buf, 0, MP3_SD_BUF_SIZE);
int syncOffset = MP3FindSyncWord(sd_buf, MP3_SD_BUF_SIZE);
if (syncOffset <= MP3_SD_BUF_SIZE - 160) {
XHEADDATA xingHeaderData;
xingHeaderData.toc = toc;
if(GetXingHeader(&xingHeaderData, sd_buf + syncOffset)){
Serial.println("using xing VBR header");
tocValid = true;
}
}
}
uint32_t targetOffset = timeMsToOffset(timesec * 1000, tocValid ? toc : NULL);
Serial.print("MP3 seeking to offset ");
Serial.println(targetOffset);
fseek(targetOffset);
// clear and refill MP3 stream buffer
sd_p = sd_buf;
sd_left = fillReadBuffer(sd_buf, sd_buf, 0, MP3_SD_BUF_SIZE);
if (!sd_left) {
Serial.println("fillReadBuffer failed EOF?");
stop();
return true;
}
decoded_length[0] = decoded_length[1] = 0;
int validFrameFound = 0;
// Sometimes after one successful decode after seek, the next decode can still fail.
// I don't know if it's possible for a decode to fail after 2 successful decodes, but I will require 3 consecutive successful decodes to be safe.
// The better way to handle it is to change decodeMp3 to not declare EOF as soon as it hits an error. For now this loop will do.
while(validFrameFound < 3) {
// MP3_SD_BUF_SIZE is not enough to fit 2 320kbps frames.
// So advance to next MP3 frame and fill buffer again to avoid buffer underflow in decoding.
int offset = MP3FindSyncWord(sd_p, sd_left);
if (offset < 0) {
Serial.println("No sync"); //no error at end of file
stop();
return true;
} else {
Serial.print("sync at buffer offset ");
Serial.println(offset);
sd_p += offset;
sd_left -= offset;
// The only way to fully know if we found a valid frame is by trying to decode it
int decode_res = MP3Decode(hMP3Decoder, &sd_p, (int*)&sd_left, buf[decoding_block] + decoded_length[decoding_block], 0);
if (decode_res) {
Serial.print("decode error ");
Serial.println(decode_res);
if (decode_res == ERR_MP3_INVALID_FRAMEHEADER) {
// keep going to find next frameheader
sd_p += 1;
sd_left -= 1;
}
validFrameFound = 0;
decoded_length[0] = decoded_length[1] = 0;
} else {
MP3GetLastFrameInfo(hMP3Decoder, &mp3FrameInfo);
// For 44100Hz, each decode yields 2304 samples. For 22050Hz, each decode yields 1152 samples
// If the decoder got the wrong channel count or sample rate after seeking (i.e. 22050Hz for a 44100Hz file),
// it will fill the buffer partially, and the next decode may write past the end of the buffer.
if (mp3FrameInfo.nChans != _channels || (uint32_t)mp3FrameInfo.samprate != samplerate) {
Serial.println("sample rate or channel count mismatch, treat as decode error");
validFrameFound = 0;
decoded_length[0] = decoded_length[1] = 0;
} else {
// don't throw away the decoding result
decoded_length[decoding_block] += mp3FrameInfo.outputSamps;
if(decoded_length[decoding_block] >= requiredBufSamples()) {
decoding_block = 1 - decoding_block;
decoded_length[decoding_block] = 0;
}
validFrameFound++;
}
}
}
sd_left = fillReadBuffer(sd_buf, sd_p, sd_left, MP3_SD_BUF_SIZE);
if (!sd_left) {
Serial.println("fillReadBuffer failed EOF?");
stop();
return true;
}
sd_p = sd_buf;
}
decoding_state = 1; // we refilled the buffer
for (int i=0; i< DECODE_NUM_STATES * 2; i++) {
if (isPlaying()) {
decodeMp3();
if (lastError) {
Serial.print("lastError ");
Serial.println(lastError);
}
}
}
play_pos = 0;
// we don't know where we are after all the retries, so calculate time from file offset
samples_played = (uint64_t)offsetToTimeMs(fposition() - MP3_SD_BUF_SIZE, tocValid ? toc : NULL) * samplerate / 1000;
if (isPlaying()) {
pause(false);
return true;
} else {
Serial.print("lastError ");
Serial.println(lastError);
return false;
}
}
uint32_t AudioPlaySdMp3::lengthMillis()
{
if (vbr_total_frames) {
// for VBR
return (uint64_t)vbr_total_frames * mp3FrameInfo.outputSamps / _channels * 1000 / samplerate;
} else {
// for CBR
uint64_t sizeWithoutID3 = fsize() - size_id3;
return sizeWithoutID3 * 1000 / (mp3FrameInfo.bitrate / 8);
}
}
//runs in ISR
__attribute__ ((optimize("O2")))
void AudioPlaySdMp3::update(void)
{
audio_block_t *block_left;
audio_block_t *block_right;
//paused or stopped ?
if (playing != codec_playing) return;
//determine the block we're playing from
int db = decoding_block;
int playing_block = 1 - db;
if (decoded_length[playing_block] <= 0) {
//Switch to the next block if we have no data to play anymore:
if (decoded_length[db] >= requiredBufSamples()) {
db = decoding_block = playing_block;
playing_block = 1 - db;
play_pos = 0;
}
}
//chain decoder-interrupt.
//to give the user-sketch some cpu-time, only chain
//if the swi is not active currently.
//In addition, check before if there waits work for it.
if (!NVIC_IS_ACTIVE(IRQ_AUDIOCODEC))
if (decoded_length[db] < requiredBufSamples())
NVIC_TRIGGER_INTERRUPT(IRQ_AUDIOCODEC);
if (decoded_length[playing_block] <= 0) return;
// allocate the audio blocks to transmit
block_left = allocate();
if (block_left == NULL) return;
uintptr_t pl = play_pos;
if (_channels == 2) {
// if we're playing stereo, allocate another
// block for the right channel output
block_right = allocate();
if (block_right == NULL) {
release(block_left);
return;
}
memcpy_frominterleaved(&block_left->data[0], &block_right->data[0], buf[playing_block] + pl);
pl += AUDIO_BLOCK_SAMPLES * 2;
transmit(block_left, 0);
transmit(block_right, 1);
release(block_right);
decoded_length[playing_block] -= AUDIO_BLOCK_SAMPLES * 2;
} else
{
// if we're playing mono, no right-side block
// let's do a (hopefully good optimized) simple memcpy
memcpy(block_left->data, buf[playing_block] + pl, AUDIO_BLOCK_SAMPLES * sizeof(short));
pl += AUDIO_BLOCK_SAMPLES;
transmit(block_left, 0);
transmit(block_left, 1);
decoded_length[playing_block] -= AUDIO_BLOCK_SAMPLES;
}
samples_played += AUDIO_BLOCK_SAMPLES;
release(block_left);
play_pos = pl;
}
//decoding-interrupt
//__attribute__ ((optimize("O2"))) <- does not work here, bug in g++
void decodeMp3(void)
{
AudioPlaySdMp3 *o = mp3objptr;
int db = o->decoding_block;
if ( o->decoded_length[db] >= o->requiredBufSamples() ) return; //this block is already filled, do NOT fill it
uint32_t cycles = ARM_DWT_CYCCNT;
int eof = false;
switch (o->decoding_state) {
case 0:
{
o->sd_left = o->fillReadBuffer(o->sd_buf, o->sd_p, o->sd_left, MP3_SD_BUF_SIZE);
if (!o->sd_left) { eof = true; goto mp3end; }
o->sd_p = o->sd_buf;
uint32_t cycles_rd = (ARM_DWT_CYCCNT - cycles);
if (cycles_rd > o->decode_cycles_max_read )o-> decode_cycles_max_read = cycles_rd;
break;
}
case 1:
{
// find start of next MP3 frame - assume EOF if no sync found
int offset = MP3FindSyncWord(o->sd_p, o->sd_left);
if (offset < 0) {
//Serial.println("No sync"); //no error at end of file
eof = true;
goto mp3end;
}
o->sd_p += offset;
o->sd_left -= offset;
int decode_res = MP3Decode(o->hMP3Decoder, &o->sd_p, (int*)&o->sd_left,o->buf[db] + o->decoded_length[db], 0);
AudioPlaySdMp3::lastError = decode_res;
switch(decode_res)
{
case ERR_MP3_NONE:
{
MP3GetLastFrameInfo(o->hMP3Decoder, &o->mp3FrameInfo);
o->_channels = o->mp3FrameInfo.nChans;
o->decoded_length[db] += o->mp3FrameInfo.outputSamps;
break;
}
case ERR_MP3_MAINDATA_UNDERFLOW:
{
break;
}
default :
{
eof = true;
break;
}
}
cycles = (ARM_DWT_CYCCNT - cycles);
if (cycles > o->decode_cycles_max ) o->decode_cycles_max = cycles;
break;
}
}//switch
mp3end:
o->decoding_state++;
if (o->decoding_state >= DECODE_NUM_STATES) o->decoding_state = 0;
if (eof) o->stop();
}