BsOAAudio.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsOAAudio.h"
  4. #include "BsOAAudioClip.h"
  5. #include "BsOAAudioListener.h"
  6. #include "BsOAAudioSource.h"
  7. #include "BsMath.h"
  8. #include "AL\al.h"
  9. namespace BansheeEngine
  10. {
  11. OAAudio::OAAudio()
  12. :mVolume(1.0f)
  13. {
  14. bool enumeratedDevices;
  15. if(_isExtensionSupported("ALC_ENUMERATE_ALL_EXT"))
  16. {
  17. const ALCchar* defaultDevice = alcGetString(nullptr, ALC_DEFAULT_ALL_DEVICES_SPECIFIER);
  18. mDefaultDevice.name = toWString(defaultDevice);
  19. const ALCchar* devices = alcGetString(nullptr, ALC_ALL_DEVICES_SPECIFIER);
  20. Vector<wchar_t> deviceName;
  21. while(true)
  22. {
  23. if(*devices == 0)
  24. {
  25. if (deviceName.size() == 0)
  26. break;
  27. mAllDevices.push_back({ WString(deviceName.data(), deviceName.size()) });
  28. deviceName.clear();
  29. continue;
  30. }
  31. deviceName.push_back(*devices);
  32. devices++;
  33. }
  34. enumeratedDevices = true;
  35. }
  36. else
  37. {
  38. mAllDevices.push_back({ L"" });
  39. enumeratedDevices = false;
  40. }
  41. mActiveDevice = mDefaultDevice;
  42. String defaultDeviceName = toString(mDefaultDevice.name);
  43. if(enumeratedDevices)
  44. mDevice = alcOpenDevice(defaultDeviceName.c_str());
  45. else
  46. mDevice = alcOpenDevice(nullptr);
  47. if (mDevice == nullptr)
  48. {
  49. BS_EXCEPT(InternalErrorException, "Failed to open OpenAL device: " + defaultDeviceName);
  50. return;
  51. }
  52. rebuildContexts();
  53. }
  54. OAAudio::~OAAudio()
  55. {
  56. assert(mListeners.size() == 0 && mSources.size() == 0); // Everything should be destroyed at this point
  57. clearContexts();
  58. alcCloseDevice(mDevice);
  59. }
  60. void OAAudio::setVolume(float volume)
  61. {
  62. mVolume = Math::clamp01(volume);
  63. for (auto& listener : mListeners)
  64. listener->rebuild();
  65. }
  66. float OAAudio::getVolume() const
  67. {
  68. return mVolume;
  69. }
  70. void OAAudio::setPaused(bool paused)
  71. {
  72. // TODO
  73. }
  74. bool OAAudio::isPaused() const
  75. {
  76. // TODO
  77. return false;
  78. }
  79. void OAAudio::update()
  80. {
  81. // TODO
  82. }
  83. void OAAudio::setActiveDevice(const AudioDevice& device)
  84. {
  85. if (mAllDevices.size() == 1)
  86. return; // No devices to change to, keep the active device as is
  87. clearContexts();
  88. alcCloseDevice(mDevice);
  89. mActiveDevice = device;
  90. String narrowName = toString(device.name);
  91. mDevice = alcOpenDevice(narrowName.c_str());
  92. if (mDevice == nullptr)
  93. {
  94. BS_EXCEPT(InternalErrorException, "Failed to open OpenAL device: " + narrowName);
  95. return;
  96. }
  97. rebuildContexts();
  98. }
  99. bool OAAudio::_isExtensionSupported(const String& extension) const
  100. {
  101. if ((extension.length() > 2) && (extension.substr(0, 3) == "ALC"))
  102. return alcIsExtensionPresent(mDevice, extension.c_str()) != AL_FALSE;
  103. else
  104. return alIsExtensionPresent(extension.c_str()) != AL_FALSE;
  105. }
  106. void OAAudio::_registerListener(OAAudioListener* listener)
  107. {
  108. mListeners.push_back(listener);
  109. rebuildContexts();
  110. }
  111. void OAAudio::_unregisterListener(OAAudioListener* listener)
  112. {
  113. auto iterFind = std::find(mListeners.begin(), mListeners.end(), listener);
  114. if (iterFind != mListeners.end())
  115. mListeners.erase(iterFind);
  116. rebuildContexts();
  117. }
  118. void OAAudio::_registerSource(OAAudioSource* source)
  119. {
  120. mSources.insert(source);
  121. }
  122. void OAAudio::_unregisterSource(OAAudioSource* source)
  123. {
  124. mSources.erase(source);
  125. }
  126. SPtr<AudioClip> OAAudio::createClip(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples,
  127. const AUDIO_CLIP_DESC& desc)
  128. {
  129. return bs_shared_ptr_new<OAAudioClip>(samples, streamSize, numSamples, desc);
  130. }
  131. SPtr<AudioListener> OAAudio::createListener()
  132. {
  133. return bs_shared_ptr_new<OAAudioListener>();
  134. }
  135. SPtr<AudioSource> OAAudio::createSource()
  136. {
  137. return bs_shared_ptr_new<OAAudioSource>();
  138. }
  139. void OAAudio::rebuildContexts()
  140. {
  141. clearContexts();
  142. UINT32 numListeners = (UINT32)mListeners.size();
  143. UINT32 numContexts = numListeners > 1 ? numListeners : 1;
  144. for (auto& source : mSources)
  145. source->clear();
  146. for(UINT32 i = 0; i < numContexts; i++)
  147. {
  148. ALCcontext* context = alcCreateContext(mDevice, nullptr);
  149. mContexts.push_back(context);
  150. }
  151. // If only one context is available keep it active as an optimization. Audio listeners and sources will avoid
  152. // excessive context switching in such case.
  153. alcMakeContextCurrent(mContexts[0]);
  154. for (auto& listener : mListeners)
  155. listener->rebuild();
  156. for (auto& source : mSources)
  157. source->rebuild();
  158. }
  159. void OAAudio::clearContexts()
  160. {
  161. alcMakeContextCurrent(nullptr);
  162. for (auto& context : mContexts)
  163. alcDestroyContext(context);
  164. mContexts.clear();
  165. }
  166. OAAudio& gOAAudio()
  167. {
  168. return static_cast<OAAudio&>(OAAudio::instance());
  169. }
  170. }