From 70fabedc09546fbd2e8c84f44974b9a1b7ce5412 Mon Sep 17 00:00:00 2001 From: Aesthete Date: Wed, 23 Apr 2014 11:39:36 +1000 Subject: [PATCH] Aesthete: Bug #829: Added exceptions for all spatial audio calls on stereo sources. I had to backout the last couple of changes because the diff tool thought I had changed the entire file, and made it impossible to see what I had actually changed. --HG-- branch : minor --- src/modules/audio/openal/Source.cpp | 52 +++++++++++++++++++++++++++++ src/modules/audio/wrap_Source.cpp | 32 +++++++++--------- 2 files changed, 68 insertions(+), 16 deletions(-) diff --git a/src/modules/audio/openal/Source.cpp b/src/modules/audio/openal/Source.cpp index b823c0d53..a8f9af8f3 100644 --- a/src/modules/audio/openal/Source.cpp +++ b/src/modules/audio/openal/Source.cpp @@ -32,6 +32,10 @@ namespace audio { namespace openal { + // Message to be used by any exception thrown here for attempting to call spatial functions on stereo sources, as this + // is not supported by openAL. + static const char* NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE = "This spatial audio functionality is only available on monaural sources. \ + Ensure your source is not stereo before calling this function."; StaticDataBuffer::StaticDataBuffer(ALenum format, const ALvoid *data, ALsizei size, ALsizei freq) { @@ -420,6 +424,9 @@ float Source::tell(Source::Unit unit) void Source::setPosition(float *v) { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + if (valid) alSourcefv(source, AL_POSITION, v); @@ -428,6 +435,9 @@ void Source::setPosition(float *v) void Source::getPosition(float *v) const { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + if (valid) alGetSourcefv(source, AL_POSITION, v); else @@ -436,6 +446,9 @@ void Source::getPosition(float *v) const void Source::setVelocity(float *v) { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + if (valid) alSourcefv(source, AL_VELOCITY, v); @@ -444,6 +457,9 @@ void Source::setVelocity(float *v) void Source::getVelocity(float *v) const { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + if (valid) alGetSourcefv(source, AL_VELOCITY, v); else @@ -452,6 +468,9 @@ void Source::getVelocity(float *v) const void Source::setDirection(float *v) { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + if (valid) alSourcefv(source, AL_DIRECTION, v); else @@ -460,6 +479,9 @@ void Source::setDirection(float *v) void Source::getDirection(float *v) const { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + if (valid) alGetSourcefv(source, AL_DIRECTION, v); else @@ -468,6 +490,9 @@ void Source::getDirection(float *v) const void Source::setCone(float innerAngle, float outerAngle, float outerVolume) { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + cone.innerAngle = (int) LOVE_TODEG(innerAngle); cone.outerAngle = (int) LOVE_TODEG(outerAngle); cone.outerVolume = outerVolume; @@ -482,6 +507,9 @@ void Source::setCone(float innerAngle, float outerAngle, float outerVolume) void Source::getCone(float &innerAngle, float &outerAngle, float &outerVolume) const { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + innerAngle = LOVE_TORAD(cone.innerAngle); outerAngle = LOVE_TORAD(cone.outerAngle); outerVolume = cone.outerVolume; @@ -489,6 +517,9 @@ void Source::getCone(float &innerAngle, float &outerAngle, float &outerVolume) c void Source::setRelative(bool enable) { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + if (valid) alSourcei(source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE); @@ -497,6 +528,9 @@ void Source::setRelative(bool enable) bool Source::isRelative() const { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + return relative; } @@ -751,6 +785,9 @@ float Source::getMaxVolume() const void Source::setReferenceDistance(float distance) { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + if (valid) { alSourcef(source, AL_REFERENCE_DISTANCE, distance); @@ -761,6 +798,9 @@ void Source::setReferenceDistance(float distance) float Source::getReferenceDistance() const { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + if (valid) { ALfloat f; @@ -774,6 +814,9 @@ float Source::getReferenceDistance() const void Source::setRolloffFactor(float factor) { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + if (valid) { alSourcef(source, AL_ROLLOFF_FACTOR, factor); @@ -784,6 +827,9 @@ void Source::setRolloffFactor(float factor) float Source::getRolloffFactor() const { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + if (valid) { ALfloat f; @@ -797,6 +843,9 @@ float Source::getRolloffFactor() const void Source::setMaxDistance(float distance) { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + if (valid) { alSourcef(source, AL_MAX_DISTANCE, distance); @@ -807,6 +856,9 @@ void Source::setMaxDistance(float distance) float Source::getMaxDistance() const { + if (channels > 1) + throw love::Exception(NO_SPATIAL_SUPPORT_FOR_STEREO_ERROR_MESSAGE); + if (valid) { ALfloat f; diff --git a/src/modules/audio/wrap_Source.cpp b/src/modules/audio/wrap_Source.cpp index c94ded3b4..c0b70df3d 100644 --- a/src/modules/audio/wrap_Source.cpp +++ b/src/modules/audio/wrap_Source.cpp @@ -146,7 +146,7 @@ int w_Source_setPosition(lua_State *L) v[0] = (float)luaL_checknumber(L, 2); v[1] = (float)luaL_checknumber(L, 3); v[2] = (float)luaL_optnumber(L, 4, 0); - t->setPosition(v); + EXCEPT_GUARD(t->setPosition(v);) return 0; } @@ -154,7 +154,7 @@ int w_Source_getPosition(lua_State *L) { Source *t = luax_checksource(L, 1); float v[3]; - t->getPosition(v); + EXCEPT_GUARD(t->getPosition(v);) lua_pushnumber(L, v[0]); lua_pushnumber(L, v[1]); lua_pushnumber(L, v[2]); @@ -168,7 +168,7 @@ int w_Source_setVelocity(lua_State *L) v[0] = (float)luaL_checknumber(L, 2); v[1] = (float)luaL_checknumber(L, 3); v[2] = (float)luaL_optnumber(L, 4, 0); - t->setVelocity(v); + EXCEPT_GUARD(t->setVelocity(v);) return 0; } @@ -176,7 +176,7 @@ int w_Source_getVelocity(lua_State *L) { Source *t = luax_checksource(L, 1); float v[3]; - t->getVelocity(v); + EXCEPT_GUARD(t->getVelocity(v);) lua_pushnumber(L, v[0]); lua_pushnumber(L, v[1]); lua_pushnumber(L, v[2]); @@ -190,7 +190,7 @@ int w_Source_setDirection(lua_State *L) v[0] = (float)luaL_checknumber(L, 2); v[1] = (float)luaL_checknumber(L, 3); v[2] = (float)luaL_optnumber(L, 4, 0); - t->setDirection(v); + EXCEPT_GUARD(t->setDirection(v);) return 0; } @@ -198,7 +198,7 @@ int w_Source_getDirection(lua_State *L) { Source *t = luax_checksource(L, 1); float v[3]; - t->getDirection(v); + EXCEPT_GUARD(t->getDirection(v);) lua_pushnumber(L, v[0]); lua_pushnumber(L, v[1]); lua_pushnumber(L, v[2]); @@ -211,7 +211,7 @@ int w_Source_setCone(lua_State *L) float innerAngle = (float) luaL_checknumber(L, 2); float outerAngle = (float) luaL_checknumber(L, 3); float outerVolume = (float) luaL_optnumber(L, 4, 0.0); - t->setCone(innerAngle, outerAngle, outerVolume); + EXCEPT_GUARD(t->setCone(innerAngle, outerAngle, outerVolume);) return 0; } @@ -219,7 +219,7 @@ int w_Source_getCone(lua_State *L) { Source *t = luax_checksource(L, 1); float innerAngle, outerAngle, outerVolume; - t->getCone(innerAngle, outerAngle, outerVolume); + EXCEPT_GUARD(t->getCone(innerAngle, outerAngle, outerVolume);) lua_pushnumber(L, innerAngle); lua_pushnumber(L, outerAngle); lua_pushnumber(L, outerVolume); @@ -229,14 +229,14 @@ int w_Source_getCone(lua_State *L) int w_Source_setRelative(lua_State *L) { Source *t = luax_checksource(L, 1); - t->setRelative(luax_toboolean(L, 2)); + EXCEPT_GUARD(t->setRelative(luax_toboolean(L, 2));) return 0; } int w_Source_isRelative(lua_State *L) { Source *t = luax_checksource(L, 1); - luax_pushboolean(L, t->isRelative()); + EXCEPT_GUARD(luax_pushboolean(L, t->isRelative());) return 1; } @@ -302,16 +302,16 @@ int w_Source_setAttenuationDistances(lua_State *L) float dmax = (float)luaL_checknumber(L, 3); if (dref < .0f || dmax < .0f) return luaL_error(L, "Invalid distances: %f, %f. Must be > 0", dref, dmax); - t->setReferenceDistance(dref); - t->setMaxDistance(dmax); + EXCEPT_GUARD(t->setReferenceDistance(dref);) + EXCEPT_GUARD(t->setMaxDistance(dmax);) return 0; } int w_Source_getAttenuationDistances(lua_State *L) { Source *t = luax_checksource(L, 1); - lua_pushnumber(L, t->getReferenceDistance()); - lua_pushnumber(L, t->getMaxDistance()); + EXCEPT_GUARD(lua_pushnumber(L, t->getReferenceDistance());) + EXCEPT_GUARD(lua_pushnumber(L, t->getMaxDistance());) return 2; } @@ -321,14 +321,14 @@ int w_Source_setRolloff(lua_State *L) float rolloff = (float)luaL_checknumber(L, 2); if (rolloff < .0f) return luaL_error(L, "Invalid rolloff: %f. Must be > 0.", rolloff); - t->setRolloffFactor(rolloff); + EXCEPT_GUARD(t->setRolloffFactor(rolloff);) return 0; } int w_Source_getRolloff(lua_State *L) { Source *t = luax_checksource(L, 1); - lua_pushnumber(L, t->getRolloffFactor()); + EXCEPT_GUARD(lua_pushnumber(L, t->getRolloffFactor());) return 1; }