From 65bafdc682db46f0f115374ad39f1fbc348832ac Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 20 Aug 2018 11:51:32 +0800 Subject: *update --- src/lua/modules/audio/luaopen_audio.cpp | 109 ++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/lua/modules/audio/luaopen_audio.cpp (limited to 'src/lua/modules/audio/luaopen_audio.cpp') diff --git a/src/lua/modules/audio/luaopen_audio.cpp b/src/lua/modules/audio/luaopen_audio.cpp new file mode 100644 index 0000000..1c44374 --- /dev/null +++ b/src/lua/modules/audio/luaopen_audio.cpp @@ -0,0 +1,109 @@ +#include "lua/modules/luax.h" +#include "lua/common/common.h" +#include "libjin/jin.h" + +namespace jin +{ +namespace lua +{ + + using namespace jin::audio; + using namespace jin::filesystem; + + typedef SDLAudio Audio; + typedef SDLSource Source; + + static int l_init(lua_State* L) + { + Audio::Setting setting; + setting.samplerate = 44100; + setting.samples = 44100; + if (!Audio::get()->init(&setting)) + { + luax_error(L, "could not init audio"); + luax_pushboolean(L, false); + return 1; + } + luax_pushboolean(L, true); + return 1; + } + + static int l_play(lua_State* L) + { + Audio::get()->play(); + return 0; + } + + static int l_stop(lua_State* L) + { + Audio::get()->stop(); + return 0; + } + + static int l_pause(lua_State* L) + { + Audio::get()->pause(); + return 0; + } + + static int l_resume(lua_State* L) + { + Audio::get()->resume(); + return 0; + } + + static int l_setVolume(lua_State* L) + { + float volume = luax_checknumber(L, 1); + Audio::get()->setVolume(volume); + return 0; + } + + static int l_newSource(lua_State* L) + { + Filesystem* fs = Filesystem::get(); + const char* f = luax_checkstring(L, 1); + if (!fs->exists(f)) + { + printf("Error: no such image %s\n", f); + exit(1); + } + Buffer b; + fs->read(f, &b); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy)); + Source* src = Source::createSource(b.data, b.size); + proxy->bind(new Ref(src, JIN_AUDIO_SOURCE)); + return 1; + } + + static int l_destroy(lua_State* L) + { + Audio* audio = Audio::get(); + audio->quit(); + return 0; + } + + static const luaL_Reg f[] = { + { "init", l_init }, + { "play", l_play }, + { "stop", l_stop }, + { "pause", l_pause }, + { "resume", l_resume }, + { "setVolume", l_setVolume }, + { "newSource", l_newSource }, + { "destroy", l_destroy }, + { 0, 0 } + }; + + extern int luaopen_Source(lua_State* L); + + int luaopen_audio(lua_State* L) + { + luaopen_Source(L); + + luax_newlib(L, f); + + return 1; + } +} +} \ No newline at end of file -- cgit v1.1-26-g67d0