wrap_Source.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_isPlaying(lua_State *L)
  189. {
  190. Source *t = luax_checksource(L, 1);
  191. luax_pushboolean(L, !t->isStopped() && !t->isPaused());
  192. return 1;
  193. }
  194. int w_Source_isStatic(lua_State *L)
  195. {
  196. Source *t = luax_checksource(L, 1);
  197. luax_pushboolean(L, t->isStatic());
  198. return 1;
  199. }
  200. int w_Source_setVolumeLimits(lua_State *L)
  201. {
  202. Source *t = luax_checksource(L, 1);
  203. float vmin = (float)luaL_checknumber(L, 2);
  204. float vmax = (float)luaL_checknumber(L, 3);
  205. if (vmin < .0f || vmin > 1.f || vmax < .0f || vmax > 1.f)
  206. return luaL_error(L, "Invalid volume limits: [%f:%f]. Must be in [0:1]", vmin, vmax);
  207. t->setMinVolume(vmin);
  208. t->setMaxVolume(vmin);
  209. return 0;
  210. }
  211. int w_Source_getVolumeLimits(lua_State *L)
  212. {
  213. Source *t = luax_checksource(L, 1);
  214. lua_pushnumber(L, t->getMinVolume());
  215. lua_pushnumber(L, t->getMaxVolume());
  216. return 2;
  217. }
  218. int w_Source_setDistance(lua_State *L)
  219. {
  220. Source *t = luax_checksource(L, 1);
  221. float dref = (float)luaL_checknumber(L, 2);
  222. float dmax = (float)luaL_checknumber(L, 3);
  223. if (dref < .0f || dmax < .0f)
  224. return luaL_error(L, "Invalid distances: %f, %f. Must be > 0", dref, dmax);
  225. t->setReferenceDistance(dref);
  226. t->setMaxDistance(dmax);
  227. return 0;
  228. }
  229. int w_Source_getDistance(lua_State *L)
  230. {
  231. Source *t = luax_checksource(L, 1);
  232. lua_pushnumber(L, t->getReferenceDistance());
  233. lua_pushnumber(L, t->getMaxDistance());
  234. return 2;
  235. }
  236. int w_Source_setRolloff(lua_State *L)
  237. {
  238. Source *t = luax_checksource(L, 1);
  239. float rolloff = (float)luaL_checknumber(L, 2);
  240. if (rolloff < .0f)
  241. return luaL_error(L, "Invalid rolloff: %f. Must be > 0.", rolloff);
  242. t->setRolloffFactor(rolloff);
  243. return 0;
  244. }
  245. int w_Source_getRolloff(lua_State *L)
  246. {
  247. Source *t = luax_checksource(L, 1);
  248. lua_pushnumber(L, t->getRolloffFactor());
  249. return 1;
  250. }
  251. static const luaL_Reg functions[] =
  252. {
  253. { "play", w_Source_play },
  254. { "stop", w_Source_stop },
  255. { "pause", w_Source_pause },
  256. { "resume", w_Source_resume },
  257. { "rewind", w_Source_rewind },
  258. { "setPitch", w_Source_setPitch },
  259. { "getPitch", w_Source_getPitch },
  260. { "setVolume", w_Source_setVolume },
  261. { "getVolume", w_Source_getVolume },
  262. { "seek", w_Source_seek },
  263. { "tell", w_Source_tell },
  264. { "setPosition", w_Source_setPosition },
  265. { "getPosition", w_Source_getPosition },
  266. { "setVelocity", w_Source_setVelocity },
  267. { "getVelocity", w_Source_getVelocity },
  268. { "setDirection", w_Source_setDirection },
  269. { "getDirection", w_Source_getDirection },
  270. { "setLooping", w_Source_setLooping },
  271. { "isLooping", w_Source_isLooping },
  272. { "isStopped", w_Source_isStopped },
  273. { "isPaused", w_Source_isPaused },
  274. { "isPlaying", w_Source_isPlaying },
  275. { "isStatic", w_Source_isStatic },
  276. { "setVolumeLimits", w_Source_setVolumeLimits },
  277. { "getVolumeLimits", w_Source_getVolumeLimits },
  278. { "setDistance", w_Source_setDistance },
  279. { "getDistance", w_Source_setDistance },
  280. { "setRolloff", w_Source_setRolloff},
  281. { "getRolloff", w_Source_getRolloff},
  282. { 0, 0 }
  283. };
  284. extern "C" int luaopen_source(lua_State *L)
  285. {
  286. return luax_register_type(L, "Source", functions);
  287. }
  288. } // audio
  289. } // love