Add config option for volume scale

This commit is contained in:
刘皓 2024-08-06 15:50:06 -04:00
parent d04b383667
commit 4a5328ba14
No known key found for this signature in database
GPG key ID: 7901753DB465B711
11 changed files with 37 additions and 12 deletions

View file

@ -467,6 +467,16 @@
// //
// "YJITEnable": false, // "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) // SoundFont to use for midi playback (via fluidsynth)
// (default: none) // (default: none)
// //

View file

@ -170,20 +170,20 @@ namespace Source
enum VolumeScale enum VolumeScale
{ {
Linear, Linear = 0,
Db35, Db35 = 1,
}; };
inline void setVolume(Source::ID id, float value, VolumeScale scale) inline void setVolume(Source::ID id, float value, VolumeScale scale)
{ {
switch (scale) { switch (scale) {
case VolumeScale::Linear:
break;
case VolumeScale::Db35: case VolumeScale::Db35:
if (value > FLT_EPSILON) { if (value > FLT_EPSILON) {
value = std::powf(10.0f, -(35.0f / 20.0f) * (1.0f - value)); value = std::powf(10.0f, -(35.0f / 20.0f) * (1.0f - value));
} }
break; break;
default:
break;
} }
alSourcef(id.al, AL_GAIN, value * 0.8f); alSourcef(id.al, AL_GAIN, value * 0.8f);
} }

View file

@ -36,9 +36,11 @@
#include <SDL_timer.h> #include <SDL_timer.h>
ALStream::ALStream(LoopMode loopMode, ALStream::ALStream(LoopMode loopMode,
AL::Source::VolumeScale volumeScale,
const std::string &threadId) const std::string &threadId)
: looped(loopMode == Looped), : looped(loopMode == Looped),
state(Closed), state(Closed),
volumeScale(volumeScale),
source(0), source(0),
thread(0), thread(0),
preemptPause(false), preemptPause(false),
@ -46,7 +48,7 @@ ALStream::ALStream(LoopMode loopMode,
{ {
alSrc = AL::Source::gen(); 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::setPitch(alSrc, 1.0f);
AL::Source::detachBuffer(alSrc); AL::Source::detachBuffer(alSrc);
@ -164,7 +166,7 @@ void ALStream::pause()
void ALStream::setVolume(float value) void ALStream::setVolume(float value)
{ {
AL::Source::setVolume(alSrc, value, AL::Source::Linear); AL::Source::setVolume(alSrc, value, volumeScale);
} }
void ALStream::setPitch(float value) void ALStream::setPitch(float value)

View file

@ -47,6 +47,8 @@ struct ALStream
bool looped; bool looped;
State state; State state;
AL::Source::VolumeScale volumeScale;
ALDataSource *source; ALDataSource *source;
SDL_Thread *thread; SDL_Thread *thread;
@ -89,6 +91,7 @@ struct ALStream
}; };
ALStream(LoopMode loopMode, ALStream(LoopMode loopMode,
AL::Source::VolumeScale volumeScale,
const std::string &threadId); const std::string &threadId);
~ALStream(); ~ALStream();

View file

@ -69,15 +69,15 @@ struct AudioPrivate
} meWatch; } meWatch;
AudioPrivate(RGSSThreadData &rtData) AudioPrivate(RGSSThreadData &rtData)
: bgs(ALStream::Looped, "bgs"), : bgs(ALStream::Looped, static_cast<AL::Source::VolumeScale>(rtData.config.volumeScale), "bgs"),
me(ALStream::NotLooped, "me"), me(ALStream::NotLooped, static_cast<AL::Source::VolumeScale>(rtData.config.volumeScale), "me"),
se(rtData.config), se(rtData.config),
syncPoint(rtData.syncPoint), syncPoint(rtData.syncPoint),
volumeRatio(1) volumeRatio(1)
{ {
for (int i = 0; i < rtData.config.BGM.trackCount; i++) { for (int i = 0; i < rtData.config.BGM.trackCount; i++) {
std::string id = std::string("bgm" + std::to_string(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<AL::Source::VolumeScale>(rtData.config.volumeScale), id.c_str()));
} }
meWatch.state = MeNotPlaying; meWatch.state = MeNotPlaying;

View file

@ -29,10 +29,11 @@
#include <SDL_timer.h> #include <SDL_timer.h>
AudioStream::AudioStream(ALStream::LoopMode loopMode, AudioStream::AudioStream(ALStream::LoopMode loopMode,
AL::Source::VolumeScale volumeScale,
const std::string &threadId) const std::string &threadId)
: extPaused(false), : extPaused(false),
noResumeStop(false), noResumeStop(false),
stream(loopMode, threadId) stream(loopMode, volumeScale, threadId)
{ {
current.volume = 1.0f; current.volume = 1.0f;
current.pitch = 1.0f; current.pitch = 1.0f;

View file

@ -121,6 +121,7 @@ struct AudioStream
} fadeIn; } fadeIn;
AudioStream(ALStream::LoopMode loopMode, AudioStream(ALStream::LoopMode loopMode,
AL::Source::VolumeScale volumeScale,
const std::string &threadId); const std::string &threadId);
~AudioStream(); ~AudioStream();

View file

@ -93,7 +93,8 @@ SoundEmitter::SoundEmitter(const Config &conf)
srcCount(conf.SE.sourceCount), srcCount(conf.SE.sourceCount),
alSrcs(srcCount), alSrcs(srcCount),
atchBufs(srcCount), atchBufs(srcCount),
srcPrio(srcCount) srcPrio(srcCount),
volumeScale(static_cast<AL::Source::VolumeScale>(conf.volumeScale))
{ {
for (size_t i = 0; i < srcCount; ++i) for (size_t i = 0; i < srcCount; ++i)
{ {
@ -172,7 +173,7 @@ void SoundEmitter::play(const std::string &filename,
if (switchBuffer) if (switchBuffer)
AL::Source::attachBuffer(src, buffer->alBuffer); 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::setPitch(src, _pitch);
AL::Source::play(src); AL::Source::play(src);

View file

@ -49,6 +49,8 @@ struct SoundEmitter
/* Indices of sources, sorted by priority (lowest first) */ /* Indices of sources, sorted by priority (lowest first) */
std::vector<size_t> srcPrio; std::vector<size_t> srcPrio;
AL::Source::VolumeScale volumeScale;
SoundEmitter(const Config &conf); SoundEmitter(const Config &conf);
~SoundEmitter(); ~SoundEmitter();

View file

@ -178,6 +178,7 @@ void Config::read(int argc, char *argv[]) {
{"dataPathApp", ""}, {"dataPathApp", ""},
{"iconPath", ""}, {"iconPath", ""},
{"execName", "Game"}, {"execName", "Game"},
{"volumeScale", 1},
{"midiSoundFont", ""}, {"midiSoundFont", ""},
{"midiChorus", false}, {"midiChorus", false},
{"midiReverb", false}, {"midiReverb", false},
@ -306,6 +307,7 @@ try { exp } catch (...) {}
SET_OPT(anyAltToggleFS, boolean); SET_OPT(anyAltToggleFS, boolean);
SET_OPT(enableReset, boolean); SET_OPT(enableReset, boolean);
SET_OPT(enableSettings, boolean); SET_OPT(enableSettings, boolean);
SET_OPT(volumeScale, integer);
SET_STRINGOPT(midi.soundFont, midiSoundFont); SET_STRINGOPT(midi.soundFont, midiSoundFont);
SET_OPT_CUSTOMKEY(midi.chorus, midiChorus, boolean); SET_OPT_CUSTOMKEY(midi.chorus, midiChorus, boolean);
SET_OPT_CUSTOMKEY(midi.reverb, midiReverb, boolean); SET_OPT_CUSTOMKEY(midi.reverb, midiReverb, boolean);

View file

@ -22,6 +22,7 @@
#ifndef CONFIG_H #ifndef CONFIG_H
#define CONFIG_H #define CONFIG_H
#include "al-util.h"
#include "util/json5pp.hpp" #include "util/json5pp.hpp"
#include <set> #include <set>
@ -93,6 +94,8 @@ struct Config {
std::string execName; std::string execName;
std::string titleLanguage; std::string titleLanguage;
int volumeScale;
struct { struct {
std::string soundFont; std::string soundFont;
bool chorus; bool chorus;