SimpleAudioEngine.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include <iostream>
  2. #include "audio/include/SimpleAudioEngine.h"
  3. #include "audio/include/AudioEngine.h"
  4. using namespace CocosDenshion;
  5. using namespace cocos2d;
  6. using namespace cocos2d::experimental;
  7. struct SimpleAudioEngineLinux {
  8. SimpleAudioEngine * engine = nullptr;
  9. int musicid;
  10. float effectsvolume;
  11. std::string musicpath;
  12. };
  13. SimpleAudioEngineLinux * g_SimpleAudioEngineLinux = nullptr;
  14. SimpleAudioEngine* SimpleAudioEngine::getInstance()
  15. {
  16. if (!g_SimpleAudioEngineLinux) {
  17. g_SimpleAudioEngineLinux = new SimpleAudioEngineLinux();
  18. g_SimpleAudioEngineLinux->engine = new SimpleAudioEngine();
  19. }
  20. return g_SimpleAudioEngineLinux->engine;
  21. }
  22. void SimpleAudioEngine::end()
  23. {
  24. if (g_SimpleAudioEngineLinux) {
  25. delete g_SimpleAudioEngineLinux->engine;
  26. delete g_SimpleAudioEngineLinux;
  27. }
  28. g_SimpleAudioEngineLinux = nullptr;
  29. }
  30. SimpleAudioEngine::SimpleAudioEngine()
  31. {
  32. g_SimpleAudioEngineLinux->musicid = -1;
  33. g_SimpleAudioEngineLinux->effectsvolume = 1.0f;
  34. }
  35. SimpleAudioEngine::~SimpleAudioEngine()
  36. {
  37. }
  38. void SimpleAudioEngine::preloadBackgroundMusic(const char* filePath)
  39. {
  40. g_SimpleAudioEngineLinux->musicpath = filePath;
  41. AudioEngine::preload(filePath);
  42. }
  43. void SimpleAudioEngine::playBackgroundMusic(const char* filePath, bool loop)
  44. {
  45. g_SimpleAudioEngineLinux->musicpath = filePath;
  46. g_SimpleAudioEngineLinux->musicid = AudioEngine::play2d(filePath, loop);
  47. }
  48. void SimpleAudioEngine::stopBackgroundMusic(bool releaseData)
  49. {
  50. AudioEngine::stop(g_SimpleAudioEngineLinux->musicid);
  51. if (releaseData) {
  52. AudioEngine::uncache(g_SimpleAudioEngineLinux->musicpath.c_str());
  53. }
  54. }
  55. void SimpleAudioEngine::pauseBackgroundMusic()
  56. {
  57. AudioEngine::pause(g_SimpleAudioEngineLinux->musicid);
  58. }
  59. void SimpleAudioEngine::resumeBackgroundMusic()
  60. {
  61. AudioEngine::resume(g_SimpleAudioEngineLinux->musicid);
  62. }
  63. void SimpleAudioEngine::rewindBackgroundMusic()
  64. {
  65. AudioEngine::setCurrentTime(g_SimpleAudioEngineLinux->musicid, 0);
  66. }
  67. bool SimpleAudioEngine::willPlayBackgroundMusic()
  68. {
  69. return g_SimpleAudioEngineLinux->musicid != -1;
  70. }
  71. bool SimpleAudioEngine::isBackgroundMusicPlaying()
  72. {
  73. return AudioEngine::getState(g_SimpleAudioEngineLinux->musicid) == AudioEngine::AudioState::PLAYING;
  74. }
  75. //
  76. // properties
  77. //
  78. /**
  79. * The volume of the background music within the range of 0.0 as the minimum and 1.0 as the maximum.
  80. * @js getMusicVolume
  81. * @lua getMusicVolume
  82. */
  83. float SimpleAudioEngine::getBackgroundMusicVolume()
  84. {
  85. return AudioEngine::getVolume(g_SimpleAudioEngineLinux->musicid);
  86. }
  87. /**
  88. * Set the volume of background music.
  89. *
  90. * @param volume must be within the range of 0.0 as the minimum and 1.0 as the maximum.
  91. * @js setMusicVolume
  92. * @lua setMusicVolume
  93. */
  94. void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
  95. {
  96. AudioEngine::setVolume(g_SimpleAudioEngineLinux->musicid, volume);
  97. }
  98. /**
  99. * The volume of the effects within the range of 0.0 as the minimum and 1.0 as the maximum.
  100. */
  101. float SimpleAudioEngine::getEffectsVolume()
  102. {
  103. return g_SimpleAudioEngineLinux->effectsvolume;
  104. }
  105. /**
  106. * Set the volume of sound effects.
  107. *
  108. * @param volume must be within the range of 0.0 as the minimum and 1.0 as the maximum.
  109. */
  110. void SimpleAudioEngine::setEffectsVolume(float volume)
  111. {
  112. g_SimpleAudioEngineLinux->effectsvolume = volume;
  113. }
  114. /**
  115. * Play sound effect with a file path, pitch, pan and gain.
  116. *
  117. * @param filePath The path of the effect file.
  118. * @param loop Determines whether to loop the effect playing or not. The default value is false.
  119. * @param pitch Frequency, normal value is 1.0. Will also change effect play time.
  120. * @param pan Stereo effect, in the range of [-1..1] where -1 enables only left channel.
  121. * @param gain Volume, in the range of [0..1]. The normal value is 1.
  122. * @return The sound id.
  123. *
  124. * @note Full support is under development, now there are limitations:
  125. * - no pitch effect on Samsung Galaxy S2 with OpenSL backend enabled;
  126. * - no pitch/pan/gain on win32.
  127. */
  128. unsigned int SimpleAudioEngine::playEffect(const char* filePath, bool loop, float pitch, float pan, float gain)
  129. {
  130. return AudioEngine::play2d(filePath, loop, gain);
  131. }
  132. /**
  133. * Pause playing sound effect.
  134. *
  135. * @param soundId The return value of function playEffect.
  136. */
  137. void SimpleAudioEngine::pauseEffect(unsigned int soundId)
  138. {
  139. AudioEngine::pause(soundId);
  140. }
  141. /**
  142. * Pause all playing sound effect.
  143. */
  144. void SimpleAudioEngine::pauseAllEffects()
  145. {
  146. AudioEngine::pauseAll();
  147. }
  148. /**
  149. * Resume playing sound effect.
  150. *
  151. * @param soundId The return value of function playEffect.
  152. */
  153. void SimpleAudioEngine::resumeEffect(unsigned int soundId)
  154. {
  155. AudioEngine::resume(soundId);
  156. }
  157. /**
  158. * Resume all playing sound effect.
  159. */
  160. void SimpleAudioEngine::resumeAllEffects()
  161. {
  162. AudioEngine::resumeAll();
  163. }
  164. /**
  165. * Stop playing sound effect.
  166. *
  167. * @param soundId The return value of function playEffect.
  168. */
  169. void SimpleAudioEngine::stopEffect(unsigned int soundId)
  170. {
  171. AudioEngine::stop(soundId);
  172. }
  173. /**
  174. * Stop all playing sound effects.
  175. */
  176. void SimpleAudioEngine::stopAllEffects()
  177. {
  178. AudioEngine::stopAll();
  179. }
  180. /**
  181. * Preload a compressed audio file.
  182. *
  183. * The compressed audio will be decoded to wave, then written into an internal buffer in SimpleAudioEngine.
  184. *
  185. * @param filePath The path of the effect file.
  186. * @js NA
  187. */
  188. void SimpleAudioEngine::preloadEffect(const char* filePath)
  189. {
  190. AudioEngine::preload(filePath);
  191. }
  192. /**
  193. * Unload the preloaded effect from internal buffer.
  194. *
  195. * @param filePath The path of the effect file.
  196. */
  197. void SimpleAudioEngine::unloadEffect(const char* filePath)
  198. {
  199. AudioEngine::uncache(filePath);
  200. }