Skip to content

Commit f24d64b

Browse files
committed
Add DMX sound lump functions, clean up I_StartSound
Additionally, use the encoded DMX sound length, not the lump length.
1 parent 0e65d18 commit f24d64b

File tree

1 file changed

+47
-29
lines changed

1 file changed

+47
-29
lines changed

prboom2/src/SDL/i_sound.c

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,24 @@ static snd_data_t *GetSndData(int sfxid, const unsigned char *data, size_t len)
267267
#define DMXHDRSIZE 8
268268
#define DMXPADSIZE 16
269269

270+
INLINE static int GetDMXSampleRate(const byte *data)
271+
{
272+
return ((data[3] << 8) | data[2]);
273+
}
274+
275+
INLINE static dboolean IsValidDMXSound(int dmx_len, int len)
276+
{
277+
// Don't play DMX format sound lumps that think they're longer than they
278+
// really are, only contain padding, or are shorter than the padding size.
279+
return (dmx_len <= len - DMXHDRSIZE && dmx_len > DMXPADSIZE * 2);
280+
}
281+
282+
INLINE static int GetDMXLength(const byte *data)
283+
{
284+
// Read the encoded number of samples. This value includes padding.
285+
return ((data[7] << 24) | (data[6] << 16) | (data[5] << 8) | data[4]);
286+
}
287+
270288
INLINE static dboolean IsDMXSound(const byte *data, int len)
271289
{
272290
return len > DMXHDRSIZE && data[0] == 0x03 && data[1] == 0x00;
@@ -297,32 +315,16 @@ void I_CacheSounds(void)
297315
// Returns a handle.
298316
//
299317

300-
static int addsfx(int sfxid, int channel, const unsigned char *data, size_t len,
301-
const snd_data_t *snd_data)
318+
static int addsfx(int sfxid, int channel, const channel_info_t *cinfo)
302319
{
303320
channel_info_t *ci = channelinfo + channel;
304321

305322
stopchan(channel);
306323

307-
if (snd_data)
308-
{
309-
ci->data = snd_data->data;
310-
ci->enddata = ci->data + snd_data->samplelen - 1;
311-
ci->samplerate = snd_data->samplerate;
312-
ci->bits = 16;
313-
}
314-
else
315-
{
316-
ci->data = data;
317-
/* Set pointer to end of raw data. */
318-
ci->enddata = ci->data + len - 1;
319-
ci->samplerate = (ci->data[3] << 8) + ci->data[2];
320-
// Skip header and padding before samples.
321-
ci->data += DMXHDRSIZE + DMXPADSIZE;
322-
// Skip padding after samples.
323-
ci->enddata -= DMXPADSIZE;
324-
ci->bits = 8;
325-
}
324+
ci->data = cinfo->data;
325+
ci->enddata = cinfo->enddata;
326+
ci->samplerate = cinfo->samplerate;
327+
ci->bits = cinfo->bits;
326328

327329
ci->stepremainder = 0;
328330
// Should be gametic, I presume.
@@ -501,6 +503,7 @@ int I_StartSound(int id, int channel, sfx_params_t *params)
501503
int lump;
502504
size_t len;
503505
snd_data_t *snd_data = NULL;
506+
channel_info_t cinfo = {0};
504507

505508
if ((channel < 0) || (channel >= MAX_CHANNELS))
506509
#ifdef RANGECHECK
@@ -527,26 +530,41 @@ int I_StartSound(int id, int channel, sfx_params_t *params)
527530

528531
if (IsDMXSound(data, len))
529532
{
530-
// Read the encoded number of samples. This value includes padding.
531-
const int num_samples =
532-
(data[7] << 24) | (data[6] << 16) | (data[5] << 8) | data[4];
533+
const int dmx_len = GetDMXLength(data);
533534

534-
// Don't play DMX format sound lumps that think they're longer than they
535-
// really are, only contain padding, or are shorter than the padding size.
536-
if (num_samples > len - DMXHDRSIZE || num_samples <= DMXPADSIZE * 2)
535+
if (IsValidDMXSound(dmx_len, len))
536+
{
537+
cinfo.data = &data[DMXHDRSIZE + DMXPADSIZE];
538+
cinfo.enddata = &cinfo.data[dmx_len - DMXPADSIZE * 2 - 1];
539+
cinfo.samplerate = GetDMXSampleRate(data);
540+
cinfo.bits = 8;
541+
}
542+
else
543+
{
537544
return -1;
545+
}
538546
}
539547
else
540548
{
541549
snd_data = GetSndData(id, data, len);
542-
if (!snd_data)
550+
551+
if (snd_data)
552+
{
553+
cinfo.data = snd_data->data;
554+
cinfo.enddata = &cinfo.data[snd_data->samplelen - 1];
555+
cinfo.samplerate = snd_data->samplerate;
556+
cinfo.bits = 16;
557+
}
558+
else
559+
{
543560
return -1;
561+
}
544562
}
545563

546564
SDL_LockMutex (sfxmutex);
547565

548566
// Returns a handle (not used).
549-
addsfx(id, channel, data, len, snd_data);
567+
addsfx(id, channel, &cinfo);
550568
updateSoundParams(channel, params);
551569

552570
SDL_UnlockMutex (sfxmutex);

0 commit comments

Comments
 (0)