Add volume slider in audio settings (#329)

Use logarithmic volume scale
This commit is contained in:
vkedwardli 2021-08-26 19:30:23 +08:00 committed by GitHub
parent 89ccdf2814
commit a51f310e96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 2 deletions

View File

@ -56,6 +56,7 @@ Option<bool> AutoLatency("aica.AutoLatency",
);
OptionString AudioBackend("backend", "auto", "audio");
AudioVolumeOption AudioVolume;
// Rendering

View File

@ -20,6 +20,7 @@
#include <string>
#include <vector>
#include <array>
#include <cmath>
#include "cfg.h"
#include "hw/maple/maple_cfg.h"
@ -305,6 +306,30 @@ extern Option<bool> AutoLatency;
extern OptionString AudioBackend;
class AudioVolumeOption : public Option<int> {
public:
AudioVolumeOption() : Option<int>("aica.Volume", 100) {};
float logarithmic_volume_scale = 1.0;
void load() override {
Option<int>::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<RenderType> {

View File

@ -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)
{

View File

@ -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,