2
0
Эх сурвалжийг харах

Add SoundData:getDuration, returns the duration in seconds

Bart van Strien 12 жил өмнө
parent
commit
8fba362fc5

+ 5 - 0
src/modules/sound/SoundData.cpp

@@ -147,6 +147,11 @@ int SoundData::getSampleRate() const
 	return sampleRate;
 	return sampleRate;
 }
 }
 
 
+int SoundData::getDuration() const
+{
+	return size/(channels*sampleRate*bits/8);
+}
+
 void SoundData::setSample(int i, float sample)
 void SoundData::setSample(int i, float sample)
 {
 {
 	// Check range.
 	// Check range.

+ 2 - 0
src/modules/sound/SoundData.h

@@ -49,6 +49,8 @@ public:
 	virtual int getBits() const;
 	virtual int getBits() const;
 	virtual int getSampleRate() const;
 	virtual int getSampleRate() const;
 
 
+	virtual int getDuration() const;
+
 	void setSample(int i, float sample);
 	void setSample(int i, float sample);
 	float getSample(int i) const;
 	float getSample(int i) const;
 
 

+ 8 - 0
src/modules/sound/wrap_SoundData.cpp

@@ -53,6 +53,13 @@ int w_SoundData_getSampleRate(lua_State *L)
 	return 1;
 	return 1;
 }
 }
 
 
+int w_SoundData_getDuration(lua_State *L)
+{
+	SoundData *t = luax_checksounddata(L, 1);
+	lua_pushinteger(L, t->getDuration());
+	return 1;
+}
+
 int w_SoundData_setSample(lua_State *L)
 int w_SoundData_setSample(lua_State *L)
 {
 {
 	SoundData *sd = luax_checksounddata(L, 1);
 	SoundData *sd = luax_checksounddata(L, 1);
@@ -80,6 +87,7 @@ static const luaL_Reg functions[] =
 	{ "getChannels", w_SoundData_getChannels },
 	{ "getChannels", w_SoundData_getChannels },
 	{ "getBits", w_SoundData_getBits },
 	{ "getBits", w_SoundData_getBits },
 	{ "getSampleRate", w_SoundData_getSampleRate },
 	{ "getSampleRate", w_SoundData_getSampleRate },
+	{ "getDuration", w_SoundData_getDuration },
 	{ "setSample", w_SoundData_setSample },
 	{ "setSample", w_SoundData_setSample },
 	{ "getSample", w_SoundData_getSample },
 	{ "getSample", w_SoundData_getSample },
 	{ 0, 0 }
 	{ 0, 0 }

+ 1 - 0
src/modules/sound/wrap_SoundData.h

@@ -36,6 +36,7 @@ int w_getSize(lua_State *L);
 int w_getChannels(lua_State *L);
 int w_getChannels(lua_State *L);
 int w_getBits(lua_State *L);
 int w_getBits(lua_State *L);
 int w_getSampleRate(lua_State *L);
 int w_getSampleRate(lua_State *L);
+int w_getDuration(lua_State *L);
 int w_setSample(lua_State *L);
 int w_setSample(lua_State *L);
 int w_getSample(lua_State *L);
 int w_getSample(lua_State *L);
 extern "C" int luaopen_sounddata(lua_State *L);
 extern "C" int luaopen_sounddata(lua_State *L);