diff --git a/src/Audio.c b/src/Audio.c index 3c0245613..435f37579 100644 --- a/src/Audio.c +++ b/src/Audio.c @@ -95,7 +95,12 @@ static cc_result Sound_ReadWaveData(struct Stream* stream, struct Sound* snd) { if ((res = Audio_AllocChunks(size, &snd->data, 1))) return res; snd->size = size; - return Stream_Read(stream, (cc_uint8*)snd->data, size); + res = Stream_Read(stream, (cc_uint8*)snd->data, size); + + #ifdef CC_BUILD_BIGENDIAN + Utils_SwapEndian16((cc_int16*)snd->data, size / 2); + #endif + return res; } /* Skip over unhandled data */ diff --git a/src/Resources.c b/src/Resources.c index fba5474ca..167fc7aba 100644 --- a/src/Resources.c +++ b/src/Resources.c @@ -144,8 +144,11 @@ static cc_result SoundPatcher_WriteWav(struct Stream* s, struct VorbisState* ctx count = Vorbis_OutputFrame(ctx, samples); len += count * 2; - /* TODO: Do we need to account for big endian */ - res = Stream_Write(s, samples, count * 2); + +#ifdef CC_BUILD_BIGENDIAN + Utils_SwapEndian16(samples, count); +#endif + res = Stream_Write(s, (cc_uint8*)samples, count * 2); if (res) break; } diff --git a/src/Utils.c b/src/Utils.c index 9de401d21..6ec7a57b7 100644 --- a/src/Utils.c +++ b/src/Utils.c @@ -138,6 +138,19 @@ void Utils_Resize(void** buffer, int* capacity, cc_uint32 elemSize, int defCapac } } +void Utils_SwapEndian16(cc_int16* values, int numValues) { + cc_uint8* data = (cc_uint8*)values; + int i; + + for (i = 0; i < numValues * 2; i += 2) + { + cc_uint8 tmp = data[i + 0]; + data[i + 0] = data[i + 1]; + data[i + 1] = tmp; + } +} + + static const char base64_table[64] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P', 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f', diff --git a/src/Utils.h b/src/Utils.h index 7d8f245ad..78995c0ef 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -45,6 +45,7 @@ cc_uint32 Utils_CRC32(const cc_uint8* data, cc_uint32 length); /* NOTE: This cannot be just indexed by byte value - see Utils_CRC32 implementation. */ extern const cc_uint32 Utils_Crc32Table[256]; CC_NOINLINE void Utils_Resize(void** buffer, int* capacity, cc_uint32 elemSize, int defCapacity, int expandElems); +void Utils_SwapEndian16(cc_int16* values, int numValues); /* Converts blocks of 3 bytes into 4 ASCII characters. (pads if needed) */ /* Returns the number of ASCII characters written. */