diff --git a/Globals.cpp b/Globals.cpp index 3b88dba2..ae01ecfe 100644 --- a/Globals.cpp +++ b/Globals.cpp @@ -203,6 +203,8 @@ double Global::fMWDlowVolt[2] = { 150, 1023 }; int Global::iMWDdivider = 5; opengl_light Global::DayLight; +Global::soundmode_t Global::soundpitchmode = Global::linear; +Global::soundmode_t Global::soundgainmode = Global::linear; //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- @@ -552,6 +554,26 @@ void Global::ConfigParse(cParser &Parser) >> Global::shadowtune.depth >> Global::shadowtune.distance; } + else if (token == "soundgainmode") + { + Parser.getTokens(); + Parser >> token; + if (token == "linear") + Global::soundgainmode = Global::linear; + else if (token == "scaled") + Global::soundgainmode = Global::scaled; + else if (token == "compat") + Global::soundgainmode = Global::compat; + } + else if (token == "soundpitchmode") + { + Parser.getTokens(); + Parser >> token; + if (token == "linear") + Global::soundpitchmode = Global::linear; + else if (token == "compat") + Global::soundpitchmode = Global::compat; + } else if (token == "smoothtraction") { // podwójna jasność ambient diff --git a/Globals.h b/Globals.h index 3ed5e2d3..c5e281fa 100644 --- a/Globals.h +++ b/Globals.h @@ -356,5 +356,13 @@ class Global static opengl_light DayLight; + enum soundmode_t + { + linear, + scaled, + compat + }; + static soundmode_t soundpitchmode; + static soundmode_t soundgainmode; }; //--------------------------------------------------------------------------- diff --git a/sound.cpp b/sound.cpp index 79ab5cea..490f4442 100644 --- a/sound.cpp +++ b/sound.cpp @@ -253,6 +253,7 @@ simple_sound::simple_sound(sound_buffer *buf) : sound::sound() buffer = buf; alSourcei(id, AL_BUFFER, buffer->get_id()); buffer->ref(); + samplerate = buffer->get_samplerate(); } sound::~sound() @@ -347,14 +348,22 @@ void simple_sound::update(float dt) sound& sound::gain(float gain) { - gain = std::min(std::max(0.0f, gain * gain_mul + gain_off), 1.0f); + gain = gain * gain_mul + gain_off; + if (Global::soundgainmode == Global::scaled) + gain /= 1.75f; + if (Global::soundgainmode == Global::compat) + gain = std::pow(10.0f, ((-50.0f + 50.0f * gain) / 20.0f)); + gain = std::min(std::max(0.0f, gain), 2.0f); alSourcef(id, AL_GAIN, gain); return *this; } sound& sound::pitch(float pitch) { - pitch = std::min(std::max(0.05f, pitch * pitch_mul + pitch_off), 20.0f); + pitch = pitch * pitch_mul + pitch_off; + if (Global::soundpitchmode == Global::compat) + pitch *= 22050.0f / (float)samplerate; + pitch = std::min(std::max(0.05f, pitch), 20.0f); alSourcef(id, AL_PITCH, pitch); return *this; } @@ -395,6 +404,8 @@ complex_sound::complex_sound(sound_buffer* pre, sound_buffer* main, sound_buffer buffer->ref(); post->ref(); + samplerate = buffer->get_samplerate(); + cs = state::post; } @@ -488,6 +499,11 @@ bool complex_sound::is_playing() return cs != state::post; // almost accurate } +int sound_buffer::get_samplerate() +{ + return samplerate; +} + sound_buffer::sound_buffer(std::string &file) { WriteLog("creating sound buffer from " + file); @@ -505,6 +521,8 @@ sound_buffer::sound_buffer(std::string &file) sf_close(sf); + samplerate = si.samplerate; + int16_t *buf = nullptr; if (si.channels == 1) buf = fbuf; diff --git a/sound.h b/sound.h index 132cff88..2b22c45e 100644 --- a/sound.h +++ b/sound.h @@ -25,11 +25,13 @@ class sound_buffer ALuint id; uint32_t refcount; std::chrono::time_point last_unref; + int samplerate; public: sound_buffer(std::string &file); ~sound_buffer(); + int get_samplerate(); ALuint get_id(); void ref(); void unref(); @@ -47,6 +49,7 @@ protected: float max_dist; bool spatial; glm::vec3 pos; + int samplerate; ALuint id; sound();