Forráskód Böngészése

decoder manual decoding and seeking implemented

decode() uses supplied SoundData to decode to, creates new SoundData if none provided
returns SoundData containing decoded data, nil if decoder didn't produce any data
seek() only works with seconds

--HG--
branch : minor-manual-decoders
rcoaxil 9 éve
szülő
commit
78f5779214
2 módosított fájl, 55 hozzáadás és 2 törlés
  1. 1 2
      src/modules/sound/SoundData.h
  2. 54 0
      src/modules/sound/wrap_Decoder.cpp

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

@@ -54,11 +54,10 @@ public:
 
 	void setSample(int i, float sample);
 	float getSample(int i) const;
+	void load(int samples, int sampleRate, int bitDepth, int channels, void *newData = 0);
 
 private:
 
-	void load(int samples, int sampleRate, int bitDepth, int channels, void *newData = 0);
-
 	uint8 *data;
 	size_t size;
 

+ 54 - 0
src/modules/sound/wrap_Decoder.cpp

@@ -19,6 +19,10 @@
  **/
 
 #include "wrap_Decoder.h"
+#include "SoundData.h"
+#include "Sound.h"
+
+#define instance() (Module::getInstance<Sound>(Module::M_SOUND))
 
 namespace love
 {
@@ -58,12 +62,62 @@ int w_Decoder_getDuration(lua_State *L)
 	return 1;
 }
 
+int w_Decoder_decode(lua_State *L)
+{
+	Decoder *t = luax_checkdecoder(L, 1);
+	SoundData *s = nullptr;
+	if (luax_istype(L, 2, SOUND_SOUND_DATA_ID))
+	{
+		s = luax_totype<SoundData>(L, 2, SOUND_SOUND_DATA_ID);
+		if (s->getSampleRate() != t->getSampleRate() ||
+			s->getBitDepth() != t->getBitDepth() ||
+			s->getChannels() != t->getChannels() )
+			return luaL_error(L, "SoundData sound format doesn't match Decoder sound format.");
+		s->retain();
+	}
+	else if (lua_gettop(L) > 1)
+		luax_typerror(L, 2, "SoundData" );
+	else
+		luax_catchexcept(L, [&](){ 
+			s = instance()->newSoundData(t->getSize() / (t->getBitDepth() / 8 * t->getChannels()), t->getSampleRate(), t->getBitDepth(), t->getChannels()); 
+		});
+
+	int decoded = t->decode();
+	if (decoded > 0)
+	{
+		luax_catchexcept(L, [&](){ 
+			s->load(decoded / (t->getBitDepth() / 8 * t->getChannels()), t->getSampleRate(), t->getBitDepth(), t->getChannels(), t->getBuffer());
+		});
+		luax_pushtype(L, SOUND_SOUND_DATA_ID, s);
+	}
+	else
+		lua_pushnil(L);
+	
+	s->release();
+	return 1;
+}
+
+int w_Decoder_seek(lua_State *L)
+{
+	Decoder *t = luax_checkdecoder(L, 1);
+	float offset = luaL_checknumber(L, 2);
+	if (offset < 0)
+		return luaL_argerror(L, 2, "can't seek to a negative position");
+	else if (offset == 0)
+		t->rewind();
+	else
+		t->seek(offset);
+	return 0;
+}
+
 static const luaL_Reg w_Decoder_functions[] =
 {
 	{ "getChannels", w_Decoder_getChannels },
 	{ "getBitDepth", w_Decoder_getBitDepth },
 	{ "getSampleRate", w_Decoder_getSampleRate },
 	{ "getDuration", w_Decoder_getDuration },
+	{ "decode", w_Decoder_decode },
+	{ "seek", w_Decoder_seek },
 	{ 0, 0 }
 };