wrap_Audio.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. // LOVE
  21. #include "wrap_Audio.h"
  22. #include "openal/Audio.h"
  23. #include "null/Audio.h"
  24. #include "common/runtime.h"
  25. namespace love
  26. {
  27. namespace audio
  28. {
  29. static Audio *instance = 0;
  30. int w_getSourceCount(lua_State *L)
  31. {
  32. lua_pushinteger(L, instance->getSourceCount());
  33. return 1;
  34. }
  35. int w_newSource(lua_State *L)
  36. {
  37. if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T))
  38. luax_convobj(L, 1, "filesystem", "newFileData");
  39. if (luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
  40. luax_convobj(L, 1, "sound", "newDecoder");
  41. Source::Type stype = Source::TYPE_STREAM;
  42. const char *stypestr = lua_isnoneornil(L, 2) ? 0 : lua_tostring(L, 2);
  43. if (stypestr && !Source::getConstant(stypestr, stype))
  44. return luaL_error(L, "Invalid source type: %s", stypestr);
  45. if (stype == Source::TYPE_STATIC && luax_istype(L, 1, SOUND_DECODER_T))
  46. luax_convobj(L, 1, "sound", "newSoundData");
  47. Source *t = 0;
  48. if (luax_istype(L, 1, SOUND_SOUND_DATA_T))
  49. t = instance->newSource(luax_totype<love::sound::SoundData>(L, 1, "SoundData", SOUND_SOUND_DATA_T));
  50. else if (luax_istype(L, 1, SOUND_DECODER_T))
  51. t = instance->newSource(luax_totype<love::sound::Decoder>(L, 1, "Decoder", SOUND_DECODER_T));
  52. if (t)
  53. {
  54. luax_pushtype(L, "Source", AUDIO_SOURCE_T, t);
  55. return 1;
  56. }
  57. else
  58. return luax_typerror(L, 1, "Decoder or SoundData");
  59. }
  60. int w_play(lua_State *L)
  61. {
  62. Source *s = luax_checksource(L, 1);
  63. instance->play(s);
  64. return 0;
  65. }
  66. int w_stop(lua_State *L)
  67. {
  68. if (lua_gettop(L) == 0)
  69. {
  70. instance->stop();
  71. }
  72. else
  73. {
  74. Source *s = luax_checksource(L, 1);
  75. s->stop();
  76. }
  77. return 0;
  78. }
  79. int w_pause(lua_State *L)
  80. {
  81. if (lua_gettop(L) == 0)
  82. {
  83. instance->pause();
  84. }
  85. else
  86. {
  87. Source *s = luax_checksource(L, 1);
  88. s->pause();
  89. }
  90. return 0;
  91. }
  92. int w_resume(lua_State *L)
  93. {
  94. if (lua_gettop(L) == 0)
  95. {
  96. instance->resume();
  97. }
  98. else
  99. {
  100. Source *s = luax_checksource(L, 1);
  101. s->resume();
  102. }
  103. return 0;
  104. }
  105. int w_rewind(lua_State *L)
  106. {
  107. if (lua_gettop(L) == 0)
  108. {
  109. instance->rewind();
  110. }
  111. else
  112. {
  113. Source *s = luax_checksource(L, 1);
  114. s->rewind();
  115. }
  116. return 0;
  117. }
  118. int w_setVolume(lua_State *L)
  119. {
  120. float v = (float)luaL_checknumber(L, 1);
  121. instance->setVolume(v);
  122. return 0;
  123. }
  124. int w_getVolume(lua_State *L)
  125. {
  126. lua_pushnumber(L, instance->getVolume());
  127. return 1;
  128. }
  129. int w_setPosition(lua_State *L)
  130. {
  131. float v[3];
  132. v[0] = (float)luaL_checknumber(L, 1);
  133. v[1] = (float)luaL_checknumber(L, 2);
  134. v[2] = (float)luaL_optnumber(L, 3, 0);
  135. instance->setPosition(v);
  136. return 0;
  137. }
  138. int w_getPosition(lua_State *L)
  139. {
  140. float v[3];
  141. instance->getPosition(v);
  142. lua_pushnumber(L, v[0]);
  143. lua_pushnumber(L, v[1]);
  144. lua_pushnumber(L, v[2]);
  145. return 3;
  146. }
  147. int w_setOrientation(lua_State *L)
  148. {
  149. float v[6];
  150. v[0] = (float)luaL_checknumber(L, 1);
  151. v[1] = (float)luaL_checknumber(L, 2);
  152. v[2] = (float)luaL_checknumber(L, 3);
  153. v[3] = (float)luaL_checknumber(L, 4);
  154. v[4] = (float)luaL_checknumber(L, 5);
  155. v[5] = (float)luaL_checknumber(L, 6);
  156. instance->setOrientation(v);
  157. return 0;
  158. }
  159. int w_getOrientation(lua_State *L)
  160. {
  161. float v[6];
  162. instance->getOrientation(v);
  163. lua_pushnumber(L, v[0]);
  164. lua_pushnumber(L, v[1]);
  165. lua_pushnumber(L, v[2]);
  166. lua_pushnumber(L, v[3]);
  167. lua_pushnumber(L, v[4]);
  168. lua_pushnumber(L, v[5]);
  169. return 6;
  170. }
  171. int w_setVelocity(lua_State *L)
  172. {
  173. float v[3];
  174. v[0] = (float)luaL_checknumber(L, 1);
  175. v[1] = (float)luaL_checknumber(L, 2);
  176. v[2] = (float)luaL_optnumber(L, 3, 0);
  177. instance->setVelocity(v);
  178. return 0;
  179. }
  180. int w_getVelocity(lua_State *L)
  181. {
  182. float v[3];
  183. instance->getVelocity(v);
  184. lua_pushnumber(L, v[0]);
  185. lua_pushnumber(L, v[1]);
  186. lua_pushnumber(L, v[2]);
  187. return 3;
  188. }
  189. int w_record(lua_State *)
  190. {
  191. instance->record();
  192. return 0;
  193. }
  194. int w_getRecordedData(lua_State *L)
  195. {
  196. love::sound::SoundData *sd = instance->getRecordedData();
  197. if (!sd)
  198. lua_pushnil(L);
  199. else
  200. luax_pushtype(L, "SoundData", SOUND_SOUND_DATA_T, sd);
  201. return 1;
  202. }
  203. int w_stopRecording(lua_State *L)
  204. {
  205. if (luax_optboolean(L, 1, true))
  206. {
  207. love::sound::SoundData *sd = instance->stopRecording(true);
  208. if (!sd) lua_pushnil(L);
  209. else luax_pushtype(L, "SoundData", SOUND_SOUND_DATA_T, sd);
  210. return 1;
  211. }
  212. instance->stopRecording(false);
  213. return 0;
  214. }
  215. int w_canRecord(lua_State *L)
  216. {
  217. luax_pushboolean(L, instance->canRecord());
  218. return 1;
  219. }
  220. int w_setDistanceModel(lua_State *L)
  221. {
  222. const char *modelStr = luaL_checkstring(L, 1);
  223. Audio::DistanceModel distanceModel;
  224. if (!Audio::getConstant(modelStr, distanceModel))
  225. return luaL_error(L, "Invalid distance model: %s", modelStr);
  226. instance->setDistanceModel(distanceModel);
  227. return 0;
  228. }
  229. int w_getDistanceModel(lua_State *L)
  230. {
  231. Audio::DistanceModel distanceModel = instance->getDistanceModel();
  232. const char *modelStr;
  233. if (!Audio::getConstant(distanceModel, modelStr))
  234. return 0;
  235. lua_pushstring(L, modelStr);
  236. return 1;
  237. }
  238. // List of functions to wrap.
  239. static const luaL_Reg functions[] =
  240. {
  241. { "getSourceCount", w_getSourceCount },
  242. { "newSource", w_newSource },
  243. { "play", w_play },
  244. { "stop", w_stop },
  245. { "pause", w_pause },
  246. { "resume", w_resume },
  247. { "rewind", w_rewind },
  248. { "setVolume", w_setVolume },
  249. { "getVolume", w_getVolume },
  250. { "setPosition", w_setPosition },
  251. { "getPosition", w_getPosition },
  252. { "setOrientation", w_setOrientation },
  253. { "getOrientation", w_getOrientation },
  254. { "setVelocity", w_setVelocity },
  255. { "getVelocity", w_getVelocity },
  256. /*{ "record", w_record },
  257. { "getRecordedData", w_getRecordedData },
  258. { "stopRecording", w_stopRecording },*/
  259. { "setDistanceModel", w_setDistanceModel },
  260. { "getDistanceModel", w_getDistanceModel },
  261. { 0, 0 }
  262. };
  263. static const lua_CFunction types[] =
  264. {
  265. luaopen_source,
  266. 0
  267. };
  268. extern "C" int luaopen_love_audio(lua_State *L)
  269. {
  270. #ifdef LOVE_ENABLE_AUDIO_OPENAL
  271. if (instance == 0)
  272. {
  273. // Try OpenAL first.
  274. try
  275. {
  276. instance = new love::audio::openal::Audio();
  277. }
  278. catch(love::Exception &e)
  279. {
  280. std::cout << e.what() << std::endl;
  281. }
  282. }
  283. else
  284. instance->retain();
  285. #endif
  286. #ifdef LOVE_ENABLE_AUDIO_NULL
  287. if (instance == 0)
  288. {
  289. // Fall back to nullaudio.
  290. try
  291. {
  292. instance = new love::audio::null::Audio();
  293. }
  294. catch(love::Exception &e)
  295. {
  296. std::cout << e.what() << std::endl;
  297. }
  298. }
  299. #endif
  300. if (instance == 0)
  301. return luaL_error(L, "Could not open any audio module.");
  302. WrappedModule w;
  303. w.module = instance;
  304. w.name = "audio";
  305. w.flags = MODULE_T;
  306. w.functions = functions;
  307. w.types = types;
  308. int n = luax_register_module(L, w);
  309. return n;
  310. }
  311. } // audio
  312. } // love