/engines/sword25/sfx/soundengine_script.cpp

https://github.com/somaen/scummvm · C++ · 361 lines · 251 code · 79 blank · 31 comment · 19 complexity · 881d7e3f8d2ef4c6bbe20e5379bb7425 MD5 · raw file

  1. /* ScummVM - Graphic Adventure Engine
  2. *
  3. * ScummVM is the legal property of its developers, whose names
  4. * are too numerous to list here. Please refer to the COPYRIGHT
  5. * file distributed with this source distribution.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. /*
  22. * This code is based on Broken Sword 2.5 engine
  23. *
  24. * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
  25. *
  26. * Licensed under GNU GPL v2
  27. *
  28. */
  29. // -----------------------------------------------------------------------------
  30. // Includes
  31. // -----------------------------------------------------------------------------
  32. #include "sword25/kernel/common.h"
  33. #include "sword25/kernel/kernel.h"
  34. #include "sword25/script/script.h"
  35. #include "sword25/script/luabindhelper.h"
  36. #include "sword25/sfx/soundengine.h"
  37. namespace Sword25 {
  38. static int init(lua_State *L) {
  39. Kernel *pKernel = Kernel::getInstance();
  40. assert(pKernel);
  41. SoundEngine *pSfx = pKernel->getSfx();
  42. assert(pSfx);
  43. if (lua_gettop(L) == 0)
  44. lua_pushbooleancpp(L, pSfx->init(44100, 32));
  45. else if (lua_gettop(L) == 1)
  46. lua_pushbooleancpp(L, pSfx->init(static_cast<uint>(luaL_checknumber(L, 1)), 32));
  47. else
  48. lua_pushbooleancpp(L, pSfx->init(static_cast<uint>(luaL_checknumber(L, 1)), static_cast<uint>(luaL_checknumber(L, 2))));
  49. return 1;
  50. }
  51. static int update(lua_State *L) {
  52. Kernel *pKernel = Kernel::getInstance();
  53. assert(pKernel);
  54. SoundEngine *pSfx = pKernel->getSfx();
  55. assert(pSfx);
  56. pSfx->update();
  57. return 0;
  58. }
  59. static int setVolume(lua_State *L) {
  60. Kernel *pKernel = Kernel::getInstance();
  61. assert(pKernel);
  62. SoundEngine *pSfx = pKernel->getSfx();
  63. assert(pSfx);
  64. pSfx->setVolume(static_cast<float>(luaL_checknumber(L, 1)),
  65. static_cast<SoundEngine::SOUND_TYPES>(static_cast<uint>(luaL_checknumber(L, 2))));
  66. return 0;
  67. }
  68. static int getVolume(lua_State *L) {
  69. Kernel *pKernel = Kernel::getInstance();
  70. assert(pKernel);
  71. SoundEngine *pSfx = pKernel->getSfx();
  72. assert(pSfx);
  73. lua_pushnumber(L, pSfx->getVolume(static_cast<SoundEngine::SOUND_TYPES>(static_cast<uint>(luaL_checknumber(L, 1)))));
  74. return 1;
  75. }
  76. static int pauseAll(lua_State *L) {
  77. Kernel *pKernel = Kernel::getInstance();
  78. assert(pKernel);
  79. SoundEngine *pSfx = pKernel->getSfx();
  80. assert(pSfx);
  81. pSfx->pauseAll();
  82. return 0;
  83. }
  84. static int resumeAll(lua_State *L) {
  85. Kernel *pKernel = Kernel::getInstance();
  86. assert(pKernel);
  87. SoundEngine *pSfx = pKernel->getSfx();
  88. assert(pSfx);
  89. pSfx->resumeAll();
  90. return 0;
  91. }
  92. static int pauseLayer(lua_State *L) {
  93. Kernel *pKernel = Kernel::getInstance();
  94. assert(pKernel);
  95. SoundEngine *pSfx = pKernel->getSfx();
  96. assert(pSfx);
  97. pSfx->pauseLayer(static_cast<int>(luaL_checknumber(L, 1)));
  98. return 0;
  99. }
  100. static int resumeLayer(lua_State *L) {
  101. Kernel *pKernel = Kernel::getInstance();
  102. assert(pKernel);
  103. SoundEngine *pSfx = pKernel->getSfx();
  104. assert(pSfx);
  105. pSfx->resumeLayer(static_cast<int>(luaL_checknumber(L, 1)));
  106. return 0;
  107. }
  108. static void processPlayParams(lua_State *L, Common::String &fileName, SoundEngine::SOUND_TYPES &type, float &volume, float &pan, bool &loop, int &loopStart, int &loopEnd, uint &layer) {
  109. fileName = luaL_checkstring(L, 1);
  110. type = static_cast<SoundEngine::SOUND_TYPES>(static_cast<uint>(luaL_checknumber(L, 2)));
  111. if (lua_gettop(L) < 3 || lua_isnil(L, 3))
  112. volume = 1.0f;
  113. else
  114. volume = static_cast<float>(luaL_checknumber(L, 3));
  115. if (lua_gettop(L) < 4 || lua_isnil(L, 4))
  116. pan = 0.0f;
  117. else
  118. pan = static_cast<float>(luaL_checknumber(L, 4));
  119. if (lua_gettop(L) < 5 || lua_isnil(L, 5))
  120. loop = false;
  121. else
  122. loop = lua_tobooleancpp(L, 5);
  123. if (lua_gettop(L) < 6 || lua_isnil(L, 6))
  124. loopStart = -1;
  125. else
  126. loopStart = static_cast<int>(luaL_checknumber(L, 6));
  127. if (lua_gettop(L) < 7 || lua_isnil(L, 7))
  128. loopEnd = -1;
  129. else
  130. loopEnd = static_cast<int>(luaL_checknumber(L, 7));
  131. if (lua_gettop(L) < 8 || lua_isnil(L, 8))
  132. layer = 0;
  133. else
  134. layer = static_cast<uint>(luaL_checknumber(L, 8));
  135. }
  136. static int playSound(lua_State *L) {
  137. Kernel *pKernel = Kernel::getInstance();
  138. assert(pKernel);
  139. SoundEngine *pSfx = pKernel->getSfx();
  140. assert(pSfx);
  141. Common::String fileName;
  142. SoundEngine::SOUND_TYPES type;
  143. float volume;
  144. float pan;
  145. bool loop;
  146. int loopStart;
  147. int loopEnd;
  148. uint layer;
  149. processPlayParams(L, fileName, type, volume, pan, loop, loopStart, loopEnd, layer);
  150. lua_pushbooleancpp(L, pSfx->playSound(fileName, type, volume, pan, loop, loopStart, loopEnd, layer));
  151. return 1;
  152. }
  153. static int playSoundEx(lua_State *L) {
  154. Kernel *pKernel = Kernel::getInstance();
  155. assert(pKernel);
  156. SoundEngine *pSfx = pKernel->getSfx();
  157. assert(pSfx);
  158. Common::String fileName;
  159. SoundEngine::SOUND_TYPES type;
  160. float volume;
  161. float pan;
  162. bool loop;
  163. int loopStart;
  164. int loopEnd;
  165. uint layer;
  166. processPlayParams(L, fileName, type, volume, pan, loop, loopStart, loopEnd, layer);
  167. lua_pushnumber(L, pSfx->playSoundEx(fileName, type, volume, pan, loop, loopStart, loopEnd, layer));
  168. return 1;
  169. }
  170. static int setSoundVolume(lua_State *L) {
  171. Kernel *pKernel = Kernel::getInstance();
  172. assert(pKernel);
  173. SoundEngine *pSfx = pKernel->getSfx();
  174. assert(pSfx);
  175. pSfx->setSoundVolume(static_cast<uint>(luaL_checknumber(L, 1)), static_cast<float>(luaL_checknumber(L, 2)));
  176. return 0;
  177. }
  178. static int setSoundPanning(lua_State *L) {
  179. Kernel *pKernel = Kernel::getInstance();
  180. assert(pKernel);
  181. SoundEngine *pSfx = pKernel->getSfx();
  182. assert(pSfx);
  183. pSfx->setSoundPanning(static_cast<uint>(luaL_checknumber(L, 1)), static_cast<float>(luaL_checknumber(L, 2)));
  184. return 0;
  185. }
  186. static int pauseSound(lua_State *L) {
  187. Kernel *pKernel = Kernel::getInstance();
  188. assert(pKernel);
  189. SoundEngine *pSfx = pKernel->getSfx();
  190. assert(pSfx);
  191. pSfx->pauseSound(static_cast<uint>(luaL_checknumber(L, 1)));
  192. return 0;
  193. }
  194. static int resumeSound(lua_State *L) {
  195. Kernel *pKernel = Kernel::getInstance();
  196. assert(pKernel);
  197. SoundEngine *pSfx = pKernel->getSfx();
  198. assert(pSfx);
  199. pSfx->resumeSound(static_cast<uint>(luaL_checknumber(L, 1)));
  200. return 0;
  201. }
  202. static int stopSound(lua_State *L) {
  203. Kernel *pKernel = Kernel::getInstance();
  204. assert(pKernel);
  205. SoundEngine *pSfx = pKernel->getSfx();
  206. assert(pSfx);
  207. pSfx->stopSound(static_cast<uint>(luaL_checknumber(L, 1)));
  208. return 0;
  209. }
  210. static int isSoundPaused(lua_State *L) {
  211. Kernel *pKernel = Kernel::getInstance();
  212. assert(pKernel);
  213. SoundEngine *pSfx = pKernel->getSfx();
  214. assert(pSfx);
  215. lua_pushbooleancpp(L, pSfx->isSoundPaused(static_cast<uint>(luaL_checknumber(L, 1))));
  216. return 1;
  217. }
  218. static int isSoundPlaying(lua_State *L) {
  219. Kernel *pKernel = Kernel::getInstance();
  220. assert(pKernel);
  221. SoundEngine *pSfx = pKernel->getSfx();
  222. assert(pSfx);
  223. lua_pushbooleancpp(L, pSfx->isSoundPlaying(static_cast<uint>(luaL_checknumber(L, 1))));
  224. return 1;
  225. }
  226. static int getSoundVolume(lua_State *L) {
  227. Kernel *pKernel = Kernel::getInstance();
  228. assert(pKernel);
  229. SoundEngine *pSfx = pKernel->getSfx();
  230. assert(pSfx);
  231. lua_pushnumber(L, pSfx->getSoundVolume(static_cast<uint>(luaL_checknumber(L, 1))));
  232. return 1;
  233. }
  234. static int getSoundPanning(lua_State *L) {
  235. Kernel *pKernel = Kernel::getInstance();
  236. assert(pKernel);
  237. SoundEngine *pSfx = pKernel->getSfx();
  238. assert(pSfx);
  239. lua_pushnumber(L, pSfx->getSoundPanning(static_cast<uint>(luaL_checknumber(L, 1))));
  240. return 1;
  241. }
  242. static const char *SFX_LIBRARY_NAME = "Sfx";
  243. static const luaL_reg SFX_FUNCTIONS[] = {
  244. {"Init", init},
  245. {"Update", update},
  246. {"__SetVolume", setVolume},
  247. {"__GetVolume", getVolume},
  248. {"PauseAll", pauseAll},
  249. {"ResumeAll", resumeAll},
  250. {"PauseLayer", pauseLayer},
  251. {"ResumeLayer", resumeLayer},
  252. {"__PlaySound", playSound},
  253. {"__PlaySoundEx", playSoundEx},
  254. {"__SetSoundVolume", setSoundVolume},
  255. {"__SetSoundPanning", setSoundPanning},
  256. {"__PauseSound", pauseSound},
  257. {"__ResumeSound", resumeSound},
  258. {"__StopSound", stopSound},
  259. {"__IsSoundPaused", isSoundPaused},
  260. {"__IsSoundPlaying", isSoundPlaying},
  261. {"__GetSoundVolume", getSoundVolume},
  262. {"__GetSoundPanning", getSoundPanning},
  263. {0, 0}
  264. };
  265. static const lua_constant_reg SFX_CONSTANTS[] = {
  266. {"MUSIC", SoundEngine::MUSIC},
  267. {"SPEECH", SoundEngine::SPEECH},
  268. {"SFX", SoundEngine::SFX},
  269. {0, 0}
  270. };
  271. bool SoundEngine::registerScriptBindings() {
  272. Kernel *pKernel = Kernel::getInstance();
  273. assert(pKernel);
  274. ScriptEngine *pScript = pKernel->getScript();
  275. assert(pScript);
  276. lua_State *L = static_cast<lua_State *>(pScript->getScriptObject());
  277. assert(L);
  278. if (!LuaBindhelper::addFunctionsToLib(L, SFX_LIBRARY_NAME, SFX_FUNCTIONS)) return false;
  279. if (!LuaBindhelper::addConstantsToLib(L, SFX_LIBRARY_NAME, SFX_CONSTANTS)) return false;
  280. return true;
  281. }
  282. } // End of namespace Sword25