forked from RetroPinUpgrade/Trident2020
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AudioHandler.cpp
1188 lines (973 loc) · 34.2 KB
/
AudioHandler.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
/****************************************************************
* Class - AudioHandler
*
* This class wraps the different audio output options for
* pinball machines (WAV Trigger, SB-100, SB-300, S&T
* -51, etc.) to provide different output options. Additionally,
* it adds a bunch of audio management features:
*
* 1) Different volume controls for FX, callouts, and music
* 2) Automatically ducks music behind callouts
* 3) Supports background soundtracks or looping songs
* 4) A sound can be queued to play at a future time
* 5) Callouts can be given different priorities
* 6) Queued callouts at the same priority will be stacked
*
*
* Typical usage:
* A global variable of class "AudioHandler" is declared
* (ex: "AudioHandler Audio;")
*
* During the setup() function:
* Audio.InitDevices(AUDIO_PLAY_TYPE_WAV_TRIGGER | AUDIO_PLAY_TYPE_ORIGINAL_SOUNDS); // declare what type of audio should be supported
* Audio.StopAllAudio(); // Stop any currently playing sounds
* Audio.SetMusicDuckingGain(12); // negative gain applied to music when callouts are being played
* Audio.QueueSound(SOUND_EFFECT_MACHINE_INTRO, AUDIO_PLAY_TYPE_WAV_TRIGGER, CurrentTime+1200); // Play machine startup sound in 1.2 s
*
* Volumes set (pulled from EEPROM or set in setup routine):
* Audio.SetMusicVolume(MusicVolume); // value from 0-10 (inclusive)
* Audio.SetSoundFXVolume(SoundEffectsVolume); // value from 0-10 (inclusive)
* Audio.SetNotificationsVolume(CalloutsVolume); // value from 0-10 (inclusive)
*
* During the loop():
* Audio.Update(CurrentTime);
*
* During game play:
* Audio.PlayBackgroundSong(songNum, true); // loop a background song
* Audio.PlaySound(soundEffectNum, AUDIO_PLAY_TYPE_WAV_TRIGGER); // play sound effect through wav trigger
* Audio.QueueSound(0x02, AUDIO_PLAY_TYPE_ORIGINAL_SOUNDS, CurrentTime); // Queue sound card command for now
* Audio.QueueNotification(soundEffectNum, VoiceNotificationDurations[soundEffectNum-SOUND_EFFECT_VP_VOICE_NOTIFICATIONS_START], priority, CurrentTime); // Queue notification
*
* End of ball:
* Audio.StopAllAudio(); // Stop audio
*/
#include <Arduino.h>
#include "AudioHandler.h"
#if defined(RPU_OS_USE_WAV_TRIGGER) || defined(RPU_OS_USE_WAV_TRIGGER_1p3)
// **************************************************************
void wavTrigger::start(void) {
uint8_t txbuf[5];
versionRcvd = false;
sysinfoRcvd = false;
WTSerial.begin(57600);
flush();
// Request version string
txbuf[0] = SOM1;
txbuf[1] = SOM2;
txbuf[2] = 0x05;
txbuf[3] = CMD_GET_VERSION;
txbuf[4] = EOM;
WTSerial.write(txbuf, 5);
// Request system info
txbuf[0] = SOM1;
txbuf[1] = SOM2;
txbuf[2] = 0x05;
txbuf[3] = CMD_GET_SYS_INFO;
txbuf[4] = EOM;
WTSerial.write(txbuf, 5);
}
// **************************************************************
void wavTrigger::flush(void) {
int i;
//uint8_t dat;
rxCount = 0;
rxLen = 0;
rxMsgReady = false;
for (i = 0; i < MAX_NUM_VOICES; i++) {
voiceTable[i] = 0xffff;
}
while(WTSerial.available())
/*dat = */WTSerial.read();
}
// **************************************************************
void wavTrigger::update(void) {
int i;
uint8_t dat;
uint8_t voice;
uint16_t track;
rxMsgReady = false;
while (WTSerial.available() > 0) {
dat = WTSerial.read();
if ((rxCount == 0) && (dat == SOM1)) {
rxCount++;
}
else if (rxCount == 1) {
if (dat == SOM2)
rxCount++;
else {
rxCount = 0;
//Serial.print("Bad msg 1\n");
}
}
else if (rxCount == 2) {
if (dat <= MAX_MESSAGE_LEN) {
rxCount++;
rxLen = dat - 1;
}
else {
rxCount = 0;
//Serial.print("Bad msg 2\n");
}
}
else if ((rxCount > 2) && (rxCount < rxLen)) {
rxMessage[rxCount - 3] = dat;
rxCount++;
}
else if (rxCount == rxLen) {
if (dat == EOM)
rxMsgReady = true;
else {
rxCount = 0;
//Serial.print("Bad msg 3\n");
}
}
else {
rxCount = 0;
//Serial.print("Bad msg 4\n");
}
if (rxMsgReady) {
switch (rxMessage[0]) {
case RSP_TRACK_REPORT:
track = rxMessage[2];
track = (track << 8) + rxMessage[1] + 1;
voice = rxMessage[3];
if (voice < MAX_NUM_VOICES) {
if (rxMessage[4] == 0) {
if (track == voiceTable[voice])
voiceTable[voice] = 0xffff;
}
else
voiceTable[voice] = track;
}
// ==========================
//Serial.print("Track ");
//Serial.print(track);
//if (rxMessage[4] == 0)
// Serial.print(" off\n");
//else
// Serial.print(" on\n");
// ==========================
break;
case RSP_VERSION_STRING:
for (i = 0; i < (VERSION_STRING_LEN - 1); i++)
version[i] = rxMessage[i + 1];
version[VERSION_STRING_LEN - 1] = 0;
versionRcvd = true;
// ==========================
//Serial.write(version);
//Serial.write("\n");
// ==========================
break;
case RSP_SYSTEM_INFO:
numVoices = rxMessage[1];
numTracks = rxMessage[3];
numTracks = (numTracks << 8) + rxMessage[2];
sysinfoRcvd = true;
// ==========================
///\Serial.print("Sys info received\n");
// ==========================
break;
}
rxCount = 0;
rxLen = 0;
rxMsgReady = false;
} // if (rxMsgReady)
} // while (WTSerial.available() > 0)
}
// **************************************************************
bool wavTrigger::isTrackPlaying(int trk) {
int i;
bool fResult = false;
update();
for (i = 0; i < MAX_NUM_VOICES; i++) {
if (voiceTable[i] == ((uint16_t)trk))
fResult = true;
}
return fResult;
}
// **************************************************************
void wavTrigger::masterGain(int gain) {
uint8_t txbuf[7];
unsigned short vol;
txbuf[0] = SOM1;
txbuf[1] = SOM2;
txbuf[2] = 0x07;
txbuf[3] = CMD_MASTER_VOLUME;
vol = (unsigned short)gain;
txbuf[4] = (uint8_t)vol;
txbuf[5] = (uint8_t)(vol >> 8);
txbuf[6] = EOM;
WTSerial.write(txbuf, 7);
}
// **************************************************************
void wavTrigger::setAmpPwr(bool enable) {
uint8_t txbuf[6];
txbuf[0] = SOM1;
txbuf[1] = SOM2;
txbuf[2] = 0x06;
txbuf[3] = CMD_AMP_POWER;
txbuf[4] = enable;
txbuf[5] = EOM;
WTSerial.write(txbuf, 6);
}
// **************************************************************
void wavTrigger::setReporting(bool enable) {
uint8_t txbuf[6];
txbuf[0] = SOM1;
txbuf[1] = SOM2;
txbuf[2] = 0x06;
txbuf[3] = CMD_SET_REPORTING;
txbuf[4] = enable;
txbuf[5] = EOM;
WTSerial.write(txbuf, 6);
}
// **************************************************************
bool wavTrigger::getVersion(char *pDst, int len) {
int i;
update();
if (!versionRcvd) {
return false;
}
for (i = 0; i < (VERSION_STRING_LEN - 1); i++) {
if (i >= (len - 1))
break;
pDst[i] = version[i];
}
pDst[++i] = 0;
return true;
}
// **************************************************************
int wavTrigger::getNumTracks(void) {
update();
return numTracks;
}
// **************************************************************
void wavTrigger::trackPlaySolo(int trk) {
trackControl(trk, TRK_PLAY_SOLO);
}
// **************************************************************
void wavTrigger::trackPlaySolo(int trk, bool lock) {
trackControl(trk, TRK_PLAY_SOLO, lock);
}
// **************************************************************
void wavTrigger::trackPlayPoly(int trk) {
trackControl(trk, TRK_PLAY_POLY);
}
// **************************************************************
void wavTrigger::trackPlayPoly(int trk, bool lock) {
trackControl(trk, TRK_PLAY_POLY, lock);
}
// **************************************************************
void wavTrigger::trackLoad(int trk) {
trackControl(trk, TRK_LOAD);
}
// **************************************************************
void wavTrigger::trackLoad(int trk, bool lock) {
trackControl(trk, TRK_LOAD, lock);
}
// **************************************************************
void wavTrigger::trackStop(int trk) {
trackControl(trk, TRK_STOP);
}
// **************************************************************
void wavTrigger::trackPause(int trk) {
trackControl(trk, TRK_PAUSE);
}
// **************************************************************
void wavTrigger::trackResume(int trk) {
trackControl(trk, TRK_RESUME);
}
// **************************************************************
void wavTrigger::trackLoop(int trk, bool enable) {
if (enable)
trackControl(trk, TRK_LOOP_ON);
else
trackControl(trk, TRK_LOOP_OFF);
}
// **************************************************************
void wavTrigger::trackControl(int trk, int code) {
uint8_t txbuf[8];
txbuf[0] = SOM1;
txbuf[1] = SOM2;
txbuf[2] = 0x08;
txbuf[3] = CMD_TRACK_CONTROL;
txbuf[4] = (uint8_t)code;
txbuf[5] = (uint8_t)trk;
txbuf[6] = (uint8_t)(trk >> 8);
txbuf[7] = EOM;
WTSerial.write(txbuf, 8);
}
// **************************************************************
void wavTrigger::trackControl(int trk, int code, bool lock) {
uint8_t txbuf[9];
txbuf[0] = SOM1;
txbuf[1] = SOM2;
txbuf[2] = 0x09;
txbuf[3] = CMD_TRACK_CONTROL_EX;
txbuf[4] = (uint8_t)code;
txbuf[5] = (uint8_t)trk;
txbuf[6] = (uint8_t)(trk >> 8);
txbuf[7] = lock;
txbuf[8] = EOM;
WTSerial.write(txbuf, 9);
}
// **************************************************************
void wavTrigger::stopAllTracks(void) {
uint8_t txbuf[5];
txbuf[0] = SOM1;
txbuf[1] = SOM2;
txbuf[2] = 0x05;
txbuf[3] = CMD_STOP_ALL;
txbuf[4] = EOM;
WTSerial.write(txbuf, 5);
}
// **************************************************************
void wavTrigger::resumeAllInSync(void) {
uint8_t txbuf[5];
txbuf[0] = SOM1;
txbuf[1] = SOM2;
txbuf[2] = 0x05;
txbuf[3] = CMD_RESUME_ALL_SYNC;
txbuf[4] = EOM;
WTSerial.write(txbuf, 5);
}
// **************************************************************
void wavTrigger::trackGain(int trk, int gain) {
uint8_t txbuf[9];
unsigned short vol;
txbuf[0] = SOM1;
txbuf[1] = SOM2;
txbuf[2] = 0x09;
txbuf[3] = CMD_TRACK_VOLUME;
txbuf[4] = (uint8_t)trk;
txbuf[5] = (uint8_t)(trk >> 8);
vol = (unsigned short)gain;
txbuf[6] = (uint8_t)vol;
txbuf[7] = (uint8_t)(vol >> 8);
txbuf[8] = EOM;
WTSerial.write(txbuf, 9);
}
// **************************************************************
void wavTrigger::trackFade(int trk, int gain, int time, bool stopFlag) {
uint8_t txbuf[12];
unsigned short vol;
txbuf[0] = SOM1;
txbuf[1] = SOM2;
txbuf[2] = 0x0c;
txbuf[3] = CMD_TRACK_FADE;
txbuf[4] = (uint8_t)trk;
txbuf[5] = (uint8_t)(trk >> 8);
vol = (unsigned short)gain;
txbuf[6] = (uint8_t)vol;
txbuf[7] = (uint8_t)(vol >> 8);
txbuf[8] = (uint8_t)time;
txbuf[9] = (uint8_t)(time >> 8);
txbuf[10] = stopFlag;
txbuf[11] = EOM;
WTSerial.write(txbuf, 12);
}
// **************************************************************
void wavTrigger::samplerateOffset(int offset) {
uint8_t txbuf[7];
unsigned short off;
txbuf[0] = SOM1;
txbuf[1] = SOM2;
txbuf[2] = 0x07;
txbuf[3] = CMD_SAMPLERATE_OFFSET;
off = (unsigned short)offset;
txbuf[4] = (uint8_t)off;
txbuf[5] = (uint8_t)(off >> 8);
txbuf[6] = EOM;
WTSerial.write(txbuf, 7);
}
// **************************************************************
void wavTrigger::setTriggerBank(int bank) {
uint8_t txbuf[6];
txbuf[0] = SOM1;
txbuf[1] = SOM2;
txbuf[2] = 0x06;
txbuf[3] = CMD_SET_TRIGGER_BANK;
txbuf[4] = (uint8_t)bank;
txbuf[5] = EOM;
WTSerial.write(txbuf, 6);
}
#endif
AudioHandler::AudioHandler() {
curSoundtrack = NULL;
curSoundtrackEntries = 0;
soundFXGain = 0;
notificationsGain = 0;
musicGain = 0;
ClearSoundQueue();
ClearSoundCardQueue();
ClearNotificationStack();
currentBackgroundTrack = BACKGROUND_TRACK_NONE;
soundtrackRandomOrder = true;
nextSoundtrackPlayTime = 0;
backgroundSongEndTime = 0;
nextVoiceNotificationPlayTime = 0;
voiceNotificationStackFirst = 0;
voiceNotificationStackLast = 0;
currentNotificationPriority = 0;
currentNotificationPlaying = INVALID_SOUND_INDEX;
ducking = 20;
for (int count=0; count<NUMBER_OF_SONGS_REMEMBERED; count++) lastSongsPlayed[count] = BACKGROUND_TRACK_NONE;
InitSoundEffectQueue();
}
AudioHandler::~AudioHandler() {
}
boolean AudioHandler::InitDevices(byte audioType) {
#if defined(RPU_OS_USE_WAV_TRIGGER) || defined(RPU_OS_USE_WAV_TRIGGER_1p3)
if (audioType & AUDIO_PLAY_TYPE_WAV_TRIGGER) {
// WAV Trigger startup at 57600
wTrig.start();
delay(10);
wTrig.stopAllTracks();
wTrig.samplerateOffset(0);
wTrig.setReporting(true);
}
#endif
if (audioType & AUDIO_PLAY_TYPE_ORIGINAL_SOUNDS) {
#ifdef RPU_OS_USE_SB300
InitSB300Registers();
PlaySB300StartupBeep();
#endif
#if defined(RPU_OS_USE_WTYPE_1_SOUND) || defined(RPU_OS_USE_WTYPE_2_SOUND)
#endif
}
return true;
}
int AudioHandler::ConvertVolumeSettingToGain(byte volumeSetting) {
if (volumeSetting==0) return -70;
if (volumeSetting>10) return 0;
return volumeToGainConversion[volumeSetting];
}
void AudioHandler::SetSoundFXVolume(byte s_volume) {
soundFXGain = ConvertVolumeSettingToGain(s_volume);
}
void AudioHandler::SetNotificationsVolume(byte s_volume) {
notificationsGain = ConvertVolumeSettingToGain(s_volume);
}
void AudioHandler::SetMusicVolume(byte s_volume) {
musicGain = ConvertVolumeSettingToGain(s_volume);
}
void AudioHandler::SetMusicDuckingGain(byte s_ducking) {
ducking = s_ducking;
}
boolean AudioHandler::StopSound(unsigned short soundIndex) {
#if defined(RPU_OS_USE_WAV_TRIGGER) || defined(RPU_OS_USE_WAV_TRIGGER_1p3)
wTrig.trackStop(soundIndex);
#else
(void)soundIndex;
#endif
return false;
}
boolean AudioHandler::StopAllMusic() {
#if defined(RPU_OS_USE_WAV_TRIGGER) || defined(RPU_OS_USE_WAV_TRIGGER_1p3)
if (currentBackgroundTrack!=BACKGROUND_TRACK_NONE) {
wTrig.trackStop(currentBackgroundTrack);
currentBackgroundTrack = BACKGROUND_TRACK_NONE;
return true;
}
#endif
currentBackgroundTrack = BACKGROUND_TRACK_NONE;
curSoundtrack = NULL;
return false;
}
void AudioHandler::ClearNotificationStack(byte priority) {
if (priority==10) {
voiceNotificationStackFirst = 0;
voiceNotificationStackLast = 0;
for (byte count=0; count<VOICE_NOTIFICATION_STACK_SIZE; count++) {
voiceNotificationNumStack[count] = VOICE_NOTIFICATION_STACK_EMPTY;
voiceNotificationDuration[count] = 0;
voiceNotificationPriorityStack[count] = 0;
}
} else {
byte tempFirst = voiceNotificationStackFirst;
byte tempLast = voiceNotificationStackLast;
while (tempFirst != tempLast) {
if (voiceNotificationPriorityStack[tempFirst]<=priority) {
voiceNotificationNumStack[tempFirst] = INVALID_SOUND_INDEX;
}
tempFirst += 1;
if (tempFirst >= VOICE_NOTIFICATION_STACK_SIZE) tempFirst = 0;
}
}
}
boolean AudioHandler::StopCurrentNotification(byte priority) {
nextVoiceNotificationPlayTime = 0;
if (currentNotificationPlaying!=INVALID_SOUND_INDEX && currentNotificationPriority<=priority) {
#if defined(RPU_OS_USE_WAV_TRIGGER) || defined(RPU_OS_USE_WAV_TRIGGER_1p3)
wTrig.trackStop(currentNotificationPlaying);
#endif
// currentNotificationPlaying = INVALID_SOUND_INDEX;
nextVoiceNotificationPlayTime = 1;
currentNotificationPriority = 0;
return true;
}
return false;
}
int AudioHandler::SpaceLeftOnNotificationStack() {
if (voiceNotificationStackFirst>=VOICE_NOTIFICATION_STACK_SIZE || voiceNotificationStackLast>=VOICE_NOTIFICATION_STACK_SIZE) return 0;
if (voiceNotificationStackLast>=voiceNotificationStackFirst) return ((VOICE_NOTIFICATION_STACK_SIZE-1) - (voiceNotificationStackLast-voiceNotificationStackFirst));
return (voiceNotificationStackFirst - voiceNotificationStackLast) - 1;
}
void AudioHandler::PushToNotificationStack(unsigned int notification, unsigned int duration, byte priority) {
// If the switch stack last index is out of range, then it's an error - return
if (SpaceLeftOnNotificationStack() == 0) return;
voiceNotificationNumStack[voiceNotificationStackLast] = notification;
voiceNotificationDuration[voiceNotificationStackLast] = duration;
voiceNotificationPriorityStack[voiceNotificationStackLast] = priority;
voiceNotificationStackLast += 1;
if (voiceNotificationStackLast >= VOICE_NOTIFICATION_STACK_SIZE) {
// If the end index is off the end, then wrap
voiceNotificationStackLast = 0;
}
}
byte AudioHandler::GetTopNotificationPriority() {
byte startStack = voiceNotificationStackFirst;
byte endStack = voiceNotificationStackLast;
if (startStack==endStack) return 0;
byte topPriorityFound = 0;
while (startStack!=endStack) {
if (voiceNotificationPriorityStack[startStack]>topPriorityFound) topPriorityFound = voiceNotificationPriorityStack[startStack];
startStack += 1;
if (startStack >= VOICE_NOTIFICATION_STACK_SIZE) startStack = 0;
}
return topPriorityFound;
}
boolean AudioHandler::QueuePrioritizedNotification(unsigned short notificationIndex, unsigned short notificationLength, byte priority, unsigned long currentTime) {
#if defined (RPU_OS_USE_WAV_TRIGGER) || defined (RPU_OS_USE_WAV_TRIGGER_1p3)
// if everything on the queue has a lower priority, kill all those
byte topQueuePriority = GetTopNotificationPriority();
if (priority>topQueuePriority) {
ClearNotificationStack();
}
// If there's nothing playing, we can play it now
if (currentNotificationPlaying == INVALID_SOUND_INDEX) {
if (currentBackgroundTrack != BACKGROUND_TRACK_NONE) {
wTrig.trackFade(currentBackgroundTrack, musicGain - ducking, 500, 0);
}
if (notificationLength) nextVoiceNotificationPlayTime = currentTime + (unsigned long)(notificationLength);
else nextVoiceNotificationPlayTime = 0;
wTrig.trackPlayPoly(notificationIndex);
wTrig.trackGain(notificationIndex, notificationsGain);
currentNotificationStartTime = currentTime;
currentNotificationPlaying = notificationIndex;
currentNotificationPriority = priority;
} else {
PushToNotificationStack(notificationIndex, notificationLength, priority);
}
#else
// Phony stuff to get rid of warnings
(void)notificationIndex;
(void)notificationLength;
(void)priority;
(void)currentTime;
#endif
return true;
}
void AudioHandler::OutputTracksPlaying() {
#if defined (RPU_OS_USE_WAV_TRIGGER) || defined (RPU_OS_USE_WAV_TRIGGER_1p3)
int i;
char buf[256];
sprintf(buf, "nothing");
Serial.write("Looking for playing tracks\n");
wTrig.getVersion(buf, 256);
Serial.write("Version: ");
Serial.write(buf);
Serial.write("\n");
for (i=0; i<1000; i++) {
if (wTrig.isTrackPlaying(i)) {
sprintf(buf, "Track %d playing\n", i);
Serial.write(buf);
}
}
#endif
}
boolean AudioHandler::ServiceNotificationQueue(unsigned long currentTime) {
boolean queueStillHasEntries = true;
#if defined (RPU_OS_USE_WAV_TRIGGER) || defined (RPU_OS_USE_WAV_TRIGGER_1p3)
boolean playNextNotification = false;
if (nextVoiceNotificationPlayTime != 0) {
if (currentTime > nextVoiceNotificationPlayTime) {
playNextNotification = true;
}
} else {
if (currentNotificationPlaying!=INVALID_SOUND_INDEX && !wTrig.isTrackPlaying(currentNotificationPlaying)) {
if (currentTime>(currentNotificationStartTime+100)) playNextNotification = true;
}
}
if (playNextNotification) {
byte nextPriority = 0;
unsigned int nextNotification = VOICE_NOTIFICATION_STACK_EMPTY;
unsigned int nextDuration = 0;
// Current notification done, see if there's another
if (voiceNotificationStackLast>=VOICE_NOTIFICATION_STACK_SIZE) voiceNotificationStackLast = (VOICE_NOTIFICATION_STACK_SIZE-1);
while (voiceNotificationStackFirst != voiceNotificationStackLast) {
nextPriority = voiceNotificationPriorityStack[voiceNotificationStackFirst];
nextNotification = voiceNotificationNumStack[voiceNotificationStackFirst];
nextDuration = voiceNotificationDuration[voiceNotificationStackFirst];
voiceNotificationStackFirst += 1;
if (voiceNotificationStackFirst >= VOICE_NOTIFICATION_STACK_SIZE) voiceNotificationStackFirst = 0;
if (nextNotification!=INVALID_SOUND_INDEX) break;
}
if (nextNotification != VOICE_NOTIFICATION_STACK_EMPTY) {
if (currentBackgroundTrack != BACKGROUND_TRACK_NONE) {
wTrig.trackFade(currentBackgroundTrack, musicGain - ducking, 500, 0);
}
if (nextDuration!=0) nextVoiceNotificationPlayTime = currentTime + (unsigned long)(nextDuration);
else nextVoiceNotificationPlayTime = 0;
wTrig.trackPlayPoly(nextNotification);
wTrig.trackGain(nextNotification, notificationsGain);
currentNotificationStartTime = currentTime;
currentNotificationPlaying = nextNotification;
currentNotificationPriority = nextPriority;
} else {
// No more notifications -- set the volume back up and clear the variable
if (currentBackgroundTrack != BACKGROUND_TRACK_NONE) {
wTrig.trackFade(currentBackgroundTrack, musicGain, 1500, 0);
}
nextVoiceNotificationPlayTime = 0;
currentNotificationPlaying = INVALID_SOUND_INDEX;
currentNotificationPriority = 0;
queueStillHasEntries = false;
}
}
#else
(void)currentTime;
#endif
return queueStillHasEntries;
}
boolean AudioHandler::StopAllNotifications(byte priority) {
ClearNotificationStack(priority);
return StopCurrentNotification(priority);
}
boolean AudioHandler::StopAllSoundFX() {
#if defined(RPU_OS_USE_WAV_TRIGGER) || defined(RPU_OS_USE_WAV_TRIGGER_1p3)
wTrig.stopAllTracks();
#endif
ClearSoundCardQueue();
ClearSoundQueue();
return false;
}
boolean AudioHandler::StopAllAudio() {
boolean anythingPlaying = false;
if (StopAllMusic()) anythingPlaying = true;
if (StopAllNotifications()) anythingPlaying = true;
if (StopAllSoundFX()) anythingPlaying = true;
return anythingPlaying;
}
void AudioHandler::InitSB300Registers() {
#ifdef RPU_OS_USE_SB300
RPU_PlaySB300SquareWave(1, 0x00); // Write 0x00 to CR2 (Timer 2 off, continuous mode, 16-bit, C2 clock, CR3 set)
RPU_PlaySB300SquareWave(0, 0x00); // Write 0x00 to CR3 (Timer 3 off, continuous mode, 16-bit, C3 clock, not prescaled)
RPU_PlaySB300SquareWave(1, 0x01); // Write 0x00 to CR2 (Timer 2 off, continuous mode, 16-bit, C2 clock, CR1 set)
RPU_PlaySB300SquareWave(0, 0x00); // Write 0x00 to CR1 (Timer 1 off, continuous mode, 16-bit, C1 clock, timers allowed)
#endif
}
void AudioHandler::PlaySB300StartupBeep() {
#ifdef RPU_OS_USE_SB300
RPU_PlaySB300SquareWave(1, 0x92); // Write 0x92 to CR2 (Timer 2 on, continuous mode, 16-bit, E clock, CR3 set)
RPU_PlaySB300SquareWave(0, 0x92); // Write 0x92 to CR3 (Timer 3 on, continuous mode, 16-bit, E clock, not prescaled)
RPU_PlaySB300SquareWave(4, 0x02); // Set Timer 2 to 0x0200
RPU_PlaySB300SquareWave(5, 0x00);
RPU_PlaySB300SquareWave(6, 0x80); // Set Timer 3 to 0x8000
RPU_PlaySB300SquareWave(7, 0x00);
RPU_PlaySB300Analog(0, 0x02);
#endif
}
void AudioHandler::ClearSoundCardQueue() {
#ifdef RPU_OS_USE_SB300
for (int count=0; count<SOUND_CARD_QUEUE_SIZE; count++) {
soundCardQueue[count].playTime = 0;
}
#endif
}
void AudioHandler::ClearSoundQueue() {
for (int count=0; count<SOUND_QUEUE_SIZE; count++) {
soundQueue[count].playTime = 0;
}
}
boolean AudioHandler::PlaySound(unsigned short soundIndex, byte audioType, byte overrideVolume) {
boolean soundPlayed = false;
int gain = soundFXGain;
if (overrideVolume!=0xFF) gain = ConvertVolumeSettingToGain(overrideVolume);
if (audioType==AUDIO_PLAY_TYPE_CHIMES) {
#if defined(RPU_OS_USE_SB100)
// RPU_PlaySB100Chime((byte)soundIndex);
soundPlayed = true;
#endif
} else if (audioType==AUDIO_PLAY_TYPE_ORIGINAL_SOUNDS) {
#ifdef RPU_OS_USE_DASH51
RPU_PlaySoundDash51((byte)soundIndex);
soundPlayed = true;
#endif
#ifdef RPU_OS_USE_S_AND_T
RPU_PlaySoundSAndT((byte)soundIndex);
soundPlayed = true;
#endif
#ifdef RPU_OS_USE_SB100
RPU_PlaySB100((byte)soundIndex);
soundPlayed = true;
#endif
} else if (audioType==AUDIO_PLAY_TYPE_WAV_TRIGGER) {
#if defined(RPU_OS_USE_WAV_TRIGGER) || defined(RPU_OS_USE_WAV_TRIGGER_1p3)
#ifdef RPU_OS_USE_WAV_TRIGGER
wTrig.trackStop(soundIndex);
#endif
wTrig.trackPlayPoly(soundIndex);
wTrig.trackGain(soundIndex, gain);
soundPlayed = true;
#endif
}
(void)gain;
(void)soundIndex;
return soundPlayed;
}
boolean AudioHandler::FadeSound(unsigned short soundIndex, int fadeGain, int numMilliseconds, boolean stopTrack) {
boolean soundFaded = false;
#if defined(RPU_OS_USE_WAV_TRIGGER) || defined(RPU_OS_USE_WAV_TRIGGER_1p3)
wTrig.trackFade(soundIndex, fadeGain, numMilliseconds, stopTrack);
soundFaded = true;
#endif
(void)soundIndex;
(void)fadeGain;
(void)numMilliseconds;
(void)stopTrack;
return soundFaded;
}
boolean AudioHandler::QueueSound(unsigned short soundIndex, byte audioType, unsigned long timeToPlay, byte overrideVolume) {
for (int count=0; count<SOUND_QUEUE_SIZE; count++) {
if (soundQueue[count].playTime==0) {
soundQueue[count].soundIndex = soundIndex;
soundQueue[count].audioType = audioType;
soundQueue[count].playTime = timeToPlay;
soundQueue[count].overrideVolume = overrideVolume;
return true;
}
}
return false;
}
boolean AudioHandler::QueueSoundCardCommand(byte scFunction, byte scRegister, byte scData, unsigned long startTime) {
#ifdef RPU_OS_USE_SB300
for (int count=0; count<SOUND_QUEUE_SIZE; count++) {
if (soundCardQueue[count].playTime==0) {
soundCardQueue[count].soundFunction = scFunction;
soundCardQueue[count].soundRegister = scRegister;
soundCardQueue[count].soundByte = scData;
soundCardQueue[count].playTime = startTime;
return true;
}
}
#else
// Phony stuff to get rid of warnings
unsigned long totalval = scFunction + scRegister + scData + startTime;
totalval += 1;
#endif
return false;
}
void AudioHandler::InitSoundEffectQueue() {
#if defined(RPU_WTYPE_1_SOUND)
CurrentSoundPlaying.soundEffectNum = 0;
CurrentSoundPlaying.requestedPlayTime = 0;
CurrentSoundPlaying.playUntil = 0;
CurrentSoundPlaying.priority = 0;
CurrentSoundPlaying.inUse = false;
for (byte count = 0; count < SOUND_EFFECT_QUEUE_SIZE; count++) {
SoundEffectQueue[count].soundEffectNum = 0;
SoundEffectQueue[count].requestedPlayTime = 0;
SoundEffectQueue[count].playUntil = 0;
SoundEffectQueue[count].priority = 0;
SoundEffectQueue[count].inUse = false;
}
#endif
}
boolean AudioHandler::PlaySoundCardWhenPossible(unsigned short soundEffectNum, unsigned long currentTime, unsigned long requestedPlayTime, unsigned long playUntil, byte priority) {
#if defined(RPU_OS_USE_WTYPE_1_SOUND) || defined(RPU_OS_USE_WTYPE_2_SOUND)
byte count = 0;
for (count = 0; count < SOUND_EFFECT_QUEUE_SIZE; count++) {
if (SoundEffectQueue[count].inUse == false) break;
}
if (count == SOUND_EFFECT_QUEUE_SIZE) return false;
SoundEffectQueue[count].soundEffectNum = soundEffectNum;
SoundEffectQueue[count].requestedPlayTime = requestedPlayTime + currentTime;
SoundEffectQueue[count].playUntil = playUntil + requestedPlayTime + currentTime;
SoundEffectQueue[count].priority = priority;
SoundEffectQueue[count].inUse = true;
#else
// Phony stuff to get rid of warnings
(void)soundEffectNum;
(void)currentTime;
(void)requestedPlayTime;
(void)playUntil;
(void)priority;
return false;
#endif
return true;
}
boolean AudioHandler::ServiceSoundQueue(unsigned long currentTime) {
boolean soundCommandSent = false;
for (int count=0; count<SOUND_QUEUE_SIZE; count++) {
if (soundQueue[count].playTime!=0 && soundQueue[count].playTime<currentTime) {
PlaySound(soundQueue[count].soundIndex, soundQueue[count].audioType, soundQueue[count].overrideVolume);
soundQueue[count].playTime = 0;