Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Replaced unions with calls to SDL_SwapFloat...

  • Loading branch information
icculus committed Sep 1, 2006
1 parent 01b8dba commit a58bbb6a841a7ddc6f3459b922a6ec9c06e05f41
Showing with 32 additions and 87 deletions.
  1. +22 −51 src/audio/SDL_audiocvt.c
  2. +10 −36 src/audio/SDL_mixer.c
@@ -187,35 +187,25 @@ SDL_ConvertMono(SDL_AudioCVT * cvt, SDL_AudioFormat format)

case AUDIO_F32:
{
/* !!! FIXME: this convert union is nasty. */
union
{
float f;
Uint32 ui32;
} f2i;
const Uint32 *src = (const Uint32 *) cvt->buf;
Uint32 *dst = (Uint32 *) cvt->buf;
const float *src = (const float *) cvt->buf;
float *dst = (float *) cvt->buf;
if (SDL_AUDIO_ISBIGENDIAN(format)) {
for (i = cvt->len_cvt / 8; i; --i, src += 2) {
float src1, src2;
f2i.ui32 = SDL_SwapBE32(src[0]);
src1 = f2i.f;
f2i.ui32 = SDL_SwapBE32(src[1]);
src2 = f2i.f;
src1 = SDL_SwapFloatBE(src[0]);
src2 = SDL_SwapFloatBE(src[1]);
const double added = ((double) src1) + ((double) src2);
f2i.f = (float) (added * 0.5);
*(dst++) = SDL_SwapBE32(f2i.ui32);
src1 = (float) (added * 0.5);
*(dst++) = SDL_SwapFloatBE(src1);
}
} else {
for (i = cvt->len_cvt / 8; i; --i, src += 2) {
float src1, src2;
f2i.ui32 = SDL_SwapLE32(src[0]);
src1 = f2i.f;
f2i.ui32 = SDL_SwapLE32(src[1]);
src2 = f2i.f;
src1 = SDL_SwapFloatLE(src[0]);
src2 = SDL_SwapFloatLE(src[1]);
const double added = ((double) src1) + ((double) src2);
f2i.f = (float) (added * 0.5);
*(dst++) = SDL_SwapLE32(f2i.ui32);
src1 = (float) (added * 0.5);
*(dst++) = SDL_SwapFloatLE(src1);
}
}
}
@@ -577,54 +567,35 @@ SDL_ConvertSurround(SDL_AudioCVT * cvt, SDL_AudioFormat format)

case AUDIO_F32:
{
union
{
float f;
Uint32 ui32;
} f2i; /* !!! FIXME: lame. */
float lf, rf, ce;
const Uint32 *src = (const Uint32 *) cvt->buf + cvt->len_cvt;
Uint32 *dst = (Uint32 *) cvt->buf + cvt->len_cvt * 3;
const float *src = (const float *) cvt->buf + cvt->len_cvt;
float *dst = (float *) cvt->buf + cvt->len_cvt * 3;

if (SDL_AUDIO_ISBIGENDIAN(format)) {
for (i = cvt->len_cvt / 8; i; --i) {
dst -= 6;
src -= 2;
f2i.ui32 = SDL_SwapBE32(src[0]);
lf = f2i.f;
f2i.ui32 = SDL_SwapBE32(src[1]);
rf = f2i.f;
lf = SDL_SwapFloatBE(src[0]);
rf = SDL_SwapFloatBE(src[1]);
ce = (lf * 0.5f) + (rf * 0.5f);
dst[0] = src[0];
dst[1] = src[1];
f2i.f = (lf - ce);
dst[2] = SDL_SwapBE32(f2i.ui32);
f2i.f = (rf - ce);
dst[3] = SDL_SwapBE32(f2i.ui32);
f2i.f = ce;
f2i.ui32 = SDL_SwapBE32(f2i.ui32);
dst[4] = f2i.ui32;
dst[5] = f2i.ui32;
dst[2] = SDL_SwapFloatBE(lf - ce);
dst[3] = SDL_SwapFloatBE(rf - ce);
dst[4] = dst[5] = SDL_SwapFloatBE(ce);
}
} else {
for (i = cvt->len_cvt / 8; i; --i) {
dst -= 6;
src -= 2;
f2i.ui32 = SDL_SwapLE32(src[0]);
lf = f2i.f;
f2i.ui32 = SDL_SwapLE32(src[1]);
rf = f2i.f;
lf = SDL_SwapFloatLE(src[0]);
rf = SDL_SwapFloatLE(src[1]);
ce = (lf * 0.5f) + (rf * 0.5f);
dst[0] = src[0];
dst[1] = src[1];
f2i.f = (lf - ce);
dst[2] = SDL_SwapLE32(f2i.ui32);
f2i.f = (rf - ce);
dst[3] = SDL_SwapLE32(f2i.ui32);
f2i.f = ce;
f2i.ui32 = SDL_SwapLE32(f2i.ui32);
dst[4] = f2i.ui32;
dst[5] = f2i.ui32;
dst[2] = SDL_SwapFloatLE(lf - ce);
dst[3] = SDL_SwapFloatLE(rf - ce);
dst[4] = dst[5] = SDL_SwapFloatLE(ce);
}
}
}
@@ -321,32 +321,19 @@ SDL_MixAudioFormat(Uint8 * dst, const Uint8 * src, SDL_AudioFormat format,
const double max_audioval = 3.40282347e+38F;
const double min_audioval = -3.40282347e+38F;

/* !!! FIXME: this is a little nasty. */
union
{
float f;
Uint32 ui32;
} cvt;

len /= 4;
while (len--) {
cvt.f = *(src32++);
cvt.ui32 = SDL_SwapLE32(cvt.ui32);
src1 = ((cvt.f * fvolume) * fmaxvolume);

cvt.f = *dst32;
cvt.ui32 = SDL_SwapLE32(cvt.ui32);
src2 = cvt.f;
src1 = ((SDL_SwapFloatLE(*src32) * fvolume) * fmaxvolume);
src2 = SDL_SwapFloatLE(*dst32);
src32++;

dst_sample = src1 + src2;
dst_sample = ((double) src1) + ((double) src2);
if (dst_sample > max_audioval) {
dst_sample = max_audioval;
} else if (dst_sample < min_audioval) {
dst_sample = min_audioval;
}
cvt.f = ((float) dst_sample);
cvt.ui32 = SDL_SwapLE32(cvt.ui32);
*(dst32++) = cvt.f;
*(dst32++) = SDL_SwapFloatLE((float) dst_sample);
}
}
break;
@@ -363,32 +350,19 @@ SDL_MixAudioFormat(Uint8 * dst, const Uint8 * src, SDL_AudioFormat format,
const double max_audioval = 3.40282347e+38F;
const double min_audioval = -3.40282347e+38F;

/* !!! FIXME: this is a little nasty. */
union
{
float f;
Uint32 ui32;
} cvt;

len /= 4;
while (len--) {
cvt.f = *(src32++);
cvt.ui32 = SDL_SwapBE32(cvt.ui32);
src1 = ((cvt.f * fvolume) * fmaxvolume);

cvt.f = *dst32;
cvt.ui32 = SDL_SwapBE32(cvt.ui32);
src2 = cvt.f;
src1 = ((SDL_SwapFloatBE(*src32) * fvolume) * fmaxvolume);
src2 = SDL_SwapFloatBE(*dst32);
src32++;

dst_sample = src1 + src2;
dst_sample = ((double) src1) + ((double) src2);
if (dst_sample > max_audioval) {
dst_sample = max_audioval;
} else if (dst_sample < min_audioval) {
dst_sample = min_audioval;
}
cvt.f = ((float) dst_sample);
cvt.ui32 = SDL_SwapBE32(cvt.ui32);
*(dst32++) = cvt.f;
*(dst32++) = SDL_SwapFloatBE((float) dst_sample);
}
}
break;

0 comments on commit a58bbb6

Please sign in to comment.