Skip to content

Commit

Permalink
audio: Some fixes to the audio data type converter code.
Browse files Browse the repository at this point in the history
Removed some needless things ("len / sizeof (Uint8)"), and made sure the
int32 -> float code uses doubles to avoid working with large integer values
in a 32-bit float.
  • Loading branch information
icculus committed Jan 15, 2017
1 parent 1e48209 commit 1e66d45
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/audio/SDL_audiocvt.c
Expand Up @@ -196,7 +196,7 @@ SDL_ResampleAudioSimple(const int chans, const double rate_incr,
float *last_sample, const float *inbuf,
const int inbuflen, float *outbuf, const int outbuflen)
{
const int framelen = chans * sizeof(float);
const int framelen = chans * sizeof (float);
const int total = (inbuflen / framelen);
const int finalpos = total - chans;
const double src_incr = 1.0 / rate_incr;
Expand All @@ -220,7 +220,7 @@ SDL_ResampleAudioSimple(const int chans, const double rate_incr,
idx += src_incr;
}

return (int)((dst - outbuf) * sizeof(float));
return (int) ((dst - outbuf) * sizeof (float));
}


Expand Down
14 changes: 7 additions & 7 deletions src/audio/SDL_audiotypecvt.c
Expand Up @@ -31,14 +31,14 @@
void SDLCALL
SDL_Convert_S8_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const Uint8 *src = ((const Uint8 *) (cvt->buf + cvt->len_cvt)) - 1;
const Sint8 *src = ((const Sint8 *) (cvt->buf + cvt->len_cvt)) - 1;
float *dst = ((float *) (cvt->buf + cvt->len_cvt * 4)) - 1;
int i;

LOG_DEBUG_CONVERT("AUDIO_S8", "AUDIO_F32");

for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) {
*dst = (((float) ((Sint8) *src)) * DIVBY127);
for (i = cvt->len_cvt; i; --i, --src, --dst) {
*dst = (((float) *src) * DIVBY127);
}

cvt->len_cvt *= 4;
Expand All @@ -56,7 +56,7 @@ SDL_Convert_U8_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)

LOG_DEBUG_CONVERT("AUDIO_U8", "AUDIO_F32");

for (i = cvt->len_cvt / sizeof (Uint8); i; --i, --src, --dst) {
for (i = cvt->len_cvt; i; --i, --src, --dst) {
*dst = ((((float) *src) * DIVBY127) - 1.0f);
}

Expand Down Expand Up @@ -107,14 +107,14 @@ SDL_Convert_U16_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
void SDLCALL
SDL_Convert_S32_to_F32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
{
const Uint32 *src = (const Uint32 *) cvt->buf;
const Sint32 *src = (const Sint32 *) cvt->buf;
float *dst = (float *) cvt->buf;
int i;

LOG_DEBUG_CONVERT("AUDIO_S32", "AUDIO_F32");

for (i = cvt->len_cvt / sizeof (Sint32); i; --i, ++src, ++dst) {
*dst = (((float) *src) * DIVBY2147483647);
*dst = (float) (((double) *src) * DIVBY2147483647);
}

if (cvt->filters[++cvt->filter_index]) {
Expand Down Expand Up @@ -208,7 +208,7 @@ SDL_Convert_F32_to_S32(SDL_AudioCVT *cvt, SDL_AudioFormat format)
LOG_DEBUG_CONVERT("AUDIO_F32", "AUDIO_S32");

for (i = cvt->len_cvt / sizeof (float); i; --i, ++src, ++dst) {
*dst = (Sint32) (*src * 2147483647.0);
*dst = (Sint32) (((double) *src) * 2147483647.0);
}

if (cvt->filters[++cvt->filter_index]) {
Expand Down

0 comments on commit 1e66d45

Please sign in to comment.