aboutsummaryrefslogtreecommitdiff
path: root/src/lua/audio/luaopen_audio.cpp
blob: 4f4f955da3ad7230fe452d705a7f60bbb18a428f (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
#include "lua/luax.h"
#include "../luaopen_types.h"
#include "libjin/jin.h"

namespace jin
{
namespace lua
{   
    using namespace jin::filesystem;
    using namespace jin::audio;

    static int l_init(lua_State* L) 
    {
        SDLAudio::Setting setting;
        setting.samplerate = 44100;
        setting.samples = 44100;
        if (! SDLAudio::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)
    {
        SDLAudio::get()->play();
        return 0;
    }

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

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

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

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

    static int l_newSound(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, TYPE_SOURCE, sizeof(Proxy));
        SDLSource* src = SDLSource::createSource(b.data, b.size);
        proxy->bind(src);
        return 1;
    }
    
    static int l_destroy(lua_State* L)
    {
        SDLAudio* audio = SDLAudio::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},
        {"Sound", l_newSound},
        // 
        {"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; 
    }
}
}