From 160e901381614ee398b3316c3d0491a9da534355 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 19 Jun 2013 22:47:17 -0700 Subject: Handle non-native endian formats with SDL_sound --- examples/common/sdl_sound.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'examples/common/sdl_sound.c') diff --git a/examples/common/sdl_sound.c b/examples/common/sdl_sound.c index 9e9f1d62..79a5bf32 100644 --- a/examples/common/sdl_sound.c +++ b/examples/common/sdl_sound.c @@ -105,9 +105,9 @@ int getAudioInfo(FilePtr file, ALuint *rate, ALenum *channels, ALenum *type) *type = AL_UNSIGNED_BYTE_SOFT; else if(file->actual.format == AUDIO_S8) *type = AL_BYTE_SOFT; - else if(file->actual.format == AUDIO_U16SYS) + else if(file->actual.format == AUDIO_U16LSB || file->actual.format == AUDIO_U16MSB) *type = AL_UNSIGNED_SHORT_SOFT; - else if(file->actual.format == AUDIO_S16SYS) + else if(file->actual.format == AUDIO_S16LSB || file->actual.format == AUDIO_S16MSB) *type = AL_SHORT_SOFT; else { @@ -126,6 +126,21 @@ uint8_t *getAudioData(FilePtr file, size_t *length) *length = Sound_Decode(file); if(*length == 0) return NULL; + if((file->actual.format == AUDIO_U16LSB && AUDIO_U16LSB != AUDIO_U16SYS) || + (file->actual.format == AUDIO_U16MSB && AUDIO_U16MSB != AUDIO_U16SYS) || + (file->actual.format == AUDIO_S16LSB && AUDIO_S16LSB != AUDIO_S16SYS) || + (file->actual.format == AUDIO_S16MSB && AUDIO_S16MSB != AUDIO_S16SYS)) + { + /* Swap bytes if the decoded endianness doesn't match the system. */ + char *buffer = file->buffer; + size_t i; + for(i = 0;i < *length;i+=2) + { + char b = buffer[i]; + buffer[i] = buffer[i+1]; + buffer[i+1] = b; + } + } return file->buffer; } -- cgit v1.2.3