From 4a5328ba1400d42dee8454b77eb1cfd5464b0cc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=9A=93?= Date: Tue, 6 Aug 2024 15:50:06 -0400 Subject: [PATCH] Add config option for volume scale --- mkxp.json | 10 ++++++++++ src/audio/al-util.h | 8 ++++---- src/audio/alstream.cpp | 6 ++++-- src/audio/alstream.h | 3 +++ src/audio/audio.cpp | 6 +++--- src/audio/audiostream.cpp | 3 ++- src/audio/audiostream.h | 1 + src/audio/soundemitter.cpp | 5 +++-- src/audio/soundemitter.h | 2 ++ src/config.cpp | 2 ++ src/config.h | 3 +++ 11 files changed, 37 insertions(+), 12 deletions(-) diff --git a/mkxp.json b/mkxp.json index 9ff4a295..2acc6950 100644 --- a/mkxp.json +++ b/mkxp.json @@ -467,6 +467,16 @@ // // "YJITEnable": false, + // Determines how the volume value of a sound affects how loud the sound is. + // The stock RPG Maker runtimes use a 35 dB scale for volume (option 1), + // but other runtimes might use a different scale. + // 0: linear (sound amplitude is multiplied by the volume) + // 1: 35 dB (a gain of -0.35 dB is applied to the sound for every percent + // the volume is set below 100%) + // (default: 1) + // + // "volumeScale": 1, + // SoundFont to use for midi playback (via fluidsynth) // (default: none) // diff --git a/src/audio/al-util.h b/src/audio/al-util.h index 98715de3..01a963ec 100644 --- a/src/audio/al-util.h +++ b/src/audio/al-util.h @@ -170,20 +170,20 @@ namespace Source enum VolumeScale { - Linear, - Db35, + Linear = 0, + Db35 = 1, }; inline void setVolume(Source::ID id, float value, VolumeScale scale) { switch (scale) { - case VolumeScale::Linear: - break; case VolumeScale::Db35: if (value > FLT_EPSILON) { value = std::powf(10.0f, -(35.0f / 20.0f) * (1.0f - value)); } break; + default: + break; } alSourcef(id.al, AL_GAIN, value * 0.8f); } diff --git a/src/audio/alstream.cpp b/src/audio/alstream.cpp index 25893ee1..f404341b 100644 --- a/src/audio/alstream.cpp +++ b/src/audio/alstream.cpp @@ -36,9 +36,11 @@ #include ALStream::ALStream(LoopMode loopMode, + AL::Source::VolumeScale volumeScale, const std::string &threadId) : looped(loopMode == Looped), state(Closed), + volumeScale(volumeScale), source(0), thread(0), preemptPause(false), @@ -46,7 +48,7 @@ ALStream::ALStream(LoopMode loopMode, { alSrc = AL::Source::gen(); - AL::Source::setVolume(alSrc, 1.0f, AL::Source::Linear); + AL::Source::setVolume(alSrc, 1.0f, volumeScale); AL::Source::setPitch(alSrc, 1.0f); AL::Source::detachBuffer(alSrc); @@ -164,7 +166,7 @@ void ALStream::pause() void ALStream::setVolume(float value) { - AL::Source::setVolume(alSrc, value, AL::Source::Linear); + AL::Source::setVolume(alSrc, value, volumeScale); } void ALStream::setPitch(float value) diff --git a/src/audio/alstream.h b/src/audio/alstream.h index 7391c4b5..0a09be03 100644 --- a/src/audio/alstream.h +++ b/src/audio/alstream.h @@ -47,6 +47,8 @@ struct ALStream bool looped; State state; + AL::Source::VolumeScale volumeScale; + ALDataSource *source; SDL_Thread *thread; @@ -89,6 +91,7 @@ struct ALStream }; ALStream(LoopMode loopMode, + AL::Source::VolumeScale volumeScale, const std::string &threadId); ~ALStream(); diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index 6fec3486..fe549d0c 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -69,15 +69,15 @@ struct AudioPrivate } meWatch; AudioPrivate(RGSSThreadData &rtData) - : bgs(ALStream::Looped, "bgs"), - me(ALStream::NotLooped, "me"), + : bgs(ALStream::Looped, static_cast(rtData.config.volumeScale), "bgs"), + me(ALStream::NotLooped, static_cast(rtData.config.volumeScale), "me"), se(rtData.config), syncPoint(rtData.syncPoint), volumeRatio(1) { for (int i = 0; i < rtData.config.BGM.trackCount; i++) { std::string id = std::string("bgm" + std::to_string(i)); - bgmTracks.push_back(new AudioStream(ALStream::Looped, id.c_str())); + bgmTracks.push_back(new AudioStream(ALStream::Looped, static_cast(rtData.config.volumeScale), id.c_str())); } meWatch.state = MeNotPlaying; diff --git a/src/audio/audiostream.cpp b/src/audio/audiostream.cpp index be0b9b3d..a917c050 100644 --- a/src/audio/audiostream.cpp +++ b/src/audio/audiostream.cpp @@ -29,10 +29,11 @@ #include AudioStream::AudioStream(ALStream::LoopMode loopMode, + AL::Source::VolumeScale volumeScale, const std::string &threadId) : extPaused(false), noResumeStop(false), - stream(loopMode, threadId) + stream(loopMode, volumeScale, threadId) { current.volume = 1.0f; current.pitch = 1.0f; diff --git a/src/audio/audiostream.h b/src/audio/audiostream.h index 5f97fcca..b8b359a2 100644 --- a/src/audio/audiostream.h +++ b/src/audio/audiostream.h @@ -121,6 +121,7 @@ struct AudioStream } fadeIn; AudioStream(ALStream::LoopMode loopMode, + AL::Source::VolumeScale volumeScale, const std::string &threadId); ~AudioStream(); diff --git a/src/audio/soundemitter.cpp b/src/audio/soundemitter.cpp index f1452cf6..7c437137 100644 --- a/src/audio/soundemitter.cpp +++ b/src/audio/soundemitter.cpp @@ -93,7 +93,8 @@ SoundEmitter::SoundEmitter(const Config &conf) srcCount(conf.SE.sourceCount), alSrcs(srcCount), atchBufs(srcCount), - srcPrio(srcCount) + srcPrio(srcCount), + volumeScale(static_cast(conf.volumeScale)) { for (size_t i = 0; i < srcCount; ++i) { @@ -172,7 +173,7 @@ void SoundEmitter::play(const std::string &filename, if (switchBuffer) AL::Source::attachBuffer(src, buffer->alBuffer); - AL::Source::setVolume(src, _volume, AL::Source::Linear); + AL::Source::setVolume(src, _volume, volumeScale); AL::Source::setPitch(src, _pitch); AL::Source::play(src); diff --git a/src/audio/soundemitter.h b/src/audio/soundemitter.h index 0a834231..ab0209ef 100644 --- a/src/audio/soundemitter.h +++ b/src/audio/soundemitter.h @@ -49,6 +49,8 @@ struct SoundEmitter /* Indices of sources, sorted by priority (lowest first) */ std::vector srcPrio; + AL::Source::VolumeScale volumeScale; + SoundEmitter(const Config &conf); ~SoundEmitter(); diff --git a/src/config.cpp b/src/config.cpp index 6d89d8e5..e2cab65e 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -178,6 +178,7 @@ void Config::read(int argc, char *argv[]) { {"dataPathApp", ""}, {"iconPath", ""}, {"execName", "Game"}, + {"volumeScale", 1}, {"midiSoundFont", ""}, {"midiChorus", false}, {"midiReverb", false}, @@ -306,6 +307,7 @@ try { exp } catch (...) {} SET_OPT(anyAltToggleFS, boolean); SET_OPT(enableReset, boolean); SET_OPT(enableSettings, boolean); + SET_OPT(volumeScale, integer); SET_STRINGOPT(midi.soundFont, midiSoundFont); SET_OPT_CUSTOMKEY(midi.chorus, midiChorus, boolean); SET_OPT_CUSTOMKEY(midi.reverb, midiReverb, boolean); diff --git a/src/config.h b/src/config.h index 74605773..5ebe3bce 100644 --- a/src/config.h +++ b/src/config.h @@ -22,6 +22,7 @@ #ifndef CONFIG_H #define CONFIG_H +#include "al-util.h" #include "util/json5pp.hpp" #include @@ -93,6 +94,8 @@ struct Config { std::string execName; std::string titleLanguage; + int volumeScale; + struct { std::string soundFont; bool chorus;