wrap_Source.cpp 7.4 KB

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