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_Decoder.h" |
22 | #include "SoundData.h" |
23 | #include "Sound.h" |
24 | |
25 | #define instance() (Module::getInstance<Sound>(Module::M_SOUND)) |
26 | |
27 | namespace love |
28 | { |
29 | namespace sound |
30 | { |
31 | |
32 | Decoder *luax_checkdecoder(lua_State *L, int idx) |
33 | { |
34 | return luax_checktype<Decoder>(L, idx); |
35 | } |
36 | |
37 | int w_Decoder_clone(lua_State *L) |
38 | { |
39 | Decoder *t = luax_checkdecoder(L, 1); |
40 | Decoder *c = nullptr; |
41 | luax_catchexcept(L, [&]() { c = t->clone(); }); |
42 | luax_pushtype(L, c); |
43 | c->release(); |
44 | return 1; |
45 | } |
46 | |
47 | int w_Decoder_getChannelCount(lua_State *L) |
48 | { |
49 | Decoder *t = luax_checkdecoder(L, 1); |
50 | lua_pushinteger(L, t->getChannelCount()); |
51 | return 1; |
52 | } |
53 | |
54 | int w_Decoder_getBitDepth(lua_State *L) |
55 | { |
56 | Decoder *t = luax_checkdecoder(L, 1); |
57 | lua_pushinteger(L, t->getBitDepth()); |
58 | return 1; |
59 | } |
60 | |
61 | int w_Decoder_getSampleRate(lua_State *L) |
62 | { |
63 | Decoder *t = luax_checkdecoder(L, 1); |
64 | lua_pushinteger(L, t->getSampleRate()); |
65 | return 1; |
66 | } |
67 | |
68 | int w_Decoder_getDuration(lua_State *L) |
69 | { |
70 | Decoder *t = luax_checkdecoder(L, 1); |
71 | lua_pushnumber(L, t->getDuration()); |
72 | return 1; |
73 | } |
74 | |
75 | int w_Decoder_decode(lua_State *L) |
76 | { |
77 | Decoder *t = luax_checkdecoder(L, 1); |
78 | |
79 | int decoded = t->decode(); |
80 | if (decoded > 0) |
81 | { |
82 | luax_catchexcept(L, [&]() { |
83 | SoundData *s = instance()->newSoundData(t->getBuffer(), |
84 | decoded / (t->getBitDepth() / 8 * t->getChannelCount()), |
85 | t->getSampleRate(), t->getBitDepth(), t->getChannelCount()); |
86 | |
87 | luax_pushtype(L, s); |
88 | s->release(); |
89 | }); |
90 | } |
91 | else |
92 | lua_pushnil(L); |
93 | return 1; |
94 | } |
95 | |
96 | int w_Decoder_seek(lua_State *L) |
97 | { |
98 | Decoder *t = luax_checkdecoder(L, 1); |
99 | double offset = luaL_checknumber(L, 2); |
100 | if (offset < 0) |
101 | return luaL_argerror(L, 2, "can't seek to a negative position" ); |
102 | else if (offset == 0) |
103 | t->rewind(); |
104 | else |
105 | t->seek(offset); |
106 | return 0; |
107 | } |
108 | |
109 | int w_Decoder_getChannels(lua_State *L) |
110 | { |
111 | luax_markdeprecated(L, "Decoder:getChannels" , API_METHOD, DEPRECATED_RENAMED, "Decoder:getChannelCount" ); |
112 | return w_Decoder_getChannelCount(L); |
113 | } |
114 | |
115 | static const luaL_Reg w_Decoder_functions[] = |
116 | { |
117 | { "clone" , w_Decoder_clone }, |
118 | { "getChannelCount" , w_Decoder_getChannelCount }, |
119 | { "getBitDepth" , w_Decoder_getBitDepth }, |
120 | { "getSampleRate" , w_Decoder_getSampleRate }, |
121 | { "getDuration" , w_Decoder_getDuration }, |
122 | { "decode" , w_Decoder_decode }, |
123 | { "seek" , w_Decoder_seek }, |
124 | |
125 | // Deprecated |
126 | { "getChannels" , w_Decoder_getChannels }, |
127 | |
128 | { 0, 0 } |
129 | }; |
130 | |
131 | extern "C" int luaopen_decoder(lua_State *L) |
132 | { |
133 | return luax_register_type(L, &Decoder::type, w_Decoder_functions, nullptr); |
134 | } |
135 | |
136 | } // sound |
137 | } // love |
138 | |