BsOAAudioSource.cpp 16 KB

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