Add dB volume scale

This commit is contained in:
刘皓 2024-08-06 12:58:16 -04:00
parent 1462dc9623
commit aaf5d3c8bf
No known key found for this signature in database
GPG key ID: 7901753DB465B711
3 changed files with 21 additions and 4 deletions

View file

@ -27,6 +27,8 @@
#include <SDL_audio.h>
#include <assert.h>
#include <cfloat>
#include <cmath>
namespace AL
{
@ -166,8 +168,23 @@ namespace Source
return value;
}
inline void setVolume(Source::ID id, float value)
enum VolumeScale
{
Linear,
Db35,
};
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., -(35. / 20.) * (1. - value));
}
break;
}
alSourcef(id.al, AL_GAIN, value);
}

View file

@ -46,7 +46,7 @@ ALStream::ALStream(LoopMode loopMode,
{
alSrc = AL::Source::gen();
AL::Source::setVolume(alSrc, 1.0f);
AL::Source::setVolume(alSrc, 1.0f, AL::Source::Linear);
AL::Source::setPitch(alSrc, 1.0f);
AL::Source::detachBuffer(alSrc);
@ -164,7 +164,7 @@ void ALStream::pause()
void ALStream::setVolume(float value)
{
AL::Source::setVolume(alSrc, value);
AL::Source::setVolume(alSrc, value, AL::Source::Linear);
}
void ALStream::setPitch(float value)

View file

@ -172,7 +172,7 @@ void SoundEmitter::play(const std::string &filename,
if (switchBuffer)
AL::Source::attachBuffer(src, buffer->alBuffer);
AL::Source::setVolume(src, _volume * GLOBAL_VOLUME);
AL::Source::setVolume(src, _volume * GLOBAL_VOLUME, AL::Source::Linear);
AL::Source::setPitch(src, _pitch);
AL::Source::play(src);