From 1d883110c5f796896ad0085cbcfde6b9fb8a6f23 Mon Sep 17 00:00:00 2001 From: Raidho Date: Wed, 19 Oct 2016 02:24:43 +0000 Subject: [PATCH 1/4] Created new branch minor-manual-decoders --HG-- branch : minor-manual-decoders From 78f5779214572326c147ac578f925397a71982fa Mon Sep 17 00:00:00 2001 From: rcoaxil Date: Wed, 19 Oct 2016 05:29:08 +0300 Subject: [PATCH 2/4] 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 --- src/modules/sound/SoundData.h | 3 +- src/modules/sound/wrap_Decoder.cpp | 54 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/modules/sound/SoundData.h b/src/modules/sound/SoundData.h index 07b13c2d3..821fb291e 100644 --- a/src/modules/sound/SoundData.h +++ b/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; diff --git a/src/modules/sound/wrap_Decoder.cpp b/src/modules/sound/wrap_Decoder.cpp index 1787890d4..56ac44a63 100644 --- a/src/modules/sound/wrap_Decoder.cpp +++ b/src/modules/sound/wrap_Decoder.cpp @@ -19,6 +19,10 @@ **/ #include "wrap_Decoder.h" +#include "SoundData.h" +#include "Sound.h" + +#define instance() (Module::getInstance(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(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 } }; From a605243709c667accc24002417bc2fc2d898f9f1 Mon Sep 17 00:00:00 2001 From: Raidho Date: Sun, 23 Oct 2016 21:09:13 +0300 Subject: [PATCH 3/4] made decode() always return new SoundData --HG-- branch : minor-manual-decoders --- src/modules/sound/Sound.cpp | 4 ++++ src/modules/sound/Sound.h | 12 ++++++++++++ src/modules/sound/wrap_Decoder.cpp | 30 +++++++----------------------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/modules/sound/Sound.cpp b/src/modules/sound/Sound.cpp index 81c95994c..38417aeb4 100644 --- a/src/modules/sound/Sound.cpp +++ b/src/modules/sound/Sound.cpp @@ -39,6 +39,10 @@ SoundData *Sound::newSoundData(int samples, int sampleRate, int bitDepth, int ch return new SoundData(samples, sampleRate, bitDepth, channels); } +SoundData *Sound::newSoundData(void *data, int samples, int sampleRate, int bitDepth, int channels) +{ + return new SoundData(data, samples, sampleRate, bitDepth, channels); +} } // sound } // love diff --git a/src/modules/sound/Sound.h b/src/modules/sound/Sound.h index 094fb8f1e..cd53ccfe5 100644 --- a/src/modules/sound/Sound.h +++ b/src/modules/sound/Sound.h @@ -66,6 +66,18 @@ public: **/ SoundData *newSoundData(int samples, int sampleRate, int bitDepth, int channels); + /** + * Creates a new SoundData with the specified number of samples and format + * and loads data from specified buffer. + * @param data Buffer to load data from. + * @param samples The number of samples. + * @param sampleRate Number of samples per second. + * @param bitDepth Bits per sample (8 or 16). + * @param channels Either 1 for mono, or 2 for stereo. + * @return A new SoundData object, or zero in case of errors. + **/ + SoundData *newSoundData(void *data, int samples, int sampleRate, int bitDepth, int channels); + /** * Attempts to find a decoder for the encoded sound data in the * specified file. diff --git a/src/modules/sound/wrap_Decoder.cpp b/src/modules/sound/wrap_Decoder.cpp index 56ac44a63..97bb31f61 100644 --- a/src/modules/sound/wrap_Decoder.cpp +++ b/src/modules/sound/wrap_Decoder.cpp @@ -65,35 +65,19 @@ int w_Decoder_getDuration(lua_State *L) 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(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_catchexcept(L, [&](){ + SoundData *s = instance()->newSoundData(t->getBuffer(), + decoded / (t->getBitDepth() / 8 * t->getChannels()), + t->getSampleRate(), t->getBitDepth(), t->getChannels()); + luax_pushtype(L, SOUND_SOUND_DATA_ID, s); - } + s->release(); + }); else lua_pushnil(L); - - s->release(); return 1; } From 3b37cc946296bc78167b8f77daee3aee9b8d3ded Mon Sep 17 00:00:00 2001 From: Raidho Date: Tue, 25 Oct 2016 00:58:44 +0300 Subject: [PATCH 4/4] moved SoundData::load back to private --HG-- branch : minor-manual-decoders --- src/modules/sound/SoundData.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/sound/SoundData.h b/src/modules/sound/SoundData.h index 821fb291e..07b13c2d3 100644 --- a/src/modules/sound/SoundData.h +++ b/src/modules/sound/SoundData.h @@ -54,10 +54,11 @@ 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;