SimpleAudioEngine.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /****************************************************************************
  2. Copyright (c) 2010 Steve Oldmeadow
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef _SIMPLE_AUDIO_ENGINE_H_
  23. #define _SIMPLE_AUDIO_ENGINE_H_
  24. #include "audio/include/Export.h"
  25. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  26. #define CC_DEPRECATED_ATTRIBUTE __attribute__((deprecated))
  27. #elif _MSC_VER >= 1400 //vs 2005 or higher
  28. #define CC_DEPRECATED_ATTRIBUTE __declspec(deprecated)
  29. #else
  30. #define CC_DEPRECATED_ATTRIBUTE
  31. #endif
  32. /**
  33. * @addtogroup audio
  34. * @{
  35. */
  36. namespace CocosDenshion {
  37. /**
  38. * @class SimpleAudioEngine
  39. *
  40. * @brief Offers a very simple interface to play background music & sound effects.
  41. *
  42. * @note Make sure to call SimpleAudioEngine::end() when the sound engine is not needed anymore to release allocated resources.
  43. * @js cc.audioEngine
  44. */
  45. class EXPORT_DLL SimpleAudioEngine
  46. {
  47. public:
  48. /**
  49. * Returns a shared instance of the SimpleAudioEngine.
  50. * @js NA
  51. */
  52. static SimpleAudioEngine* getInstance();
  53. /**
  54. * @deprecated Use `getInstance` instead.
  55. * @js NA
  56. * @lua NA
  57. */
  58. CC_DEPRECATED_ATTRIBUTE static SimpleAudioEngine* sharedEngine() { return SimpleAudioEngine::getInstance(); }
  59. /**
  60. * Release the shared Engine object.
  61. *
  62. * @warning It must be called before the application exit, or it will lead to memory leaks.
  63. * @lua destroyInstance
  64. */
  65. static void end();
  66. /**
  67. * Preload background music.
  68. *
  69. * @param filePath The path of the background music file.
  70. * @js NA
  71. * @lua preloadMusic
  72. */
  73. virtual void preloadBackgroundMusic(const char* filePath);
  74. /**
  75. * Play background music.
  76. *
  77. * @param filePath The path of the background music file,or the FileName of T_SoundResInfo.
  78. * @param loop Whether the background music loop or not.
  79. * @js playMusic
  80. * @lua playMusic
  81. */
  82. virtual void playBackgroundMusic(const char* filePath, bool loop = false);
  83. /**
  84. * Stop playing background music.
  85. *
  86. * @param releaseData If release the background music data or not.As default value is false.
  87. * @js stopMusic
  88. * @lua stopMusic
  89. */
  90. virtual void stopBackgroundMusic(bool releaseData = false);
  91. /**
  92. * Pause playing background music.
  93. * @js pauseMusic
  94. * @lua pauseMusic
  95. */
  96. virtual void pauseBackgroundMusic();
  97. /**
  98. * Resume playing background music.
  99. * @js resumeMusic
  100. * @lua resumeMusic
  101. */
  102. virtual void resumeBackgroundMusic();
  103. /**
  104. * Rewind playing background music.
  105. * @js rewindMusic
  106. * @lua rewindMusic
  107. */
  108. virtual void rewindBackgroundMusic();
  109. /**
  110. * Indicates whether any background music can be played or not.
  111. *
  112. * @return <i>true</i> if background music can be played, otherwise <i>false</i>.
  113. * @js willPlayMusic
  114. * @lua willPlayMusic
  115. */
  116. virtual bool willPlayBackgroundMusic();
  117. /**
  118. * Indicates whether the background music is playing.
  119. *
  120. * @return <i>true</i> if the background music is playing, otherwise <i>false</i>.
  121. * @js isMusicPlaying
  122. * @lua isMusicPlaying
  123. */
  124. virtual bool isBackgroundMusicPlaying();
  125. //
  126. // properties
  127. //
  128. /**
  129. * The volume of the background music within the range of 0.0 as the minimum and 1.0 as the maximum.
  130. * @js getMusicVolume
  131. * @lua getMusicVolume
  132. */
  133. virtual float getBackgroundMusicVolume();
  134. /**
  135. * Set the volume of background music.
  136. *
  137. * @param volume must be within the range of 0.0 as the minimum and 1.0 as the maximum.
  138. * @js setMusicVolume
  139. * @lua setMusicVolume
  140. */
  141. virtual void setBackgroundMusicVolume(float volume);
  142. /**
  143. * The volume of the effects within the range of 0.0 as the minimum and 1.0 as the maximum.
  144. */
  145. virtual float getEffectsVolume();
  146. /**
  147. * Set the volume of sound effects.
  148. *
  149. * @param volume must be within the range of 0.0 as the minimum and 1.0 as the maximum.
  150. */
  151. virtual void setEffectsVolume(float volume);
  152. //
  153. // for sound effects
  154. /**
  155. * Play sound effect with a file path, pitch, pan and gain.
  156. *
  157. * @param filePath The path of the effect file.
  158. * @param loop Determines whether to loop the effect playing or not. The default value is false.
  159. * @param pitch Frequency, normal value is 1.0. Will also change effect play time.
  160. * @param pan Stereo effect, in the range of [-1..1] where -1 enables only left channel.
  161. * @param gain Volume, in the range of [0..1]. The normal value is 1.
  162. * @return The sound id.
  163. *
  164. * @note Full support is under development, now there are limitations:
  165. * - no pitch effect on Samsung Galaxy S2 with OpenSL backend enabled;
  166. * - no pitch/pan/gain on win32.
  167. */
  168. virtual unsigned int playEffect(const char* filePath, bool loop = false,
  169. float pitch = 1.0f, float pan = 0.0f, float gain = 1.0f);
  170. /**
  171. * Pause playing sound effect.
  172. *
  173. * @param soundId The return value of function playEffect.
  174. */
  175. virtual void pauseEffect(unsigned int soundId);
  176. /**
  177. * Pause all playing sound effect.
  178. */
  179. virtual void pauseAllEffects();
  180. /**
  181. * Resume playing sound effect.
  182. *
  183. * @param soundId The return value of function playEffect.
  184. */
  185. virtual void resumeEffect(unsigned int soundId);
  186. /**
  187. * Resume all playing sound effect.
  188. */
  189. virtual void resumeAllEffects();
  190. /**
  191. * Stop playing sound effect.
  192. *
  193. * @param soundId The return value of function playEffect.
  194. */
  195. virtual void stopEffect(unsigned int soundId);
  196. /**
  197. * Stop all playing sound effects.
  198. */
  199. virtual void stopAllEffects();
  200. /**
  201. * Preload a compressed audio file.
  202. *
  203. * The compressed audio will be decoded to wave, then written into an internal buffer in SimpleAudioEngine.
  204. *
  205. * @param filePath The path of the effect file.
  206. * @js NA
  207. */
  208. virtual void preloadEffect(const char* filePath);
  209. /**
  210. * Unload the preloaded effect from internal buffer.
  211. *
  212. * @param filePath The path of the effect file.
  213. */
  214. virtual void unloadEffect(const char* filePath);
  215. protected:
  216. /**
  217. * Constructor of SimpleAudioEngine.
  218. */
  219. SimpleAudioEngine();
  220. /**
  221. * Destructor of SimpleAudioEngine.
  222. */
  223. virtual ~SimpleAudioEngine();
  224. };
  225. } // end of namespace CocosDenshion
  226. // end audio group
  227. /// @}
  228. #endif // _SIMPLE_AUDIO_ENGINE_H_