Commits

Bill Meltsner  committed 07245c9

added Source:seek/tell

  • Participants
  • Parent commits 1350783
  • Branches minor

Comments (0)

Files changed (8)

File src/modules/audio/Source.cpp

 	{
 		return types.find(in, out);
 	}
+	
+	bool Source::getConstant(const char * in, Unit & out)
+	{
+		return units.find(in, out);
+	}
+	
+	bool Source::getConstant(Unit in, const char *& out)
+	{
+		return units.find(in, out);
+	}
 
 	StringMap<Source::Type, Source::TYPE_MAX_ENUM>::Entry Source::typeEntries[] =
 	{
 	};
 
 	StringMap<Source::Type, Source::TYPE_MAX_ENUM> Source::types(Source::typeEntries, sizeof(Source::typeEntries));
+	
+	StringMap<Source::Unit, Source::UNIT_MAX_ENUM>::Entry Source::unitEntries[] =
+	{
+		{"seconds", Source::UNIT_SECONDS},
+		{"samples", Source::UNIT_SAMPLES},
+	};
+	
+	StringMap<Source::Unit, Source::UNIT_MAX_ENUM> Source::units(Source::unitEntries, sizeof(Source::unitEntries));
 
 } // audio
 } // love

File src/modules/audio/Source.h

 			TYPE_STREAM,
 			TYPE_MAX_ENUM
 		}; // Type
+		
+		enum Unit
+		{
+			UNIT_SECONDS = 1,
+			UNIT_SAMPLES,
+			UNIT_MAX_ENUM
+		};
 
 	protected:
 		Type type;
 
 		virtual void setVolume(float volume) = 0;
 		virtual float getVolume() const = 0;
+		
+		virtual void seek(float offset, Unit unit) = 0;
+		virtual float tell(Unit unit) const = 0;
 
 		// all float * v must be of size 3
 		virtual void setPosition(float * v) = 0;
 
 		static bool getConstant(const char * in, Type & out);
 		static bool getConstant(Type in, const char *& out);
+		static bool getConstant(const char * in, Unit & out);
+		static bool getConstant(Unit in, const char *& out);
 
 	private:
 
 		static StringMap<Type, TYPE_MAX_ENUM>::Entry typeEntries[];
 		static StringMap<Type, TYPE_MAX_ENUM> types;
+		static StringMap<Unit, UNIT_MAX_ENUM>::Entry unitEntries[];
+		static StringMap<Unit, UNIT_MAX_ENUM> units;
 
 	}; // Source
 

File src/modules/audio/null/Source.cpp

 	{
 		return volume;
 	}
+	
+	void Source::seek(float, Source::Unit)
+	{
+	}
+	
+	float Source::tell(Source::Unit) const
+	{
+		return 0.0f;
+	}
 
 	void Source::setPosition(float *)
 	{

File src/modules/audio/null/Source.h

 		virtual float getPitch() const;
 		virtual void setVolume(float volume);
 		virtual float getVolume() const;
+		virtual void seek(float offset, Unit unit);
+		virtual float tell(Unit unit) const;
 		virtual void setPosition(float * v);
 		virtual void getPosition(float * v) const;
 		virtual void setVelocity(float * v);

File src/modules/audio/openal/Source.cpp

 		// In case the Source isn't playing.
 		return volume;
 	}
+	
+	void Source::seek(float offset, Source::Unit unit)
+	{
+		if (valid)
+		{
+			switch (unit) {
+				case Source::UNIT_SAMPLES:
+					alSourcef(source, AL_SAMPLE_OFFSET, offset);
+					break;
+				case Source::UNIT_SECONDS:	
+				default:
+					alSourcef(source, AL_SEC_OFFSET, offset);
+					break;
+			}
+		}
+	}
+	
+	float Source::tell(Source::Unit unit) const
+	{
+		if (valid)
+		{
+			ALfloat offset;
+			switch (unit) {
+				case Source::UNIT_SAMPLES:
+					alGetSourcef(source, AL_SAMPLE_OFFSET, &offset);
+					break;
+				case Source::UNIT_SECONDS:
+				default:
+					alGetSourcef(source, AL_SEC_OFFSET, &offset);
+					break;
+			}
+			return offset;
+		}
+		return 0.0f;
+	}
 
 	void Source::setPosition(float * v)
 	{

File src/modules/audio/openal/Source.h

 		virtual float getPitch() const;
 		virtual void setVolume(float volume);
 		virtual float getVolume() const;
+		virtual void seek(float offset, Unit unit);
+		virtual float tell(Unit unit) const;
 		virtual void setPosition(float * v);
 		virtual void getPosition(float * v) const;
 		virtual void setVelocity(float * v);

File src/modules/audio/wrap_Source.cpp

 		lua_pushnumber(L, t->getVolume());
 		return 1;
 	}
+	
+	int w_Source_seek(lua_State * L)
+	{
+		Source * t = luax_checksource(L, 1);
+		float offset = (float)luaL_checknumber(L, 2);
+		const char * unit = luaL_optstring(L, 3, "seconds");
+		Source::Unit u;
+		t->getConstant(unit, u);
+		t->seek(offset, u);
+		return 0;
+	}
+	
+	int w_Source_tell(lua_State * L)
+	{
+		Source * t = luax_checksource(L, 1);
+		const char * unit = luaL_optstring(L, 2, "seconds");
+		Source::Unit u;
+		t->getConstant(unit, u);
+		lua_pushnumber(L, t->tell(u));
+		return 1;
+	}
 
 	int w_Source_setPosition(lua_State * L)
 	{
 		{ "getPitch", w_Source_getPitch },
 		{ "setVolume", w_Source_setVolume },
 		{ "getVolume", w_Source_getVolume },
+		{ "seek", w_Source_seek },
+		{ "tell", w_Source_tell },
 		{ "setPosition", w_Source_setPosition },
 		{ "getPosition", w_Source_getPosition },
 		{ "setVelocity", w_Source_setVelocity },

File src/modules/audio/wrap_Source.h

 	int w_Source_getPitch(lua_State * L);
 	int w_Source_setVolume(lua_State * L);
 	int w_Source_getVolume(lua_State * L);
+	int w_Source_seek(lua_State * L);
+	int w_Source_tell(lua_State * L);
 	int w_Source_setPosition(lua_State * L);
 	int w_Source_getPosition(lua_State * L);
 	int w_Source_setVelocity(lua_State * L);