wrap_Source.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**
  2. * Copyright (c) 2006-2014 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. #include <limits>
  21. #include "wrap_Source.h"
  22. namespace love
  23. {
  24. namespace audio
  25. {
  26. Source *luax_checksource(lua_State *L, int idx)
  27. {
  28. return luax_checktype<Source>(L, idx, "Source", AUDIO_SOURCE_T);
  29. }
  30. int w_Source_clone(lua_State *L)
  31. {
  32. Source *t = luax_checksource(L, 1);
  33. Source *clone = nullptr;
  34. EXCEPT_GUARD(clone = t->clone();)
  35. luax_pushtype(L, "Source", AUDIO_SOURCE_T, clone);
  36. return 1;
  37. }
  38. int w_Source_play(lua_State *L)
  39. {
  40. Source *t = luax_checksource(L, 1);
  41. t->play();
  42. return 0;
  43. }
  44. int w_Source_stop(lua_State *L)
  45. {
  46. Source *t = luax_checksource(L, 1);
  47. t->stop();
  48. return 0;
  49. }
  50. int w_Source_pause(lua_State *L)
  51. {
  52. Source *t = luax_checksource(L, 1);
  53. t->pause();
  54. return 0;
  55. }
  56. int w_Source_resume(lua_State *L)
  57. {
  58. Source *t = luax_checksource(L, 1);
  59. t->resume();
  60. return 0;
  61. }
  62. int w_Source_rewind(lua_State *L)
  63. {
  64. Source *t = luax_checksource(L, 1);
  65. t->rewind();
  66. return 0;
  67. }
  68. int w_Source_setPitch(lua_State *L)
  69. {
  70. Source *t = luax_checksource(L, 1);
  71. float p = (float)luaL_checknumber(L, 2);
  72. if (p > std::numeric_limits<lua_Number>::max() ||
  73. p < std::numeric_limits<lua_Number>::min() ||
  74. p != p)
  75. return luaL_error(L, "Pitch has to be finite and not NaN.");
  76. t->setPitch(p);
  77. return 0;
  78. }
  79. int w_Source_getPitch(lua_State *L)
  80. {
  81. Source *t = luax_checksource(L, 1);
  82. lua_pushnumber(L, t->getPitch());
  83. return 1;
  84. }
  85. int w_Source_setVolume(lua_State *L)
  86. {
  87. Source *t = luax_checksource(L, 1);
  88. float p = (float)luaL_checknumber(L, 2);
  89. t->setVolume(p);
  90. return 0;
  91. }
  92. int w_Source_getVolume(lua_State *L)
  93. {
  94. Source *t = luax_checksource(L, 1);
  95. lua_pushnumber(L, t->getVolume());
  96. return 1;
  97. }
  98. int w_Source_seek(lua_State *L)
  99. {
  100. Source *t = luax_checksource(L, 1);
  101. float offset = (float)luaL_checknumber(L, 2);
  102. if (offset < 0)
  103. return luaL_argerror(L, 2, "can't seek to a negative position");
  104. Source::Unit u = Source::UNIT_SECONDS;
  105. const char *unit = lua_isnoneornil(L, 3) ? 0 : lua_tostring(L, 3);
  106. if (unit && !t->getConstant(unit, u))
  107. return luaL_error(L, "Invalid Source time unit: %s", unit);
  108. t->seek(offset, u);
  109. return 0;
  110. }
  111. int w_Source_tell(lua_State *L)
  112. {
  113. Source *t = luax_checksource(L, 1);
  114. Source::Unit u = Source::UNIT_SECONDS;
  115. const char *unit = lua_isnoneornil(L, 2) ? 0 : lua_tostring(L, 2);
  116. if (unit && !t->getConstant(unit, u))
  117. return luaL_error(L, "Invalid Source time unit: %s", unit);
  118. lua_pushnumber(L, t->tell(u));
  119. return 1;
  120. }
  121. int w_Source_setPosition(lua_State *L)
  122. {
  123. Source *t = luax_checksource(L, 1);
  124. float v[3];
  125. v[0] = (float)luaL_checknumber(L, 2);
  126. v[1] = (float)luaL_checknumber(L, 3);
  127. v[2] = (float)luaL_optnumber(L, 4, 0);
  128. t->setPosition(v);
  129. return 0;
  130. }
  131. int w_Source_getPosition(lua_State *L)
  132. {
  133. Source *t = luax_checksource(L, 1);
  134. float v[3];
  135. t->getPosition(v);
  136. lua_pushnumber(L, v[0]);
  137. lua_pushnumber(L, v[1]);
  138. lua_pushnumber(L, v[2]);
  139. return 3;
  140. }
  141. int w_Source_setVelocity(lua_State *L)
  142. {
  143. Source *t = luax_checksource(L, 1);
  144. float v[3];
  145. v[0] = (float)luaL_checknumber(L, 2);
  146. v[1] = (float)luaL_checknumber(L, 3);
  147. v[2] = (float)luaL_optnumber(L, 4, 0);
  148. t->setVelocity(v);
  149. return 0;
  150. }
  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. int w_Source_setDirection(lua_State *L)
  162. {
  163. Source *t = luax_checksource(L, 1);
  164. float v[3];
  165. v[0] = (float)luaL_checknumber(L, 2);
  166. v[1] = (float)luaL_checknumber(L, 3);
  167. v[2] = (float)luaL_optnumber(L, 4, 0);
  168. t->setDirection(v);
  169. return 0;
  170. }
  171. int w_Source_getDirection(lua_State *L)
  172. {
  173. Source *t = luax_checksource(L, 1);
  174. float v[3];
  175. t->getDirection(v);
  176. lua_pushnumber(L, v[0]);
  177. lua_pushnumber(L, v[1]);
  178. lua_pushnumber(L, v[2]);
  179. return 3;
  180. }
  181. int w_Source_setCone(lua_State *L)
  182. {
  183. Source *t = luax_checksource(L, 1);
  184. float innerAngle = (float) luaL_checknumber(L, 2);
  185. float outerAngle = (float) luaL_checknumber(L, 3);
  186. float outerVolume = (float) luaL_optnumber(L, 4, 0.0);
  187. t->setCone(innerAngle, outerAngle, outerVolume);
  188. return 0;
  189. }
  190. int w_Source_getCone(lua_State *L)
  191. {
  192. Source *t = luax_checksource(L, 1);
  193. float innerAngle, outerAngle, outerVolume;
  194. t->getCone(innerAngle, outerAngle, outerVolume);
  195. lua_pushnumber(L, innerAngle);
  196. lua_pushnumber(L, outerAngle);
  197. lua_pushnumber(L, outerVolume);
  198. return 3;
  199. }
  200. int w_Source_setRelative(lua_State *L)
  201. {
  202. Source *t = luax_checksource(L, 1);
  203. t->setRelative(luax_toboolean(L, 2));
  204. return 0;
  205. }
  206. int w_Source_isRelative(lua_State *L)
  207. {
  208. Source *t = luax_checksource(L, 1);
  209. luax_pushboolean(L, t->isRelative());
  210. return 1;
  211. }
  212. int w_Source_setLooping(lua_State *L)
  213. {
  214. Source *t = luax_checksource(L, 1);
  215. t->setLooping(luax_toboolean(L, 2));
  216. return 0;
  217. }
  218. int w_Source_isLooping(lua_State *L)
  219. {
  220. Source *t = luax_checksource(L, 1);
  221. luax_pushboolean(L, t->isLooping());
  222. return 1;
  223. }
  224. int w_Source_isStopped(lua_State *L)
  225. {
  226. Source *t = luax_checksource(L, 1);
  227. luax_pushboolean(L, t->isStopped());
  228. return 1;
  229. }
  230. int w_Source_isPaused(lua_State *L)
  231. {
  232. Source *t = luax_checksource(L, 1);
  233. luax_pushboolean(L, t->isPaused());
  234. return 1;
  235. }
  236. int w_Source_isPlaying(lua_State *L)
  237. {
  238. Source *t = luax_checksource(L, 1);
  239. luax_pushboolean(L, !t->isStopped() && !t->isPaused());
  240. return 1;
  241. }
  242. int w_Source_isStatic(lua_State *L)
  243. {
  244. Source *t = luax_checksource(L, 1);
  245. luax_pushboolean(L, t->isStatic());
  246. return 1;
  247. }
  248. int w_Source_setVolumeLimits(lua_State *L)
  249. {
  250. Source *t = luax_checksource(L, 1);
  251. float vmin = (float)luaL_checknumber(L, 2);
  252. float vmax = (float)luaL_checknumber(L, 3);
  253. if (vmin < .0f || vmin > 1.f || vmax < .0f || vmax > 1.f)
  254. return luaL_error(L, "Invalid volume limits: [%f:%f]. Must be in [0:1]", vmin, vmax);
  255. t->setMinVolume(vmin);
  256. t->setMaxVolume(vmax);
  257. return 0;
  258. }
  259. int w_Source_getVolumeLimits(lua_State *L)
  260. {
  261. Source *t = luax_checksource(L, 1);
  262. lua_pushnumber(L, t->getMinVolume());
  263. lua_pushnumber(L, t->getMaxVolume());
  264. return 2;
  265. }
  266. int w_Source_setAttenuationDistances(lua_State *L)
  267. {
  268. Source *t = luax_checksource(L, 1);
  269. float dref = (float)luaL_checknumber(L, 2);
  270. float dmax = (float)luaL_checknumber(L, 3);
  271. if (dref < .0f || dmax < .0f)
  272. return luaL_error(L, "Invalid distances: %f, %f. Must be > 0", dref, dmax);
  273. t->setReferenceDistance(dref);
  274. t->setMaxDistance(dmax);
  275. return 0;
  276. }
  277. int w_Source_getAttenuationDistances(lua_State *L)
  278. {
  279. Source *t = luax_checksource(L, 1);
  280. lua_pushnumber(L, t->getReferenceDistance());
  281. lua_pushnumber(L, t->getMaxDistance());
  282. return 2;
  283. }
  284. int w_Source_setRolloff(lua_State *L)
  285. {
  286. Source *t = luax_checksource(L, 1);
  287. float rolloff = (float)luaL_checknumber(L, 2);
  288. if (rolloff < .0f)
  289. return luaL_error(L, "Invalid rolloff: %f. Must be > 0.", rolloff);
  290. t->setRolloffFactor(rolloff);
  291. return 0;
  292. }
  293. int w_Source_getRolloff(lua_State *L)
  294. {
  295. Source *t = luax_checksource(L, 1);
  296. lua_pushnumber(L, t->getRolloffFactor());
  297. return 1;
  298. }
  299. int w_Source_getChannels(lua_State *L)
  300. {
  301. Source *t = luax_checksource(L, 1);
  302. lua_pushinteger(L, t->getChannels());
  303. return 1;
  304. }
  305. static const luaL_Reg functions[] =
  306. {
  307. { "clone", w_Source_clone },
  308. { "play", w_Source_play },
  309. { "stop", w_Source_stop },
  310. { "pause", w_Source_pause },
  311. { "resume", w_Source_resume },
  312. { "rewind", w_Source_rewind },
  313. { "setPitch", w_Source_setPitch },
  314. { "getPitch", w_Source_getPitch },
  315. { "setVolume", w_Source_setVolume },
  316. { "getVolume", w_Source_getVolume },
  317. { "seek", w_Source_seek },
  318. { "tell", w_Source_tell },
  319. { "setPosition", w_Source_setPosition },
  320. { "getPosition", w_Source_getPosition },
  321. { "setVelocity", w_Source_setVelocity },
  322. { "getVelocity", w_Source_getVelocity },
  323. { "setDirection", w_Source_setDirection },
  324. { "getDirection", w_Source_getDirection },
  325. { "setCone", w_Source_setCone },
  326. { "getCone", w_Source_getCone },
  327. { "setRelative", w_Source_setRelative },
  328. { "isRelative", w_Source_isRelative },
  329. { "setLooping", w_Source_setLooping },
  330. { "isLooping", w_Source_isLooping },
  331. { "isStopped", w_Source_isStopped },
  332. { "isPaused", w_Source_isPaused },
  333. { "isPlaying", w_Source_isPlaying },
  334. { "isStatic", w_Source_isStatic },
  335. { "setVolumeLimits", w_Source_setVolumeLimits },
  336. { "getVolumeLimits", w_Source_getVolumeLimits },
  337. { "setAttenuationDistances", w_Source_setAttenuationDistances },
  338. { "getAttenuationDistances", w_Source_getAttenuationDistances },
  339. { "setRolloff", w_Source_setRolloff},
  340. { "getRolloff", w_Source_getRolloff},
  341. { "getChannels", w_Source_getChannels },
  342. { 0, 0 }
  343. };
  344. extern "C" int luaopen_source(lua_State *L)
  345. {
  346. return luax_register_type(L, "Source", functions);
  347. }
  348. } // audio
  349. } // love