Swap .wav audio endian so that it is read/written as little endian on big endian machines

This commit is contained in:
UnknownShadow200 2024-04-03 19:52:39 +11:00
parent b98cb2fc74
commit d31122eb0d
4 changed files with 25 additions and 3 deletions

View File

@ -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 */

View File

@ -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;
}

View File

@ -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',

View File

@ -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. */