AudioEngine.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #pragma once
  22. #include "platform/CCPlatformConfig.h"
  23. #include "platform/CCPlatformMacros.h"
  24. #include "audio/include/Export.h"
  25. #include <functional>
  26. #include <list>
  27. #include <string>
  28. #include <unordered_map>
  29. #ifdef ERROR
  30. #undef ERROR
  31. #endif // ERROR
  32. /**
  33. * @addtogroup audio
  34. * @{
  35. */
  36. NS_CC_BEGIN
  37. namespace experimental {
  38. /**
  39. * @class AudioProfile
  40. *
  41. * @brief
  42. * @js NA
  43. */
  44. class EXPORT_DLL AudioProfile
  45. {
  46. public:
  47. //Profile name can't be empty.
  48. std::string name;
  49. //The maximum number of simultaneous audio instance.
  50. unsigned int maxInstances;
  51. /* Minimum delay in between sounds */
  52. double minDelay;
  53. /**
  54. * Default constructor
  55. *
  56. * @lua new
  57. */
  58. AudioProfile()
  59. : maxInstances(0)
  60. , minDelay(0.0)
  61. {
  62. }
  63. };
  64. class AudioEngineImpl;
  65. /**
  66. * @class AudioEngine
  67. *
  68. * @brief Offers a interface to play audio.
  69. *
  70. * @note Make sure to call AudioEngine::end() when the audio engine is not needed anymore to release resources.
  71. * @js NA
  72. */
  73. class EXPORT_DLL AudioEngine
  74. {
  75. public:
  76. /** AudioState enum,all possible states of an audio instance.*/
  77. enum class AudioState
  78. {
  79. ERROR = -1,
  80. INITIALIZING,
  81. PLAYING,
  82. PAUSED
  83. };
  84. static const int INVALID_AUDIO_ID;
  85. static const float TIME_UNKNOWN;
  86. static bool lazyInit();
  87. /**
  88. * Release objects relating to AudioEngine.
  89. *
  90. * @warning It must be called before the application exit.
  91. * @lua endToLua
  92. */
  93. static void end();
  94. /**
  95. * Gets the default profile of audio instances.
  96. *
  97. * @return The default profile of audio instances.
  98. */
  99. static AudioProfile* getDefaultProfile();
  100. /**
  101. * Play 2d sound.
  102. *
  103. * @param filePath The path of an audio file.
  104. * @param loop Whether audio instance loop or not.
  105. * @param volume Volume value (range from 0.0 to 1.0).
  106. * @param profile A profile for audio instance. When profile is not specified, default profile will be used.
  107. * @return An audio ID. It allows you to dynamically change the behavior of an audio instance on the fly.
  108. *
  109. * @see `AudioProfile`
  110. */
  111. static int play2d(const std::string& filePath, bool loop = false, float volume = 1.0f, const AudioProfile *profile = nullptr);
  112. /**
  113. * Sets whether an audio instance loop or not.
  114. *
  115. * @param audioID An audioID returned by the play2d function.
  116. * @param loop Whether audio instance loop or not.
  117. */
  118. static void setLoop(int audioID, bool loop);
  119. /**
  120. * Checks whether an audio instance is loop.
  121. *
  122. * @param audioID An audioID returned by the play2d function.
  123. * @return Whether or not an audio instance is loop.
  124. */
  125. static bool isLoop(int audioID);
  126. /**
  127. * Sets volume for an audio instance.
  128. *
  129. * @param audioID An audioID returned by the play2d function.
  130. * @param volume Volume value (range from 0.0 to 1.0).
  131. */
  132. static void setVolume(int audioID, float volume);
  133. /**
  134. * Gets the volume value of an audio instance.
  135. *
  136. * @param audioID An audioID returned by the play2d function.
  137. * @return Volume value (range from 0.0 to 1.0).
  138. */
  139. static float getVolume(int audioID);
  140. /**
  141. * Pause an audio instance.
  142. *
  143. * @param audioID An audioID returned by the play2d function.
  144. */
  145. static void pause(int audioID);
  146. /** Pause all playing audio instances. */
  147. static void pauseAll();
  148. /**
  149. * Resume an audio instance.
  150. *
  151. * @param audioID An audioID returned by the play2d function.
  152. */
  153. static void resume(int audioID);
  154. /** Resume all suspended audio instances. */
  155. static void resumeAll();
  156. /**
  157. * Stop an audio instance.
  158. *
  159. * @param audioID An audioID returned by the play2d function.
  160. */
  161. static void stop(int audioID);
  162. /** Stop all audio instances. */
  163. static void stopAll();
  164. /**
  165. * Sets the current playback position of an audio instance.
  166. *
  167. * @param audioID An audioID returned by the play2d function.
  168. * @param sec The offset in seconds from the start to seek to.
  169. * @return
  170. */
  171. static bool setCurrentTime(int audioID, float sec);
  172. /**
  173. * Gets the current playback position of an audio instance.
  174. *
  175. * @param audioID An audioID returned by the play2d function.
  176. * @return The current playback position of an audio instance.
  177. */
  178. static float getCurrentTime(int audioID);
  179. /**
  180. * Gets the duration of an audio instance.
  181. *
  182. * @param audioID An audioID returned by the play2d function.
  183. * @return The duration of an audio instance.
  184. */
  185. static float getDuration(int audioID);
  186. /**
  187. * Returns the state of an audio instance.
  188. *
  189. * @param audioID An audioID returned by the play2d function.
  190. * @return The status of an audio instance.
  191. */
  192. static AudioState getState(int audioID);
  193. /**
  194. * Register a callback to be invoked when an audio instance has completed playing.
  195. *
  196. * @param audioID An audioID returned by the play2d function.
  197. * @param callback
  198. */
  199. static void setFinishCallback(int audioID, const std::function<void(int,const std::string&)>& callback);
  200. /**
  201. * Gets the maximum number of simultaneous audio instance of AudioEngine.
  202. */
  203. static int getMaxAudioInstance() {return _maxInstances;}
  204. /**
  205. * Sets the maximum number of simultaneous audio instance for AudioEngine.
  206. *
  207. * @param maxInstances The maximum number of simultaneous audio instance.
  208. */
  209. static bool setMaxAudioInstance(int maxInstances);
  210. /**
  211. * Uncache the audio data from internal buffer.
  212. * AudioEngine cache audio data on ios,mac, and win32 platform.
  213. *
  214. * @warning This can lead to stop related audio first.
  215. * @param filePath Audio file path.
  216. */
  217. static void uncache(const std::string& filePath);
  218. /**
  219. * Uncache all audio data from internal buffer.
  220. *
  221. * @warning All audio will be stopped first.
  222. */
  223. static void uncacheAll();
  224. /**
  225. * Gets the audio profile by id of audio instance.
  226. *
  227. * @param audioID An audioID returned by the play2d function.
  228. * @return The audio profile.
  229. */
  230. static AudioProfile* getProfile(int audioID);
  231. /**
  232. * Gets an audio profile by name.
  233. *
  234. * @param profileName A name of audio profile.
  235. * @return The audio profile.
  236. */
  237. static AudioProfile* getProfile(const std::string &profileName);
  238. /**
  239. * Preload audio file.
  240. * @param filePath The file path of an audio.
  241. */
  242. static void preload(const std::string& filePath) { preload(filePath, nullptr); }
  243. /**
  244. * Preload audio file.
  245. * @param filePath The file path of an audio.
  246. * @param callback A callback which will be called after loading is finished.
  247. */
  248. static void preload(const std::string& filePath, std::function<void(bool isSuccess)> callback);
  249. /**
  250. * Gets playing audio count.
  251. */
  252. static int getPlayingAudioCount();
  253. /**
  254. * Whether to enable playing audios
  255. * @note If it's disabled, current playing audios will be stopped and the later 'preload', 'play2d' methods will take no effects.
  256. */
  257. static void setEnabled(bool isEnabled);
  258. /**
  259. * Check whether AudioEngine is enabled.
  260. */
  261. static bool isEnabled();
  262. protected:
  263. static void addTask(const std::function<void()>& task);
  264. static void remove(int audioID);
  265. struct ProfileHelper
  266. {
  267. AudioProfile profile;
  268. std::list<int> audioIDs;
  269. double lastPlayTime;
  270. ProfileHelper()
  271. : lastPlayTime(0.0)
  272. {
  273. }
  274. };
  275. struct AudioInfo
  276. {
  277. const std::string* filePath;
  278. ProfileHelper* profileHelper;
  279. float volume;
  280. bool loop;
  281. float duration;
  282. AudioState state;
  283. AudioInfo();
  284. ~AudioInfo();
  285. private:
  286. AudioInfo(const AudioInfo& info);
  287. AudioInfo(AudioInfo&& info);
  288. AudioInfo& operator=(const AudioInfo& info);
  289. AudioInfo& operator=(AudioInfo&& info);
  290. };
  291. //audioID,audioAttribute
  292. static std::unordered_map<int, AudioInfo> _audioIDInfoMap;
  293. //audio file path,audio IDs
  294. static std::unordered_map<std::string,std::list<int>> _audioPathIDMap;
  295. //profileName,ProfileHelper
  296. static std::unordered_map<std::string, ProfileHelper> _audioPathProfileHelperMap;
  297. static unsigned int _maxInstances;
  298. static ProfileHelper* _defaultProfileHelper;
  299. static AudioEngineImpl* _audioEngineImpl;
  300. class AudioEngineThreadPool;
  301. static AudioEngineThreadPool* s_threadPool;
  302. static bool _isEnabled;
  303. friend class AudioEngineImpl;
  304. };
  305. } // namespace experimental {
  306. NS_CC_END
  307. // end group
  308. /// @}