1 /**
2 * Copyright (c) 2006-2012 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_Source.h"
22 
23 namespace love
24 {
25 namespace audio
26 {
luax_checksource(lua_State * L,int idx)27 	Source * luax_checksource(lua_State * L, int idx)
28 	{
29 		return luax_checktype<Source>(L, idx, "Source", AUDIO_SOURCE_T);
30 	}
31 
w_Source_play(lua_State * L)32 	int w_Source_play(lua_State * L)
33 	{
34 		Source * t = luax_checksource(L, 1);
35 		t->play();
36 		return 0;
37 	}
38 
w_Source_stop(lua_State * L)39 	int w_Source_stop(lua_State * L)
40 	{
41 		Source * t = luax_checksource(L, 1);
42 		t->stop();
43 		return 0;
44 	}
45 
w_Source_pause(lua_State * L)46 	int w_Source_pause(lua_State * L)
47 	{
48 		Source * t = luax_checksource(L, 1);
49 		t->pause();
50 		return 0;
51 	}
52 
w_Source_resume(lua_State * L)53 	int w_Source_resume(lua_State * L)
54 	{
55 		Source * t = luax_checksource(L, 1);
56 		t->resume();
57 		return 0;
58 	}
59 
w_Source_rewind(lua_State * L)60 	int w_Source_rewind(lua_State * L)
61 	{
62 		Source * t = luax_checksource(L, 1);
63 		t->rewind();
64 		return 0;
65 	}
66 
w_Source_setPitch(lua_State * L)67 	int w_Source_setPitch(lua_State * L)
68 	{
69 		Source * t = luax_checksource(L, 1);
70 		float p = (float)luaL_checknumber(L, 2);
71 		t->setPitch(p);
72 		return 0;
73 	}
74 
w_Source_getPitch(lua_State * L)75 	int w_Source_getPitch(lua_State * L)
76 	{
77 		Source * t = luax_checksource(L, 1);
78 		lua_pushnumber(L, t->getPitch());
79 		return 1;
80 	}
81 
w_Source_setVolume(lua_State * L)82 	int w_Source_setVolume(lua_State * L)
83 	{
84 		Source * t = luax_checksource(L, 1);
85 		float p = (float)luaL_checknumber(L, 2);
86 		t->setVolume(p);
87 		return 0;
88 	}
89 
w_Source_getVolume(lua_State * L)90 	int w_Source_getVolume(lua_State * L)
91 	{
92 		Source * t = luax_checksource(L, 1);
93 		lua_pushnumber(L, t->getVolume());
94 		return 1;
95 	}
96 
w_Source_seek(lua_State * L)97 	int w_Source_seek(lua_State * L)
98 	{
99 		Source * t = luax_checksource(L, 1);
100 		float offset = (float)luaL_checknumber(L, 2);
101 		const char * unit = luaL_optstring(L, 3, "seconds");
102 		Source::Unit u;
103 		t->getConstant(unit, u);
104 		t->seek(offset, u);
105 		return 0;
106 	}
107 
w_Source_tell(lua_State * L)108 	int w_Source_tell(lua_State * L)
109 	{
110 		Source * t = luax_checksource(L, 1);
111 		const char * unit = luaL_optstring(L, 2, "seconds");
112 		Source::Unit u;
113 		t->getConstant(unit, u);
114 		lua_pushnumber(L, t->tell(u));
115 		return 1;
116 	}
117 
w_Source_setPosition(lua_State * L)118 	int w_Source_setPosition(lua_State * L)
119 	{
120 		Source * t = luax_checksource(L, 1);
121 		float v[3];
122 		v[0] = (float)luaL_checknumber(L, 2);
123 		v[1] = (float)luaL_checknumber(L, 3);
124 		v[2] = (float)luaL_checknumber(L, 4);
125 		t->setPosition(v);
126 		return 0;
127 	}
128 
w_Source_getPosition(lua_State * L)129 	int w_Source_getPosition(lua_State * L)
130 	{
131 		Source * t = luax_checksource(L, 1);
132 		float v[3];
133 		t->getPosition(v);
134 		lua_pushnumber(L, v[0]);
135 		lua_pushnumber(L, v[1]);
136 		lua_pushnumber(L, v[2]);
137 		return 3;
138 	}
139 
w_Source_setVelocity(lua_State * L)140 	int w_Source_setVelocity(lua_State * L)
141 	{
142 		Source * t = luax_checksource(L, 1);
143 		float v[3];
144 		v[0] = (float)luaL_checknumber(L, 2);
145 		v[1] = (float)luaL_checknumber(L, 3);
146 		v[2] = (float)luaL_checknumber(L, 4);
147 		t->setVelocity(v);
148 		return 0;
149 	}
150 
w_Source_getVelocity(lua_State * L)151 	int w_Source_getVelocity(lua_State * L)
152 	{
153 		Source * t = luax_checksource(L, 1);
154 		float v[3];
155 		t->getVelocity(v);
156 		lua_pushnumber(L, v[0]);
157 		lua_pushnumber(L, v[1]);
158 		lua_pushnumber(L, v[2]);
159 		return 3;
160 	}
161 
w_Source_setDirection(lua_State * L)162 	int w_Source_setDirection(lua_State * L)
163 	{
164 		Source * t = luax_checksource(L, 1);
165 		float v[3];
166 		v[0] = (float)luaL_checknumber(L, 2);
167 		v[1] = (float)luaL_checknumber(L, 3);
168 		v[2] = (float)luaL_checknumber(L, 4);
169 		t->setDirection(v);
170 		return 0;
171 	}
172 
w_Source_getDirection(lua_State * L)173 	int w_Source_getDirection(lua_State * L)
174 	{
175 		Source * t = luax_checksource(L, 1);
176 		float v[3];
177 		t->getDirection(v);
178 		lua_pushnumber(L, v[0]);
179 		lua_pushnumber(L, v[1]);
180 		lua_pushnumber(L, v[2]);
181 		return 3;
182 	}
183 
w_Source_setLooping(lua_State * L)184 	int w_Source_setLooping(lua_State * L)
185 	{
186 		Source * t = luax_checksource(L, 1);
187 		t->setLooping(luax_toboolean(L, 2));
188 		return 0;
189 	}
190 
w_Source_isLooping(lua_State * L)191 	int w_Source_isLooping(lua_State * L)
192 	{
193 		Source * t = luax_checksource(L, 1);
194 		luax_pushboolean(L, t->isLooping());
195 		return 1;
196 	}
197 
w_Source_isStopped(lua_State * L)198 	int w_Source_isStopped(lua_State * L)
199 	{
200 		Source * t = luax_checksource(L, 1);
201 		luax_pushboolean(L, t->isStopped());
202 		return 1;
203 	}
204 
w_Source_isPaused(lua_State * L)205 	int w_Source_isPaused(lua_State * L)
206 	{
207 		Source * t = luax_checksource(L, 1);
208 		luax_pushboolean(L, t->isPaused());
209 		return 1;
210 	}
211 
w_Source_isStatic(lua_State * L)212 	int w_Source_isStatic(lua_State * L)
213 	{
214 		Source * t = luax_checksource(L, 1);
215 		luax_pushboolean(L, t->isStatic());
216 		return 1;
217 	}
218 
w_Source_setVolumeLimits(lua_State * L)219 	int w_Source_setVolumeLimits(lua_State * L)
220 	{
221 		Source * t = luax_checksource(L, 1);
222 		float vmin = (float)luaL_checknumber(L, 2);
223 		float vmax = (float)luaL_checknumber(L, 3);
224 		if (vmin < .0f || vmin > 1.f || vmax < .0f || vmax > 1.f)
225 			return luaL_error(L, "Invalid volume limits: [%f:%f]. Must be in [0:1]", vmin, vmax);
226 		t->setMinVolume(vmin);
227 		t->setMaxVolume(vmin);
228 		return 0;
229 	}
230 
w_Source_getVolumeLimits(lua_State * L)231 	int w_Source_getVolumeLimits(lua_State * L)
232 	{
233 		Source * t = luax_checksource(L, 1);
234 		lua_pushnumber(L, t->getMinVolume());
235 		lua_pushnumber(L, t->getMaxVolume());
236 		return 2;
237 	}
238 
w_Source_setDistance(lua_State * L)239 	int w_Source_setDistance(lua_State * L)
240 	{
241 		Source * t = luax_checksource(L, 1);
242 		float dref = (float)luaL_checknumber(L, 2);
243 		float dmax = (float)luaL_checknumber(L, 3);
244 		if (dref < .0f || dmax < .0f)
245 			return luaL_error(L, "Invalid distances: %f, %f. Must be > 0", dref, dmax);
246 		t->setReferenceDistance(dref);
247 		t->setMaxDistance(dmax);
248 		return 0;
249 	}
250 
w_Source_getDistance(lua_State * L)251 	int w_Source_getDistance(lua_State * L)
252 	{
253 		Source * t = luax_checksource(L, 1);
254 		lua_pushnumber(L, t->getReferenceDistance());
255 		lua_pushnumber(L, t->getMaxDistance());
256 		return 2;
257 	}
258 
w_Source_setRolloff(lua_State * L)259 	int w_Source_setRolloff(lua_State * L)
260 	{
261 		Source * t = luax_checksource(L, 1);
262 		float rolloff = (float)luaL_checknumber(L, 2);
263 		if (rolloff < .0f)
264 			return luaL_error(L, "Invalid rolloff: %f. Must be > 0.", rolloff);
265 		t->setRolloffFactor(rolloff);
266 		return 0;
267 	}
268 
w_Source_getRolloff(lua_State * L)269 	int w_Source_getRolloff(lua_State * L)
270 	{
271 		Source * t = luax_checksource(L, 1);
272 		lua_pushnumber(L, t->getRolloffFactor());
273 		return 1;
274 	}
275 
276 	static const luaL_Reg functions[] = {
277 		{ "play", w_Source_play },
278 		{ "stop", w_Source_stop },
279 		{ "pause", w_Source_pause },
280 		{ "resume", w_Source_resume },
281 		{ "rewind", w_Source_rewind },
282 
283 		{ "setPitch", w_Source_setPitch },
284 		{ "getPitch", w_Source_getPitch },
285 		{ "setVolume", w_Source_setVolume },
286 		{ "getVolume", w_Source_getVolume },
287 		{ "seek", w_Source_seek },
288 		{ "tell", w_Source_tell },
289 		{ "setPosition", w_Source_setPosition },
290 		{ "getPosition", w_Source_getPosition },
291 		{ "setVelocity", w_Source_setVelocity },
292 		{ "getVelocity", w_Source_getVelocity },
293 		{ "setDirection", w_Source_setDirection },
294 		{ "getDirection", w_Source_getDirection },
295 
296 		{ "setLooping", w_Source_setLooping },
297 		{ "isLooping", w_Source_isLooping },
298 		{ "isStopped", w_Source_isStopped },
299 		{ "isPaused", w_Source_isPaused },
300 		{ "isStatic", w_Source_isStatic },
301 
302 		{ "setVolumeLimits", w_Source_setVolumeLimits },
303 		{ "getVolumeLimits", w_Source_getVolumeLimits },
304 		{ "setDistance", w_Source_setDistance },
305 		{ "getDistance", w_Source_setDistance },
306 		{ "setRolloff", w_Source_setRolloff},
307 		{ "getRolloff", w_Source_getRolloff},
308 
309 		{ 0, 0 }
310 	};
311 
luaopen_source(lua_State * L)312 	extern "C" int luaopen_source(lua_State * L)
313 	{
314 		return luax_register_type(L, "Source", functions);
315 	}
316 
317 } // audio
318 } // love
319