From 493da2a7bd9dffb62da6a33ade709ead1fc84676 Mon Sep 17 00:00:00 2001 From: casey langen Date: Wed, 8 Mar 2017 22:04:29 -0800 Subject: [PATCH] Improved DirectSound volume ramping algorithm. --- src/contrib/directsoundout/DirectSoundOut.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/contrib/directsoundout/DirectSoundOut.cpp b/src/contrib/directsoundout/DirectSoundOut.cpp index ca1b48d07..e6f4a292c 100644 --- a/src/contrib/directsoundout/DirectSoundOut.cpp +++ b/src/contrib/directsoundout/DirectSoundOut.cpp @@ -158,14 +158,21 @@ void DirectSoundOut::SetVolume(double volume) { if (this->state != StateStopped) { if (this->secondaryBuffer) { - double db = (volume < 0.0001f) - ? DSBVOLUME_MIN - : log10(this->volume) * 6000.f; + /* normalize between 1 and 10000 (DSBVOLUME_MIN) */ + double scaled = fabs(volume * 10000); + scaled = scaled < 0 ? 1 : scaled; + + /* found on experts-exchange (20181717) */ + double db = ((log10(scaled) / 4) * 10000) - 10000; if (db > DSBVOLUME_MAX) { db = DSBVOLUME_MAX; } + if (db < DSBVOLUME_MIN) { + db = DSBVOLUME_MIN; + } + this->secondaryBuffer->SetVolume((LONG) db); } }