Skip to content

Commit fb5e19b

Browse files
committed
Apply fade-in/out when caching sounds
Don't apply fading to looping ambient sounds.
1 parent f24d64b commit fb5e19b

File tree

1 file changed

+77
-3
lines changed

1 file changed

+77
-3
lines changed

prboom2/src/SDL/i_sound.c

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
//e6y
7272
#include "e6y.h"
7373

74+
#include "dsda/ambient.h"
7475
#include "dsda/settings.h"
7576

7677
static dboolean registered_non_rw = false;
@@ -195,7 +196,7 @@ static Uint8 *ConvertAudioFormat(void **data, SDL_AudioSpec *sample, Uint32 *len
195196
typedef struct snd_data_s
196197
{
197198
int sfxid;
198-
const unsigned char *data;
199+
unsigned char *data;
199200
int samplelen;
200201
int samplerate;
201202
struct snd_data_s *next;
@@ -264,6 +265,58 @@ static snd_data_t *GetSndData(int sfxid, const unsigned char *data, size_t len)
264265
return target;
265266
}
266267

268+
#define FADETIME 1000 // microseconds
269+
270+
static void FadeInOutMono16(short *data, int len, int rate)
271+
{
272+
const int fadelen = rate * FADETIME / 1000000;
273+
int i;
274+
275+
if (len < fadelen)
276+
return;
277+
278+
if (data[0] != 0)
279+
{
280+
for (i = 0; i < fadelen; i++)
281+
{
282+
data[i] = data[i] * i / fadelen;
283+
}
284+
}
285+
286+
if (data[len - 1] != 0)
287+
{
288+
for (i = 0; i < fadelen; i++)
289+
{
290+
data[len - 1 - i] = data[len - 1 - i] * i / fadelen;
291+
}
292+
}
293+
}
294+
295+
static void FadeInOutMono8(byte *data, int len, int rate)
296+
{
297+
const int fadelen = rate * FADETIME / 1000000;
298+
int i;
299+
300+
if (len < fadelen)
301+
return;
302+
303+
if (data[0] != 128)
304+
{
305+
for (i = 0; i < fadelen; i++)
306+
{
307+
data[i] = (data[i] - 128) * i / fadelen + 128;
308+
}
309+
}
310+
311+
if (data[len - 1] != 128)
312+
{
313+
for (i = 0; i < fadelen; i++)
314+
{
315+
data[len - 1 - i] = (data[len - 1 - i] - 128) * i / fadelen + 128;
316+
}
317+
}
318+
}
319+
267320
#define DMXHDRSIZE 8
268321
#define DMXPADSIZE 16
269322

@@ -301,8 +354,29 @@ void I_CacheSounds(void)
301354
const byte *data = W_LumpByNum(lump);
302355
int len = W_LumpLength(lump);
303356

304-
if (!IsDMXSound(data, len))
305-
GetSndData(id, data, len);
357+
if (IsDMXSound(data, len))
358+
{
359+
int dmx_len = GetDMXLength(data);
360+
361+
if (IsValidDMXSound(dmx_len, len) && !dsda_IsLoopingAmbientSFX(id))
362+
{
363+
const int dmx_rate = GetDMXSampleRate(data);
364+
byte *dmx_data = W_GetModifiableLumpData(lump);
365+
dmx_data = &dmx_data[DMXHDRSIZE + DMXPADSIZE];
366+
dmx_len -= DMXPADSIZE * 2;
367+
FadeInOutMono8(dmx_data, dmx_len, dmx_rate);
368+
}
369+
}
370+
else
371+
{
372+
snd_data_t *snd_data = GetSndData(id, data, len);
373+
374+
if (snd_data && !dsda_IsLoopingAmbientSFX(id))
375+
{
376+
len = snd_data->samplelen / sizeof(short);
377+
FadeInOutMono16((short *)snd_data->data, len, snd_data->samplerate);
378+
}
379+
}
306380
}
307381
}
308382
}

0 commit comments

Comments
 (0)