AudioCache.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /****************************************************************************
  2. Copyright (c) 2014-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include "platform/CCPlatformConfig.h"
  22. #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
  23. #include <string>
  24. #include <mutex>
  25. #include <vector>
  26. #include <memory>
  27. #ifdef OPENAL_PLAIN_INCLUDES
  28. #include <al.h>
  29. #else
  30. #include <AL/al.h>
  31. #endif
  32. #include "platform/CCPlatformMacros.h"
  33. #include "audio/apple/AudioMacros.h"
  34. NS_CC_BEGIN
  35. namespace experimental{
  36. class AudioEngineImpl;
  37. class AudioPlayer;
  38. class CC_DLL AudioCache
  39. {
  40. public:
  41. enum class State
  42. {
  43. INITIAL,
  44. LOADING,
  45. READY,
  46. FAILED
  47. };
  48. AudioCache();
  49. ~AudioCache();
  50. void addPlayCallback(const std::function<void()>& callback);
  51. void addLoadCallback(const std::function<void(bool)>& callback);
  52. protected:
  53. void setSkipReadDataTask(bool isSkip) { _isSkipReadDataTask = isSkip; };
  54. void readDataTask(unsigned int selfId);
  55. void invokingPlayCallbacks();
  56. void invokingLoadCallbacks();
  57. //pcm data related stuff
  58. ALenum _format;
  59. ALsizei _sampleRate;
  60. float _duration;
  61. uint32_t _totalFrames;
  62. uint32_t _framesRead;
  63. /*Cache related stuff;
  64. * Cache pcm data when sizeInBytes less than PCMDATA_CACHEMAXSIZE
  65. */
  66. ALuint _alBufferId;
  67. char* _pcmData;
  68. /*Queue buffer related stuff
  69. * Streaming in OpenAL when sizeInBytes greater then PCMDATA_CACHEMAXSIZE
  70. */
  71. char* _queBuffers[QUEUEBUFFER_NUM];
  72. ALsizei _queBufferSize[QUEUEBUFFER_NUM];
  73. uint32_t _queBufferFrames;
  74. std::mutex _playCallbackMutex;
  75. std::vector< std::function<void()> > _playCallbacks;
  76. // loadCallbacks doesn't need mutex since it's invoked only in Cocos thread.
  77. std::vector< std::function<void(bool)> > _loadCallbacks;
  78. std::mutex _readDataTaskMutex;
  79. State _state;
  80. std::shared_ptr<bool> _isDestroyed;
  81. std::string _fileFullPath;
  82. unsigned int _id;
  83. bool _isLoadingFinished;
  84. bool _isSkipReadDataTask;
  85. friend class AudioEngineImpl;
  86. friend class AudioPlayer;
  87. };
  88. }
  89. NS_CC_END
  90. #endif