mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-22 17:29:18 +02:00
add sound gain and pitch compatibility modes
This commit is contained in:
22
Globals.cpp
22
Globals.cpp
@@ -203,6 +203,8 @@ double Global::fMWDlowVolt[2] = { 150, 1023 };
|
|||||||
int Global::iMWDdivider = 5;
|
int Global::iMWDdivider = 5;
|
||||||
|
|
||||||
opengl_light Global::DayLight;
|
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.depth
|
||||||
>> Global::shadowtune.distance;
|
>> 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")
|
else if (token == "smoothtraction")
|
||||||
{
|
{
|
||||||
// podwójna jasność ambient
|
// podwójna jasność ambient
|
||||||
|
|||||||
@@ -356,5 +356,13 @@ class Global
|
|||||||
|
|
||||||
static opengl_light DayLight;
|
static opengl_light DayLight;
|
||||||
|
|
||||||
|
enum soundmode_t
|
||||||
|
{
|
||||||
|
linear,
|
||||||
|
scaled,
|
||||||
|
compat
|
||||||
|
};
|
||||||
|
static soundmode_t soundpitchmode;
|
||||||
|
static soundmode_t soundgainmode;
|
||||||
};
|
};
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|||||||
22
sound.cpp
22
sound.cpp
@@ -253,6 +253,7 @@ simple_sound::simple_sound(sound_buffer *buf) : sound::sound()
|
|||||||
buffer = buf;
|
buffer = buf;
|
||||||
alSourcei(id, AL_BUFFER, buffer->get_id());
|
alSourcei(id, AL_BUFFER, buffer->get_id());
|
||||||
buffer->ref();
|
buffer->ref();
|
||||||
|
samplerate = buffer->get_samplerate();
|
||||||
}
|
}
|
||||||
|
|
||||||
sound::~sound()
|
sound::~sound()
|
||||||
@@ -347,14 +348,22 @@ void simple_sound::update(float dt)
|
|||||||
|
|
||||||
sound& sound::gain(float gain)
|
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);
|
alSourcef(id, AL_GAIN, gain);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
sound& sound::pitch(float pitch)
|
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);
|
alSourcef(id, AL_PITCH, pitch);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -395,6 +404,8 @@ complex_sound::complex_sound(sound_buffer* pre, sound_buffer* main, sound_buffer
|
|||||||
buffer->ref();
|
buffer->ref();
|
||||||
post->ref();
|
post->ref();
|
||||||
|
|
||||||
|
samplerate = buffer->get_samplerate();
|
||||||
|
|
||||||
cs = state::post;
|
cs = state::post;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -488,6 +499,11 @@ bool complex_sound::is_playing()
|
|||||||
return cs != state::post; // almost accurate
|
return cs != state::post; // almost accurate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int sound_buffer::get_samplerate()
|
||||||
|
{
|
||||||
|
return samplerate;
|
||||||
|
}
|
||||||
|
|
||||||
sound_buffer::sound_buffer(std::string &file)
|
sound_buffer::sound_buffer(std::string &file)
|
||||||
{
|
{
|
||||||
WriteLog("creating sound buffer from " + file);
|
WriteLog("creating sound buffer from " + file);
|
||||||
@@ -505,6 +521,8 @@ sound_buffer::sound_buffer(std::string &file)
|
|||||||
|
|
||||||
sf_close(sf);
|
sf_close(sf);
|
||||||
|
|
||||||
|
samplerate = si.samplerate;
|
||||||
|
|
||||||
int16_t *buf = nullptr;
|
int16_t *buf = nullptr;
|
||||||
if (si.channels == 1)
|
if (si.channels == 1)
|
||||||
buf = fbuf;
|
buf = fbuf;
|
||||||
|
|||||||
3
sound.h
3
sound.h
@@ -25,11 +25,13 @@ class sound_buffer
|
|||||||
ALuint id;
|
ALuint id;
|
||||||
uint32_t refcount;
|
uint32_t refcount;
|
||||||
std::chrono::time_point<std::chrono::steady_clock> last_unref;
|
std::chrono::time_point<std::chrono::steady_clock> last_unref;
|
||||||
|
int samplerate;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
sound_buffer(std::string &file);
|
sound_buffer(std::string &file);
|
||||||
~sound_buffer();
|
~sound_buffer();
|
||||||
|
|
||||||
|
int get_samplerate();
|
||||||
ALuint get_id();
|
ALuint get_id();
|
||||||
void ref();
|
void ref();
|
||||||
void unref();
|
void unref();
|
||||||
@@ -47,6 +49,7 @@ protected:
|
|||||||
float max_dist;
|
float max_dist;
|
||||||
bool spatial;
|
bool spatial;
|
||||||
glm::vec3 pos;
|
glm::vec3 pos;
|
||||||
|
int samplerate;
|
||||||
|
|
||||||
ALuint id;
|
ALuint id;
|
||||||
sound();
|
sound();
|
||||||
|
|||||||
Reference in New Issue
Block a user