Skip to content

Commit

Permalink
Fixed bug #292
Browse files Browse the repository at this point in the history
I might be on crack here.

It looks like SDL_ConvertMono() in src/audio/SDL_audiocvt.c adds the left and
right channels of a stereo stream together, and clamps the new mono channel if
it would overflow.

Shouldn't it be dividing by 2 to average the two sample points instead of
clamping? Otherwise the mono sample point's volume doubles in the conversion.
This would also make the conversion faster, as it replaces two branches per
sample frame with a bitwise shift.

--ryan.
  • Loading branch information
slouken committed Sep 24, 2006
1 parent 003a368 commit bcbc632
Showing 1 changed file with 18 additions and 53 deletions.
71 changes: 18 additions & 53 deletions src/audio/SDL_audiocvt.c
Expand Up @@ -44,11 +44,7 @@ void SDLCALL SDL_ConvertMono(SDL_AudioCVT *cvt, Uint16 format)
dst = cvt->buf;
for ( i=cvt->len_cvt/2; i; --i ) {
sample = src[0] + src[1];
if ( sample > 255 ) {
*dst = 255;
} else {
*dst = (Uint8)sample;
}
*dst = (Uint8)(sample / 2);
src += 2;
dst += 1;
}
Expand All @@ -62,14 +58,7 @@ void SDLCALL SDL_ConvertMono(SDL_AudioCVT *cvt, Uint16 format)
dst = (Sint8 *)cvt->buf;
for ( i=cvt->len_cvt/2; i; --i ) {
sample = src[0] + src[1];
if ( sample > 127 ) {
*dst = 127;
} else
if ( sample < -128 ) {
*dst = -128;
} else {
*dst = (Sint8)sample;
}
*dst = (Sint8)(sample / 2);
src += 2;
dst += 1;
}
Expand All @@ -85,29 +74,21 @@ void SDLCALL SDL_ConvertMono(SDL_AudioCVT *cvt, Uint16 format)
for ( i=cvt->len_cvt/4; i; --i ) {
sample = (Uint16)((src[0]<<8)|src[1])+
(Uint16)((src[2]<<8)|src[3]);
if ( sample > 65535 ) {
dst[0] = 0xFF;
dst[1] = 0xFF;
} else {
dst[1] = (sample&0xFF);
sample >>= 8;
dst[0] = (sample&0xFF);
}
sample /= 2;
dst[1] = (sample&0xFF);
sample >>= 8;
dst[0] = (sample&0xFF);
src += 4;
dst += 2;
}
} else {
for ( i=cvt->len_cvt/4; i; --i ) {
sample = (Uint16)((src[1]<<8)|src[0])+
(Uint16)((src[3]<<8)|src[2]);
if ( sample > 65535 ) {
dst[0] = 0xFF;
dst[1] = 0xFF;
} else {
dst[0] = (sample&0xFF);
sample >>= 8;
dst[1] = (sample&0xFF);
}
sample /= 2;
dst[0] = (sample&0xFF);
sample >>= 8;
dst[1] = (sample&0xFF);
src += 4;
dst += 2;
}
Expand All @@ -124,37 +105,21 @@ void SDLCALL SDL_ConvertMono(SDL_AudioCVT *cvt, Uint16 format)
for ( i=cvt->len_cvt/4; i; --i ) {
sample = (Sint16)((src[0]<<8)|src[1])+
(Sint16)((src[2]<<8)|src[3]);
if ( sample > 32767 ) {
dst[0] = 0x7F;
dst[1] = 0xFF;
} else
if ( sample < -32768 ) {
dst[0] = 0x80;
dst[1] = 0x00;
} else {
dst[1] = (sample&0xFF);
sample >>= 8;
dst[0] = (sample&0xFF);
}
sample /= 2;
dst[1] = (sample&0xFF);
sample >>= 8;
dst[0] = (sample&0xFF);
src += 4;
dst += 2;
}
} else {
for ( i=cvt->len_cvt/4; i; --i ) {
sample = (Sint16)((src[1]<<8)|src[0])+
(Sint16)((src[3]<<8)|src[2]);
if ( sample > 32767 ) {
dst[1] = 0x7F;
dst[0] = 0xFF;
} else
if ( sample < -32768 ) {
dst[1] = 0x80;
dst[0] = 0x00;
} else {
dst[0] = (sample&0xFF);
sample >>= 8;
dst[1] = (sample&0xFF);
}
sample /= 2;
dst[0] = (sample&0xFF);
sample >>= 8;
dst[1] = (sample&0xFF);
src += 4;
dst += 2;
}
Expand Down

0 comments on commit bcbc632

Please sign in to comment.