AudioCachePlayer.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * cocos2d-x http://www.cocos2d-x.org
  3. *
  4. * Copyright (c) 2010-2011 - cocos2d-x community
  5. *
  6. * Portions Copyright (c) Microsoft Open Technologies, Inc.
  7. * All Rights Reserved
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and limitations under the License.
  17. */
  18. #include "platform/CCPlatformConfig.h"
  19. #if CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
  20. #ifndef __AUDIO_CACHE_PLAYER_H_
  21. #define __AUDIO_CACHE_PLAYER_H_
  22. #include "audio/winrt/AudioSourceReader.h"
  23. NS_CC_BEGIN
  24. namespace experimental{
  25. typedef struct AudioInfo
  26. {
  27. size_t _totalAudioBytes;
  28. WAVEFORMATEX _wfx;
  29. } AudioInfo;
  30. enum class AudioPlayerState
  31. {
  32. ERRORED = -1,
  33. INITIALIZING,
  34. READY,
  35. PLAYING,
  36. PAUSED,
  37. STOPPED
  38. };
  39. class AudioCache
  40. {
  41. public:
  42. AudioCache();
  43. ~AudioCache();
  44. void readDataTask();
  45. void addPlayCallback(const std::function<void()> &callback);
  46. void addLoadCallback(const std::function<void(bool)> &callback);
  47. bool getChunk(AudioDataChunk& chunk);
  48. void doBuffering();
  49. bool isStreamingSource();
  50. void seek(const float ratio);
  51. protected:
  52. void invokePlayCallbacks();
  53. void invokeLoadCallbacks();
  54. private:
  55. AudioCache(const AudioCache&);
  56. AudioCache& operator=(const AudioCache&);
  57. private:
  58. bool _retry;
  59. bool _isReady;
  60. AudioInfo _audInfo;
  61. std::mutex _cbMutex;
  62. FileFormat _fileFormat;
  63. std::string _fileFullPath;
  64. AudioSourceReader *_srcReader;
  65. std::vector<std::function<void()>> _callbacks;
  66. std::vector<std::function<void(bool)>> _loadCallbacks;
  67. friend class AudioPlayer;
  68. friend class AudioEngineImpl;
  69. };
  70. class AudioPlayer : public IXAudio2EngineCallback, IXAudio2VoiceCallback
  71. {
  72. public:
  73. AudioPlayer();
  74. virtual ~AudioPlayer();
  75. void stop();
  76. void pause();
  77. bool update();
  78. void resume();
  79. bool isInError();
  80. float getDuration();
  81. float getCurrentTime();
  82. bool setTime(float time);
  83. void setVolume(float volume);
  84. bool play2d(AudioCache* cache);
  85. AudioPlayerState getState() { return _state; }
  86. protected:
  87. AudioPlayer(AudioPlayer&);
  88. AudioPlayer& operator=(AudioPlayer&);
  89. void init();
  90. void free();
  91. void error();
  92. void popBuffer();
  93. void updateState();
  94. bool submitBuffers();
  95. void onBufferRunOut();
  96. void invokeFinishCallback();
  97. void _stop(bool pause = false);
  98. bool _play(bool resume = false);
  99. XAUDIO2_VOICE_STATE getSourceVoiceState(bool fast = false);
  100. // IXAudio2EngineCallback
  101. STDMETHOD_(void, OnProcessingPassStart) () override;
  102. STDMETHOD_(void, OnProcessingPassEnd) () override;
  103. STDMETHOD_(void, OnCriticalError) (HRESULT error) override;
  104. // IXAudio2VoiceCallback
  105. STDMETHOD_(void, OnVoiceProcessingPassStart) (UINT32 uBytesRequired) override;
  106. STDMETHOD_(void, OnVoiceProcessingPassEnd) () override;
  107. STDMETHOD_(void, OnStreamEnd) () override;
  108. STDMETHOD_(void, OnBufferStart) (void* pBufferContext) override;
  109. STDMETHOD_(void, OnBufferEnd) (void* pBufferContext) override;
  110. STDMETHOD_(void, OnLoopEnd) (void* pBufferContext) override;
  111. STDMETHOD_(void, OnVoiceError) (void *pBufferContext, HRESULT error) override;
  112. private:
  113. bool _loop;
  114. bool _ready;
  115. float _volume;
  116. float _current;
  117. float _duration;
  118. bool _criticalError;
  119. bool _isStreaming;
  120. UINT64 _totalSamples;
  121. UINT64 _samplesOffset;
  122. XAUDIO2_BUFFER _xaBuffer;
  123. IXAudio2SourceVoice *_xaSourceVoice;
  124. IXAudio2MasteringVoice *_xaMasterVoice;
  125. Microsoft::WRL::ComPtr<IXAudio2> _xaEngine;
  126. AudioCache *_cache;
  127. std::mutex _stMutex;
  128. std::mutex _bqMutex;
  129. AudioPlayerState _state;
  130. std::queue<AudioDataChunk> _cachedBufferQ;
  131. std::function<void(int, const std::string &)> _finishCallback;
  132. friend class AudioEngineImpl;
  133. };
  134. }
  135. NS_CC_END
  136. #endif // __AUDIO_CACHE_PLAYER_H_
  137. #endif