Fade in sounds to prevent clicking (#1220)

* Fade in sounds to prevent clicking

* Fix range check

* Use absolute value

* Add `FADETIME`

* Remove redundant check
This commit is contained in:
ceski 2023-10-16 23:31:49 -07:00 committed by GitHub
parent a6f4274553
commit 46eeb2ef09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 3 deletions

View File

@ -3564,8 +3564,8 @@ static const unsigned char dsdgact[] = {
// http://creativecommons.org/publicdomain/zero/1.0/
static const unsigned char dsdgatk[] = {
3,0,34,86,213,30,0,0,120,112,113,110,109,106,105,103,102,102,102,102,104,106,
110,113,117,122,127,132,137,141,144,147,149,150,151,152,152,152,151,150,148,
3,0,34,86,213,30,0,0,128,127,126,125,124,123,121,120,118,117,116,115,114,115,
116,117,120,123,127,131,136,140,144,147,149,150,151,152,152,152,151,150,148,
145,142,138,135,131,128,125,123,121,121,120,121,121,121,121,121,121,121,121,
120,120,119,119,118,118,117,116,116,116,116,117,119,120,122,125,127,130,133,
136,139,141,144,146,147,148,149,149,148,146,144,142,139,136,134,131,128,125,

View File

@ -546,6 +546,22 @@ static boolean IsPaddedSound(const byte *data, int size)
return true;
}
static void FadeInMono8(byte *data, ALsizei size, ALsizei freq)
{
const int fadelen = freq * FADETIME / 1000000;
int i;
if (data[0] == 128 || size < fadelen)
{
return;
}
for (i = 0; i < fadelen; i++)
{
data[i] = (data[i] - 128) * i / fadelen + 128;
}
}
boolean I_OAL_CacheSound(sfxinfo_t *sfx)
{
int lumpnum;
@ -604,6 +620,9 @@ boolean I_OAL_CacheSound(sfxinfo_t *sfx)
// All Doom sounds are 8-bit
format = AL_FORMAT_MONO8;
// Fade in sounds that start at a non-zero amplitude to prevent clicking.
FadeInMono8(sampledata, size, freq);
}
else
{

View File

@ -23,6 +23,7 @@
#include <sndfile.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "i_oalstream.h"
#include "m_swap.h"
@ -483,6 +484,38 @@ static boolean OpenFile(sndfile_t *file, void *data, sf_count_t size)
return true;
}
static void FadeInMono16(short *data, ALsizei size, ALsizei freq)
{
const int fadelen = freq * FADETIME / 1000000;
int i;
if (!data[0] || size / sizeof(short) < fadelen)
{
return;
}
for (i = 0; i < fadelen; i++)
{
data[i] = data[i] * i / fadelen;
}
}
static void FadeInMonoFloat32(float *data, ALsizei size, ALsizei freq)
{
const int fadelen = freq * FADETIME / 1000000;
int i;
if (fabsf(data[0]) < 0.000001f || size / sizeof(float) < fadelen)
{
return;
}
for (i = 0; i < fadelen; i++)
{
data[i] = data[i] * i / fadelen;
}
}
boolean I_SND_LoadFile(void *data, ALenum *format, byte **wavdata,
ALsizei *size, ALsizei *freq)
{
@ -519,10 +552,21 @@ boolean I_SND_LoadFile(void *data, ALenum *format, byte **wavdata,
return false;
}
*wavdata = local_wavdata;
*size = num_frames * file.frame_size / file.sfinfo.channels;
*freq = file.sfinfo.samplerate;
// Fade in sounds that start at a non-zero amplitude to prevent clicking.
if (*format == AL_FORMAT_MONO16)
{
FadeInMono16(local_wavdata, *size, *freq);
}
else if (*format == AL_FORMAT_MONO_FLOAT32)
{
FadeInMonoFloat32(local_wavdata, *size, *freq);
}
*wavdata = local_wavdata;
CloseFile(&file);
return true;

View File

@ -22,6 +22,8 @@
#include "al.h"
#include "doomtype.h"
#define FADETIME 1000 // microseconds
boolean I_SND_LoadFile(void *data, ALenum *format, byte **wavdata,
ALsizei *size, ALsizei *freq);