#include "audio/audio.h" #include "audio/cadi_bank.h" #include #include #include #include extern "C" int stb_vorbis_decode_memory(const unsigned char* mem, int len, int* channels, int* sample_rate, short** output); namespace playground { namespace { bool fail(std::string* error, std::string message) { if (error) *error = std::move(message); return false; } } bool decodeOgg(std::span bytes, DecodedSound& sound, std::string* error) { constexpr size_t maxCompressed = 8 * 1024 * 1024; constexpr size_t maxSamples = 32 * 1024 * 1024; if (bytes.size() < 4 || !std::equal(bytes.begin(), bytes.begin()+4, "OggS")) return fail(error, "not an Ogg stream"); if (bytes.size() > maxCompressed || bytes.size() > size_t(std::numeric_limits::max())) return fail(error, "Ogg stream exceeds size limit"); int channels = 0, rate = 0; short* decoded = nullptr; const int frames = stb_vorbis_decode_memory(bytes.data(), int(bytes.size()), &channels, &rate, &decoded); if (frames <= 0 || !decoded || channels < 1 || channels > 2 || rate < 8000 || rate > 192000 || size_t(frames) > maxSamples / size_t(std::max(channels, 1))) { std::free(decoded); return fail(error, "invalid or oversized Ogg Vorbis stream"); } DecodedSound result; result.channels = channels; result.sampleRate = rate; result.samples.assign(decoded, decoded + size_t(frames) * size_t(channels)); std::free(decoded); sound = std::move(result); return true; } Audio::~Audio() { stop(); if (initialized_) SDL_QuitSubSystem(SDL_INIT_AUDIO); } void Audio::stop() { stopLane(music_); stopLane(sfx_); stopLane(environment_); } bool Audio::active() const { return musicActive() || sfxActive() || environmentActive(); } void Audio::stopLane(Lane& lane) { if (lane.stream) { SDL_DestroyAudioStream(lane.stream); lane.stream=nullptr; } lane.pcm.clear(); lane.loop=false; } void Audio::feedLoop(void* userdata, SDL_AudioStream* stream, int additional, int) { auto& lane=*static_cast(userdata); if (!lane.loop || lane.pcm.empty() || additional<=0) return; const size_t bytes=lane.pcm.size()*sizeof(int16_t); const auto* pcm=reinterpret_cast(lane.pcm.data()); // SDL asks for a bounded amount; repeat the decoded clip without gaps. size_t remaining=std::min(size_t(additional),1024*1024); while (remaining) { const int chunk=int(std::min(remaining,bytes)); if (!SDL_PutAudioStreamData(stream,pcm,chunk)) break; remaining-=size_t(chunk); } } bool Audio::playAsset(std::string_view path, std::string* error) { Bytes bytes; if (!assets_.read(std::string(path), bytes) && (!looseAssets_ || !looseAssets_->read(std::string(path), bytes))) return fail(error, "sound asset missing: " + std::string(path)); return playEncoded(bytes, sfx_, false, error); } bool Audio::playEncoded(std::span bytes, Lane& lane, bool loop, std::string* error) { DecodedSound decoded; if (!decodeOgg(bytes, decoded, error)) return false; if (!initialized_) { if (!SDL_InitSubSystem(SDL_INIT_AUDIO)) return fail(error, SDL_GetError()); initialized_ = true; } const SDL_AudioSpec spec{SDL_AUDIO_S16, decoded.channels, decoded.sampleRate}; stopLane(lane); lane.pcm=std::move(decoded.samples); lane.loop=loop; lane.stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, loop ? &Audio::feedLoop : nullptr,loop ? &lane : nullptr); if (!lane.stream) { lane.pcm.clear(); return fail(error, SDL_GetError()); } const auto byteCount = lane.pcm.size() * sizeof(int16_t); if (byteCount > size_t(std::numeric_limits::max()) || !SDL_PutAudioStreamData(lane.stream, lane.pcm.data(), int(byteCount)) || !SDL_ResumeAudioStreamDevice(lane.stream)) { const std::string cause = SDL_GetError(); stopLane(lane); return fail(error, cause); } return true; } bool Audio::playEvent(std::string_view event, std::string* error) { if (event.empty() || event.find('/') != std::string_view::npos || event.find('\\') != std::string_view::npos || event.find("..") != std::string_view::npos) return fail(error, "invalid sound event"); if (event=="Stop_Music") { stopLane(music_); return true; } if (event=="Stop_Environments") { stopLane(environment_); return true; } if (event=="Stop_Sfx") { stopLane(sfx_); return true; } if (event=="Stop_travel_plane") { // The source travel sequence stops this cue before the destination sting. if (bank_) { if (auto info=bank_->eventInfo("travel_plane")) { stopLane(info->channel==CadiChannel::Music?music_: info->channel==CadiChannel::Environment?environment_:sfx_); } } else stopLane(sfx_); return true; } if (event.starts_with("Stop_")) return fail(error,"unknown stop event"); const std::string path = "sfx/" + std::string(event) + ".ogg"; if (assets_.exists(path) || (looseAssets_ && looseAssets_->exists(path))) return playAsset(path, error); if (looseAssets_ && !bankPath_.empty() && !bankAttempted_) { bankAttempted_ = true; Bytes data; if (looseAssets_->read(bankPath_, data)) { auto candidate=std::make_unique(); if (candidate->load(std::move(data))) bank_=std::move(candidate); } } if (bank_) { if (auto info=bank_->eventInfo(event); info && info->clips.size()==1) { Lane& lane=info->channel==CadiChannel::Music?music_: info->channel==CadiChannel::Environment?environment_:sfx_; return playEncoded(bank_->data(info->clips.front()),lane,info->loop,error); } } return fail(error,"sound event unavailable: "+std::string(event)); } } // namespace playground