16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-19 14:39:18 +02:00

add sound gain and pitch compatibility modes

This commit is contained in:
milek7
2017-08-19 11:27:11 +02:00
parent 6d592815c0
commit eaddd4b91a
4 changed files with 53 additions and 2 deletions

View File

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

View File

@@ -356,5 +356,13 @@ class Global
static opengl_light DayLight;
enum soundmode_t
{
linear,
scaled,
compat
};
static soundmode_t soundpitchmode;
static soundmode_t soundgainmode;
};
//---------------------------------------------------------------------------

View File

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

View File

@@ -25,11 +25,13 @@ class sound_buffer
ALuint id;
uint32_t refcount;
std::chrono::time_point<std::chrono::steady_clock> 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();