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.
This commit is contained in:
Jonas Kulla 2015-02-17 02:09:55 +01:00
parent 0f91bdefea
commit 531441d4e3
2 changed files with 41 additions and 16 deletions

View File

@ -25,9 +25,11 @@
#include "sharedmidistate.h" #include "sharedmidistate.h"
#include "eventthread.h" #include "eventthread.h"
#include "filesystem.h" #include "filesystem.h"
#include "exception.h"
#include "aldatasource.h" #include "aldatasource.h"
#include "fluid-fun.h" #include "fluid-fun.h"
#include "sdl-util.h" #include "sdl-util.h"
#include "debugwriter.h"
#include <SDL_mutex.h> #include <SDL_mutex.h>
#include <SDL_thread.h> #include <SDL_thread.h>
@ -123,6 +125,9 @@ void ALStream::stop()
void ALStream::play(float offset) void ALStream::play(float offset)
{ {
if (!source)
return;
checkStopped(); checkStopped();
switch (state) switch (state)
@ -205,6 +210,8 @@ void ALStream::openSource(const std::string &filename)
SDL_RWread(&srcOps, sig, 1, 4); SDL_RWread(&srcOps, sig, 1, 4);
SDL_RWseek(&srcOps, 0, RW_SEEK_SET); SDL_RWseek(&srcOps, 0, RW_SEEK_SET);
try
{
if (!strcmp(sig, "OggS")) if (!strcmp(sig, "OggS"))
{ {
source = createVorbisSource(srcOps, looped); source = createVorbisSource(srcOps, looped);
@ -223,6 +230,16 @@ void ALStream::openSource(const std::string &filename)
} }
source = createSDLSource(srcOps, ext, STREAM_BUF_SIZE, looped); 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() void ALStream::stopStream()

View File

@ -26,6 +26,7 @@
#include "exception.h" #include "exception.h"
#include "config.h" #include "config.h"
#include "util.h" #include "util.h"
#include "debugwriter.h"
#include <SDL_sound.h> #include <SDL_sound.h>
@ -127,6 +128,9 @@ void SoundEmitter::play(const std::string &filename,
SoundBuffer *buffer = allocateBuffer(filename); SoundBuffer *buffer = allocateBuffer(filename);
if (!buffer)
return;
/* Try to find first free source */ /* Try to find first free source */
size_t i; size_t i;
for (i = 0; i < srcCount; ++i) for (i = 0; i < srcCount; ++i)
@ -206,9 +210,13 @@ SoundBuffer *SoundEmitter::allocateBuffer(const std::string &filename)
if (!sampleHandle) if (!sampleHandle)
{ {
SDL_RWclose(&dataSource); char buf[512];
throw Exception(Exception::SDLError, "%s.%s: %s", snprintf(buf, sizeof(buf), "Unable to decode sound: %s.%s: %s",
filename.c_str(), extension, Sound_GetError()); filename.c_str(), ext, Sound_GetError());
buf[sizeof(buf)-1] = '\0';
Debug() << buf;
return 0;
} }
uint32_t decBytes = Sound_DecodeAll(sampleHandle); uint32_t decBytes = Sound_DecodeAll(sampleHandle);