Commits

Alex Szpakowski committed 104bacd

Added Source:setCone(innerAngle, outerAngle, outerVolume). Resolves issue #643.

When combined with Source:setDirection (and Source:setPosition or love.audio.setPosition), the cone parameters allow for directional sound output from a Source.

Comments (0)

Files changed (7)

src/modules/audio/Source.h

 	virtual void setDirection(float *v) = 0;
 	virtual void getDirection(float *v) const = 0;
 
+	virtual void setCone(float innerAngle, float outerAngle, float outerVolume) = 0;
+	virtual void getCone(float &innerAngle, float &outerAngle, float &outerVolume) const = 0;
+
 	virtual void setRelativePosition(bool relative) = 0;
 	virtual bool hasRelativePosition() const = 0;
 

src/modules/audio/null/Source.cpp

 {
 }
 
+void Source::setCone(float innerAngle, float outerAngle, float outerVolume)
+{
+	coneInnerAngle = innerAngle;
+	coneOuterAngle = outerAngle;
+	coneOuterVolume = outerVolume;
+}
+
+void Source::getCone(float &innerAngle, float &outerAngle, float &outerVolume) const
+{
+	innerAngle = coneInnerAngle;
+	outerAngle = coneOuterAngle;
+	outerVolume = coneOuterVolume;
+}
+
 void Source::setRelativePosition(bool relative)
 {
 	relativePosition = relative;

src/modules/audio/null/Source.h

 	virtual void getVelocity(float *v) const;
 	virtual void setDirection(float *v);
 	virtual void getDirection(float *v) const;
+	virtual void setCone(float innerAngle, float outerAngle, float outerVolume);
+	virtual void getCone(float &innerAngle, float &outerAngle, float &outerVolume) const;
 	virtual void setRelativePosition(bool relative);
 	virtual bool hasRelativePosition() const;
 	void setLooping(bool looping);
 
 	float pitch;
 	float volume;
+	float coneInnerAngle;
+	float coneOuterAngle;
+	float coneOuterVolume;
 	bool relativePosition;
 	bool looping;
 	float minVolume;

src/modules/audio/openal/Source.cpp

  **/
 
 #include "Source.h"
-
 #include "Pool.h"
+#include "common/math.h"
 
 // STD
 #include <iostream>
 	, referenceDistance(1.0f)
 	, rolloffFactor(1.0f)
 	, maxDistance(FLT_MAX)
+	, cone()
 	, offsetSamples(0)
 	, offsetSeconds(0)
 	, decoder(0)
 	, referenceDistance(1.0f)
 	, rolloffFactor(1.0f)
 	, maxDistance(FLT_MAX)
+	, cone()
 	, offsetSamples(0)
 	, offsetSeconds(0)
 	, decoder(decoder)
 		setFloatv(v, direction);
 }
 
+void Source::setCone(float innerAngle, float outerAngle, float outerVolume)
+{
+	cone.innerAngle = LOVE_TODEG(innerAngle);
+	cone.outerAngle = LOVE_TODEG(outerAngle);
+	cone.outerVolume = outerVolume;
+
+	if (valid)
+	{
+		alSourcei(source, AL_CONE_INNER_ANGLE, cone.innerAngle);
+		alSourcei(source, AL_CONE_OUTER_ANGLE, cone.outerAngle);
+		alSourcef(source, AL_CONE_OUTER_GAIN, cone.outerVolume);
+	}
+}
+
+void Source::getCone(float &innerAngle, float &outerAngle, float &outerVolume) const
+{
+	innerAngle = LOVE_TORAD(cone.innerAngle);
+	outerAngle = LOVE_TORAD(cone.outerAngle);
+	outerVolume = cone.outerVolume;
+}
+
 void Source::setRelativePosition(bool relative)
 {
 	if (valid)
 	alSourcef(source, AL_MAX_DISTANCE, maxDistance);
 	alSourcei(source, AL_LOOPING, isStatic() && isLooping() ? AL_TRUE : AL_FALSE);
 	alSourcei(source, AL_SOURCE_RELATIVE, relativePosition ? AL_TRUE : AL_FALSE);
+	alSourcei(source, AL_CONE_INNER_ANGLE, cone.innerAngle);
+	alSourcei(source, AL_CONE_OUTER_ANGLE, cone.outerAngle);
+	alSourcef(source, AL_CONE_OUTER_GAIN, cone.outerVolume);
 }
 
 void Source::setFloatv(float *dst, const float *src) const

src/modules/audio/openal/Source.h

 	virtual void getVelocity(float *v) const;
 	virtual void setDirection(float *v);
 	virtual void getDirection(float *v) const;
+	virtual void setCone(float innerAngle, float outerAngle, float outerVolume);
+	virtual void getCone(float &innerAngle, float &outerAngle, float &outerVolume) const;
 	virtual void setRelativePosition(bool relative);
 	virtual bool hasRelativePosition() const;
 	void setLooping(bool looping);
 	float rolloffFactor;
 	float maxDistance;
 
+	struct Cone
+	{
+		int innerAngle; // degrees
+		int outerAngle; // degrees
+		float outerVolume;
+
+		Cone()
+			: innerAngle(360)
+			, outerAngle(360)
+			, outerVolume(0.0f)
+		{}
+	} cone;
+
 	float offsetSamples;
 	float offsetSeconds;
 

src/modules/audio/wrap_Source.cpp

 	return 3;
 }
 
+int w_Source_setCone(lua_State *L)
+{
+	Source *t = luax_checksource(L, 1);
+	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);
+	return 0;
+}
+
+int w_Source_getCone(lua_State *L)
+{
+	Source *t = luax_checksource(L, 1);
+	float innerAngle, outerAngle, outerVolume;
+	t->getCone(innerAngle, outerAngle, outerVolume);
+	lua_pushnumber(L, innerAngle);
+	lua_pushnumber(L, outerAngle);
+	lua_pushnumber(L, outerVolume);
+	return 3;
+}
+
 int w_Source_setRelativePosition(lua_State *L)
 {
 	Source *t = luax_checksource(L, 1);
 	{ "getVelocity", w_Source_getVelocity },
 	{ "setDirection", w_Source_setDirection },
 	{ "getDirection", w_Source_getDirection },
+	{ "setCone", w_Source_setCone },
+	{ "getCone", w_Source_getCone },
 
 	{ "setRelativePosition", w_Source_setRelativePosition },
 	{ "hasRelativePosition", w_Source_hasRelativePosition },

src/modules/audio/wrap_Source.h

 int w_Source_getVelocity(lua_State *L);
 int w_Source_setDirection(lua_State *L);
 int w_Source_getDirection(lua_State *L);
+int w_Source_setCone(lua_State *L);
+int w_Source_getCone(lua_State *L);
 int w_Source_setRelativePosition(lua_State *L);
 int w_Source_hasRelativePosition(lua_State *L);
 int w_Source_setLooping(lua_State *L);