Skip to content

Commit

Permalink
Made the volume controls logarithmic instead of linear.
Browse files Browse the repository at this point in the history
  • Loading branch information
apanteleev committed Dec 9, 2019
1 parent 2d5c6a7 commit 958a5d7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions inc/client/sound/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ void S_RawSamples(int samples, int rate, int width,

void S_UnqueueRawSamples();

float S_GetLinearVolume(float perceptual);

typedef enum {
SS_NOT,
#if USE_SNDDMA
Expand Down
2 changes: 1 addition & 1 deletion src/client/sound/al.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ void AL_Update(void)
AL_CopyVector(listener_forward, orientation);
AL_CopyVector(listener_up, orientation + 3);
qalListenerfv(AL_ORIENTATION, orientation);
qalListenerf(AL_GAIN, s_volume->value);
qalListenerf(AL_GAIN, S_GetLinearVolume(s_volume->value));
qalDistanceModel(AL_LINEAR_DISTANCE_CLAMPED);

// update spatialization for dynamic sounds
Expand Down
17 changes: 17 additions & 0 deletions src/client/sound/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1168,4 +1168,21 @@ void S_Update(void)
#endif
}

float S_GetLinearVolume(float perceptual)
{
float volume = perceptual;

// clamp anything below 1% to zero
if (volume < 0.01f)
return 0.f;

// 50 dB exponential curve
// more info: https://www.dr-lex.be/info-stuff/volumecontrols.html
volume = 0.003161f * expf(perceptual * 5.757f);

// upper limit
volume = min(1.f, volume);

return volume;
}

5 changes: 2 additions & 3 deletions src/client/sound/mix.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,13 @@ void S_PaintChannels(int endtime)
}
}


void S_InitScaletable(void)
{
int i, j;
int scale;

Cvar_ClampValue(s_volume, 0, 1);

snd_vol = s_volume->value * 256;
snd_vol = S_GetLinearVolume(s_volume->value) * 256;
for (i = 0; i < 32; i++) {
scale = i * 8 * snd_vol;
for (j = 0; j < 256; j++) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/sound/ogg.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ static OGG_Read(void)
ogg_numsamples += read_samples;

S_RawSamples(read_samples, ogg_file->sample_rate, ogg_file->channels, ogg_file->channels,
(byte *)samples, ogg_volume->value);
(byte *)samples, S_GetLinearVolume(ogg_volume->value));
}
else
{
Expand Down

0 comments on commit 958a5d7

Please sign in to comment.