1 | /** |
2 | * Copyright (c) 2006-2023 LOVE Development Team |
3 | * |
4 | * This software is provided 'as-is', without any express or implied |
5 | * warranty. In no event will the authors be held liable for any damages |
6 | * arising from the use of this software. |
7 | * |
8 | * Permission is granted to anyone to use this software for any purpose, |
9 | * including commercial applications, and to alter it and redistribute it |
10 | * freely, subject to the following restrictions: |
11 | * |
12 | * 1. The origin of this software must not be misrepresented; you must not |
13 | * claim that you wrote the original software. If you use this software |
14 | * in a product, an acknowledgment in the product documentation would be |
15 | * appreciated but is not required. |
16 | * 2. Altered source versions must be plainly marked as such, and must not be |
17 | * misrepresented as being the original software. |
18 | * 3. This notice may not be removed or altered from any source distribution. |
19 | **/ |
20 | |
21 | #include "wrap_Sound.h" |
22 | |
23 | #include "filesystem/wrap_Filesystem.h" |
24 | |
25 | // Implementations. |
26 | #include "lullaby/Sound.h" |
27 | |
28 | namespace love |
29 | { |
30 | namespace sound |
31 | { |
32 | |
33 | #define instance() (Module::getInstance<Sound>(Module::M_SOUND)) |
34 | |
35 | int w_newDecoder(lua_State *L) |
36 | { |
37 | love::filesystem::FileData *data = love::filesystem::luax_getfiledata(L, 1); |
38 | int bufferSize = (int) luaL_optinteger(L, 2, Decoder::DEFAULT_BUFFER_SIZE); |
39 | |
40 | Decoder *t = nullptr; |
41 | luax_catchexcept(L, |
42 | [&]() { t = instance()->newDecoder(data, bufferSize); }, |
43 | [&](bool) { data->release(); } |
44 | ); |
45 | |
46 | if (t == nullptr) |
47 | return luaL_error(L, "Extension \"%s\" not supported." , data->getExtension().c_str()); |
48 | |
49 | luax_pushtype(L, t); |
50 | t->release(); |
51 | return 1; |
52 | } |
53 | |
54 | int w_newSoundData(lua_State *L) |
55 | { |
56 | SoundData *t = nullptr; |
57 | |
58 | if (lua_isnumber(L, 1)) |
59 | { |
60 | int samples = (int) luaL_checkinteger(L, 1); |
61 | int sampleRate = (int) luaL_optinteger(L, 2, Decoder::DEFAULT_SAMPLE_RATE); |
62 | int bitDepth = (int) luaL_optinteger(L, 3, Decoder::DEFAULT_BIT_DEPTH); |
63 | int channels = (int) luaL_optinteger(L, 4, Decoder::DEFAULT_CHANNELS); |
64 | |
65 | luax_catchexcept(L, [&](){ t = instance()->newSoundData(samples, sampleRate, bitDepth, channels); }); |
66 | } |
67 | // Must be string or decoder. |
68 | else |
69 | { |
70 | // Convert to Decoder, if necessary. |
71 | if (!luax_istype(L, 1, Decoder::type)) |
72 | { |
73 | w_newDecoder(L); |
74 | lua_replace(L, 1); |
75 | } |
76 | |
77 | luax_catchexcept(L, [&](){ t = instance()->newSoundData(luax_checkdecoder(L, 1)); }); |
78 | } |
79 | |
80 | luax_pushtype(L, t); |
81 | t->release(); |
82 | return 1; |
83 | } |
84 | |
85 | // List of functions to wrap. |
86 | static const luaL_Reg functions[] = |
87 | { |
88 | { "newDecoder" , w_newDecoder }, |
89 | { "newSoundData" , w_newSoundData }, |
90 | { 0, 0 } |
91 | }; |
92 | |
93 | static const lua_CFunction types[] = |
94 | { |
95 | luaopen_sounddata, |
96 | luaopen_decoder, |
97 | 0 |
98 | }; |
99 | |
100 | extern "C" int luaopen_love_sound(lua_State *L) |
101 | { |
102 | Sound *instance = instance(); |
103 | if (instance == nullptr) |
104 | { |
105 | luax_catchexcept(L, [&](){ instance = new lullaby::Sound(); }); |
106 | } |
107 | else |
108 | instance->retain(); |
109 | |
110 | |
111 | WrappedModule w; |
112 | w.module = instance; |
113 | w.name = "sound" ; |
114 | w.type = &Sound::type; |
115 | w.functions = functions; |
116 | w.types = types; |
117 | |
118 | return luax_register_module(L, w); |
119 | } |
120 | |
121 | } // sound |
122 | } // love |
123 | |