[sound] Make all volumes 0-1 (float) instead of 0-255 (int)

The scaling up of the volumes when setting a channel's volume bothered
me. The biggest issue being it hasn't been necessary for over a decade
since the conversion to a float-mixer. Now the volume and attenuation
scaling from protocol bytes is entirely in the client's hands.
This commit is contained in:
Bill Currie 2022-06-03 16:54:57 +09:00
parent e1a0bde5ee
commit 5b38117689
6 changed files with 52 additions and 56 deletions

View file

@ -225,8 +225,8 @@ struct sfxblock_s {
struct channel_s {
channel_t *next; //!< next channel in "free" list
sfx_t *sfx; //!< sound played by this channel
int leftvol; //!< 0-255 volume
int rightvol; //!< 0-255 volume
float leftvol; //!< 0-1 volume
float rightvol; //!< 0-1 volume
unsigned end; //!< end time in global paintsamples
unsigned pos; //!< sample position in sfx
unsigned looping; //!< where to loop, -1 = no looping
@ -235,7 +235,7 @@ struct channel_s {
vec3_t origin; //!< origin of sound effect
vec_t dist_mult; //!< distance multiplier (attenuation/clip)
int pause; //!< don't update the channel at all
int master_vol; //!< 0-255 master volume
float volume; //!< 0-1 overall channel volume
int phase; //!< phase shift between l-r in samples
int oldphase; //!< phase shift between l-r in samples
/** signal between main program and mixer thread that the channel is to be

View file

@ -448,8 +448,8 @@ s_updateAmbientSounds (snd_t *snd, const byte *ambient_sound_level)
ambient_channel++) {
chan = ambient_channels[ambient_channel];
if (chan) {
chan->master_vol = 0;
chan->leftvol = chan->rightvol = chan->master_vol;
chan->volume = 0;
chan->leftvol = chan->rightvol = chan->volume;
}
}
return;
@ -480,24 +480,23 @@ s_updateAmbientSounds (snd_t *snd, const byte *ambient_sound_level)
// sfx will be written to chan->sfx later to ensure mixer doesn't use
// channel prematurely.
vol = ambient_level * ambient_sound_level[ambient_channel];
if (vol < 8)
vol = ambient_level * ambient_sound_level[ambient_channel] * (1/255.0);
if (vol < 8/255.0)
vol = 0;
// don't adjust volume too fast
if (chan->master_vol < vol) {
chan->master_vol += *snd_render_data.host_frametime
* ambient_fade;
if (chan->master_vol > vol)
chan->master_vol = vol;
} else if (chan->master_vol > vol) {
chan->master_vol -= *snd_render_data.host_frametime
* ambient_fade;
if (chan->master_vol < vol)
chan->master_vol = vol;
float fade = ambient_fade * (1/255.0);
if (chan->volume < vol) {
chan->volume += *snd_render_data.host_frametime * fade;
if (chan->volume > vol)
chan->volume = vol;
} else if (chan->volume > vol) {
chan->volume -= *snd_render_data.host_frametime * fade;
if (chan->volume < vol)
chan->volume = vol;
}
chan->leftvol = chan->rightvol = chan->master_vol;
chan->leftvol = chan->rightvol = chan->volume;
chan->sfx = sfx;
}
}
@ -515,8 +514,8 @@ s_spatialize (snd_t *snd, channel_t *ch)
// anything coming from the view entity will always be full volume
if (!snd_render_data.viewentity
|| ch->entnum == *snd_render_data.viewentity) {
ch->leftvol = ch->master_vol;
ch->rightvol = ch->master_vol;
ch->leftvol = ch->volume;
ch->rightvol = ch->volume;
ch->phase = 0;
return;
}
@ -542,12 +541,12 @@ s_spatialize (snd_t *snd, channel_t *ch)
// add in distance effect
scale = (1.0 - dist) * rscale;
ch->rightvol = (int) (ch->master_vol * scale);
ch->rightvol = ch->volume * scale;
if (ch->rightvol < 0)
ch->rightvol = 0;
scale = (1.0 - dist) * lscale;
ch->leftvol = (int) (ch->master_vol * scale);
ch->leftvol = ch->volume * scale;
if (ch->leftvol < 0)
ch->leftvol = 0;
@ -647,9 +646,8 @@ snd_check_channels (snd_t *snd, channel_t *target_chan, const channel_t *check,
void
SND_StartSound (snd_t *snd, int entnum, int entchannel, sfx_t *sfx,
vec4f_t origin, float fvol, float attenuation)
vec4f_t origin, float vol, float attenuation)
{
int vol;
int looped;
channel_t *target_chan, *check;
sfx_t *osfx;
@ -662,12 +660,10 @@ SND_StartSound (snd_t *snd, int entnum, int entchannel, sfx_t *sfx,
if (!target_chan)
return;
vol = fvol * 255;
// spatialize
VectorCopy (origin, target_chan->origin);
target_chan->dist_mult = attenuation / sound_nominal_clip_dist;
target_chan->master_vol = vol;
target_chan->volume = vol;
target_chan->entnum = entnum;
target_chan->entchannel = entchannel;
s_spatialize (snd, target_chan);
@ -747,8 +743,8 @@ SND_StaticSound (snd_t *snd, sfx_t *sfx, vec4f_t origin, float vol,
return;
VectorCopy (origin, ss->origin);
ss->master_vol = vol;
ss->dist_mult = (attenuation / 64) / sound_nominal_clip_dist;
ss->volume = vol;
ss->dist_mult = attenuation / sound_nominal_clip_dist;
ss->end = 0;
s_spatialize (snd, ss);

View file

@ -522,8 +522,8 @@ s_channel_get_state (channel_t *chan)
static void
s_channel_set_volume (channel_t *chan, float volume)
{
chan->master_vol = volume * 256; //FIXME
chan->leftvol = chan->rightvol = chan->master_vol;
chan->volume = volume;
chan->leftvol = chan->rightvol = chan->volume;
}
static void

View file

@ -44,9 +44,7 @@
#include "compat.h"
#include "snd_internal.h"
#define VOLSCALE 512.0 // so mixing is less likely to overflow
// note: must be >= 255 due to the channel
// volumes being 0-255.
#define VOLSCALE 0.5 // so mixing is less likely to overflow
portable_samplepair_t snd_paintbuffer[PAINTBUFFER_SIZE * 2];
static int max_overpaint; // number of extra samples painted
@ -226,8 +224,8 @@ snd_paint_mono (int offs, channel_t *ch, float *sfx, unsigned count)
unsigned i = 0;
portable_samplepair_t *pair;
leftvol = ch->leftvol / VOLSCALE;
rightvol = ch->rightvol / VOLSCALE;
leftvol = ch->leftvol * VOLSCALE;
rightvol = ch->rightvol * VOLSCALE;
max_overpaint = max (abs (ch->phase),
max (abs (ch->oldphase), max_overpaint));
@ -320,8 +318,8 @@ static void
snd_paint_stereo (int offs, channel_t *ch, float *samp, unsigned count)
{
portable_samplepair_t *pair;
float leftvol = ch->leftvol / VOLSCALE;
float rightvol = ch->rightvol / VOLSCALE;
float leftvol = ch->leftvol * VOLSCALE;
float rightvol = ch->rightvol * VOLSCALE;
pair = snd_paintbuffer + offs;
while (count-- > 0) {
@ -338,8 +336,8 @@ static void
snd_paint_3 (int offs, channel_t *ch, float *samp, unsigned count)
{
portable_samplepair_t *pair;
float leftvol = ch->leftvol / VOLSCALE;
float rightvol = ch->rightvol / VOLSCALE;
float leftvol = ch->leftvol * VOLSCALE;
float rightvol = ch->rightvol * VOLSCALE;
pair = snd_paintbuffer + offs;
while (count-- > 0) {
@ -357,8 +355,8 @@ static void
snd_paint_4 (int offs, channel_t *ch, float *samp, unsigned count)
{
portable_samplepair_t *pair;
float leftvol = ch->leftvol / VOLSCALE;
float rightvol = ch->rightvol / VOLSCALE;
float leftvol = ch->leftvol * VOLSCALE;
float rightvol = ch->rightvol * VOLSCALE;
pair = snd_paintbuffer + offs;
while (count-- > 0) {
@ -377,8 +375,8 @@ static void
snd_paint_5 (int offs, channel_t *ch, float *samp, unsigned count)
{
portable_samplepair_t *pair;
float leftvol = ch->leftvol / VOLSCALE;
float rightvol = ch->rightvol / VOLSCALE;
float leftvol = ch->leftvol * VOLSCALE;
float rightvol = ch->rightvol * VOLSCALE;
pair = snd_paintbuffer + offs;
while (count-- > 0) {
@ -398,8 +396,8 @@ static void
snd_paint_6 (int offs, channel_t *ch, float *samp, unsigned count)
{
portable_samplepair_t *pair;
float leftvol = ch->leftvol / VOLSCALE;
float rightvol = ch->rightvol / VOLSCALE;
float leftvol = ch->leftvol * VOLSCALE;
float rightvol = ch->rightvol * VOLSCALE;
pair = snd_paintbuffer + offs;
while (count-- > 0) {
@ -421,8 +419,8 @@ static void
snd_paint_7 (int offs, channel_t *ch, float *samp, unsigned count)
{
portable_samplepair_t *pair;
float leftvol = ch->leftvol / VOLSCALE;
float rightvol = ch->rightvol / VOLSCALE;
float leftvol = ch->leftvol * VOLSCALE;
float rightvol = ch->rightvol * VOLSCALE;
pair = snd_paintbuffer + offs;
while (count-- > 0) {
@ -445,8 +443,8 @@ static void
snd_paint_8 (int offs, channel_t *ch, float *samp, unsigned count)
{
portable_samplepair_t *pair;
float leftvol = ch->leftvol / VOLSCALE;
float rightvol = ch->rightvol / VOLSCALE;
float leftvol = ch->leftvol * VOLSCALE;
float rightvol = ch->rightvol * VOLSCALE;
pair = snd_paintbuffer + offs;
while (count-- > 0) {

View file

@ -685,7 +685,8 @@ CL_ParseClientdata (void)
static void
CL_ParseStaticSound (int version)
{
int sound_num, vol, atten;
int sound_num;
float vol, atten;
vec4f_t org = { 0, 0, 0, 1 };
MSG_ReadCoordV (net_message, (vec_t*)&org);//FIXME
@ -693,8 +694,8 @@ CL_ParseStaticSound (int version)
sound_num = MSG_ReadShort (net_message);
else
sound_num = MSG_ReadByte (net_message);
vol = MSG_ReadByte (net_message);
atten = MSG_ReadByte (net_message);
vol = MSG_ReadByte (net_message) / 255.0;
atten = MSG_ReadByte (net_message) / 64.0;
S_StaticSound (cl.sound_precache[sound_num], org, vol, atten);
}

View file

@ -900,13 +900,14 @@ CL_ParseModellist (void)
static void
CL_ParseStaticSound (void)
{
int sound_num, vol, atten;
int sound_num;
float vol, atten;
vec4f_t org = { 0, 0, 0, 1 };
MSG_ReadCoordV (net_message, (vec_t*)&org);//FIXME
sound_num = MSG_ReadByte (net_message);
vol = MSG_ReadByte (net_message);
atten = MSG_ReadByte (net_message);
vol = MSG_ReadByte (net_message) / 255.0;
atten = MSG_ReadByte (net_message) / 64.0;
S_StaticSound (cl.sound_precache[sound_num], org, vol, atten);
}