~aleteoryx/muditaos

aee82f548dbc68d4ace8d483717a3cc0d99f1aea — Maciej Gibowicz 1 year, 4 months ago f41c227
[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.
1 files changed, 10 insertions(+), 0 deletions(-)

M products/BellHybrid/services/audio/VolumeFade.cpp
M products/BellHybrid/services/audio/VolumeFade.cpp => products/BellHybrid/services/audio/VolumeFade.cpp +10 -0
@@ 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<std::chrono::milliseconds>(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);
    }