From 57e20208c51d51c512eaedc40736479f0d6bba25 Mon Sep 17 00:00:00 2001 From: Chris Speciale Date: Thu, 13 Nov 2025 15:31:03 -0500 Subject: [PATCH] add SDL_sound decoder backend (SDLSound.cpp/.h) --- project/include/media/SDLSound.h | 26 ++++++++++++ project/src/media/SDLSound.cpp | 70 ++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 project/include/media/SDLSound.h create mode 100644 project/src/media/SDLSound.cpp diff --git a/project/include/media/SDLSound.h b/project/include/media/SDLSound.h new file mode 100644 index 000000000..2a71f9b0b --- /dev/null +++ b/project/include/media/SDLSound.h @@ -0,0 +1,26 @@ +#ifndef LIME_MEDIA_DECODERS_SDL_SOUND_H +#define LIME_MEDIA_DECODERS_SDL_SOUND_H + + +#include +#include + + +namespace lime { + + + class SDLSound { + + + public: + + static bool Decode (Resource *resource, AudioBuffer *audioBuffer); + + + }; + + +} + + +#endif \ No newline at end of file diff --git a/project/src/media/SDLSound.cpp b/project/src/media/SDLSound.cpp new file mode 100644 index 000000000..92f4bfefb --- /dev/null +++ b/project/src/media/SDLSound.cpp @@ -0,0 +1,70 @@ +#include "media/SDLSound.h" +#include "SDL_sound.h" +#include + +namespace lime { + + bool SDLSound::Decode (Resource* resource, AudioBuffer* audioBuffer) { + Sound_AudioInfo want = {}; + // Lets force 16 bit system endian since thats what we expect and mojo doesn't support AL extensions for 32 bit + want.format = AUDIO_S16SYS; + // Just set it to 0 and let the decoder choose + want.channels = 0; + want.rate = 0; + + Sound_Sample* sample = nullptr; + + if (resource->path) { + sample = Sound_NewSampleFromFile(resource->path, &want, 64 * 1024); + } else { + const Uint8* bytes = reinterpret_cast(resource->data->b); + const Uint32 len = static_cast(resource->data->length); + sample = Sound_NewSampleFromMem(bytes, len, nullptr, &want, 64 * 1024); + } + + if (!sample) { + printf("SDL_sound: %s\n", Sound_GetError()); + return false; + } + + const Sound_AudioInfo& got = sample->actual; + + if (got.channels < 1 || got.rate <= 0) { + printf("SDL_sound: unsupported stream format (channels=%d, rate=%d)\n", got.channels, got.rate); + Sound_FreeSample(sample); + return false; + } + + audioBuffer->sampleRate = (int)got.rate; + audioBuffer->channels = (int)got.channels; + //For some reason it still reports the original format so we force 16 + audioBuffer->bitsPerSample = 16;//(got.format == AUDIO_F32LSB || got.format == AUDIO_F32MSB) ? 32 : (got.format == AUDIO_U8 || got.format == AUDIO_S8) ? 8 : 16; + + audioBuffer->data->Resize(0); + Uint32 total = 0; + + for (;;) { + if (sample->flags & SOUND_SAMPLEFLAG_ERROR) { + printf("SDL_sound decode error: %s\n", Sound_GetError()); + Sound_FreeSample(sample); + return false; + } + + const long decoded = Sound_Decode(sample); + if (decoded <= 0) { + if (sample->flags & SOUND_SAMPLEFLAG_EOF) break; + break; + } + + const Uint32 chunk = (Uint32)decoded; + audioBuffer->data->Resize(total + chunk); + unsigned char* out = audioBuffer->data->buffer->b; + memcpy(out + total, sample->buffer, chunk); + total += chunk; + } + + Sound_FreeSample(sample); + return true; + } + +}