Add error checking to Audio API

This commit is contained in:
Doyle Thai 2016-07-28 13:11:30 +10:00
parent 09972ced6e
commit ac775b39c2
4 changed files with 152 additions and 34 deletions

View File

@ -224,7 +224,8 @@ const i32 asset_loadTTFont(AssetManager *assetManager, MemoryArena *arena,
const char *filePath)
{
PlatformFileRead fontFileRead = {0};
platform_readFileToBuffer(arena, filePath, &fontFileRead);
i32 result = platform_readFileToBuffer(arena, filePath, &fontFileRead);
if (result) return result;
stbtt_fontinfo fontInfo = {0};
stbtt_InitFont(&fontInfo, fontFileRead.buffer,

View File

@ -134,9 +134,19 @@ INTERNAL i32 rendererAcquire(AudioManager *audioManager,
AUDIO_NO_FREE_SOURCE)
{
// TODO(doyle): Error messaging return paths
DEBUG_LOG("rendererAcquire(): Failed to acquire free source, all busy");
return -1;
}
u32 checkSource = getSourceId(audioManager, audioRenderer);
if (alIsSource(checkSource) == AL_TRUE)
{
DEBUG_LOG(
"rendererAcquire(): Renderer has not been released before "
"acquiring, force release by stopping stream");
audio_streamStopVorbis(audioManager, audioRenderer);
}
/* Assign a vacant source slot to renderer */
audioRenderer->sourceIndex = vacantSource;
@ -156,18 +166,35 @@ INTERNAL i32 rendererAcquire(AudioManager *audioManager,
return 0;
}
INTERNAL void rendererRelease(AudioManager *audioManager,
INTERNAL const i32 rendererRelease(AudioManager *audioManager,
AudioRenderer *audioRenderer)
{
i32 result = 0;
u32 alSourceId = getSourceId(audioManager, audioRenderer);
if (alIsSource(alSourceId) == AL_FALSE)
{
DEBUG_LOG(
"rendererRelease(): Trying to release invalid source, early exit");
alSourceUnqueueBuffers(alSourceId, ARRAY_COUNT(audioRenderer->bufferId),
audioRenderer->bufferId);
AL_CHECK_ERROR();
alDeleteBuffers(ARRAY_COUNT(audioRenderer->bufferId),
audioRenderer->bufferId);
result = -1;
return result;
}
for (i32 i = 0; i < ARRAY_COUNT(audioRenderer->bufferId); i++)
{
if (alIsBuffer(audioRenderer->bufferId[i]) == AL_TRUE)
{
alSourceUnqueueBuffers(alSourceId, 1, &audioRenderer->bufferId[i]);
alDeleteBuffers(1, &audioRenderer->bufferId[i]);
AL_CHECK_ERROR();
}
else
{
DEBUG_LOG("rendererRelease(): Invalid buffer identified in renderer");
result = -1;
}
}
for (i32 i = 0; i < ARRAY_COUNT(audioRenderer->bufferId); i++)
{
@ -184,12 +211,14 @@ INTERNAL void rendererRelease(AudioManager *audioManager,
audioManager->freeSourceIndex;
audioManager->freeSourceIndex = sourceIndexToFree;
return result;
}
#define AUDIO_CHUNK_SIZE_ 65536
void audio_streamPlayVorbis(AudioManager *audioManager,
AudioRenderer *audioRenderer, AudioVorbis *vorbis,
i32 numPlays)
const i32 audio_streamPlayVorbis(AudioManager *audioManager,
AudioRenderer *audioRenderer,
AudioVorbis *vorbis, i32 numPlays)
{
#ifdef DENGINE_DEBUG
ASSERT(audioManager && audioRenderer && vorbis);
@ -203,7 +232,7 @@ void audio_streamPlayVorbis(AudioManager *audioManager,
if (result)
{
DEBUG_LOG("audio_streamPlayVorbis() failed: Could not acquire renderer");
return;
return result;
}
/* Determine format */
@ -214,38 +243,93 @@ void audio_streamPlayVorbis(AudioManager *audioManager,
{
#ifdef DENGINE_DEBUG
DEBUG_LOG(
"audio_streamPlayVorbis() warning: Unaccounted channel format");
ASSERT(INVALID_CODE_PATH);
"audio_streamPlayVorbis() warning: Unexpected channel format");
#endif
}
audioRenderer->audio = vorbis;
audioRenderer->numPlays = numPlays;
return 0;
}
void audio_streamStopVorbis(AudioManager *audioManager,
const i32 audio_streamStopVorbis(AudioManager *audioManager,
AudioRenderer *audioRenderer)
{
i32 result = 0;
u32 alSourceId = getSourceId(audioManager, audioRenderer);
if (alIsSource(alSourceId) == AL_TRUE)
{
alSourceStop(alSourceId);
AL_CHECK_ERROR();
rendererRelease(audioManager, audioRenderer);
result = rendererRelease(audioManager, audioRenderer);
}
else
{
DEBUG_LOG("audio_streamStopVorbis(): Tried to stop invalid source");
result = -1;
}
return result;
}
void audio_updateAndPlay(AudioManager *audioManager,
const i32 audio_streamPauseVorbis(AudioManager *audioManager,
AudioRenderer *audioRenderer)
{
i32 result = 0;
u32 alSourceId = getSourceId(audioManager, audioRenderer);
if (alIsSource(alSourceId) == AL_TRUE)
{
alSourcePause(alSourceId);
AL_CHECK_ERROR();
}
else
{
DEBUG_LOG("audio_streamPauseVorbis(): Tried to pause invalid source");
result = -1;
}
return result;
}
const i32 audio_streamResumeVorbis(AudioManager *audioManager,
AudioRenderer *audioRenderer)
{
i32 result = 0;
u32 alSourceId = getSourceId(audioManager, audioRenderer);
if (alIsSource(alSourceId) == AL_TRUE)
{
alSourcePlay(alSourceId);
AL_CHECK_ERROR();
}
else
{
DEBUG_LOG("audio_streamResumeVorbis(): Tried to resume invalid source");
result = -1;
}
return result;
}
const i32 audio_updateAndPlay(AudioManager *audioManager,
AudioRenderer *audioRenderer)
{
AudioVorbis *audio = audioRenderer->audio;
if (!audio) return;
if (!audio) return 0;
if (audioRenderer->numPlays != AUDIO_REPEAT_INFINITE &&
audioRenderer->numPlays <= 0)
{
rendererRelease(audioManager, audioRenderer);
return;
i32 result = rendererRelease(audioManager, audioRenderer);
return result;
}
u32 alSourceId = getSourceId(audioManager, audioRenderer);
if (alIsSource(alSourceId) == AL_FALSE)
{
DEBUG_LOG("audio_updateAndPlay(): Update failed on invalid source id");
return -1;
}
ALint audioState;
alGetSourcei(alSourceId, AL_SOURCE_STATE, &audioState);
AL_CHECK_ERROR();
@ -271,8 +355,8 @@ void audio_updateAndPlay(AudioManager *audioManager,
}
else
{
rendererRelease(audioManager, audioRenderer);
return;
i32 result = rendererRelease(audioManager, audioRenderer);
return result;
}
}
@ -329,4 +413,6 @@ void audio_updateAndPlay(AudioManager *audioManager,
}
}
}
return 0;
}

View File

@ -224,7 +224,10 @@ void worldTraveller_gameInit(GameState *state, v2 windowSize)
"data/shaders/sprite.frag.glsl",
shaderlist_sprite);
result =
asset_loadTTFont(assetManager, arena, "C:/Windows/Fonts/Arialbd.ttf");
if (result) DEBUG_LOG("Font loading failed");
GL_CHECK_ERROR();
#ifdef DENGINE_DEBUG
@ -495,6 +498,7 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
ddPos = v2_scale(ddPos, 0.70710678118f);
}
LOCAL_PERSIST b32 toggleFlag = TRUE;
// TODO(doyle): Revisit key input with state checking for last ended down
if (state->keys[GLFW_KEY_SPACE] && spaceBarWasDown == FALSE)
{
@ -506,6 +510,7 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
v2 pos = V2(renderer->size.w - (renderer->size.w / xModifier), yPos);
addGenericMob(&state->arena, &state->assetManager, world, pos);
#endif
#if 0
if (world->soundscape->audio->sourceIndex == AUDIO_SOURCE_UNASSIGNED)
{
audio_streamPlayVorbis(
@ -517,6 +522,28 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
{
audio_streamStopVorbis(&state->audioManager,
world->soundscape->audio);
audio_streamPauseVorbis(&state->audioManager,
world->soundscape->audio);
}
#endif
if (toggleFlag)
{
audio_streamPauseVorbis(&state->audioManager,
world->soundscape->audio);
toggleFlag = FALSE;
}
else
{
audio_streamResumeVorbis(&state->audioManager,
world->soundscape->audio);
#if 0
audio_streamPlayVorbis(
&state->audioManager, world->soundscape->audio,
asset_getVorbis(&state->assetManager, audiolist_battle),
AUDIO_REPEAT_INFINITE);
#endif
toggleFlag = TRUE;
}
spaceBarWasDown = TRUE;
}

View File

@ -36,11 +36,15 @@ struct AudioRenderer
typedef struct AudioRenderer AudioRenderer;
const i32 audio_init(AudioManager *audioManager);
void audio_streamPlayVorbis(AudioManager *audioManager,
AudioRenderer *audioRenderer, AudioVorbis *vorbis,
i32 numPlays);
void audio_streamStopVorbis(AudioManager *audioManager,
const i32 audio_streamPlayVorbis(AudioManager *audioManager,
AudioRenderer *audioRenderer,
AudioVorbis *vorbis, i32 numPlays);
const i32 audio_streamStopVorbis(AudioManager *audioManager,
AudioRenderer *audioRenderer);
void audio_updateAndPlay(AudioManager *audioManager,
const i32 audio_streamPauseVorbis(AudioManager *audioManager,
AudioRenderer *audioRenderer);
const i32 audio_streamResumeVorbis(AudioManager *audioManager,
AudioRenderer *audioRenderer);
const i32 audio_updateAndPlay(AudioManager *audioManager,
AudioRenderer *audioRenderer);
#endif