-
-
Notifications
You must be signed in to change notification settings - Fork 177
/
TMRpcm.cpp
1864 lines (1538 loc) · 53.4 KB
/
TMRpcm.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*Library by TMRh20 2012-2014*/
#define RESOLUTION_BASE ((F_CPU) / 10)
#include <pcmConfig.h>
#if !defined (SDFAT)
#include <SD.h>
#else
#include <SdFat.h>
#endif
#include <TMRpcm.h>
#if !defined (RF_ONLY)
//********************* Timer arrays and pointers **********************
//********** Enables use of different timers on different boards********
#if !defined (USE_TIMER2) //NOT using TIMER2
const byte togByte = _BV(ICIE1); //Get the value for toggling the buffer interrupt on/off
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#if !defined (buffSize)
#define buffSize 254
#endif
#if !defined (rampMega)
#define rampMega
#endif
volatile byte *TIMSK[] = {&TIMSK1,&TIMSK3,&TIMSK4,&TIMSK5};
volatile byte *TCCRnA[] = {&TCCR1A,&TCCR3A,&TCCR4A,&TCCR5A};
volatile byte *TCCRnB[] = {&TCCR1B, &TCCR3B,&TCCR4B,&TCCR5B};
volatile unsigned int *OCRnA[] = {&OCR1A,&OCR3A,&OCR4A,&OCR5A};
volatile unsigned int *ICRn[] = {&ICR1, &ICR3,&ICR4,&ICR5};
volatile unsigned int *TCNT[] = {&TCNT1,&TCNT3,&TCNT4,&TCNT5};
#if !defined (DISABLE_SPEAKER2)
volatile unsigned int *OCRnB[] = {&OCR1B, &OCR3B,&OCR4B,&OCR5B};
#else
volatile unsigned int one, two, three, four;
volatile unsigned int *OCRnB[] ={&one,&two,&three,&four};
#endif
//*** These aliases can be commented out to enable full use of alternate timers for other things
ISR(TIMER3_OVF_vect, ISR_ALIASOF(TIMER1_OVF_vect));
ISR(TIMER3_CAPT_vect, ISR_ALIASOF(TIMER1_CAPT_vect));
ISR(TIMER4_OVF_vect, ISR_ALIASOF(TIMER1_OVF_vect));
ISR(TIMER4_CAPT_vect, ISR_ALIASOF(TIMER1_CAPT_vect));
ISR(TIMER5_OVF_vect, ISR_ALIASOF(TIMER1_OVF_vect));
ISR(TIMER5_CAPT_vect, ISR_ALIASOF(TIMER1_CAPT_vect));
#if defined (ENABLE_RECORDING)
ISR(TIMER3_COMPA_vect, ISR_ALIASOF(TIMER1_COMPA_vect));
ISR(TIMER3_COMPB_vect, ISR_ALIASOF(TIMER1_COMPB_vect));
ISR(TIMER4_COMPA_vect, ISR_ALIASOF(TIMER1_COMPA_vect));
ISR(TIMER4_COMPB_vect, ISR_ALIASOF(TIMER1_COMPB_vect));
ISR(TIMER5_COMPA_vect, ISR_ALIASOF(TIMER1_COMPA_vect));
ISR(TIMER5_COMPB_vect, ISR_ALIASOF(TIMER1_COMPB_vect));
#endif
#elif defined (__AVR_ATmega32U4__) || (__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) || (__AVR_ATmega128__) ||defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__)
#if !defined buffSize
#define buffSize 128
#endif
#if !defined (rampMega)
#define rampMega
#endif
volatile byte *TIMSK[] = {&TIMSK1,&TIMSK3};
volatile byte *TCCRnA[] = {&TCCR1A,&TCCR3A};
volatile byte *TCCRnB[] = {&TCCR1B, &TCCR3B};
volatile unsigned int *OCRnA[] = {&OCR1A,&OCR3A};
volatile unsigned int *ICRn[] = {&ICR1, &ICR3};
volatile unsigned int *TCNT[] = {&TCNT1,&TCNT3};
#if !defined (DISABLE_SPEAKER2)
volatile unsigned int *OCRnB[] = {&OCR1B, &OCR3B};
#else
volatile unsigned int one, two;
volatile unsigned int *OCRnB[] ={&one,&two};
#endif
//*** These aliases can be commented out to enable full use of alternate timers for other things
ISR(TIMER3_OVF_vect, ISR_ALIASOF(TIMER1_OVF_vect));
ISR(TIMER3_CAPT_vect, ISR_ALIASOF(TIMER1_CAPT_vect));
#if defined (ENABLE_RECORDING)
ISR(TIMER3_COMPA_vect, ISR_ALIASOF(TIMER1_COMPA_vect));
ISR(TIMER3_COMPB_vect, ISR_ALIASOF(TIMER1_COMPB_vect));
#endif
#else
#if !defined (buffSize)
#define buffSize 64
#endif
volatile byte *TIMSK[] = {&TIMSK1};
volatile byte *TCCRnA[] = {&TCCR1A};
volatile byte *TCCRnB[] = {&TCCR1B};
volatile unsigned int *OCRnA[] = {&OCR1A};
volatile unsigned int *ICRn[] = {&ICR1};
volatile unsigned int *TCNT[] = {&TCNT1};
#if !defined (DISABLE_SPEAKER2)
volatile unsigned int *OCRnB[] = {&OCR1B};
#else
volatile unsigned int one;
volatile unsigned int *OCRnB[] ={&one};
#endif
#endif
#else //USING TIMER 2
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#if !defined (buffSize)
#define buffSize 128
#endif
#elif defined (__AVR_ATmega32U4__) || (__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) || (__AVR_ATmega128__) ||defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__)
#if !defined (buffSize)
#define buffSize 128
#endif
#else
#if !defined (buffSize)
#define buffSize 64
#endif
#endif
volatile byte *TIMSK[] = {&TIMSK2};
volatile byte *TCCRnA[] = {&TCCR2A};
volatile byte *TCCRnB[] = {&TCCR2B};
volatile byte *OCRnA[] = {&OCR2A};
volatile byte *OCRnB[] = {&OCR2B};
volatile byte *TCNT[] = {&TCNT2};
const byte togByte = _BV(OCIE2B);
#endif
//**********Global Variables for Regular and Multi modes ************
#if defined (ENABLE_MULTI)
volatile byte buffer2[2][buffSize];
volatile byte buffCount2 = 0;
volatile boolean buffEmpty2[2] = {false,false}, whichBuff2 = false, playing2 = 0;
char volMod2=0;
char *fileName2;
#if !defined (SDFAT)
File tFile;
#else
SdFile tFile;
#endif
#endif
//*********** Standard Global Variables ***************
volatile int dataEnd;
volatile boolean buffEmpty[2] = {true,true}, whichBuff = false, playing = 0, a, b;
//*** Options/Indicators from MSb to LSb: paused, qual, rampUp, 2-byte samples, loop, loop2nd track, 16-bit ***
byte optionByte = B01100000;
volatile byte buffer[2][buffSize], buffCount = 0;
char volMod=0;
byte tt;
#if !defined (USE_TIMER2)
volatile boolean loadCounter=0;
#else
volatile byte loadCounter = 0, SR = 3;
#endif
#if !defined (SDFAT)
File sFile;
#else
SdFile sFile;
#endif
#if defined (MODE2)
byte tt2 = 0;
#endif
#if defined (ENABLE_RECORDING)
#if defined(SDFAT)
SdCard card1;
#else
Sd2Card card1;
#endif
byte recording = 0;
#endif
//**************************************************************
//********** Core Playback Functions used in all modes *********
#if !defined (USE_TIMER2) //NOT using timer2
void TMRpcm::timerSt(){
#if defined (SPEED_CONTROL)
if(!speedPersist){
#endif
*ICRn[tt] = resolution;
#if defined (SPEED_CONTROL)
}else{
*ICRn[tt] = speedControlVar[tt];
}
#endif
#if !defined (DISABLE_SPEAKER2)
*TCCRnA[tt] = _BV(WGM11) | _BV(COM1A1) | _BV(COM1B0) | _BV(COM1B1); //WGM11,12,13 all set to 1 = fast PWM/w ICR TOP
#else
*TCCRnA[tt] = _BV(WGM11) | _BV(COM1A1); //WGM11,12,13 all set to 1 = fast PWM/w ICR TOP
#endif
*TCCRnB[tt] = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
#if defined (MODE2)
#if defined (SPEED_CONTROL)
if(!speedPersist){
#endif
*ICRn[tt2] = resolution;
#if defined (SPEED_CONTROL)
}else{
*ICRn[tt2] = speedControlVar[tt2];
}
#endif
*TCCRnA[tt2] = _BV(WGM11) | _BV(COM1A1) | _BV(COM1B0) | _BV(COM1B1); //WGM11,12,13 all set to 1 = fast PWM/w ICR TOP
*TCCRnB[tt2] = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
#endif
}
#else //Using TIMER2
void TMRpcm::timerSt(){
*TCCRnA[tt] = _BV(WGM21) | _BV(WGM20) | _BV(COM2B1); //Fast PWM with 0xFF (255) top
}
#endif
void TMRpcm::setPin(){
disable();
pinMode(speakerPin,OUTPUT);
#if !defined (USE_TIMER2) //NOT using TIMER2
switch(speakerPin){
case 5: tt=1; break; //use TIMER3
case 6: tt=2; break;//use TIMER4
case 46:tt=3; break;//use TIMER5
default:tt=0; break; //useTIMER1 as default
}
#else //Using TIMER2 only
tt = 0;
#endif
#if defined (SD_FULLSPEED)
SPSR |= (1 << SPI2X);
SPCR &= ~((1 <<SPR1) | (1 << SPR0));
#endif
}
#if defined (MODE2)
void TMRpcm::setPins(){
disable();
pinMode(speakerPin,OUTPUT);
pinMode(speakerPin2,OUTPUT);
switch(speakerPin){
case 5: tt=1; break; //use TIMER3
case 6: tt=2; break;//use TIMER4
case 46:tt=3; break;//use TIMER5
default:tt=0; break; //useTIMER1 as default
}
switch(speakerPin2){
case 5: tt2=1; break; //use TIMER3
case 6: tt2=2; break;//use TIMER4
case 46:tt2=3; break;//use TIMER5
default:tt2=0; break; //useTIMER1 as default
}
#if defined (SD_FULLSPEED)
SPSR |= (1 << SPI2X);
SPCR &= ~((1 <<SPR1) | (1 << SPR0));
#endif
}
#endif
boolean TMRpcm::wavInfo(const char* filename){
//check for the string WAVE starting at 8th bit in header of file
#if !defined (SDFAT)
sFile = SD.open(filename);
#else
sFile.open(filename);
#endif
if( !ifOpen() ){ return 0; }
seek(8);
char wavStr[] = {'W','A','V','E'};
for (byte i =0; i<4; i++){
if(sFile.read() != wavStr[i]){
#if defined (debug)
Serial.println("WAV ERROR 1");
#endif
break; }
}
#if defined (STEREO_OR_16BIT)
byte stereo, bps;
seek(22);
stereo = sFile.read();
seek(24);
#else
seek(24);
#endif
SAMPLE_RATE = sFile.read();
SAMPLE_RATE = sFile.read() << 8 | SAMPLE_RATE;
#if defined (USE_TIMER2)
if(SAMPLE_RATE < 9000 ){ SR = 0; }
else if(SAMPLE_RATE < 20000){ SR = 1; }
else if(SAMPLE_RATE < 28000){ SR = 2; }
else{ SR = 3; }
#endif
#if defined (STEREO_OR_16BIT)
//verify that Bits Per Sample is 8 (0-255)
seek(34);
bps = sFile.read();
bps = sFile.read() << 8 | bps;
if( stereo == 2){ //_2bytes=1;
bitSet(optionByte,4);
}else
if( bps == 16 ){
bitSet(optionByte,1);
bitSet(optionByte,4);
}else{ bitClear(optionByte,4); bitClear(optionByte,1);}
#endif
#if defined (HANDLE_TAGS)
seek(36);
char datStr[4] = {'d','a','t','a'};
for (byte i =0; i<4; i++){
if(sFile.read() != datStr[i]){
seek(40);
unsigned int siz = sFile.read();
siz = (sFile.read() << 8 | siz)+2;
seek(fPosition() + siz);
for (byte i =0; i<4; i++){
if(sFile.read() != datStr[i]){
return 0;
}
}
}
}
unsigned long dataBytes = sFile.read();
for (byte i =8; i<32; i+=8){
dataBytes = sFile.read() << i | dataBytes;
}
#if !defined (SDFAT)
dataEnd = sFile.size() - fPosition() - dataBytes + buffSize;
#else
dataEnd = sFile.fileSize() - fPosition() - dataBytes + buffSize;
#endif
#else //No Tag handling
seek(44); dataEnd = buffSize;
#endif
return 1;
}
//*************** General Playback Functions *****************
void TMRpcm::quality(boolean q){
if(!playing){ bitWrite(optionByte,6,q); } //qual = q; }
}
void TMRpcm::stopPlayback(){
playing = 0;
*TIMSK[tt] &= ~(togByte | _BV(TOIE1));
if(ifOpen()){ sFile.close(); }
#if defined (ENABLE_MULTI)
playing2 = 0;
#if !defined (SDFAT)
if(tFile){tFile.close();}
#else
if(tFile.isOpen()){tFile.close();}
#endif
#endif
}
void TMRpcm::pause(){
//paused = !paused;
if(bitRead(optionByte,7) && playing){
bitClear(optionByte,7);
#if !defined (USE_TIMER2)
*TIMSK[tt] |= ( _BV(ICIE1) | _BV(TOIE1) );
#else
*TIMSK[tt] |= ( _BV(OCIE2B) | _BV(TOIE1) );
#endif
}else if(!bitRead(optionByte,7) && playing){
bitSet(optionByte,7);
*TIMSK[tt] &= ~( _BV(TOIE1) );
}
}
void TMRpcm::loop(boolean set){
bitWrite(optionByte,3,set);
}
/**************************************************
This section used for translation of functions between
SDFAT library and regular SD library
Prevents a whole lot more #if defined statements */
#if !defined (SDFAT)
boolean TMRpcm::seek( unsigned long pos ){
return sFile.seek(pos);
}
unsigned long TMRpcm::fPosition( ){
return sFile.position();
}
boolean TMRpcm::ifOpen(){
if(sFile){ return 1;}
return 0;
}
#else
boolean TMRpcm::seek(unsigned long pos ){
return sFile.seekSet(pos);
}
unsigned long TMRpcm::fPosition(){
return sFile.curPosition();
}
boolean TMRpcm::ifOpen(){
return sFile.isOpen();
}
#endif
//***************************************************************************************
//********************** Functions for single track playback ****************************
unsigned int TMRpcm::FSHlength(const __FlashStringHelper * FSHinput) {
PGM_P FSHinputPointer = reinterpret_cast<PGM_P>(FSHinput);
unsigned int stringLength = 0;
while (pgm_read_byte(FSHinputPointer++)) {
stringLength++;
}
return stringLength;
}
#if !defined (ENABLE_MULTI) //Below section for normal playback of 1 track at a time
void TMRpcm::play(const __FlashStringHelper* FS, unsigned long seekPoint){
const byte textLength = FSHlength(FS);
char buffer[textLength + 1];
memcpy_P(buffer, FS, textLength + 1);
play(buffer,seekPoint);
}
void TMRpcm::play(const char* filename, unsigned long seekPoint){
if(speakerPin != lastSpeakPin){
#if !defined (MODE2)
setPin();
#else
setPins();
#endif
lastSpeakPin=speakerPin;
}
stopPlayback();
if(!wavInfo(filename)){
#if defined (debug)
Serial.println("WAV ERROR 2");
#endif
return;
}//verify its a valid wav file
if(seekPoint > 0){ seekPoint = (SAMPLE_RATE*seekPoint) + fPosition();
seek(seekPoint); //skip the header info
}
playing = 1; bitClear(optionByte,7); //paused = 0;
if(SAMPLE_RATE > 45050 ){ SAMPLE_RATE = 24000;
#if defined (debug)
Serial.print("SAMPLE RATE TOO HIGH: ");
Serial.println(SAMPLE_RATE);
#endif
}
#if !defined (USE_TIMER2)
//if(qual)
if(bitRead(optionByte,6)){resolution = 10 * ((RESOLUTION_BASE/2)/SAMPLE_RATE);}
else{ resolution = 10 * (RESOLUTION_BASE/SAMPLE_RATE);
}
#else
resolution = 255;
if(SAMPLE_RATE < 9000){
*TCCRnB[tt] &= ~_BV(CS20);
*TCCRnB[tt] |= _BV(CS21);
}else{
*TCCRnB[tt] &= ~_BV(CS21);
*TCCRnB[tt] |= _BV(CS20);
}
#endif
byte tmp = (sFile.read() + sFile.peek()) / 2;
#if defined(rampMega)
if(bitRead(optionByte,5)){
*OCRnA[tt] = 0; *OCRnB[tt] = resolution;
timerSt();
for(unsigned int i=0; i < resolution; i++){
*OCRnB[tt] = constrain(resolution-i,0,resolution);
//if(bitRead(*TCCRnB[tt],0)){
// for(int i=0; i<10; i++){
// while(*TCNT[tt] < resolution-50){}
// }
//}else{
delayMicroseconds(150);
//}
}
}
bitClear(optionByte,5);
#endif
//rampUp = 0;
unsigned int mod;
if(volMod > 0){ mod = *OCRnA[tt] >> volMod; }else{ mod = *OCRnA[tt] << (volMod*-1); }
if(tmp > mod){
for(unsigned int i=0; i<buffSize; i++){ mod = constrain(mod+1,mod, tmp); buffer[0][i] = mod; }
for(unsigned int i=0; i<buffSize; i++){ mod = constrain(mod+1,mod, tmp); buffer[1][i] = mod; }
}else{
for(unsigned int i=0; i<buffSize; i++){ mod = constrain(mod-1,tmp ,mod); buffer[0][i] = mod; }
for(unsigned int i=0; i<buffSize; i++){ mod = constrain(mod-1,tmp, mod); buffer[1][i] = mod; }
}
whichBuff = 0; buffEmpty[0] = 0; buffEmpty[1] = 0; buffCount = 0;
noInterrupts();
timerSt();
*TIMSK[tt] = ( togByte | _BV(TOIE1) );
interrupts();
}
void TMRpcm::volume(char upDown){
if(upDown){
volMod++;
}else{
volMod--;
}
}
void TMRpcm::setVolume(char vol) {
volMod = vol - 4 ;
}
#if defined (ENABLE_RECORDING)
ISR(TIMER1_COMPA_vect){
if(buffEmpty[!whichBuff] == 0){
a = !whichBuff;
*TIMSK[tt] &= ~(_BV(OCIE1A));
sei();
sFile.write((byte*)buffer[a], buffSize );
buffEmpty[a] = 1;
*TIMSK[tt] |= _BV(OCIE1A);
}
}
#endif
#if !defined (USE_TIMER2) //Not using TIMER2
ISR(TIMER1_CAPT_vect){
#else //Using TIMER2
ISR(TIMER2_COMPB_vect){
#endif
// The first step is to disable this interrupt before manually enabling global interrupts.
// This allows this interrupt vector (COMPB) to continue loading data while allowing the overflow interrupt
// to interrupt it. ( Nested Interrupts )
// TIMSK1 &= ~_BV(ICIE1);
//Then enable global interupts before this interrupt is finished, so the music can interrupt the buffering
//sei();
if(buffEmpty[!whichBuff]){
a = !whichBuff;
*TIMSK[tt] &= ~togByte;
sei();
if( sFile.available() <= dataEnd){
#if !defined (SDFAT)
if(bitRead(optionByte,3)){ sFile.seek(44); *TIMSK[tt] |= togByte; return;}
*TIMSK[tt] &= ~( togByte | _BV(TOIE1) );
if(sFile){ sFile.close();}
#else
if(bitRead(optionByte,3)){ sFile.seekSet(44); *TIMSK[tt] |= togByte; return;}
*TIMSK[tt] &= ~( togByte | _BV(TOIE1) );
if(sFile.isOpen()){ sFile.close();}
#endif
playing = 0;
return;
}
sFile.read((byte*)buffer[a],buffSize);
buffEmpty[a] = 0;
*TIMSK[tt] |= togByte;
}
}
#if defined(USE_TIMER2)
ISR(TIMER2_OVF_vect){
//TIMER2 runs at 16mhz/255 (62,745Hz), so the timing for sample rate must be manually counted
switch (SR){
case 0: break;
case 3: if(loadCounter){loadCounter = 0;return;} break;
case 1: if(loadCounter){ if(loadCounter >= 3){loadCounter=0; return;} loadCounter++; return;} break;
case 2: if(loadCounter){ if(loadCounter >= 2){loadCounter=0; return; }loadCounter++; return;} break;
}
loadCounter++;
if(volMod < 0 ){ *OCRnB[tt] = buffer[whichBuff][buffCount] >> (volMod*-1);
}else{ *OCRnB[tt] = buffer[whichBuff][buffCount] << volMod;
}
++buffCount;
if(buffCount >= buffSize){
buffCount = 0;
buffEmpty[whichBuff] = true;
whichBuff = !whichBuff;
}
}
#else
#if defined (ENABLE_RECORDING)
ISR(TIMER1_COMPB_vect){
buffer[whichBuff][buffCount] = ADCH;
if(recording > 1){
if(volMod < 0 ){ *OCRnA[tt] = ADCH >> (volMod*-1);
}else{ *OCRnA[tt] = ADCH << volMod;
}
}
buffCount++;
if(buffCount >= buffSize){
buffCount = 0;
buffEmpty[!whichBuff] = 0;
whichBuff = !whichBuff;
}
}
#endif
ISR(TIMER1_OVF_vect){
if(bitRead(optionByte,6)){loadCounter = !loadCounter;if(loadCounter){ return; } }
#if defined (STEREO_OR_16BIT)
if( !bitRead(optionByte,4) ){
#endif
if(volMod < 0 ){ *OCRnA[tt] = *OCRnB[tt] = buffer[whichBuff][buffCount] >> (volMod*-1);
}else{ *OCRnA[tt] = *OCRnB[tt] = buffer[whichBuff][buffCount] << volMod;
}
++buffCount;
#if defined (STEREO_OR_16BIT)
}else{
if(bitRead(optionByte,1)){
buffer[whichBuff][buffCount] += 127;
//buffer[whichBuff][buffCount+1] += 127;
}
#if !defined (MODE2)
if(volMod < 0 ){ *OCRnA[tt] = buffer[whichBuff][buffCount] >> (volMod*-1);
*OCRnB[tt] = buffer[whichBuff][buffCount+1] >> (volMod*-1);
}else{ *OCRnA[tt] = buffer[whichBuff][buffCount] << volMod;
*OCRnB[tt] = buffer[whichBuff][buffCount+1] << volMod;
}
#else
if(volMod < 0 ){
*OCRnA[tt] = *OCRnB[tt] = buffer[whichBuff][buffCount] >> (volMod*-1);
*OCRnA[tt2] = *OCRnB[tt2] =buffer[whichBuff][buffCount+1] >> (volMod*-1);
}else{ *OCRnA[tt] = *OCRnB[tt] = buffer[whichBuff][buffCount] << volMod;
*OCRnA[tt2] = *OCRnB[tt2] = buffer[whichBuff][buffCount+1] << volMod;
}
#endif
buffCount+=2;
}
#endif
if(buffCount >= buffSize){
buffCount = 0;
buffEmpty[whichBuff] = true;
whichBuff = !whichBuff;
}
}
#endif
void TMRpcm::disable(){
playing = 0;
*TIMSK[tt] &= ~( togByte | _BV(TOIE1) );
if(ifOpen()){ sFile.close();}
if(bitRead(*TCCRnA[tt],7) > 0){
int current = *OCRnA[tt];
for(int i=0; i < resolution; i++){
#if defined(rampMega)
*OCRnB[tt] = constrain((current + i),0,resolution);
*OCRnA[tt] = constrain((current - i),0,resolution);
#else
*OCRnB[tt] = constrain((current - i),0,resolution);
*OCRnA[tt] = constrain((current - i),0,resolution);
#endif
for(int i=0; i<10; i++){ while(*TCNT[tt] < (unsigned int)(resolution-50) ){} }
}
}
bitSet(optionByte,5);
//rampUp = 1;
*TCCRnA[tt] = *TCCRnB[tt] = 0;
}
boolean TMRpcm::isPlaying(){
return playing;
}
//***************************************************************************************
//********************** Functions for dual track playback ****************************
#else //Else Dual
void TMRpcm::loop(boolean set, boolean which){
if(!which){ bitWrite(optionByte,3,set); }
else{ bitWrite(optionByte,2,set); }
}
void TMRpcm::play(const char* filename, boolean which){
play(filename,0,which);
}
void TMRpcm::play(const __FlashStringHelper* FS, boolean which){
const byte textLength = FSHlength(FS);
char buffer[textLength + 1];
memcpy_P(buffer, FS, textLength + 1);
play(buffer,0, which);
}
void TMRpcm::play(const __FlashStringHelper* FS, unsigned long seekPoint, boolean which){
const byte textLength = FSHlength(FS);
char buffer[textLength + 1];
memcpy_P(buffer, FS, textLength + 1);
play(buffer,seekPoint, which);
}
byte hold = 0;
void TMRpcm::play(const char* filename, unsigned long seekPoint, boolean which){
if(speakerPin != lastSpeakPin){
#if defined (MODE2)
setPins();
#else
setPin();
#endif
lastSpeakPin=speakerPin;
}
byte dual = 0;
#if defined(rampMega)
if(bitRead(optionByte,5)){//RampUp
*OCRnA[tt] = 0; *OCRnB[tt] = resolution;
#if defined (MODE2)
*OCRnA[tt2] = 0; *OCRnB[tt2] = resolution;
#endif
timerSt();
//rampUp = 0;
bitClear(optionByte,5);
for(unsigned int i=0; i < resolution; i++){
*OCRnB[tt] = constrain(resolution-i,0,resolution);
#if defined (MODE2)
*OCRnB[tt2] = constrain(resolution-i,0,resolution);
#endif
delayMicroseconds(50);
}
}
#endif
if((!playing && !playing2) || (playing && !playing2 && !which) || (playing2 && !playing && which) ){
stopPlayback();
if(!wavInfo(filename) ){ return; }//verify its a valid wav file
if(seekPoint > 0){ seekPoint = SAMPLE_RATE*seekPoint;}
if(!which){
#if !defined (SDFAT)
sFile = SD.open(filename);
#else
sFile.open(filename,O_READ);
#endif
}else{
#if !defined (SDFAT)
tFile = SD.open(filename);
#else
tFile.open(filename,O_READ);
#endif
}
if(SAMPLE_RATE > 45050 ){ SAMPLE_RATE = 24000;
#if defined (debug)
Serial.print("SampleRate Too High");
#endif
}
#if !defined (USE_TIMER2)
if(bitRead(optionByte,6)){resolution = 10 * ((RESOLUTION_BASE/2)/SAMPLE_RATE);}
else{ resolution = 10 * (RESOLUTION_BASE/SAMPLE_RATE);}
#else
resolution = 255;
#endif
}else{
if(!which){ playing = 0; sFile.close(); dual = 1; }
if(which ){ playing2 = 0; tFile.close(); dual = 1; }
}
if(!dual){
if(!which){
//paused = 0;
bitClear(optionByte,7);
if(ifOpen()){
if(seekPoint > 0){ seek(seekPoint); }else{ seek(44); }
ramp(0);
playing = 1;
}else{
#if defined (debug)
Serial.println("Read fail");
#endif
return;
}
}else{
//paused = 0;
bitClear(optionByte,7);
#if !defined (SDFAT)
if(tFile){
if(seekPoint > 0){ tFile.seek(seekPoint); }else{ tFile.seek(44); }
#else
if(tFile.isOpen()){
tFile.seekSet(seekPoint);
if(seekPoint > 0){ tFile.seekSet(seekPoint); }else{ tFile.seekSet(44); }
#endif
ramp(1);
playing2 = 2;
}else{
#if defined (debug)
Serial.println("Read fail");
#endif
return;
}
}
noInterrupts();
timerSt();
*TIMSK[tt] = ( togByte | _BV(TOIE1) );
interrupts();
}
if(dual){
if(seekPoint > 0){ seekPoint = SAMPLE_RATE*seekPoint;}
*TIMSK[tt] &= ~togByte;
if(!which){
if(ifOpen()){ sFile.close(); }
#if !defined (SDFAT)
sFile = SD.open(filename);
#else
sFile.open(filename,O_READ);
#endif
if(ifOpen()){
if(seekPoint > 0){ seek(seekPoint); }else{ seek(44); }
ramp(0); playing = 1;
}else{
#if defined (debug)
Serial.println("Dual Read fail");
#endif
return;
}//open1 = 1; fileName2 = filename; }
}else
if(which){
#if !defined (SDFAT)
if(tFile){tFile.close();}
tFile = SD.open(filename);
if(tFile){
if(seekPoint > 0){ tFile.seek(seekPoint); }else{ tFile.seek(44); }
#else
if(tFile.isOpen()){tFile.close();}
tFile.open(filename);
if(tFile.isOpen()){
if(seekPoint > 0){ tFile.seekSet(seekPoint); }else{ tFile.seekSet(44); }
#endif
ramp(1); playing2 = 1;
}else{
#if defined (debug)
Serial.println("Dual Read fail");
#endif
return;
} //open1 = 1; fileName2 = filename; }
}
*TIMSK[tt] |= togByte;
//interrupts();
}
}
void TMRpcm::ramp(boolean wBuff){
byte tmp;
byte mod;
#if !defined (MODE2)
if(!wBuff){
//seek(44);
tmp = sFile.read();
if(volMod >= 0){ mod = *OCRnA[tt] >> volMod; }else{ mod = *OCRnA[tt] << (volMod*-1); }
if(tmp > mod){
for(byte i=0; i<buffSize; i++){ mod = constrain(mod+1,mod, tmp); buffer[0][i] = mod; }
}else{
for(byte i=0; i<buffSize; i++){ mod = constrain(mod-1,tmp ,mod); buffer[0][i] = mod; }
}
whichBuff = 0; buffEmpty[0] = 0;buffCount = 0; //buffEmpty[1] = 0;
}else{
//#if !defined (SDFAT)
//tFile.seek(44);
//#else
//tFile.seekSet(44);
//#endif
tmp = tFile.read();
if(volMod >= 0){ mod = *OCRnB[tt] >> volMod; }else{ mod = *OCRnB[tt] << (volMod*-1); }
if(tmp > mod){
for(byte i=0; i<buffSize; i++){ mod = constrain(mod+1,mod, tmp); buffer2[0][i] = mod; }
}else{
for(byte i=0; i<buffSize; i++){ mod = constrain(mod-1,tmp ,mod); buffer2[0][i] = mod; }
}
whichBuff2 = 0; buffEmpty2[0] = 0; buffCount2 = 0; //buffEmpty2[1] = 0;
}
#else //MODE2
if(!wBuff){
//seek(44);
tmp = sFile.read();
if(volMod >= 0){ mod = *OCRnA[tt] >> volMod; }else{ mod = *OCRnA[tt] << (volMod*-1); }
if(tmp > mod){
for(byte i=0; i<buffSize; i++){ mod = constrain(mod+1,mod, tmp); buffer[0][i] = mod; }