Skip to content

Commit

Permalink
Fade in sounds to prevent clicking (#1220)
Browse files Browse the repository at this point in the history
* Fade in sounds to prevent clicking

* Fix range check

* Use absolute value

* Add `FADETIME`

* Remove redundant check
  • Loading branch information
ceski-1 authored Oct 17, 2023
1 parent a6f4274 commit 46eeb2e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/dogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions src/i_oalsound.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down
46 changes: 45 additions & 1 deletion src/i_sndfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <sndfile.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "i_oalstream.h"
#include "m_swap.h"
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/i_sndfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 46eeb2e

Please sign in to comment.