From aee82f548dbc68d4ace8d483717a3cc0d99f1aea Mon Sep 17 00:00:00 2001 From: Maciej Gibowicz Date: Wed, 17 Jul 2024 15:39:11 +0200 Subject: [PATCH] [BH-2032] Add fade in and fade out support for very short songs If the song is shorter than the fade in and out durations, we reduce the target volume value so that both phases have time to complete. --- products/BellHybrid/services/audio/VolumeFade.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/products/BellHybrid/services/audio/VolumeFade.cpp b/products/BellHybrid/services/audio/VolumeFade.cpp index 0fe1e1e6e9f06cf6386d46bbb679abd9357d76c0..ecf4ad6eb249636300418d58623ff5dbb8d66257 100644 --- a/products/BellHybrid/services/audio/VolumeFade.cpp +++ b/products/BellHybrid/services/audio/VolumeFade.cpp @@ -53,6 +53,16 @@ namespace audio currentVolume = minVolume + fadeStep; phase = Phase::FadeIn; timestamp = std::chrono::system_clock::now(); + + if (this->fadeParams.playbackDuration.has_value()) { + // If the song is shorter than the fade in and out durations, we reduce the target volume value so that both + // phases have time to complete + const auto maxFadePeriod = + std::chrono::duration_cast(fadeParams.playbackDuration.value()) / 2; + const auto maxTargetVolume = (maxFadePeriod.count() * fadeStep) / fadeInterval.count(); + this->targetVolume = std::clamp(std::min(targetVolume, maxTargetVolume), minVolume, maxVolume); + } + Restart(); timerHandle.restart(fadeInterval); }