BsOAAudio.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsOAPrerequisites.h"
  5. #include "BsAudio.h"
  6. #include "AL/alc.h"
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup OpenAudio
  10. * @{
  11. */
  12. /** Global manager for the audio implementation using OpenAL as the backend. */
  13. class OAAudio : public Audio
  14. {
  15. public:
  16. OAAudio();
  17. virtual ~OAAudio();
  18. /** @copydoc Audio::setVolume */
  19. void setVolume(float volume) override;
  20. /** @copydoc Audio::getVolume */
  21. float getVolume() const override;
  22. /** @copydoc Audio::setPaused */
  23. void setPaused(bool paused) override;
  24. /** @copydoc Audio::isPaused */
  25. bool isPaused() const override { return mIsPaused; }
  26. /** @copydoc Audio::_update */
  27. void _update() override;
  28. /** @copydoc Audio::setActiveDevice */
  29. void setActiveDevice(const AudioDevice& device) override;
  30. /** @copydoc Audio::getActiveDevice */
  31. AudioDevice getActiveDevice() const override { return mActiveDevice; }
  32. /** @copydoc Audio::getDefaultDevice */
  33. AudioDevice getDefaultDevice() const override { return mDefaultDevice; }
  34. /** @copydoc Audio::getAllDevices */
  35. const Vector<AudioDevice>& getAllDevices() const override { return mAllDevices; };
  36. /** @name Internal
  37. * @{
  38. */
  39. /** Checks is a specific OpenAL extension supported. */
  40. bool _isExtensionSupported(const String& extension) const;
  41. /** Registers a new AudioListener. Should be called on listener creation. */
  42. void _registerListener(OAAudioListener* listener);
  43. /** Unregisters an existing AudioListener. Should be called before listener destruction. */
  44. void _unregisterListener(OAAudioListener* listener);
  45. /** Registers a new AudioSource. Should be called on source creation. */
  46. void _registerSource(OAAudioSource* source);
  47. /** Unregisters an existing AudioSource. Should be called before source destruction. */
  48. void _unregisterSource(OAAudioSource* source);
  49. /** Returns a list of all OpenAL contexts. Each listener has its own context. */
  50. const Vector<ALCcontext*>& _getContexts() const { return mContexts; }
  51. /** Returns an OpenAL context assigned to the provided listener. */
  52. ALCcontext* _getContext(const OAAudioListener* listener) const;
  53. /**
  54. * Returns optimal format for the provided number of channels and bit depth. It is assumed the user has checked if
  55. * extensions providing these formats are actually available.
  56. */
  57. INT32 _getOpenALBufferFormat(UINT32 numChannels, UINT32 bitDepth);
  58. /**
  59. * Writes provided samples into the OpenAL buffer with the provided ID. If the provided format is not supported the
  60. * samples will first be converted into a valid format.
  61. */
  62. void _writeToOpenALBuffer(UINT32 bufferId, UINT8* samples, const AudioDataInfo& info);
  63. /** @} */
  64. private:
  65. friend class OAAudioSource;
  66. /** Type of a command that can be queued for a streaming audio source. */
  67. enum class StreamingCommandType
  68. {
  69. Start,
  70. Stop
  71. };
  72. /** Command queued for a streaming audio source. */
  73. struct StreamingCommand
  74. {
  75. StreamingCommandType type;
  76. OAAudioSource* source;
  77. };
  78. /** @copydoc Audio::createClip */
  79. SPtr<AudioClip> createClip(const SPtr<DataStream>& samples, UINT32 streamSize, UINT32 numSamples,
  80. const AUDIO_CLIP_DESC& desc) override;
  81. /** @copydoc Audio::createListener */
  82. SPtr<AudioListener> createListener() override;
  83. /** @copydoc Audio::createSource */
  84. SPtr<AudioSource> createSource() override;
  85. /**
  86. * Delete all existing contexts and rebuild them according to the listener list. All audio sources will be rebuilt
  87. * as well.
  88. *
  89. * This should be called when listener count changes, or audio device is changed.
  90. */
  91. void rebuildContexts();
  92. /** Delete all existing OpenAL contexts. */
  93. void clearContexts();
  94. /** Streams new data to audio sources that require it. */
  95. void updateStreaming();
  96. /** Starts data streaming for the provided source. */
  97. void startStreaming(OAAudioSource* source);
  98. /** Stops data streaming for the provided source. */
  99. void stopStreaming(OAAudioSource* source);
  100. float mVolume;
  101. bool mIsPaused;
  102. ALCdevice* mDevice;
  103. Vector<AudioDevice> mAllDevices;
  104. AudioDevice mDefaultDevice;
  105. AudioDevice mActiveDevice;
  106. Vector<OAAudioListener*> mListeners;
  107. Vector<ALCcontext*> mContexts;
  108. UnorderedSet<OAAudioSource*> mSources;
  109. // Streaming thread
  110. Vector<StreamingCommand> mStreamingCommandQueue;
  111. UnorderedSet<OAAudioSource*> mStreamingSources;
  112. UnorderedSet<OAAudioSource*> mDestroyedSources;
  113. SPtr<Task> mStreamingTask;
  114. mutable Mutex mMutex;
  115. };
  116. /** Provides easier access to OAAudio. */
  117. OAAudio& gOAAudio();
  118. /** @} */
  119. }