AudioCache.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC
  24. #import <OpenAL/al.h>
  25. #include <string>
  26. #include <mutex>
  27. #include <vector>
  28. #include "base/ccMacros.h"
  29. #include "audio/apple/AudioMacros.h"
  30. NS_CC_BEGIN
  31. class AudioEngineImpl;
  32. class AudioPlayer;
  33. class AudioCache
  34. {
  35. public:
  36. enum class State
  37. {
  38. INITIAL,
  39. LOADING,
  40. READY,
  41. FAILED
  42. };
  43. AudioCache();
  44. ~AudioCache();
  45. void addPlayCallback(const std::function<void()>& callback);
  46. void addLoadCallback(const std::function<void(bool)>& callback);
  47. protected:
  48. void setSkipReadDataTask(bool isSkip) { _isSkipReadDataTask = isSkip; };
  49. void readDataTask(unsigned int selfId);
  50. void invokingPlayCallbacks();
  51. void invokingLoadCallbacks();
  52. //pcm data related stuff
  53. ALenum _format;
  54. ALsizei _sampleRate;
  55. float _duration;
  56. uint32_t _totalFrames;
  57. uint32_t _framesRead;
  58. /*Cache related stuff;
  59. * Cache pcm data when sizeInBytes less than PCMDATA_CACHEMAXSIZE
  60. */
  61. ALuint _alBufferId;
  62. char* _pcmData;
  63. /*Queue buffer related stuff
  64. * Streaming in openal when sizeInBytes greater then PCMDATA_CACHEMAXSIZE
  65. */
  66. char* _queBuffers[QUEUEBUFFER_NUM];
  67. ALsizei _queBufferSize[QUEUEBUFFER_NUM];
  68. uint32_t _queBufferFrames;
  69. std::mutex _playCallbackMutex;
  70. std::vector< std::function<void()> > _playCallbacks;
  71. // loadCallbacks doesn't need mutex since it's invoked only in Cocos thread.
  72. std::vector< std::function<void(bool)> > _loadCallbacks;
  73. std::mutex _readDataTaskMutex;
  74. State _state;
  75. std::shared_ptr<bool> _isDestroyed;
  76. std::string _fileFullPath;
  77. unsigned int _id;
  78. bool _isLoadingFinished;
  79. bool _isSkipReadDataTask;
  80. friend class AudioEngineImpl;
  81. friend class AudioPlayer;
  82. };
  83. NS_CC_END
  84. #endif