AudioCachePlayer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. #include "audio/winrt/AudioCachePlayer.h"
  21. #include "base/CCDirector.h"
  22. #include "base/CCScheduler.h"
  23. using namespace cocos2d;
  24. using namespace cocos2d::experimental;
  25. inline void ThrowIfFailed(HRESULT hr)
  26. {
  27. if (FAILED(hr)) {
  28. // Set a breakpoint on this line to catch XAudio2 API errors.
  29. throw Platform::Exception::CreateException(hr);
  30. }
  31. }
  32. // AudioCache
  33. AudioCache::AudioCache()
  34. : _isReady(false)
  35. , _retry(false)
  36. , _fileFullPath("")
  37. , _srcReader(nullptr)
  38. , _fileFormat(FileFormat::UNKNOWN)
  39. {
  40. _callbacks.clear();
  41. memset(&_audInfo, 0, sizeof(AudioInfo));
  42. }
  43. AudioCache::~AudioCache()
  44. {
  45. _callbacks.clear();
  46. if (nullptr != _srcReader) {
  47. delete _srcReader;
  48. _srcReader = nullptr;
  49. }
  50. }
  51. void AudioCache::readDataTask()
  52. {
  53. if (_isReady) {
  54. return;
  55. }
  56. if (nullptr != _srcReader) {
  57. delete _srcReader;
  58. _srcReader = nullptr;
  59. }
  60. switch (_fileFormat)
  61. {
  62. case FileFormat::WAV:
  63. _srcReader = new (std::nothrow) WAVReader();
  64. break;
  65. case FileFormat::OGG:
  66. _srcReader = new (std::nothrow) OGGReader();
  67. break;
  68. case FileFormat::MP3:
  69. _srcReader = new (std::nothrow) MP3Reader();
  70. break;
  71. case FileFormat::UNKNOWN:
  72. default:
  73. break;
  74. }
  75. if (_srcReader && _srcReader->initialize(_fileFullPath)) {
  76. _audInfo._totalAudioBytes = _srcReader->getTotalAudioBytes();
  77. _audInfo._wfx = _srcReader->getWaveFormatInfo();
  78. _isReady = true;
  79. _retry = false;
  80. invokePlayCallbacks();
  81. }
  82. if (!_isReady) {
  83. _retry = true;
  84. log("Failed to read input file: %s.\n", _fileFullPath.c_str());
  85. }
  86. invokeLoadCallbacks();
  87. }
  88. void AudioCache::addPlayCallback(const std::function<void()> &callback)
  89. {
  90. _cbMutex.lock();
  91. if (_isReady) {
  92. callback();
  93. }
  94. else {
  95. _callbacks.push_back(callback);
  96. }
  97. _cbMutex.unlock();
  98. if (_retry) {
  99. readDataTask();
  100. }
  101. }
  102. void AudioCache::addLoadCallback(const std::function<void(bool)> &callback)
  103. {
  104. if (_isReady) {
  105. callback(true);
  106. }
  107. else {
  108. _loadCallbacks.push_back(callback);
  109. }
  110. if (_retry) {
  111. readDataTask();
  112. }
  113. }
  114. void AudioCache::invokePlayCallbacks()
  115. {
  116. _cbMutex.lock();
  117. auto cnt = _callbacks.size();
  118. for (size_t ind = 0; ind < cnt; ind++)
  119. {
  120. _callbacks[ind]();
  121. }
  122. _callbacks.clear();
  123. _cbMutex.unlock();
  124. }
  125. void AudioCache::invokeLoadCallbacks()
  126. {
  127. auto scheduler = Director::getInstance()->getScheduler();
  128. scheduler->performFunctionInCocosThread([&](){
  129. auto cnt = _loadCallbacks.size();
  130. for (size_t ind = 0; ind < cnt; ind++)
  131. {
  132. _loadCallbacks[ind](_isReady);
  133. }
  134. _loadCallbacks.clear();
  135. });
  136. }
  137. bool AudioCache::getChunk(AudioDataChunk& chunk)
  138. {
  139. bool ret = false;
  140. if (nullptr != _srcReader) {
  141. ret = _srcReader->consumeChunk(chunk);
  142. }
  143. return ret;
  144. }
  145. void AudioCache::doBuffering()
  146. {
  147. if (isStreamingSource()){
  148. _srcReader->produceChunk();
  149. }
  150. }
  151. bool AudioCache::isStreamingSource()
  152. {
  153. if (nullptr != _srcReader) {
  154. return _srcReader->isStreamingSource();
  155. }
  156. return false;
  157. }
  158. void AudioCache::seek(const float ratio)
  159. {
  160. if (nullptr != _srcReader){
  161. _srcReader->seekTo(ratio);
  162. }
  163. }
  164. // AudioPlayer
  165. AudioPlayer::AudioPlayer()
  166. : _loop(false)
  167. , _ready(false)
  168. , _current(0.0)
  169. , _volume(0.0)
  170. , _duration(0.0)
  171. , _cache(nullptr)
  172. , _totalSamples(0)
  173. , _samplesOffset(0)
  174. , _criticalError(false)
  175. , _isStreaming(false)
  176. , _finishCallback(nullptr)
  177. , _xaMasterVoice(nullptr)
  178. , _xaSourceVoice(nullptr)
  179. , _state(AudioPlayerState::INITIALIZING)
  180. {
  181. init();
  182. }
  183. AudioPlayer::~AudioPlayer()
  184. {
  185. free();
  186. }
  187. void AudioPlayer::stop()
  188. {
  189. _stop();
  190. }
  191. void AudioPlayer::pause()
  192. {
  193. _stop(true);
  194. }
  195. bool AudioPlayer::update()
  196. {
  197. if (_criticalError){
  198. free();
  199. init();
  200. }
  201. if (_cache != nullptr) {
  202. _cache->doBuffering();
  203. }
  204. //log("bufferQueued: %d, _current: %f", _cachedBufferQ.size(), _current);
  205. return _criticalError;
  206. }
  207. void AudioPlayer::resume()
  208. {
  209. _play(true);
  210. }
  211. bool AudioPlayer::isInError()
  212. {
  213. return _criticalError;
  214. }
  215. float AudioPlayer::getDuration()
  216. {
  217. if (nullptr == _cache) {
  218. return _duration;
  219. }
  220. auto fmt = _cache->_audInfo._wfx;
  221. if (!fmt.nChannels) {
  222. return _duration;
  223. }
  224. if ((int)_duration <= 0)
  225. {
  226. switch (fmt.wFormatTag)
  227. {
  228. case WAVE_FORMAT_PCM:
  229. case WAVE_FORMAT_ADPCM:
  230. _duration = (float(_cache->_audInfo._totalAudioBytes / ((fmt.wBitsPerSample / 8) * fmt.nChannels)) / fmt.nSamplesPerSec) * 1000;
  231. _totalSamples = fmt.nSamplesPerSec * _duration / 1000;
  232. break;
  233. default:
  234. break;
  235. }
  236. }
  237. return _duration;
  238. }
  239. float AudioPlayer::getCurrentTime()
  240. {
  241. _stMutex.lock();
  242. auto samplesPlayed = getSourceVoiceState().SamplesPlayed;
  243. //log("_samplesOffset: %lu, samplesPlayed: %lu, _current: %f", (UINT32)_samplesOffset, (UINT32)samplesPlayed, _current);
  244. _current += ((samplesPlayed - _samplesOffset) / (float)_totalSamples) * _duration;
  245. _current = _current > _duration ? 0.0 : _current;
  246. _samplesOffset = samplesPlayed;
  247. _stMutex.unlock();
  248. return _current;
  249. }
  250. bool AudioPlayer::setTime(float time)
  251. {
  252. bool ret = true;
  253. _stop();
  254. if (!_isStreaming) {
  255. auto fmt = _cache->_audInfo._wfx;
  256. int seek = (time / _duration) * _totalSamples;
  257. seek -= (seek % (fmt.nChannels * fmt.nBlockAlign));
  258. _xaBuffer.LoopCount = 0;
  259. _xaBuffer.PlayBegin = seek;
  260. _xaBuffer.PlayLength = _totalSamples - seek;
  261. if (_xaBuffer.PlayBegin >= _totalSamples) {
  262. _xaBuffer.PlayBegin = _totalSamples - (fmt.nChannels * fmt.nBlockAlign);
  263. _xaBuffer.PlayLength = (fmt.nChannels * fmt.nBlockAlign);
  264. }
  265. }
  266. _stMutex.lock();
  267. _samplesOffset = getSourceVoiceState().SamplesPlayed;
  268. _current = time;
  269. _stMutex.unlock();
  270. _play();
  271. return ret;
  272. }
  273. void AudioPlayer::setVolume(float volume)
  274. {
  275. if (_xaMasterVoice != nullptr){
  276. if (FAILED(_xaMasterVoice->SetVolume(volume))) {
  277. error();
  278. }
  279. else {
  280. _volume = volume;
  281. }
  282. }
  283. }
  284. bool AudioPlayer::play2d(AudioCache* cache)
  285. {
  286. bool ret = false;
  287. HRESULT hr = S_OK;
  288. if (cache != nullptr)
  289. {
  290. _cache = cache;
  291. if (nullptr == _xaSourceVoice && _ready) {
  292. XAUDIO2_SEND_DESCRIPTOR descriptors[1];
  293. descriptors[0].pOutputVoice = _xaMasterVoice;
  294. descriptors[0].Flags = 0;
  295. XAUDIO2_VOICE_SENDS sends = { 0 };
  296. sends.SendCount = 1;
  297. sends.pSends = descriptors;
  298. hr = _xaEngine->CreateSourceVoice(&_xaSourceVoice, &cache->_audInfo._wfx, 0, 1.0, this, &sends);
  299. }
  300. if (SUCCEEDED(hr)) {
  301. _isStreaming = _cache->isStreamingSource();
  302. _duration = getDuration();
  303. ret = _play();
  304. }
  305. else {
  306. error();
  307. }
  308. }
  309. return ret;
  310. }
  311. void AudioPlayer::init()
  312. {
  313. do {
  314. memset(&_xaBuffer, 0, sizeof(_xaBuffer));
  315. if (FAILED(XAudio2Create(_xaEngine.ReleaseAndGetAddressOf()))) {
  316. error();
  317. break;
  318. }
  319. #if defined(_DEBUG)
  320. XAUDIO2_DEBUG_CONFIGURATION debugConfig = { 0 };
  321. debugConfig.BreakMask = XAUDIO2_LOG_ERRORS;
  322. debugConfig.TraceMask = XAUDIO2_LOG_ERRORS;
  323. _xaEngine->SetDebugConfiguration(&debugConfig);
  324. #endif
  325. _xaEngine->RegisterForCallbacks(this);
  326. if (FAILED(_xaEngine->CreateMasteringVoice(&_xaMasterVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, nullptr, nullptr, AudioCategory_GameMedia))) {
  327. error();
  328. break;
  329. }
  330. _ready = true;
  331. _state = AudioPlayerState::READY;
  332. } while (false);
  333. }
  334. void AudioPlayer::free()
  335. {
  336. _ready = false;
  337. _stop();
  338. memset(&_xaBuffer, 0, sizeof(_xaBuffer));
  339. if (_xaEngine) {
  340. _xaEngine->UnregisterForCallbacks(this);
  341. _xaEngine->StopEngine();
  342. }
  343. if (_xaSourceVoice != nullptr) {
  344. _xaSourceVoice->DestroyVoice();
  345. _xaSourceVoice = nullptr;
  346. }
  347. if (_xaMasterVoice != nullptr) {
  348. _xaMasterVoice->DestroyVoice();
  349. _xaMasterVoice = nullptr;
  350. }
  351. while (!_cachedBufferQ.empty()) {
  352. popBuffer();
  353. }
  354. }
  355. bool AudioPlayer::_play(bool resume)
  356. {
  357. do {
  358. if (!resume) {
  359. _cache->seek(_current / _duration);
  360. submitBuffers();
  361. }
  362. if (_state == AudioPlayerState::PAUSED && !resume || nullptr == _xaSourceVoice) break;
  363. if (FAILED(_xaMasterVoice->SetVolume(_volume)) || FAILED(_xaSourceVoice->Start())) {
  364. error();
  365. }
  366. else {
  367. _state = AudioPlayerState::PLAYING;
  368. }
  369. } while (false);
  370. return !_criticalError;
  371. }
  372. void AudioPlayer::_stop(bool pause)
  373. {
  374. if (_xaSourceVoice != nullptr) {
  375. if (FAILED(_xaSourceVoice->Stop())) {
  376. error();
  377. }
  378. else {
  379. if (!pause) {
  380. _xaSourceVoice->FlushSourceBuffers();
  381. if (_state != AudioPlayerState::PAUSED) _state = AudioPlayerState::STOPPED;
  382. }
  383. else {
  384. _state = AudioPlayerState::PAUSED;
  385. }
  386. }
  387. }
  388. }
  389. void AudioPlayer::error()
  390. {
  391. _criticalError = true;
  392. _ready = false;
  393. _state = AudioPlayerState::ERRORED;
  394. CCLOG("Audio system encountered error.");
  395. }
  396. void AudioPlayer::popBuffer()
  397. {
  398. _bqMutex.lock();
  399. if (!_cachedBufferQ.empty()) {
  400. _cachedBufferQ.pop();
  401. }
  402. _bqMutex.unlock();
  403. }
  404. bool AudioPlayer::submitBuffers()
  405. {
  406. bool ret = false;
  407. _bqMutex.lock();
  408. do {
  409. if (nullptr == _xaSourceVoice) break;
  410. if (!_cachedBufferQ.size() || (_isStreaming && _cachedBufferQ.size() < QUEUEBUFFER_NUM)) {
  411. AudioDataChunk chunk;
  412. if (_cache->getChunk(chunk) && chunk._dataSize) {
  413. _xaBuffer.AudioBytes = static_cast<UINT32>(chunk._dataSize);
  414. _xaBuffer.pAudioData = chunk._data->data();
  415. _xaBuffer.Flags = chunk._endOfStream ? XAUDIO2_END_OF_STREAM : 0;
  416. _cachedBufferQ.push(chunk);
  417. ret = SUCCEEDED(_xaSourceVoice->SubmitSourceBuffer(&_xaBuffer));
  418. if (!_isStreaming) break;
  419. }
  420. else {
  421. break;
  422. }
  423. }
  424. else if (!_isStreaming) {
  425. ret = SUCCEEDED(_xaSourceVoice->SubmitSourceBuffer(&_xaBuffer));
  426. break;
  427. }
  428. else {
  429. break;
  430. }
  431. } while (ret);
  432. _bqMutex.unlock();
  433. return ret;
  434. }
  435. void AudioPlayer::updateState()
  436. {
  437. if (!_isStreaming) {
  438. _stMutex.lock();
  439. _samplesOffset = getSourceVoiceState().SamplesPlayed;
  440. _stMutex.unlock();
  441. }
  442. else {
  443. if (_cachedBufferQ.size() > getSourceVoiceState(true).BuffersQueued) {
  444. popBuffer();
  445. }
  446. }
  447. }
  448. void AudioPlayer::onBufferRunOut()
  449. {
  450. _stMutex.lock();
  451. _samplesOffset = 0;
  452. _current = 0.0;
  453. _xaBuffer.PlayBegin = _xaBuffer.PlayLength = 0;
  454. _stMutex.unlock();
  455. if (!_loop) {
  456. _stop();
  457. //invokeFinishCallback();
  458. }
  459. else {
  460. _play();
  461. }
  462. }
  463. void AudioPlayer::invokeFinishCallback()
  464. {
  465. if (_finishCallback) {
  466. _finishCallback(0, "");
  467. }
  468. }
  469. XAUDIO2_VOICE_STATE AudioPlayer::getSourceVoiceState(bool fast)
  470. {
  471. XAUDIO2_VOICE_STATE state;
  472. memset(&state, 0, sizeof(XAUDIO2_VOICE_STATE));
  473. if (_xaSourceVoice != nullptr) {
  474. _xaSourceVoice->GetState(&state, fast ? XAUDIO2_VOICE_NOSAMPLESPLAYED : 0);
  475. }
  476. return state;
  477. }
  478. // IXAudio2EngineCallback
  479. void AudioPlayer::OnProcessingPassStart()
  480. {
  481. }
  482. void AudioPlayer::OnProcessingPassEnd()
  483. {
  484. }
  485. void AudioPlayer::OnCriticalError(HRESULT err)
  486. {
  487. UNREFERENCED_PARAMETER(err);
  488. if (_ready) {
  489. error();
  490. }
  491. }
  492. // IXAudio2VoiceCallback
  493. void AudioPlayer::OnVoiceProcessingPassStart(UINT32 uBytesRequired)
  494. {
  495. if (_ready && uBytesRequired && _isStreaming){
  496. submitBuffers();
  497. }
  498. }
  499. void AudioPlayer::OnVoiceProcessingPassEnd()
  500. {
  501. }
  502. void AudioPlayer::OnStreamEnd()
  503. {
  504. if (_ready) {
  505. onBufferRunOut();
  506. }
  507. }
  508. void AudioPlayer::OnBufferStart(void* pBufferContext)
  509. {
  510. UNREFERENCED_PARAMETER(pBufferContext);
  511. }
  512. void AudioPlayer::OnBufferEnd(void* pBufferContext)
  513. {
  514. UNREFERENCED_PARAMETER(pBufferContext);
  515. if (_ready) {
  516. updateState();
  517. }
  518. }
  519. void AudioPlayer::OnLoopEnd(void* pBufferContext)
  520. {
  521. UNREFERENCED_PARAMETER(pBufferContext);
  522. if (_ready && !_loop) {
  523. _stop();
  524. }
  525. }
  526. void AudioPlayer::OnVoiceError(void* pBufferContext, HRESULT err)
  527. {
  528. UNREFERENCED_PARAMETER(pBufferContext);
  529. UNREFERENCED_PARAMETER(err);
  530. if (_ready) {
  531. error();
  532. }
  533. }
  534. #endif