@@ -267,6 +267,24 @@ static snd_data_t *GetSndData(int sfxid, const unsigned char *data, size_t len)
267
267
#define DMXHDRSIZE 8
268
268
#define DMXPADSIZE 16
269
269
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
+
270
288
INLINE static dboolean IsDMXSound (const byte * data , int len )
271
289
{
272
290
return len > DMXHDRSIZE && data [0 ] == 0x03 && data [1 ] == 0x00 ;
@@ -297,32 +315,16 @@ void I_CacheSounds(void)
297
315
// Returns a handle.
298
316
//
299
317
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 )
302
319
{
303
320
channel_info_t * ci = channelinfo + channel ;
304
321
305
322
stopchan (channel );
306
323
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 ;
326
328
327
329
ci -> stepremainder = 0 ;
328
330
// Should be gametic, I presume.
@@ -501,6 +503,7 @@ int I_StartSound(int id, int channel, sfx_params_t *params)
501
503
int lump ;
502
504
size_t len ;
503
505
snd_data_t * snd_data = NULL ;
506
+ channel_info_t cinfo = {0 };
504
507
505
508
if ((channel < 0 ) || (channel >= MAX_CHANNELS ))
506
509
#ifdef RANGECHECK
@@ -527,26 +530,41 @@ int I_StartSound(int id, int channel, sfx_params_t *params)
527
530
528
531
if (IsDMXSound (data , len ))
529
532
{
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 );
533
534
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
+ {
537
544
return -1 ;
545
+ }
538
546
}
539
547
else
540
548
{
541
549
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
+ {
543
560
return -1 ;
561
+ }
544
562
}
545
563
546
564
SDL_LockMutex (sfxmutex );
547
565
548
566
// Returns a handle (not used).
549
- addsfx (id , channel , data , len , snd_data );
567
+ addsfx (id , channel , & cinfo );
550
568
updateSoundParams (channel , params );
551
569
552
570
SDL_UnlockMutex (sfxmutex );
0 commit comments