Skip to content

Commit 1910e40

Browse files
committed
AudioPlayer make fade optional
1 parent 96f633b commit 1910e40

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

src/AudioTools/AudioPlayer.h

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ namespace audio_tools {
253253
return info;
254254
}
255255

256-
/// starts / resumes the playing of a matching song
256+
/// starts / resumes the playing after calling stop()
257257
virtual void play() {
258258
TRACED();
259259
setActive(true);
@@ -331,13 +331,15 @@ namespace audio_tools {
331331

332332
/// The same like start() / stop()
333333
virtual void setActive(bool isActive){
334-
if (isActive){
335-
fade.setFadeInActive(true);
336-
} else {
337-
fade.setFadeOutActive(true);
338-
copier.copy();
339-
writeSilence(2048);
340-
}
334+
if (is_auto_fade){
335+
if (isActive){
336+
fade.setFadeInActive(true);
337+
} else {
338+
fade.setFadeOutActive(true);
339+
copier.copy();
340+
writeSilence(2048);
341+
}
342+
}
341343
active = isActive;
342344
}
343345

@@ -450,6 +452,15 @@ namespace audio_tools {
450452
return &volume_out;
451453
}
452454

455+
/// Activates/deactivates the automatic fade in and fade out to prevent popping sounds: default is active
456+
bool setAutoFade(bool active){
457+
is_auto_fade = active;
458+
}
459+
460+
bool isAutoFade() {
461+
return is_auto_fade;
462+
}
463+
453464
protected:
454465
bool active = false;
455466
bool autonext = true;
@@ -472,13 +483,14 @@ namespace audio_tools {
472483
int stream_increment = 1; // +1 moves forward; -1 moves backward
473484
float current_volume = -1.0; // illegal value which will trigger an update
474485
int delay_if_full = 100;
486+
bool is_auto_fade = true;
475487

476488
void setupFade() {
477489
if (p_final_print != nullptr) {
478490
fade.setAudioInfo(p_final_print->audioInfo());
479491
} else if (p_final_stream != nullptr){
480492
fade.setAudioInfo(p_final_stream->audioInfo());
481-
}
493+
}
482494
}
483495

484496

@@ -487,7 +499,7 @@ namespace audio_tools {
487499
if (p_final_stream==nullptr) return;
488500
if (p_final_stream->availableForWrite()==0) return;
489501
if (p_input_stream == nullptr || millis() > timeout) {
490-
fade.setFadeInActive(true);
502+
if (is_auto_fade) fade.setFadeInActive(true);
491503
if (autonext) {
492504
LOGI("-> timeout - moving by %d", stream_increment);
493505
// open next stream
@@ -505,10 +517,12 @@ namespace audio_tools {
505517
void writeEnd() {
506518
// end silently
507519
TRACEI();
508-
fade.setFadeOutActive(true);
509-
copier.copy();
510-
// start by fading in
511-
fade.setFadeInActive(true);
520+
if (is_auto_fade){
521+
fade.setFadeOutActive(true);
522+
copier.copy();
523+
// start by fading in
524+
fade.setFadeInActive(true);
525+
}
512526
// restart the decoder to make sure it does not contain any audio when we continue
513527
p_decoder->begin();
514528
}

src/AudioTools/Fade.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class Fade {
101101
}
102102
}
103103
}
104+
is_fade_out = false;
104105
LOGI("faded out %d frames to volume %f",frames, volume);
105106
}
106107

0 commit comments

Comments
 (0)