Skip to content

Commit ef920c2

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents 52f6531 + 0b1b731 commit ef920c2

File tree

6 files changed

+68
-23
lines changed

6 files changed

+68
-23
lines changed
192 Bytes
Binary file not shown.

src/dashgauges-A10001986.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@
115115

116116
/* Changelog
117117
*
118+
* 2025/01/15 (A10001986) [1.13]
119+
* - Optimize play_key; keyX will be stopped instead of (re)started if it is
120+
* currently played when repeatedly triggered
118121
* 2025/01/13 (A10001986) [1.12]
119122
* - BTTFN: Minor code optimization
120123
* 2024/10/27 (A10001986)

src/dg_audio.cpp

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static int sampleCnt = 0;
109109

110110
bool playingEmpty = false;
111111
bool playingEmptyEnds = false;
112+
uint16_t key_playing = 0;
112113

113114
bool playingDoor = false;
114115

@@ -117,6 +118,9 @@ static float append_vol;
117118
static uint16_t append_flags;
118119
static bool appendFile = false;
119120

121+
static char keySnd[] = "/key3.mp3"; // not const
122+
static bool haveKeySnd[10];
123+
120124
static const char *tcdrdone = "/TCD_DONE.TXT"; // leave "TCD", SD is interchangable this way
121125
unsigned long renNow1;
122126

@@ -183,6 +187,13 @@ void audio_setup()
183187
endWaitSequence();
184188
}
185189

190+
// Check for keyX sounds to avoid unsuccessful file-lookups every time
191+
for(int i = 1; i < 10; i++) {
192+
if(i == 8) continue;
193+
keySnd[4] = '0' + i;
194+
haveKeySnd[i] = check_file_SD(keySnd);
195+
}
196+
186197
audioInitDone = true;
187198
}
188199

@@ -196,31 +207,33 @@ void audio_loop()
196207
if(!mp3->loop()) {
197208
mp3->stop();
198209
playingEmpty = playingDoor = false;
210+
key_playing = 0;
199211
if(appendFile) {
200212
play_file(append_audio_file, append_flags, append_vol);
201213
} else if(mpActive) {
202214
mp_next(true);
203215
}
204-
} else {
216+
} else if(dynVol) {
205217
sampleCnt++;
206218
if(sampleCnt > 1) {
207-
if(dynVol) out->SetGain(getVolume());
219+
out->SetGain(getVolume());
208220
sampleCnt = 0;
209221
}
210222
}
211223
} else if(wav->isRunning()) {
212224
if(!wav->loop()) {
213225
wav->stop();
214226
playingEmpty = playingDoor = false;
227+
key_playing = 0;
215228
if(appendFile) {
216229
play_file(append_audio_file, append_flags, append_vol);
217230
} else if(mpActive) {
218231
mp_next(true);
219232
}
220-
} else {
233+
} else if(dynVol) {
221234
sampleCnt++;
222235
if(sampleCnt > 1) {
223-
if(dynVol) out->SetGain(getVolume());
236+
out->SetGain(getVolume());
224237
sampleCnt = 0;
225238
}
226239
}
@@ -302,8 +315,10 @@ void play_file(const char *audio_file, uint16_t flags, float volumeFactor)
302315

303316
playingEmpty = (flags & PA_ISEMPTY) ? true : false;
304317
playingEmptyEnds = false;
305-
306318
playingDoor = (flags & PA_DOOR) ? true : false;
319+
key_playing = flags & 0xff00;
320+
321+
sampleCnt = 0;
307322

308323
out->SetGain(getVolume());
309324

@@ -317,17 +332,14 @@ void play_file(const char *audio_file, uint16_t flags, float volumeFactor)
317332
curSeek = findWAVdata(buf);
318333
mySD0L->setStartPos(curSeek);
319334
mySD0L->seek(0, SEEK_SET);
320-
321335
wav->begin(mySD0L, out);
322336
} else {
323337
mySD0L->read((void *)buf, 10);
324338
curSeek = skipID3(buf);
325339
mySD0L->setStartPos(curSeek);
326340
mySD0L->seek(curSeek, SEEK_SET);
327-
328341
mp3->begin(mySD0L, out);
329342
}
330-
331343
#ifdef DG_DBG
332344
Serial.println(F("Playing from SD"));
333345
#endif
@@ -352,11 +364,12 @@ void play_file(const char *audio_file, uint16_t flags, float volumeFactor)
352364
myFS0L->seek(curSeek, SEEK_SET);
353365
mp3->begin(myFS0L, out);
354366
}
355-
356367
#ifdef DG_DBG
357368
Serial.println(F("Playing from flash FS"));
358369
#endif
359370
} else {
371+
key_playing = 0;
372+
playingEmpty = playingDoor = false;
360373
#ifdef DG_DBG
361374
Serial.println(F("Audio file not found"));
362375
#endif
@@ -385,6 +398,26 @@ void remove_appended_empty()
385398
}
386399
}
387400

401+
void play_key(int k, bool stopOnly)
402+
{
403+
uint16_t pa_key = (k == 9) ? 0x8000 : (1 << (7+k));
404+
405+
if(!haveKeySnd[k]) return;
406+
407+
if(pa_key == key_playing) {
408+
mp3->stop();
409+
key_playing = 0;
410+
return;
411+
}
412+
413+
if(stopOnly)
414+
return;
415+
416+
keySnd[4] = '0' + k;
417+
418+
play_file(keySnd, pa_key|PA_INTRMUS|PA_ALLOWSD|PA_DYNVOL);
419+
}
420+
388421
// Returns value for volume based on the position of the pot
389422
// Since the values vary we do some noise reduction
390423
#ifdef DG_HAVEVOLKNOB
@@ -479,6 +512,11 @@ static float getVolume()
479512
* Helpers for external
480513
*/
481514

515+
bool check_file_SD(const char *audio_file)
516+
{
517+
return (haveSD && SD.exists(audio_file));
518+
}
519+
482520
bool checkAudioDone()
483521
{
484522
if(mp3->isRunning() || wav->isRunning()) return false;
@@ -489,13 +527,13 @@ void stopAudio()
489527
{
490528
if(mp3->isRunning()) {
491529
mp3->stop();
492-
playingEmpty = playingDoor = false;
493530
}
494531
if(wav->isRunning()) {
495532
wav->stop();
496-
playingEmpty = playingDoor = false;
497533
}
498534
appendFile = false; // Clear appended, stop means stop.
535+
playingEmpty = playingDoor = false;
536+
key_playing = 0;
499537
}
500538

501539
void stopAudioAtLoopEnd()

src/dg_audio.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,25 @@
6666
#define PA_ISEMPTY 0x0010
6767
#define PA_WAV 0x0020
6868
#define PA_DOOR 0x0040
69+
// upper 8 bits all taken
6970
#define PA_MASKA (PA_LOOP|PA_INTRMUS|PA_ALLOWSD|PA_DYNVOL|PA_ISEMPTY)
7071

7172
void audio_setup();
7273
void audio_loop();
7374

7475
void play_file(const char *audio_file, uint16_t flags, float volumeFactor = 1.0);
7576
void append_file(const char *audio_file, uint16_t flags, float volumeFactor = 1.0);
76-
bool checkAudioDone();
77-
void stopAudio();
78-
void stopAudioAtLoopEnd();
79-
bool append_pending();
8077

8178
void play_empty();
8279
//void append_empty();
8380
void remove_appended_empty();
81+
void play_key(int k, bool stopOnly = false);
82+
83+
bool check_file_SD(const char *audio_file);
84+
bool checkAudioDone();
85+
void stopAudio();
86+
void stopAudioAtLoopEnd();
87+
bool append_pending();
8488

8589
void mp_init(bool isSetup);
8690
void mp_play(bool forcePlay = true);

src/dg_global.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*** Version Strings ***
1616
*************************************************************************/
1717

18-
#define DG_VERSION "V1.12"
19-
#define DG_VERSION_EXTRA "JAN132025"
18+
#define DG_VERSION "V1.13"
19+
#define DG_VERSION_EXTRA "JAN152025"
2020

2121
//#define DG_DBG // debug output on Serial
2222

src/dg_main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,18 +1347,18 @@ static void execute_remote_command()
13471347
if(command < 10) { // 900x
13481348
switch(command) {
13491349
case 1:
1350-
play_file("/key1.mp3", PA_INTRMUS|PA_ALLOWSD|PA_DYNVOL);
1350+
play_key(1);
13511351
break;
13521352
case 2:
13531353
if(haveMusic) {
13541354
mp_prev(mpActive);
13551355
}
13561356
break;
13571357
case 3:
1358-
play_file("/key3.mp3", PA_INTRMUS|PA_ALLOWSD|PA_DYNVOL);
1358+
play_key(3);
13591359
break;
13601360
case 4:
1361-
play_file("/key4.mp3", PA_INTRMUS|PA_ALLOWSD|PA_DYNVOL);
1361+
play_key(4);
13621362
break;
13631363
case 5:
13641364
if(haveMusic) {
@@ -1371,18 +1371,18 @@ static void execute_remote_command()
13711371
}
13721372
break;
13731373
case 6:
1374-
play_file("/key6.mp3", PA_INTRMUS|PA_ALLOWSD|PA_DYNVOL);
1374+
play_key(6);
13751375
break;
13761376
case 7:
1377-
play_file("/key7.mp3", PA_INTRMUS|PA_ALLOWSD|PA_DYNVOL);
1377+
play_key(7);
13781378
break;
13791379
case 8:
13801380
if(haveMusic) {
13811381
mp_next(mpActive);
13821382
}
13831383
break;
13841384
case 9:
1385-
play_file("/key9.mp3", PA_INTRMUS|PA_ALLOWSD|PA_DYNVOL);
1385+
play_key(9);
13861386
break;
13871387
}
13881388

0 commit comments

Comments
 (0)