-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.cpp
575 lines (517 loc) · 15.3 KB
/
player.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#include "player.h"
#include "ui.h"
#include "myxmpcallbacks.h"
#include <EEPROM.h>
#include <AudioStream_F32.h>
#include <AudioConvert_F32.h>
#include <output_i2s_f32.h>
#include <AudioEffectGain_F32.h>
//////////////////// Teensy Audio library
// GUItool: begin automatically generated code
AudioPlaySdMp3 playMp31; //xy=100.25,89.25
AudioPlaySdAac playAac1; //xy=110.25,130.25
AudioPlaySdFlac playFlac1; //xy=119.25,173.25
AudioPlaySdOpus playOpus1;
//AudioSynthWaveformSine sine1; //xy=150.25,217.25
AudioMixer4 mixer1; //xy=371.25,109.25
AudioMixer4 mixer2; //xy=371.25,221.25
AudioMixer4 mixer4;
AudioMixer4 mixer5;
AudioConnection patchCord1(playMp31, 0, mixer1, 0);
AudioConnection patchCord2(playMp31, 1, mixer2, 0);
AudioConnection patchCord3(playAac1, 0, mixer1, 1);
AudioConnection patchCord4(playAac1, 1, mixer2, 1);
AudioConnection patchCord5(playFlac1, 0, mixer1, 2);
AudioConnection patchCord6(playFlac1, 1, mixer2, 2);
AudioConnection patchCord7(playOpus1, 0, mixer4, 0);
AudioConnection patchCord8(playOpus1, 1, mixer5, 0);
AudioConnection patchCord16(mixer4, 0, mixer1, 3);
AudioConnection patchCord17(mixer5, 0, mixer2, 3);
#if defined(__IMXRT1062__)
// Only T4.1 has enough memory for modules
TeensyXmp playModule1;
AudioConnection patchCord14(playModule1, 0, mixer4, 0);
AudioConnection patchCord15(playModule1, 1, mixer5, 0);
EXTMEM uint8_t psram_heap[16*1024*1024];
tlsf_t psram_alloc;
int psram_used = 0;
#endif
//AudioConnection patchCord7(sine1, 0, mixer4, 3);
//AudioConnection patchCord8(sine1, 0, mixer5, 3);
// GUItool: end automatically generated code
AudioPeakHold analyzePeak1;
AudioPeakHold analyzePeak2;
AudioConnection patchCord101(mixer1, analyzePeak1);
AudioConnection patchCord102(mixer2, analyzePeak2);
#if !USE_F32
// 16 bit out
AudioOutputI2S i2s1;
// 16 bit fft
AudioAnalyzeFFT256 fft256;
AudioMixer4 mixer3;
AudioConnection patchCord9(mixer1, 0, i2s1, 0);
AudioConnection patchCord11(mixer2, 0, i2s1, 1);
AudioConnection patchCord10(mixer1, 0, mixer3, 0);
AudioConnection patchCord12(mixer2, 0, mixer3, 3);
AudioConnection patchCord13(mixer3, fft256);
#else
// 32 bit out
AudioConvert_I16toF32 i16tof32l, i16tof32r;
AudioEffectGain_F32 gain1, gain2;
AudioOutputI2S_F32 i2s32;
// float fft
AudioAnalyzeFFT256_F32 fft256;
AudioMixer4_F32 mixer3;
AudioConnection patchCordi01(mixer1, 0, i16tof32l, 0);
AudioConnection patchCordi02(mixer2, 0, i16tof32r, 0);
AudioConnection_F32 patchCordf01(i16tof32l, 0, gain1, 0);
AudioConnection_F32 patchCordf02(i16tof32r, 0, gain2, 0);
AudioConnection_F32 patchCordf03(gain1, 0, i2s32, 0);
AudioConnection_F32 patchCordf04(gain2, 0, i2s32, 1);
AudioConnection_F32 patchCordf05(i16tof32l, 0, mixer3, 0);
AudioConnection_F32 patchCordf06(i16tof32r, 0, mixer3, 3);
AudioConnection_F32 patchCordf07(mixer3, 0, fft256, 0);
#if USE_MRFFT
// multiresolution FFT (downsampled)
AudioAnalyzeFFT256MR_F32 fft256mr;
AudioConnection_F32 patchCordf08(mixer3, 0, fft256mr, 0);
#endif
#endif
FsFile currentFile;
char currentFileName[256] = {0};
MyCodecFile myCodecFile(NULL);
#if defined(__IMXRT1062__)
struct xmp_io_callbacks myXmpIoCb = {
.read = my_xmp_read,
.seek = my_xmp_seek,
.tell = my_xmp_tell,
.eof = my_xmp_eof,
.size = my_xmp_size,
.user_data = NULL,
};
#endif
bool isPaused = false;
#if defined(__IMXRT1062__)
DMAMEM
#endif
DirectoryNavigator dirNav;
#if defined(__IMXRT1062__)
void *xmp_malloc(size_t bytes){
void *ptr = tlsf_malloc(psram_alloc, bytes);
if(!ptr){
Serial.print("xmp_malloc failed to allocate ");
Serial.print(bytes);
Serial.println("bytes");
}else{
psram_used += tlsf_block_size(ptr);
}
return ptr;
}
void xmp_free(void *ptr){
psram_used -= tlsf_block_size(ptr);
tlsf_free(psram_alloc, ptr);
}
void *xmp_realloc(void *ptr, size_t bytes){
int old_block_size = tlsf_block_size(ptr);
ptr = tlsf_realloc(psram_alloc, ptr, bytes);
if(!ptr){
Serial.print("xmp_realloc failed to allocate ");
Serial.print(bytes);
Serial.println("bytes");
}else{
psram_used += tlsf_block_size(ptr) - old_block_size;
}
return ptr;
}
#endif
void startPlayback(){
#if USE_F32
int8_t gain8 = EEPROM.read(EEPROM_OFFSET_VOLUME);
setGain(gain8);
int8_t rgMode = EEPROM.read(EEPROM_OFFSET_REPLAYGAIN);
setReplayGainMode((ReplayGainMode)rgMode);
int8_t fbGain8 = EEPROM.read(EEPROM_OFFSET_REPLAYGAIN + 1);
setReplayGainFallbackGain(fbGain8);
#endif
dirNav.openRoot(&sd);
int8_t resumeAttempt = EEPROM.read(EEPROM_OFFSET_RESUME_ATTEMPT);
if(resumeAttempt < 2){
EEPROM.write(EEPROM_OFFSET_RESUME_ATTEMPT, resumeAttempt + 1);
}else{
Serial.println("resume crashed 2+ times, skip resume this time");
goto noresume;
}
currentFile = dirNav.restoreCurrentFile();
if(currentFile.isOpen()){
Serial.println("continue playing last played file");
playFile(¤tFile);
uint16_t playPosSec = 0;
for(int i = sizeof(playPosSec) - 1; i >= 0; i--){
playPosSec <<= 8;
playPosSec |= EEPROM.read(EEPROM_OFFSET_PLAYTIME + i);
}
if(resumeAttempt < 1){
Serial.print("resume to ");
Serial.println(playPosSec);
if(!seekAbsolute(playPosSec)){
Serial.println("resume seek failed, start from beginning");
stop();
currentFile.rewind();
playFile(¤tFile);
}
}else{
Serial.print("resume crashed once, skip seek this time");
}
EEPROM.write(EEPROM_OFFSET_RESUME_ATTEMPT, 0);
return;
}
// resume failed. go back to root.
while(dirNav.upDir());
noresume:
if (!playNext()) {
displayError("NO FILES ", " ", 10000);
while (true) {
Serial.println("NO FILES");
delay(1000);
}
}
EEPROM.write(EEPROM_OFFSET_RESUME_ATTEMPT, 0);
}
void savePlayerState(){
uint16_t playPosSec = positionMs() / 1000;
for(unsigned int i = 0; i < sizeof(playPosSec); i++){
EEPROM.write(EEPROM_OFFSET_PLAYTIME + i, playPosSec & 0xff);
playPosSec >>= 8;
}
dirNav.saveCurrentFile();
#if USE_F32
int8_t gain8 = min(max(currentGain, -127), 127);
EEPROM.write(EEPROM_OFFSET_VOLUME, gain8);
EEPROM.write(EEPROM_OFFSET_REPLAYGAIN, replayGainMode);
int8_t fbGain8 = min(max(rgFallbackGain, -127), 127);
EEPROM.write(EEPROM_OFFSET_REPLAYGAIN + 1, fbGain8);
#endif
}
// we can't disable the audio interrupt because otherwise the audio output will glitch.
// so we suspend the decoders' decoding while keeping their outputs running
void suspendDecoding() {
// for MP3 and AAC, just disable the ISR
if (playMp31.isPlaying() || playAac1.isPlaying() || playOpus1.isPlaying()) {
NVIC_DISABLE_IRQ(IRQ_AUDIOCODEC);
}
// for FLAC, use the suspend/resumeDecoding interface
if (playFlac1.isPlaying()) {
playFlac1.suspendDecoding();
}
// There is no need to suspend decoding for modules.
// libxmp loads the whole module into memory and plays from there
// so it doesn't conflict with sd card access
}
void resumeDecoding() {
// for MP3 and AAC, just enable and trigger the ISR
if (playMp31.isPlaying() || playAac1.isPlaying() || playOpus1.isPlaying()) {
NVIC_ENABLE_IRQ(IRQ_AUDIOCODEC);
NVIC_TRIGGER_INTERRUPT(IRQ_AUDIOCODEC);
}
// for FLAC, use the resumeDecoding interface to safely fill the buffer
if (playFlac1.isPlaying()) {
playFlac1.resumeDecoding();
}
}
void setSampleRate(unsigned long long sampleRate) {
Serial.print("changing sample rate to ");
Serial.println((int)sampleRate);
float result = setI2SFreq(sampleRate);
if(!result){
Serial.println("sample rate not supported");
}
int nAvg;
if(sampleRate <= 24000){
nAvg = 1;
}else if(sampleRate <= 48000){
nAvg = 2;
}else{
nAvg = 4;
}
fft256.averageTogether(nAvg);
#if USE_MRFFT
fft256mr.averageTogether(nAvg);
#endif
}
AudioCodec *getPlayingCodec() {
if (playMp31.isPlaying()) {
return &playMp31;
} else if (playAac1.isPlaying()) {
return &playAac1;
} else if (playFlac1.isPlaying()) {
return &playFlac1;
} else if (playOpus1.isPlaying()) {
return &playOpus1;
}
return NULL;
}
void stop() {
AudioCodec *playingCodec = getPlayingCodec();
if (playingCodec) {
playingCodec->stop();
}
#if defined(__IMXRT1062__)
if (playModule1.isPlaying()) {
playModule1.stop();
}
#endif
}
void playFile(FsFile *file) {
Serial.print("play file: ");
file->getName(currentFileName, sizeof(currentFileName));
Serial.println(currentFileName);
stop();
file->rewind();
myCodecFile = MyCodecFile(file);
int error = 0;
switch (getFileType(file)) {
case FileType::MP3:
error = playMp31.play(&myCodecFile);
break;
case FileType::AAC:
error = playAac1.play(&myCodecFile);
break;
case FileType::FLAC:
error = playFlac1.play(&myCodecFile);
break;
case FileType::OPUS:
error = playOpus1.play(&myCodecFile);
break;
#if defined(__IMXRT1062__)
case FileType::MODULE:
myXmpIoCb.user_data = file;
error = !playModule1.playModuleWithCallbacks(&myXmpIoCb);
break;
#endif
default:
Serial.println("WTF attempting to play dir or unsupported file?");
break;
}
Serial.print("error: 0x");
Serial.println(error, HEX);
isPaused = false;
if(!error){
AudioCodec *playingCodec = getPlayingCodec();
if(playingCodec){
uint32_t sr = playingCodec->sampleRate();
if(sr > DAC_MAX_SAMPLE_RATE){
char srStr[21];
snprintf(srStr, 21, "%-20lu", sr);
displayError("UNSUP. SAMPLE RATE: ", srStr, 3000);
stop();
}else{
setSampleRate(sr);
}
Serial.print("RG track peak: ");
Serial.println(playingCodec->replaygainPeak(false));
Serial.print("RG track gain: ");
Serial.println(playingCodec->replaygainGainDb(false));
Serial.print("RG album peak: ");
Serial.println(playingCodec->replaygainPeak(true));
Serial.print("RG album gain: ");
Serial.println(playingCodec->replaygainGainDb(true));
#if defined(__IMXRT1062__)
}else{
// XMP
setSampleRate(44100);
Serial.print("psram_used: ");
Serial.println(psram_used);
#endif
}
#if USE_F32
setGain(currentGain); // apply new replaygain
#endif
}else{
displayError("UNSUP. FILE SKIPPED:", currentFileName, 3000);
}
}
bool playNext() {
stop();
currentFile = dirNav.nextFile();
if(currentFile.isOpen()){
playFile(¤tFile);
return true;
}else{
return false;
}
}
bool playPrev() {
stop();
currentFile = dirNav.prevFile();
if(currentFile.isOpen()){
playFile(¤tFile);
return true;
}else{
return false;
}
}
bool isPlaying() {
if (getPlayingCodec()) {
return true;
}
#if defined(__IMXRT1062__)
if (playModule1.isPlaying()) {
return true;
}
#endif
return false;
}
void togglePause() {
AudioCodec *playingCodec = getPlayingCodec();
if (playingCodec) {
isPaused = !isPaused;
playingCodec->pause(isPaused);
#if defined(__IMXRT1062__)
} else if(playModule1.isPlaying()) {
isPaused = !isPaused;
playModule1.pause(isPaused);
#endif
}
}
uint32_t lengthMs() {
AudioCodec *playingCodec = getPlayingCodec();
if (playingCodec) {
return playingCodec->lengthMillis();
#if defined(__IMXRT1062__)
} else if (playModule1.isPlaying()) {
return playModule1.lengthMs();
#endif
}
return 0;
}
uint32_t positionMs() {
AudioCodec *playingCodec = getPlayingCodec();
if (playingCodec) {
return playingCodec->positionMillis();
#if defined(__IMXRT1062__)
} else if (playModule1.isPlaying()) {
return playModule1.positionMs();
#endif
}
return 0;
}
bool seekAbsolute(uint32_t timesec) {
bool result = false;
AudioCodec *playingCodec = getPlayingCodec();
if (playingCodec) {
if (timesec > playingCodec->lengthMillis() / 1000) {
return false;
}
Serial.print("seeking to ");
Serial.println(timesec);
result = playingCodec->seek(timesec);
Serial.print("result: ");
Serial.println(result);
Serial.print("positionMillis: ");
Serial.println(playingCodec->positionMillis());
#if defined(__IMXRT1062__)
} else if (playModule1.isPlaying()) {
if (timesec > (uint32_t)playModule1.lengthMs() / 1000) {
return false;
}
Serial.print("seeking to ");
Serial.println(timesec);
result = playModule1.seekSec(timesec);
Serial.print("result: ");
Serial.println(result);
Serial.print("positionMs: ");
Serial.println(playModule1.positionMs());
#endif
}
return result;
}
void seekRelative(int dtsec) {
AudioCodec *playingCodec = getPlayingCodec();
if (playingCodec) {
int timesec = playingCodec->positionMillis() / 1000 + dtsec;
if (timesec < 0) {
timesec = 0;
}
seekAbsolute(timesec);
#if defined(__IMXRT1062__)
} else if (playModule1.isPlaying()) {
// module seeking is not accurate. just seek to next/prev pattern
if (dtsec > 0) {
playModule1.seekNextPos();
} else if (dtsec < 0) {
playModule1.seekPrevPos();
}
#endif
}
}
#if USE_F32
float currentGain = 0;
ReplayGainMode replayGainMode = REPLAY_GAIN_OFF;
float rgFallbackGain = 0;
void setGain(float dB) {
Serial.print("setting gain to ");
Serial.println(dB);
currentGain = dB;
dB += effectiveReplayGain();
Serial.print("effective gain (with RG): ");
Serial.println(dB);
gain1.setGain_dB(dB);
gain2.setGain_dB(dB);
}
void setReplayGainMode(ReplayGainMode rgMode){
replayGainMode = (ReplayGainMode)(rgMode % REPLAY_GAIN_MODES);
setGain(currentGain);
}
void setReplayGainFallbackGain(float dB){
rgFallbackGain = dB;
setGain(currentGain);
}
float effectiveReplayGain(){
if(replayGainMode == REPLAY_GAIN_OFF){
return 0;
}
AudioCodec *playingCodec = getPlayingCodec();
if(!playingCodec){
return 0;
}
float replayGain = NAN;
float peak = 1;
if(replayGainMode == REPLAY_GAIN_TRACK || replayGainMode == REPLAY_GAIN_ALBUM){
bool preferAlbumGain = replayGainMode == REPLAY_GAIN_ALBUM;
replayGain = playingCodec->replaygainGainDb(preferAlbumGain);
peak = playingCodec->replaygainPeak(preferAlbumGain);
if(isnan(replayGain)){
// try the other one
replayGain = playingCodec->replaygainGainDb(!preferAlbumGain);
peak = playingCodec->replaygainPeak(!preferAlbumGain);
}
}else{
// REPLAY_GAIN_AVERAGE
float rgAlbum = playingCodec->replaygainGainDb(true);
float rgTrack = playingCodec->replaygainGainDb(false);
float peakAlbum = playingCodec->replaygainPeak(true);
float peakTrack = playingCodec->replaygainPeak(false);
if(!isnan(rgAlbum) && !isnan(rgTrack)){
// for the gain, use the average
replayGain = (rgAlbum + rgTrack)/2;
// the album peak should alwayw be >= the track peak
peak = (peakAlbum + peakTrack)/2;
}else if(!isnan(rgAlbum)){
replayGain = rgAlbum;
peak = peakAlbum;
}else if(!isnan(rgTrack)){
replayGain = rgTrack;
peak = peakTrack;
}
}
if(isnan(replayGain)){
return rgFallbackGain;
}
float peakDb = log10(peak) * 20;
float peakAfterGain = peakDb + currentGain + replayGain;
if(peakAfterGain > 0){
replayGain -= peakAfterGain;
}
return replayGain;
}
#endif