wrap_Audio.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Copyright (c) 2006-2011 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 "Audio.h"
  23. #include "audio/null/Audio.h"
  24. #include <scripts/audio.lua.h>
  25. #include <common/runtime.h>
  26. namespace love
  27. {
  28. namespace audio
  29. {
  30. extern Audio * instance;
  31. namespace openal
  32. {
  33. // List of functions to wrap.
  34. static const luaL_Reg functions[] = {
  35. { "getNumSources", w_getNumSources },
  36. { "newSource1", w_newSource1 },
  37. { "play", w_play },
  38. { "stop", w_stop },
  39. { "pause", w_pause },
  40. { "resume", w_resume },
  41. { "rewind", w_rewind },
  42. { "setVolume", w_setVolume },
  43. { "getVolume", w_getVolume },
  44. { "setPosition", w_setPosition },
  45. { "getPosition", w_getPosition },
  46. { "setOrientation", w_setOrientation },
  47. { "getOrientation", w_getOrientation },
  48. { "setVelocity", w_setVelocity },
  49. { "getVelocity", w_getVelocity },
  50. /*{ "record", w_record },
  51. { "getRecordedData", w_getRecordedData },
  52. { "stopRecording", w_stopRecording },*/
  53. { 0, 0 }
  54. };
  55. static const lua_CFunction types[] = {
  56. luaopen_source,
  57. 0
  58. };
  59. int luaopen_love_audio_openal(lua_State * L)
  60. {
  61. if(instance == 0)
  62. {
  63. // Try OpenAL first.
  64. try
  65. {
  66. instance = new love::audio::openal::Audio();
  67. }
  68. catch(love::Exception & e)
  69. {
  70. std::cout << e.what() << std::endl;
  71. }
  72. }
  73. else
  74. instance->retain();
  75. if(instance == 0)
  76. {
  77. // Fall back to nullaudio.
  78. try
  79. {
  80. instance = new love::audio::null::Audio();
  81. }
  82. catch(love::Exception & e)
  83. {
  84. std::cout << e.what() << std::endl;
  85. }
  86. }
  87. if(instance == 0)
  88. return luaL_error(L, "Could not open any audio module.");
  89. WrappedModule w;
  90. w.module = instance;
  91. w.name = "audio";
  92. w.flags = MODULE_T;
  93. w.functions = functions;
  94. w.types = types;
  95. luax_register_module(L, w);
  96. if (luaL_loadbuffer(L, (const char *)audio_lua, sizeof(audio_lua), "audio.lua") == 0)
  97. lua_call(L, 0, 0);
  98. return 0;
  99. }
  100. } // openal
  101. } // audio
  102. } // love