From 531441d4e320ec309e28e907250f8fa9d2a32d18 Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Tue, 17 Feb 2015 02:09:55 +0100 Subject: [PATCH] Make audio asset decoding failures non-fatal Matches RMXP behavior. This is also useful in case midi files are to be played, but fluidsynth isn't available. --- src/alstream.cpp | 43 ++++++++++++++++++++++++++++++------------- src/soundemitter.cpp | 14 +++++++++++--- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/alstream.cpp b/src/alstream.cpp index ac5afac..7f225b9 100644 --- a/src/alstream.cpp +++ b/src/alstream.cpp @@ -25,9 +25,11 @@ #include "sharedmidistate.h" #include "eventthread.h" #include "filesystem.h" +#include "exception.h" #include "aldatasource.h" #include "fluid-fun.h" #include "sdl-util.h" +#include "debugwriter.h" #include #include @@ -123,6 +125,9 @@ void ALStream::stop() void ALStream::play(float offset) { + if (!source) + return; + checkStopped(); switch (state) @@ -205,24 +210,36 @@ void ALStream::openSource(const std::string &filename) SDL_RWread(&srcOps, sig, 1, 4); SDL_RWseek(&srcOps, 0, RW_SEEK_SET); - if (!strcmp(sig, "OggS")) + try { - source = createVorbisSource(srcOps, looped); - return; - } - - if (!strcmp(sig, "MThd")) - { - shState->midiState().initIfNeeded(shState->config()); - - if (HAVE_FLUID) + if (!strcmp(sig, "OggS")) { - source = createMidiSource(srcOps, looped); + source = createVorbisSource(srcOps, looped); return; } - } - source = createSDLSource(srcOps, ext, STREAM_BUF_SIZE, looped); + if (!strcmp(sig, "MThd")) + { + shState->midiState().initIfNeeded(shState->config()); + + if (HAVE_FLUID) + { + source = createMidiSource(srcOps, looped); + return; + } + } + + source = createSDLSource(srcOps, ext, STREAM_BUF_SIZE, looped); + } + catch (const Exception &e) + { + char buf[512]; + snprintf(buf, sizeof(buf), "Unable to decode audio stream: %s.%s: %s", + filename.c_str(), ext, e.msg.c_str()); + buf[sizeof(buf)-1] = '\0'; + + Debug() << buf; + } } void ALStream::stopStream() diff --git a/src/soundemitter.cpp b/src/soundemitter.cpp index 521c4eb..59a1b9b 100644 --- a/src/soundemitter.cpp +++ b/src/soundemitter.cpp @@ -26,6 +26,7 @@ #include "exception.h" #include "config.h" #include "util.h" +#include "debugwriter.h" #include @@ -127,6 +128,9 @@ void SoundEmitter::play(const std::string &filename, SoundBuffer *buffer = allocateBuffer(filename); + if (!buffer) + return; + /* Try to find first free source */ size_t i; for (i = 0; i < srcCount; ++i) @@ -206,9 +210,13 @@ SoundBuffer *SoundEmitter::allocateBuffer(const std::string &filename) if (!sampleHandle) { - SDL_RWclose(&dataSource); - throw Exception(Exception::SDLError, "%s.%s: %s", - filename.c_str(), extension, Sound_GetError()); + char buf[512]; + snprintf(buf, sizeof(buf), "Unable to decode sound: %s.%s: %s", + filename.c_str(), ext, Sound_GetError()); + buf[sizeof(buf)-1] = '\0'; + Debug() << buf; + + return 0; } uint32_t decBytes = Sound_DecodeAll(sampleHandle);