forked from reeltwo/PenumbraShadowMD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarcduinoSound.h
629 lines (585 loc) · 19 KB
/
MarcduinoSound.h
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
#include "DFRobotDFPlayerMini.h"
/***********************************************************
* MP3sound.c
* MarcDuino interface to play sounds from an MP3Trigger board
*
* Created on: Sep 17, 2013
* Author: Marc Verdiell
* Copyright � 2013 Marc Verdiell, All Rights Reserved
*
***********************************************************/
/***********************************************************
* On the MP3, there are a maximum of 255 sound files
* They must be named NNN-xxxx.mp3
* Where NNN = 001 to 255
* The numbering ranges are predetermined, 25 sounds per
* bank category
* Bank 1: gen sounds, numbered 001 to 025
* Bank 2: chat sounds, numbered 026 to 050
* Bank 3: happy sounds, numbered 051 to 075
* Bank 4: sad sounds, numbered 076 to 100
* Bank 5: whistle sounds, numbered 101 to 125
* Bank 6: scream sounds, numbered 126 to 150
* Bank 7: Leia sounds, numbered 151 to 175
* Bank 8: sing sounds (deprecated, not used by R2 Touch)
* Bank 9: mus sounds, numbered 176 to 181
*
* The pre-made R2 sound library contains only a few non-copyrighted music sounds.
* Sound 202, 203 and 205 are "beeped music" place-holders, meant to be replaced with the
* original score of Star Wars, Empire March, and Cantina respectively
*
* If you add your own sounds in Bank 9, make sure you update the last variable
* MP3_BANK9_SOUNDS below to reflect the total number of sounds
*
***********************************************************/
#define MP3_MAX_BANKS 9 // nine banks
#define MP3_MAX_SOUNDS_PER_BANK 25 // no more than 25 sound in each
#define MP3_BANK_CUTOFF 4 // cutoff for banks that play "next" sound on $x
// for the random sounds, needs to know max sounds of first 5 banks
// only important for sounds below cutoff
#define MP3_BANK1_SOUNDS 19 // gen sounds, numbered 001 to 025
#define MP3_BANK2_SOUNDS 18 // chat sounds, numbered 026 to 050
#define MP3_BANK3_SOUNDS 7 // happy sounds, numbered 051 to 075
#define MP3_BANK4_SOUNDS 4 // sad sounds, numbered 076 to 100
#define MP3_BANK5_SOUNDS 3 // whistle sounds, numbered 101 to 125
// unless you change bank cutoff, these are ignored, so I set them to max
#define MP3_BANK6_SOUNDS MP3_MAX_SOUNDS_PER_BANK // scream sounds, numbered 126 to 150
#define MP3_BANK7_SOUNDS MP3_MAX_SOUNDS_PER_BANK // Leia sounds, numbered 151 to 175
#define MP3_BANK8_SOUNDS MP3_MAX_SOUNDS_PER_BANK // sing sounds (deprecated, not used by R2 Touch)
#define MP3_BANK9_SOUNDS MP3_MAX_SOUNDS_PER_BANK // mus sounds, numbered 201 t0 225
// this defines where the startup sound is
#define MP3_EMPTY_SOUND 252 // workaround, used to stop sounds
// Defines for the Volume control
// DFPlayer Mini has a different set of values used.
#define DF_VOLUME_MID 15 // guessing mid volume 32 is right in-between...
#define DF_VOLUME_MIN 1 // doc says anything below 64 is inaudible, not true, 100 is. 82 is another good value
#define DF_VOLUME_MAX 30 // doc says max is 0
#define DF_VOLUME_OFF 0 // to turn it off... 255 gets a buzz.
// Defines for the SparkFun MP3 Trigger
#define MP3_VOLUME_MID 50 // guessing mid volume 32 is right in-between...
#define MP3_VOLUME_MIN 100 // doc says anything below 64 is inaudible, not true, 100 is. 82 is another good value
#define MP3_VOLUME_MAX 0 // doc says max is 0
#define MP3_VOLUME_OFF 254 // to turn it off... 255 gets a buzz.
#define MP3_VOLUME_STEPS 20 // R2 Touch app has 20 steps from min to max
#define MP3_PLAY_CMD 't' // command to play sound file on the MP3 trigger
#define MP3_VOLUME_CMD 'v' // command to play sound file on the MP3 trigger
#define MP3_STOP_CMD 'O' // command to stop/play - not used
#define MP3_MIN_RANDOM_PAUSE 600 // min wait on random sounds
#define MP3_MAX_RANDOM_PAUSE 10000 // max wait on random sounds
#define MP3_MAX_PAUSE_ON_RESUME 12 // default wait to resume random. Works for short sound. Set mp3_random_timer manually for long ones.
#ifndef SOUND_DEBUG
#define SOUND_DEBUG(...)
#endif
/////////////COMMAND VOCABULARY///////////
// Play sound command by bank/sound numbers
// $xyy
// x=bank number
// yy=sound number. If none, next sound is played in the bank
//
// Other commands
// $c
// where c is a command character
// R - random from 4 first banks
// O - sound off
// L - Leia message (bank 7 sound 1)
// C - Cantina music (bank 9 sound 5)
// c - Beep cantina (bank 9 sound 1)
// S - Scream (bank 6 sound 1)
// F - Faint/Short Circuit (bank 6 sound 3)
// D - Disco (bank 9 sound 6)
// s - stop sounds
// + - volume up
// - - volume down
// m - volume mid
// f - volume max
// p - volume min
// W - Star Wars music (bank 9 sound 2)
// M - Imperial March (bank 9 sound 3)
//
///////////////////////////////////////////////
class MarcSound
{
public:
enum Module
{
kDisabled,
kMP3Trigger,
kDFMini,
kHCR
};
void idle()
{
if (fModule != kDisabled)
{
uint32_t currentMillis = millis();
if (fRandomEnabled && fNextRandomEvent && currentMillis >= fNextRandomEvent)
{
playRandom();
fNextRandomEvent = millis() + random(fRandomMinDelay, fRandomMaxDelay);
}
}
}
void setRandomMin(uint32_t delay)
{
fRandomMinDelay = delay;
}
void setRandomMax(uint32_t delay)
{
fRandomMaxDelay = delay;
}
void playSound(uint8_t bank, uint8_t sound)
{
uint8_t filenum;
if (bank > MP3_MAX_BANKS)
return;
if (bank != 0 && sound > MP3_MAX_SOUNDS_PER_BANK)
return;
// if bank=0 play the sound number provided
if (bank == 0)
filenum = sound;
else if (sound != 0)
{
// calculate actual file number on the MP3 memory card
filenum = (bank - 1) * MP3_MAX_SOUNDS_PER_BANK + sound;
// also adjust last sound played index for the next sound command
// make sure not to go past max sounds
if (sound > fMaxSounds[bank])
fBankIndexes[bank] = fMaxSounds[bank];
else
fBankIndexes[bank] = sound;
}
// sound "0", play first or next sound depending on bank
else
{
if (bank <= MP3_BANK_CUTOFF)
{
// advance index, rewind to first sound if at end
if ((++fBankIndexes[bank]) > fMaxSounds[bank])
fBankIndexes[bank] = 1;
// we'll play the new indexed sound
sound = fBankIndexes[bank];
}
else
{
// for banks that always play the first sound
sound = 1;
}
filenum = (bank - 1) * MP3_MAX_SOUNDS_PER_BANK + sound;
}
switch (fModule)
{
case kDisabled:
break;
case kDFMini:
fDFMini.play(filenum);
break;
case kMP3Trigger:
// send a 't'nnn number where nnn=file number
SOUND_SERIAL.write(MP3_PLAY_CMD);
SOUND_SERIAL.write(filenum);
break;
case kHCR:
{
char buffer[10];
snprintf(buffer, sizeof(buffer), "<CA%04d>", filenum);
switch (bank)
{
case kGenSounds:
sendHCR("<SS0>");
break;
case kChatSounds:
sendHCR("<MT><MT>");
break;
case kHappySounds:
if (filenum < fMaxSounds[bank] / 2)
sendHCR("<SH0>");
else
sendHCR("<SH1>");
break;
case kSadSounds:
if (filenum < fMaxSounds[bank] / 2)
sendHCR("<SS0>");
else
sendHCR("<SS1>");
break;
case kWhistleSounds:
sendHCR("<MT><MT>");
break;
case kScreamSounds:
switch (sound)
{
case 1:
sendHCR("<SC0>");
break;
case 2:
sendHCR("<SC1>");
break;
case 3:
default:
sendHCR("<SE>");
break;
}
break;
case 0:
case kLeiaSounds:
case kSingSounds:
case kMusicSounds:
sendHCR(buffer);
break;
}
break;
}
}
}
void playRandom()
{
if (fModule == kHCR)
{
sendHCR("<MT><MT>");
return;
}
uint8_t num;
// Plays a random sound from the first 5 banks only
num = random(1, MP3_BANK1_SOUNDS + MP3_BANK2_SOUNDS + MP3_BANK3_SOUNDS + MP3_BANK4_SOUNDS + MP3_BANK5_SOUNDS);
if(num <= MP3_BANK1_SOUNDS)
{
playSound(1, num);
return;
}
num -= MP3_BANK1_SOUNDS;
if (num <= MP3_BANK2_SOUNDS)
{
playSound(2, num);
return;
}
num -= MP3_BANK2_SOUNDS;
if (num <= MP3_BANK3_SOUNDS)
{
playSound(3, num);
return;
}
num -= MP3_BANK3_SOUNDS;
if (num <= MP3_BANK4_SOUNDS)
{
playSound(4, num);
return;
}
num -= MP3_BANK4_SOUNDS;
if (num <= MP3_BANK5_SOUNDS)
{
playSound(5, num);
return;
}
}
void playStartSound()
{
if (fStartupSound != -1)
playSound(0, fStartupSound);
}
void stop()
{
switch (fModule)
{
case kDisabled:
break;
case kDFMini:
fDFMini.stop();
break;
case kMP3Trigger:
playSound(0, MP3_EMPTY_SOUND);
break;
case kHCR:
sendHCR("<PSG>");
break;
}
}
inline void startRandom()
{
startRandomInSeconds(1);
}
void startRandomInSeconds(uint32_t seconds)
{
fRandomEnabled = true;
fNextRandomEvent = millis() + seconds * 1000L;
}
void stopRandom()
{
fRandomEnabled = true;
}
void suspendRandom()
{
fRandomEnabledSaved = fRandomEnabled;
fRandomEnabled = false;
}
void resumeRandomInSeconds(uint32_t seconds)
{
fRandomEnabled = fRandomEnabledSaved;
if (fRandomEnabled)
fNextRandomEvent = millis() + seconds * 1000L;
}
inline void resumeRandom()
{
resumeRandomInSeconds(MP3_MAX_PAUSE_ON_RESUME);
}
void volumeMid()
{
setVolume(0.5);
}
void volumeOff()
{
setVolume(0.0);
}
void volumeMax()
{
setVolume(1.0);
}
void volumeMin()
{
setVolume(0.01);
}
void volumeUp()
{
setVolume(fVolume + (1.0 / MP3_VOLUME_STEPS));
}
void volumeDown()
{
setVolume(fVolume - (1.0 / MP3_VOLUME_STEPS));
}
void setVolume(float volume)
{
// clamp value to 0.0 - 1.0
volume = std::min<float>(volume, 1.0);
volume = std::max<float>(volume, 0.0);
fVolume = volume;
switch (fModule)
{
case kDisabled:
break;
case kDFMini:
fDFMini.volume(ceil(volume * DF_VOLUME_MAX));
break;
case kMP3Trigger:
sendMP3(MP3_VOLUME_CMD);
sendMP3(MP3_VOLUME_MIN - volume * MP3_VOLUME_MIN);
break;
case kHCR:
{
char buffer[30];
snprintf(buffer, sizeof(buffer), "<PVV100><PVA%d><PVB%d>", int(volume * 100), int(volume * 100));
sendHCR(buffer);
break;
}
}
}
bool begin(Module module, Stream& stream, int startupSound = -1)
{
fModule = kDisabled;
fStartupSound = startupSound;
switch (module)
{
case kDisabled:
fStream = nullptr;
break;
case kDFMini:
fStream = &stream;
if (!fDFMini.begin(stream))
{
DEBUG_PRINTLN("Unable to begin:");
DEBUG_PRINTLN("1.Please recheck the connection!");
DEBUG_PRINTLN("2.Please insert the SD card!");
DEBUG_PRINTLN("2.Reboot!");
return false;
}
fDFMini.EQ(DFPLAYER_EQ_NORMAL);
break;
case kMP3Trigger:
fStream = &stream;
break;
case kHCR:
fStream = &stream;
break;
}
for (uint8_t i=0; i < SizeOfArray(fBankIndexes); i++)
{
fBankIndexes[i] = 0;
}
fModule = module;
return true;
}
bool handleCommand(const char* cmd, bool skipStart = false)
{
////////////////////////////////////////////////
// Play sound command by bank/sound numbers
// $xyy
// x=bank number
// yy=sound number. If none, next sound is played in the bank
//
// Other commands
// $c
// where c is a command character
// R - random from 4 first banks
// O - sound off
// L - Leia message (bank 7 sound 1)
// C - Cantina music (bank 9 sound 5)
// c - Beep cantina (bank 9 sound 1)
// S - Scream (bank 6 sound 1)
// F - Faint/Short Circuit (bank 6 sound 3)
// D - Disco (bank 9 sound 6)
// s - stop sounds
// + - volume up
// - - volume down
// m - volume mid
// f - volume max
// p - volume min
// W - Star Wars music (bank 9 sound 2)
// M - Imperial March (bank 9 sound 3)
//
///////////////////////////////////////////////
if (skipStart)
cmd--;
uint8_t len = strlen(cmd);
// check the start character
if (!skipStart && cmd[0] != '$')
return false;
// should have between 2 and 4 characters
if (len<2 || len>4)
{
return false;
}
char cmdch = cmd[1];
// if the command character is a digit, this is a sound play command
if (isdigit(cmdch))
{
stopRandom(); // any manual sound command stops random automatically
uint8_t bank = (uint8_t)cmdch - '0';
uint8_t sound = 0;
if (len > 2)
{
sound = atoi(cmd + 2);
}
playSound(bank, sound);
return true;
}
// the command is a character
switch(cmdch)
{
case 'R': // R - random from 4 first banks
startRandom(); // keep firing random sounds
break;
case 'O': // O - sound off
stopRandom();
volumeOff();
break;
case 'L': // L - Leia message (bank 7 sound 1)
suspendRandom();
playSound(7,1);
resumeRandomInSeconds(44); // 34s + 10s extra long delay
break;
case 'C': // C - Cantina music (bank 9 sound 5)
suspendRandom();
playSound(8,5);
resumeRandomInSeconds(56); // extra long delay
break;
case 'c': // c - Beep cantina (bank 9 sound 1)
suspendRandom();
playSound(8,1);
resumeRandomInSeconds(27); // extra long delay
break;
case 'S': // S - Scream (bank 6 sound 1)
suspendRandom();
playSound(6,2);
resumeRandom();
break;
case 'F': // F - Faint/Short Circuit (bank 6 sound 3)
suspendRandom();
playSound(6,3);
resumeRandom();
break;
case 'D': // D - Disco (bank 9 sound 6)
suspendRandom();
playSound(8,6);
resumeRandomInSeconds(40); // 6:26 +10s min extra long delay
break;
case 's': // s - stop sounds
stopRandom();
stop();
break;
case '+': // + - volume up
volumeUp();
break;
case '-': // - - volume down
volumeDown();
break;
case 'm': // m - volume mid
volumeMid();
break;
case 'f': // f - volume max
volumeMax();
break;
case 'p': // p - volume min
volumeMin();
break;
case 'W': // W - Star Wars music (bank 9 sound 2)
stopRandom(); // so long, just stop random
playSound(8,2);
break;
case 'M': // M - Imperial March (bank 9 sound 3)
stopRandom(); // so long, just stop random
playSound(8,3);
break;
default:
return false;
}
return true;
}
private:
DFRobotDFPlayerMini fDFMini;
Stream* fStream = nullptr;
float fVolume = 0.5;
Module fModule = kDisabled;
bool fRandomEnabled = false;
bool fRandomEnabledSaved = false;
uint32_t fNextRandomEvent = 0;
uint32_t fRandomMinDelay = 600;
uint32_t fRandomMaxDelay = 10000;
int fStartupSound = -1;
// global variables, current indexes to banks
uint8_t fBankIndexes[MP3_MAX_BANKS];
const uint8_t fMaxSounds[MP3_MAX_BANKS] =
{
MP3_BANK1_SOUNDS,
MP3_BANK2_SOUNDS,
MP3_BANK3_SOUNDS,
MP3_BANK4_SOUNDS,
MP3_BANK5_SOUNDS,
MP3_BANK6_SOUNDS,
MP3_BANK7_SOUNDS,
MP3_BANK8_SOUNDS,
MP3_BANK9_SOUNDS
};
enum
{
kGenSounds = 1,
kChatSounds = 2,
kHappySounds = 3,
kSadSounds = 4,
kWhistleSounds = 5,
kScreamSounds = 6,
kLeiaSounds = 7,
kSingSounds = 8,
kMusicSounds = 9
};
void sendMP3(uint8_t cmd)
{
SOUND_DEBUG("MP3:0x%02X\n", cmd);
if (fStream != nullptr)
fStream->write(cmd);
}
void sendHCR(const char* cmd)
{
SOUND_DEBUG("HCR: %s\n", cmd);
if (fStream != nullptr)
fStream->print(cmd);
}
};
MarcSound sMarcSound;