Skip to content

Commit

Permalink
Fix SDL audio playback with surround sound
Browse files Browse the repository at this point in the history
If user has surround sound enabled, ioq3 would not play any sound.
Fix painting sound buffer for 4/5.1 audio channels. Extra channels
currently play no audio.
  • Loading branch information
zturtleman committed Sep 13, 2018
1 parent e4208cf commit 812a3db
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
2 changes: 1 addition & 1 deletion code/client/snd_dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ void S_Update_(void) {
& ~(dma.submission_chunk-1);

// never mix more than the complete buffer
samps = dma.samples >> (dma.channels-1);
samps = dma.samples / dma.channels;
if (endtime - s_soundtime > samps)
endtime = s_soundtime + samps;

Expand Down
54 changes: 36 additions & 18 deletions code/client/snd_mix.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,16 @@ void S_TransferPaintBuffer(int endtime)
{
int out_idx;
int count;
int out_mask;
int *p;
int step;
int val;
int i;
unsigned long *pbuf;

pbuf = (unsigned long *)dma.buffer;


if ( s_testsound->integer ) {
int i;

// write a fixed sine wave
count = (endtime - s_paintedtime);
for (i=0 ; i<count ; i++)
Expand All @@ -177,53 +175,73 @@ void S_TransferPaintBuffer(int endtime)
{ // general case
p = (int *) paintbuffer;
count = (endtime - s_paintedtime) * dma.channels;
out_mask = dma.samples - 1;
out_idx = s_paintedtime * dma.channels & out_mask;
step = 3 - dma.channels;
out_idx = s_paintedtime * dma.channels % dma.samples;
step = 3 - MIN( dma.channels, 2 );

if ((dma.isfloat) && (dma.samplebits == 32))
{
float *out = (float *) pbuf;
while (count--)
for (i=0 ; i<count ; i++)
{
val = *p >> 8;
p+= step;
if ( i % dma.channels >= 2 )
{
val = 0;
}
else
{
val = *p >> 8;
p+= step;
}
if (val > 0x7fff)
val = 0x7fff;
else if (val < -32767) /* clamp to one less than max to make division max out at -1.0f. */
val = -32767;
out[out_idx] = ((float) val) / 32767.0f;
out_idx = (out_idx + 1) & out_mask;
out_idx = (out_idx + 1) % dma.samples;
}
}
else if (dma.samplebits == 16)
{
short *out = (short *) pbuf;
while (count--)
for (i=0 ; i<count ; i++)
{
val = *p >> 8;
p+= step;
if ( i % dma.channels >= 2 )
{
val = 0;
}
else
{
val = *p >> 8;
p+= step;
}
if (val > 0x7fff)
val = 0x7fff;
else if (val < -32768)
val = -32768;
out[out_idx] = val;
out_idx = (out_idx + 1) & out_mask;
out_idx = (out_idx + 1) % dma.samples;
}
}
else if (dma.samplebits == 8)
{
unsigned char *out = (unsigned char *) pbuf;
while (count--)
for (i=0 ; i<count ; i++)
{
val = *p >> 8;
p+= step;
if ( i % dma.channels >= 2 )
{
val = 0;
}
else
{
val = *p >> 8;
p+= step;
}
if (val > 0x7fff)
val = 0x7fff;
else if (val < -32768)
val = -32768;
out[out_idx] = (val>>8) + 128;
out_idx = (out_idx + 1) & out_mask;
out_idx = (out_idx + 1) % dma.samples;
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions code/sdl/sdl_snd.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,8 @@ qboolean SNDDMA_Init(void)
if (!tmp)
tmp = (obtained.samples * obtained.channels) * 10;

if (tmp & (tmp - 1)) // not a power of two? Seems to confuse something.
{
int val = 1;
while (val < tmp)
val <<= 1;

tmp = val;
}
// samples must be divisible by number of channels
tmp -= tmp % obtained.channels;

dmapos = 0;
dma.samplebits = SDL_AUDIO_BITSIZE(obtained.format);
Expand Down

0 comments on commit 812a3db

Please sign in to comment.