aboutsummaryrefslogtreecommitdiff
path: root/src/lua/modules/audio/je_lua_audio.cpp
blob: 698d88a4d8091588af5f84f68e902fc479d64564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "lua/modules/luax.h"
#include "lua/modules/types.h"
#include "lua/common/je_lua_common.h"
#include "libjin/jin.h"

using namespace JinEngine::Audio;
using namespace JinEngine::Audio::SDL;
using namespace JinEngine::Filesystem;

namespace JinEngine
{
    namespace Lua
    {   

	    typedef SDLAudio Audio;
	    typedef SDLSource Source;

        struct
        {
            bool initialized = false;
        } context;

        LUA_IMPLEMENT int l_init(lua_State* L) 
        {
            if (context.initialized)
            {
                // Already initialized.
                luax_pushboolean(L, true);
                return 1;
            }
		    Audio::Setting setting;
            setting.samplerate = 44100;
            setting.samples = 44100;
            context.initialized = Audio::get()->init(&setting);
            if (!context.initialized)
            {
                luax_error(L, "could not init audio");
                luax_pushboolean(L, false);
                return 1;
            }
            luax_pushboolean(L, true); 
            return 1;
        }

        LUA_IMPLEMENT int l_play(lua_State* L)
        {
            Audio::get()->play();
            return 0;
        }

        LUA_IMPLEMENT int l_stop(lua_State* L)
        {
            Audio::get()->stop();
            return 0;
        }

        LUA_IMPLEMENT int l_pause(lua_State* L)
        {
            Audio::get()->pause();
            return 0;
        }

        LUA_IMPLEMENT int l_resume(lua_State* L)
        {
            Audio::get()->resume();
            return 0;
        }

        LUA_IMPLEMENT int l_setVolume(lua_State* L)
        {
            float volume = luax_checknumber(L, 1);
            Audio::get()->setVolume(volume);
            return 0;
        }

        LUA_IMPLEMENT int l_newSource(lua_State* L)
        {
            AssetDatabase* fs = AssetDatabase::get();
            const char* f = luax_checkstring(L, 1);
            Buffer b;
            try
            {
                if (!fs->exists(f))
                    throw Exception("No such source file %s.", f);
                fs->read(f, b);
            }
            catch (Exception& e)
            {
                error(L, "Failed to read source file %s", f);
                luax_pushnil(L);
                return 1;
            }
            Source* src = Source::createSource((void*)&b, b.size());
            if (src == nullptr)
            {
                error(L, "Failed to decode source file %s", f);
                luax_pushnil(L);
                return 1;
            }
            Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy));
            proxy->bind(new Ref<Source>(src, JIN_AUDIO_SOURCE));
            return 1;
        }
    
        LUA_IMPLEMENT int l_destroy(lua_State* L)
        {
            Audio* audio = Audio::get();
            audio->quit();
            return 0;
        }

        LUA_IMPLEMENT 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           }
        };

        LUA_PORT int luaopen_Source(lua_State* L);

        LUA_EXPORT int luaopen_audio(lua_State* L)
        {
            luaopen_Source(L);

            luax_newlib(L, f);

            return 1; 
        }

    } // namespace Lua
} // namespace JinEngine