From a51f310e96a0605b25e8fb59fd0007245335d46b Mon Sep 17 00:00:00 2001 From: vkedwardli Date: Thu, 26 Aug 2021 19:30:23 +0800 Subject: [PATCH] Add volume slider in audio settings (#329) Use logarithmic volume scale --- core/cfg/option.cpp | 1 + core/cfg/option.h | 25 +++++++++++++++++++++++++ core/oslib/audiostream.cpp | 4 ++-- core/rend/gui.cpp | 4 ++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/core/cfg/option.cpp b/core/cfg/option.cpp index 31399801a..8c6174183 100644 --- a/core/cfg/option.cpp +++ b/core/cfg/option.cpp @@ -56,6 +56,7 @@ Option AutoLatency("aica.AutoLatency", ); OptionString AudioBackend("backend", "auto", "audio"); +AudioVolumeOption AudioVolume; // Rendering diff --git a/core/cfg/option.h b/core/cfg/option.h index 844fd876e..40a603b0f 100644 --- a/core/cfg/option.h +++ b/core/cfg/option.h @@ -20,6 +20,7 @@ #include #include #include +#include #include "cfg.h" #include "hw/maple/maple_cfg.h" @@ -305,6 +306,30 @@ extern Option AutoLatency; extern OptionString AudioBackend; +class AudioVolumeOption : public Option { +public: + AudioVolumeOption() : Option("aica.Volume", 100) {}; + float logarithmic_volume_scale = 1.0; + + void load() override { + Option::load(); + calcDbPower(); + } + + float dbPower() + { + return logarithmic_volume_scale; + } + void calcDbPower() + { + // dB scaling calculation: https://www.dr-lex.be/info-stuff/volumecontrols.html + logarithmic_volume_scale = fmin(exp(4.605 * float(value) / 100.0) / 100.0, 1.0); + if (value < 10) + logarithmic_volume_scale *= value / 10.0; + } +}; +extern AudioVolumeOption AudioVolume; + // Rendering class RendererOption : public Option { diff --git a/core/oslib/audiostream.cpp b/core/oslib/audiostream.cpp index 506bd07a7..f638bb614 100644 --- a/core/oslib/audiostream.cpp +++ b/core/oslib/audiostream.cpp @@ -79,8 +79,8 @@ audiobackend_t* GetAudioBackend(const std::string& slug) void WriteSample(s16 r, s16 l) { - Buffer[writePtr].r = r; - Buffer[writePtr].l = l; + Buffer[writePtr].r = r * config::AudioVolume.dbPower(); + Buffer[writePtr].l = l * config::AudioVolume.dbPower(); if (++writePtr == SAMPLE_COUNT) { diff --git a/core/rend/gui.cpp b/core/rend/gui.cpp index e60685719..ffe4ace56 100644 --- a/core/rend/gui.cpp +++ b/core/rend/gui.cpp @@ -1558,6 +1558,10 @@ static void gui_display_settings() OptionCheckbox("Disable Sound", config::DisableSound, "Disable the emulator sound output"); OptionCheckbox("Enable DSP", config::DSPEnabled, "Enable the Dreamcast Digital Sound Processor. Only recommended on fast platforms"); + if (OptionSlider("Volume Level", config::AudioVolume, 0, 100, "Adjust the emulator's audio level")) + { + config::AudioVolume.calcDbPower(); + }; #ifdef __ANDROID__ if (config::AudioBackend.get() == "auto" || config::AudioBackend.get() == "android") OptionCheckbox("Automatic Latency", config::AutoLatency,