Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed bug #292
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 208b75c commit 19cf0b6
Showing 1 changed file with 18 additions and 50 deletions.
68 changes: 18 additions & 50 deletions src/audio/SDL_audiocvt.c
Expand Up @@ -45,11 +45,7 @@ SDL_ConvertMono(SDL_AudioCVT * cvt, SDL_AudioFormat 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 @@ -64,13 +60,7 @@ SDL_ConvertMono(SDL_AudioCVT * cvt, SDL_AudioFormat 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 @@ -87,29 +77,21 @@ SDL_ConvertMono(SDL_AudioCVT * cvt, SDL_AudioFormat 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 @@ -127,35 +109,21 @@ SDL_ConvertMono(SDL_AudioCVT * cvt, SDL_AudioFormat 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 19cf0b6

Please sign in to comment.