-
Notifications
You must be signed in to change notification settings - Fork 42
/
play_sd_mp3.cpp
306 lines (236 loc) · 7.85 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
/*
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 2048 //Enough space for a complete stereo frame
#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;};
fclose();
}
/*
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;
}
//Read-ahead 10 Bytes to detect ID3
sd_left = fread(sd_buf, 10);
//Skip ID3, if existent
int skip = skipID3(sd_buf);
if (skip) {
size_id3 = skip;
int b = skip & 0xfffffe00;
fseek(b);
sd_left = 0;
// Serial.print("skip = ");
// Serial.println(fposition());
} else size_id3 = 0;
//Fill buffer from the beginning with fresh data
sd_left = fillReadBuffer(file, sd_buf, sd_buf, sd_left, MP3_SD_BUF_SIZE);
if (!sd_left) {
lastError = ERR_CODEC_FILE_NOT_FOUND;
stop();
return lastError;
}
_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; i++) decodeMp3();
if((mp3FrameInfo.samprate != AUDIOCODECS_SAMPLE_RATE ) || (mp3FrameInfo.bitsPerSample != 16) || (mp3FrameInfo.nChans > 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;
}
//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;
//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.
int db = decoding_block;
if (!NVIC_IS_ACTIVE(IRQ_AUDIOCODEC))
if (decoded_length[db]==0)
NVIC_TRIGGER_INTERRUPT(IRQ_AUDIOCODEC);
//determine the block we're playing from
int playing_block = 1 - db;
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 (mp3FrameInfo.nChans == 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);
//Switch to the next block if we have no data to play anymore:
if (decoded_length[playing_block] == 0)
{
decoding_block = playing_block;
play_pos = 0;
} else
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] > 0 ) return; //this block is playing, 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->file, 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], 0);
switch(decode_res)
{
case ERR_MP3_NONE:
{
MP3GetLastFrameInfo(o->hMP3Decoder, &o->mp3FrameInfo);
o->decoded_length[db] = o->mp3FrameInfo.outputSamps;
break;
}
case ERR_MP3_MAINDATA_UNDERFLOW:
{
break;
}
default :
{
AudioPlaySdMp3::lastError = decode_res;
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();
}