BsOAAudioSource.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsOAAudioSource.h"
  4. #include "BsOAAudio.h"
  5. #include "BsOAAudioClip.h"
  6. #include "AL/al.h"
  7. namespace bs
  8. {
  9. OAAudioSource::OAAudioSource()
  10. : mSavedTime(0.0f), mSavedState(AudioSourceState::Stopped), mState(AudioSourceState::Stopped)
  11. , mGloballyPaused(false), mStreamBuffers(), mBusyBuffers(), mStreamProcessedPosition(0), mStreamQueuedPosition(0)
  12. , mIsStreaming(false)
  13. {
  14. gOAAudio()._registerSource(this);
  15. rebuild();
  16. }
  17. OAAudioSource::~OAAudioSource()
  18. {
  19. clear();
  20. gOAAudio()._unregisterSource(this);
  21. }
  22. void OAAudioSource::setClip(const HAudioClip& clip)
  23. {
  24. stop();
  25. Lock lock(mMutex);
  26. AudioSource::setClip(clip);
  27. applyClip();
  28. }
  29. void OAAudioSource::setTransform(const Transform& transform)
  30. {
  31. AudioSource::setTransform(transform);
  32. auto& contexts = gOAAudio()._getContexts();
  33. UINT32 numContexts = (UINT32)contexts.size();
  34. for (UINT32 i = 0; i < numContexts; i++)
  35. {
  36. if (contexts.size() > 1)
  37. alcMakeContextCurrent(contexts[i]);
  38. if (is3D())
  39. {
  40. Vector3 position = transform.getPosition();
  41. alSource3f(mSourceIDs[i], AL_POSITION, position.x, position.y, position.z);
  42. }
  43. else
  44. alSource3f(mSourceIDs[i], AL_POSITION, 0.0f, 0.0f, 0.0f);
  45. }
  46. }
  47. void OAAudioSource::setVelocity(const Vector3& velocity)
  48. {
  49. AudioSource::setVelocity(velocity);
  50. auto& contexts = gOAAudio()._getContexts();
  51. UINT32 numContexts = (UINT32)contexts.size();
  52. for (UINT32 i = 0; i < numContexts; i++)
  53. {
  54. if (contexts.size() > 1)
  55. alcMakeContextCurrent(contexts[i]);
  56. if (is3D())
  57. alSource3f(mSourceIDs[i], AL_VELOCITY, velocity.x, velocity.y, velocity.z);
  58. else
  59. alSource3f(mSourceIDs[i], AL_VELOCITY, 0.0f, 0.0f, 0.0f);
  60. }
  61. }
  62. void OAAudioSource::setVolume(float volume)
  63. {
  64. AudioSource::setVolume(volume);
  65. auto& contexts = gOAAudio()._getContexts();
  66. UINT32 numContexts = (UINT32)contexts.size();
  67. for (UINT32 i = 0; i < numContexts; i++)
  68. {
  69. if (contexts.size() > 1)
  70. alcMakeContextCurrent(contexts[i]);
  71. alSourcef(mSourceIDs[i], AL_GAIN, mVolume);
  72. }
  73. }
  74. void OAAudioSource::setPitch(float pitch)
  75. {
  76. AudioSource::setPitch(pitch);
  77. auto& contexts = gOAAudio()._getContexts();
  78. UINT32 numContexts = (UINT32)contexts.size();
  79. for (UINT32 i = 0; i < numContexts; i++)
  80. {
  81. if (contexts.size() > 1)
  82. alcMakeContextCurrent(contexts[i]);
  83. alSourcef(mSourceIDs[i], AL_PITCH, pitch);
  84. }
  85. }
  86. void OAAudioSource::setIsLooping(bool loop)
  87. {
  88. AudioSource::setIsLooping(loop);
  89. // When streaming we handle looping manually
  90. if (requiresStreaming())
  91. loop = false;
  92. auto& contexts = gOAAudio()._getContexts();
  93. UINT32 numContexts = (UINT32)contexts.size();
  94. for (UINT32 i = 0; i < numContexts; i++)
  95. {
  96. if (contexts.size() > 1)
  97. alcMakeContextCurrent(contexts[i]);
  98. alSourcef(mSourceIDs[i], AL_LOOPING, loop);
  99. }
  100. }
  101. void OAAudioSource::setPriority(INT32 priority)
  102. {
  103. AudioSource::setPriority(priority);
  104. // Do nothing, OpenAL doesn't support priorities (perhaps emulate the behaviour by manually disabling sources?)
  105. }
  106. void OAAudioSource::setMinDistance(float distance)
  107. {
  108. AudioSource::setMinDistance(distance);
  109. auto& contexts = gOAAudio()._getContexts();
  110. UINT32 numContexts = (UINT32)contexts.size();
  111. for (UINT32 i = 0; i < numContexts; i++)
  112. {
  113. if (contexts.size() > 1)
  114. alcMakeContextCurrent(contexts[i]);
  115. alSourcef(mSourceIDs[i], AL_REFERENCE_DISTANCE, distance);
  116. }
  117. }
  118. void OAAudioSource::setAttenuation(float attenuation)
  119. {
  120. AudioSource::setAttenuation(attenuation);
  121. auto& contexts = gOAAudio()._getContexts();
  122. UINT32 numContexts = (UINT32)contexts.size();
  123. for (UINT32 i = 0; i < numContexts; i++)
  124. {
  125. if (contexts.size() > 1)
  126. alcMakeContextCurrent(contexts[i]);
  127. alSourcef(mSourceIDs[i], AL_ROLLOFF_FACTOR, attenuation);
  128. }
  129. }
  130. void OAAudioSource::play()
  131. {
  132. mState = AudioSourceState::Playing;
  133. if (mGloballyPaused)
  134. return;
  135. if(requiresStreaming())
  136. {
  137. Lock lock(mMutex);
  138. if (!mIsStreaming)
  139. {
  140. startStreaming();
  141. stream(); // Stream first block on this thread to ensure something can play right away
  142. }
  143. }
  144. auto& contexts = gOAAudio()._getContexts();
  145. UINT32 numContexts = (UINT32)contexts.size();
  146. for (UINT32 i = 0; i < numContexts; i++)
  147. {
  148. if (contexts.size() > 1)
  149. alcMakeContextCurrent(contexts[i]);
  150. alSourcePlay(mSourceIDs[i]);
  151. // Non-3D clips need to play only on a single source
  152. // Note: I'm still creating sourcs objects (and possibly queuing streaming buffers) for these non-playing
  153. // sources. It would be possible to optimize them out at cost of more complexity. At this time it doesn't feel
  154. // worth it.
  155. if(!is3D())
  156. break;
  157. }
  158. }
  159. void OAAudioSource::pause()
  160. {
  161. mState = AudioSourceState::Paused;
  162. auto& contexts = gOAAudio()._getContexts();
  163. UINT32 numContexts = (UINT32)contexts.size();
  164. for (UINT32 i = 0; i < numContexts; i++)
  165. {
  166. if (contexts.size() > 1)
  167. alcMakeContextCurrent(contexts[i]);
  168. alSourcePause(mSourceIDs[i]);
  169. }
  170. }
  171. void OAAudioSource::stop()
  172. {
  173. mState = AudioSourceState::Stopped;
  174. auto& contexts = gOAAudio()._getContexts();
  175. UINT32 numContexts = (UINT32)contexts.size();
  176. for (UINT32 i = 0; i < numContexts; i++)
  177. {
  178. if (contexts.size() > 1)
  179. alcMakeContextCurrent(contexts[i]);
  180. alSourceStop(mSourceIDs[i]);
  181. alSourcef(mSourceIDs[i], AL_SEC_OFFSET, 0.0f);
  182. }
  183. {
  184. Lock lock(mMutex);
  185. mStreamProcessedPosition = 0;
  186. mStreamQueuedPosition = 0;
  187. if (mIsStreaming)
  188. stopStreaming();
  189. }
  190. }
  191. void OAAudioSource::setGlobalPause(bool pause)
  192. {
  193. if (mGloballyPaused == pause)
  194. return;
  195. mGloballyPaused = pause;
  196. if (getState() == AudioSourceState::Playing)
  197. {
  198. if (pause)
  199. {
  200. auto& contexts = gOAAudio()._getContexts();
  201. UINT32 numContexts = (UINT32)contexts.size();
  202. for (UINT32 i = 0; i < numContexts; i++)
  203. {
  204. if (contexts.size() > 1)
  205. alcMakeContextCurrent(contexts[i]);
  206. alSourcePause(mSourceIDs[i]);
  207. }
  208. }
  209. else
  210. {
  211. play();
  212. }
  213. }
  214. }
  215. void OAAudioSource::setTime(float time)
  216. {
  217. if (!mAudioClip.isLoaded())
  218. return;
  219. AudioSourceState state = getState();
  220. stop();
  221. bool needsStreaming = requiresStreaming();
  222. float clipTime;
  223. {
  224. Lock lock(mMutex);
  225. if (!needsStreaming)
  226. clipTime = time;
  227. else
  228. {
  229. if (mAudioClip.isLoaded())
  230. mStreamProcessedPosition = (UINT32)(time * mAudioClip->getFrequency() * mAudioClip->getNumChannels());
  231. else
  232. mStreamProcessedPosition = 0;
  233. mStreamQueuedPosition = mStreamProcessedPosition;
  234. clipTime = 0.0f;
  235. }
  236. }
  237. auto& contexts = gOAAudio()._getContexts();
  238. UINT32 numContexts = (UINT32)contexts.size();
  239. for (UINT32 i = 0; i < numContexts; i++)
  240. {
  241. if (contexts.size() > 1)
  242. alcMakeContextCurrent(contexts[i]);
  243. alSourcef(mSourceIDs[i], AL_SEC_OFFSET, clipTime);
  244. }
  245. if (state != AudioSourceState::Stopped)
  246. play();
  247. if (state == AudioSourceState::Paused)
  248. pause();
  249. }
  250. float OAAudioSource::getTime() const
  251. {
  252. Lock lock(mMutex);
  253. auto& contexts = gOAAudio()._getContexts();
  254. if (contexts.size() > 1)
  255. alcMakeContextCurrent(contexts[0]);
  256. bool needsStreaming = requiresStreaming();
  257. float time;
  258. if (!needsStreaming)
  259. {
  260. alGetSourcef(mSourceIDs[0], AL_SEC_OFFSET, &time);
  261. return time;
  262. }
  263. else
  264. {
  265. float timeOffset = 0.0f;
  266. if (mAudioClip.isLoaded())
  267. timeOffset = (float)mStreamProcessedPosition / mAudioClip->getFrequency() / mAudioClip->getNumChannels();
  268. // When streaming, the returned offset is relative to the last queued buffer
  269. alGetSourcef(mSourceIDs[0], AL_SEC_OFFSET, &time);
  270. return timeOffset + time;
  271. }
  272. }
  273. void OAAudioSource::clear()
  274. {
  275. mSavedState = getState();
  276. mSavedTime = getTime();
  277. stop();
  278. auto& contexts = gOAAudio()._getContexts();
  279. UINT32 numContexts = (UINT32)contexts.size();
  280. Lock lock(mMutex);
  281. for (UINT32 i = 0; i < numContexts; i++)
  282. {
  283. if (contexts.size() > 1)
  284. alcMakeContextCurrent(contexts[i]);
  285. alSourcei(mSourceIDs[i], AL_BUFFER, 0);
  286. alDeleteSources(1, &mSourceIDs[i]);
  287. }
  288. mSourceIDs.clear();
  289. }
  290. void OAAudioSource::rebuild()
  291. {
  292. auto& contexts = gOAAudio()._getContexts();
  293. UINT32 numContexts = (UINT32)contexts.size();
  294. {
  295. Lock lock(mMutex);
  296. for (UINT32 i = 0; i < numContexts; i++)
  297. {
  298. if (contexts.size() > 1)
  299. alcMakeContextCurrent(contexts[i]);
  300. UINT32 source = 0;
  301. alGenSources(1, &source);
  302. mSourceIDs.push_back(source);
  303. }
  304. }
  305. for (UINT32 i = 0; i < numContexts; i++)
  306. {
  307. if (contexts.size() > 1)
  308. alcMakeContextCurrent(contexts[i]);
  309. alSourcef(mSourceIDs[i], AL_PITCH, mPitch);
  310. alSourcef(mSourceIDs[i], AL_REFERENCE_DISTANCE, mMinDistance);
  311. alSourcef(mSourceIDs[i], AL_ROLLOFF_FACTOR, mAttenuation);
  312. if(requiresStreaming())
  313. alSourcef(mSourceIDs[i], AL_LOOPING, false);
  314. else
  315. alSourcef(mSourceIDs[i], AL_LOOPING, mLoop);
  316. if (is3D())
  317. {
  318. Vector3 position = mTransform.getPosition();
  319. alSourcei(mSourceIDs[i], AL_SOURCE_RELATIVE, false);
  320. alSource3f(mSourceIDs[i], AL_POSITION, position.x, position.y, position.z);
  321. alSource3f(mSourceIDs[i], AL_VELOCITY, mVelocity.x, mVelocity.y, mVelocity.z);
  322. }
  323. else
  324. {
  325. alSourcei(mSourceIDs[i], AL_SOURCE_RELATIVE, true);
  326. alSource3f(mSourceIDs[i], AL_POSITION, 0.0f, 0.0f, 0.0f);
  327. alSource3f(mSourceIDs[i], AL_VELOCITY, 0.0f, 0.0f, 0.0f);
  328. }
  329. {
  330. Lock lock(mMutex);
  331. if (!mIsStreaming)
  332. {
  333. UINT32 oaBuffer = 0;
  334. if (mAudioClip.isLoaded())
  335. {
  336. OAAudioClip* oaClip = static_cast<OAAudioClip*>(mAudioClip.get());
  337. oaBuffer = oaClip->_getOpenALBuffer();
  338. }
  339. alSourcei(mSourceIDs[i], AL_BUFFER, oaBuffer);
  340. }
  341. }
  342. }
  343. setTime(mSavedTime);
  344. if (mSavedState != AudioSourceState::Stopped)
  345. play();
  346. if (mSavedState == AudioSourceState::Paused)
  347. pause();
  348. }
  349. void OAAudioSource::startStreaming()
  350. {
  351. assert(!mIsStreaming);
  352. alGenBuffers(StreamBufferCount, mStreamBuffers);
  353. gOAAudio().startStreaming(this);
  354. memset(&mBusyBuffers, 0, sizeof(mBusyBuffers));
  355. mIsStreaming = true;
  356. }
  357. void OAAudioSource::stopStreaming()
  358. {
  359. assert(mIsStreaming);
  360. mIsStreaming = false;
  361. gOAAudio().stopStreaming(this);
  362. auto& contexts = gOAAudio()._getContexts();
  363. UINT32 numContexts = (UINT32)contexts.size();
  364. for (UINT32 i = 0; i < numContexts; i++)
  365. {
  366. if (contexts.size() > 1)
  367. alcMakeContextCurrent(contexts[i]);
  368. INT32 numQueuedBuffers;
  369. alGetSourcei(mSourceIDs[i], AL_BUFFERS_QUEUED, &numQueuedBuffers);
  370. UINT32 buffer;
  371. for (INT32 j = 0; j < numQueuedBuffers; j++)
  372. alSourceUnqueueBuffers(mSourceIDs[i], 1, &buffer);
  373. }
  374. alDeleteBuffers(StreamBufferCount, mStreamBuffers);
  375. }
  376. void OAAudioSource::stream()
  377. {
  378. Lock lock(mMutex);
  379. AudioDataInfo info;
  380. info.bitDepth = mAudioClip->getBitDepth();
  381. info.numChannels = mAudioClip->getNumChannels();
  382. info.sampleRate = mAudioClip->getFrequency();
  383. info.numSamples = 0;
  384. UINT32 totalNumSamples = mAudioClip->getNumSamples();
  385. // Note: It is safe to access contexts here only because it is guaranteed by the OAAudio manager that it will always
  386. // stop all streaming before changing contexts. Otherwise a mutex lock would be needed for every context access.
  387. auto& contexts = gOAAudio()._getContexts();
  388. UINT32 numContexts = (UINT32)contexts.size();
  389. for (UINT32 i = 0; i < numContexts; i++)
  390. {
  391. if (contexts.size() > 1)
  392. alcMakeContextCurrent(contexts[i]);
  393. INT32 numProcessedBuffers = 0;
  394. alGetSourcei(mSourceIDs[i], AL_BUFFERS_PROCESSED, &numProcessedBuffers);
  395. for (INT32 j = numProcessedBuffers; j > 0; j--)
  396. {
  397. UINT32 buffer;
  398. alSourceUnqueueBuffers(mSourceIDs[i], 1, &buffer);
  399. INT32 bufferIdx = -1;
  400. for (UINT32 k = 0; k < StreamBufferCount; k++)
  401. {
  402. if (buffer == mStreamBuffers[k])
  403. {
  404. bufferIdx = k;
  405. break;
  406. }
  407. }
  408. // Possibly some buffer from previous playback remained unqueued, in which case ignore it
  409. if (bufferIdx == -1)
  410. continue;
  411. mBusyBuffers[bufferIdx] &= ~(1 << bufferIdx);
  412. // Check if all sources are done with this buffer
  413. if (mBusyBuffers[bufferIdx] != 0)
  414. break;
  415. INT32 bufferSize;
  416. INT32 bufferBits;
  417. alGetBufferi(buffer, AL_SIZE, &bufferSize);
  418. alGetBufferi(buffer, AL_BITS, &bufferBits);
  419. if (bufferBits == 0)
  420. {
  421. LOGERR("Error decoding stream.");
  422. return;
  423. }
  424. else
  425. {
  426. UINT32 bytesPerSample = bufferBits / 8;
  427. mStreamProcessedPosition += bufferSize / bytesPerSample;
  428. }
  429. if (mStreamProcessedPosition == totalNumSamples) // Reached the end
  430. {
  431. mStreamProcessedPosition = 0;
  432. if (!mLoop) // Variable used on both threads and not thread safe, but it doesn't matter
  433. {
  434. stopStreaming();
  435. return;
  436. }
  437. }
  438. }
  439. }
  440. for(UINT32 i = 0; i < StreamBufferCount; i++)
  441. {
  442. if (mBusyBuffers[i] != 0)
  443. continue;
  444. if (fillBuffer(mStreamBuffers[i], info, totalNumSamples))
  445. {
  446. for (auto& source : mSourceIDs)
  447. alSourceQueueBuffers(source, 1, &mStreamBuffers[i]);
  448. mBusyBuffers[i] |= 1 << i;
  449. }
  450. else
  451. break;
  452. }
  453. }
  454. bool OAAudioSource::fillBuffer(UINT32 buffer, AudioDataInfo& info, UINT32 maxNumSamples)
  455. {
  456. UINT32 numRemainingSamples = maxNumSamples - mStreamQueuedPosition;
  457. if (numRemainingSamples == 0) // Reached the end
  458. {
  459. if (mLoop)
  460. {
  461. mStreamQueuedPosition = 0;
  462. numRemainingSamples = maxNumSamples;
  463. }
  464. else // If not looping, don't queue any more buffers, we're done
  465. return false;
  466. }
  467. // Read audio data
  468. UINT32 numSamples = std::min(numRemainingSamples, info.sampleRate * info.numChannels); // 1 second of data
  469. UINT32 sampleBufferSize = numSamples * (info.bitDepth / 8);
  470. UINT8* samples = (UINT8*)bs_stack_alloc(sampleBufferSize);
  471. OAAudioClip* audioClip = static_cast<OAAudioClip*>(mAudioClip.get());
  472. audioClip->getSamples(samples, mStreamQueuedPosition, numSamples);
  473. mStreamQueuedPosition += numSamples;
  474. info.numSamples = numSamples;
  475. gOAAudio()._writeToOpenALBuffer(buffer, samples, info);
  476. bs_stack_free(samples);
  477. return true;
  478. }
  479. void OAAudioSource::applyClip()
  480. {
  481. auto& contexts = gOAAudio()._getContexts();
  482. UINT32 numContexts = (UINT32)contexts.size();
  483. for (UINT32 i = 0; i < numContexts; i++)
  484. {
  485. if (contexts.size() > 1)
  486. alcMakeContextCurrent(contexts[i]);
  487. alSourcei(mSourceIDs[i], AL_SOURCE_RELATIVE, !is3D());
  488. if (!requiresStreaming())
  489. {
  490. UINT32 oaBuffer = 0;
  491. if (mAudioClip.isLoaded())
  492. {
  493. OAAudioClip* oaClip = static_cast<OAAudioClip*>(mAudioClip.get());
  494. oaBuffer = oaClip->_getOpenALBuffer();
  495. }
  496. alSourcei(mSourceIDs[i], AL_BUFFER, oaBuffer);
  497. }
  498. }
  499. // Looping is influenced by streaming mode, so re-apply it in case it changed
  500. setIsLooping(mLoop);
  501. }
  502. void OAAudioSource::onClipChanged()
  503. {
  504. AudioSourceState state = getState();
  505. float savedTime = getTime();
  506. stop();
  507. {
  508. Lock lock(mMutex);
  509. applyClip();
  510. }
  511. setTime(savedTime);
  512. if (state != AudioSourceState::Stopped)
  513. play();
  514. if (state == AudioSourceState::Paused)
  515. pause();
  516. }
  517. bool OAAudioSource::is3D() const
  518. {
  519. if (!mAudioClip.isLoaded())
  520. return true;
  521. return mAudioClip->is3D();
  522. }
  523. bool OAAudioSource::requiresStreaming() const
  524. {
  525. if (!mAudioClip.isLoaded())
  526. return false;
  527. AudioReadMode readMode = mAudioClip->getReadMode();
  528. bool isCompressed = readMode == AudioReadMode::LoadCompressed && mAudioClip->getFormat() != AudioFormat::PCM;
  529. return (readMode == AudioReadMode::Stream) || isCompressed;
  530. }
  531. }