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_SoundData.h"
22
23#include "data/wrap_Data.h"
24
25// Shove the wrap_SoundData.lua code directly into a raw string literal.
26static const char sounddata_lua[] =
27#include "wrap_SoundData.lua"
28;
29
30namespace love
31{
32namespace sound
33{
34
35/**
36 * NOTE: Additional wrapper code is in wrap_SoundData.lua. Be sure to keep it
37 * in sync with any changes made to this file!
38 **/
39
40SoundData *luax_checksounddata(lua_State *L, int idx)
41{
42 return luax_checktype<SoundData>(L, idx);
43}
44
45int w_SoundData_clone(lua_State *L)
46{
47 SoundData *t = luax_checksounddata(L, 1), *c = nullptr;
48 luax_catchexcept(L, [&](){ c = t->clone(); });
49 luax_pushtype(L, c);
50 c->release();
51 return 1;
52}
53
54int w_SoundData_getChannelCount(lua_State *L)
55{
56 SoundData *t = luax_checksounddata(L, 1);
57 lua_pushinteger(L, t->getChannelCount());
58 return 1;
59}
60
61int w_SoundData_getBitDepth(lua_State *L)
62{
63 SoundData *t = luax_checksounddata(L, 1);
64 lua_pushinteger(L, t->getBitDepth());
65 return 1;
66}
67
68int w_SoundData_getSampleRate(lua_State *L)
69{
70 SoundData *t = luax_checksounddata(L, 1);
71 lua_pushinteger(L, t->getSampleRate());
72 return 1;
73}
74
75int w_SoundData_getSampleCount(lua_State *L)
76{
77 SoundData *t = luax_checksounddata(L, 1);
78 lua_pushinteger(L, t->getSampleCount());
79 return 1;
80}
81
82int w_SoundData_getDuration(lua_State *L)
83{
84 SoundData *t = luax_checksounddata(L, 1);
85 lua_pushnumber(L, t->getDuration());
86 return 1;
87}
88
89int w_SoundData_setSample(lua_State *L)
90{
91 SoundData *sd = luax_checksounddata(L, 1);
92 int i = (int) luaL_checkinteger(L, 2);
93
94 if (lua_gettop(L) > 3)
95 {
96 int channel = luaL_checkinteger(L, 3);
97 float sample = (float) luaL_checknumber(L, 4);
98 luax_catchexcept(L, [&](){ sd->setSample(i, channel, sample); });
99 }
100 else
101 {
102 float sample = (float) luaL_checknumber(L, 3);
103 luax_catchexcept(L, [&](){ sd->setSample(i, sample); });
104 }
105
106 return 0;
107}
108
109int w_SoundData_getSample(lua_State *L)
110{
111 SoundData *sd = luax_checksounddata(L, 1);
112 int i = (int) luaL_checkinteger(L, 2);
113
114 if (lua_gettop(L) > 2)
115 {
116 int channel = luaL_checkinteger(L, 3);
117 luax_catchexcept(L, [&](){ lua_pushnumber(L, sd->getSample(i, channel)); });
118 }
119 else
120 luax_catchexcept(L, [&](){ lua_pushnumber(L, sd->getSample(i)); });
121 return 1;
122}
123
124int w_SoundData_getChannels(lua_State *L)
125{
126 luax_markdeprecated(L, "SoundData:getChannels", API_METHOD, DEPRECATED_RENAMED, "SoundData:getChannelCount");
127 return w_SoundData_getChannelCount(L);
128}
129
130static const luaL_Reg w_SoundData_functions[] =
131{
132 { "clone", w_SoundData_clone },
133 { "getChannelCount", w_SoundData_getChannelCount },
134 { "getBitDepth", w_SoundData_getBitDepth },
135 { "getSampleRate", w_SoundData_getSampleRate },
136 { "getSampleCount", w_SoundData_getSampleCount },
137 { "getDuration", w_SoundData_getDuration },
138 { "setSample", w_SoundData_setSample },
139 { "getSample", w_SoundData_getSample },
140
141 // Deprecated
142 { "getChannels", w_SoundData_getChannels },
143
144 { 0, 0 }
145};
146
147extern "C" int luaopen_sounddata(lua_State *L)
148{
149 int ret = luax_register_type(L, &SoundData::type, data::w_Data_functions, w_SoundData_functions, nullptr);
150
151 love::data::luax_rundatawrapper(L, SoundData::type);
152 luax_runwrapper(L, sounddata_lua, sizeof(sounddata_lua), "SoundData.lua", SoundData::type, nullptr);
153
154 return ret;
155}
156
157} // sound
158} // love
159