AudioSystem.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <ATL.h>
  10. #include <AudioAllocators.h>
  11. #include <AzCore/Debug/Budget.h>
  12. #include <AzCore/std/containers/deque.h>
  13. #include <AzCore/std/containers/vector.h>
  14. #include <AzCore/std/parallel/binary_semaphore.h>
  15. #include <AzCore/std/parallel/thread.h>
  16. #if !defined(AUDIO_RELEASE)
  17. #include <AzFramework/Entity/EntityDebugDisplayBus.h>
  18. #endif // !AUDIO_RELEASE
  19. AZ_DECLARE_BUDGET(Audio);
  20. namespace Audio
  21. {
  22. // Forward declarations.
  23. class CAudioSystem;
  24. class CAudioProxy;
  25. ///////////////////////////////////////////////////////////////////////////////////////////////////
  26. class CAudioThread
  27. {
  28. public:
  29. CAudioThread()
  30. : m_running(false)
  31. {}
  32. ~CAudioThread();
  33. void Activate(CAudioSystem* const audioSystem);
  34. void Run();
  35. void Deactivate();
  36. private:
  37. CAudioSystem* m_audioSystem = nullptr;
  38. AZStd::atomic_bool m_running;
  39. AZStd::thread m_thread;
  40. };
  41. ///////////////////////////////////////////////////////////////////////////////////////////////////
  42. class CAudioSystem
  43. : public AZ::Interface<Audio::IAudioSystem>::Registrar
  44. #if !defined(AUDIO_RELEASE)
  45. , AzFramework::DebugDisplayEventBus::Handler
  46. #endif
  47. {
  48. friend class CAudioThread;
  49. public:
  50. AZ_RTTI(CAudioSystem, "{96254647-000D-4896-93C4-92E0F258F21D}", IAudioSystem);
  51. AZ_CLASS_ALLOCATOR(CAudioSystem, AZ::SystemAllocator);
  52. CAudioSystem();
  53. ~CAudioSystem() override;
  54. CAudioSystem(const CAudioSystem&) = delete;
  55. CAudioSystem& operator=(const CAudioSystem&) = delete;
  56. bool Initialize() override;
  57. void Release() override;
  58. void ExternalUpdate() override;
  59. void PushRequest(AudioRequestVariant&& request) override;
  60. void PushRequests(AudioRequestsQueue& requests) override;
  61. void PushRequestBlocking(AudioRequestVariant&& request) override;
  62. void PushCallback(AudioRequestVariant&& callback) override;
  63. TAudioControlID GetAudioTriggerID(const char* const sAudioTriggerName) const override;
  64. TAudioControlID GetAudioRtpcID(const char* const sAudioRtpcName) const override;
  65. TAudioControlID GetAudioSwitchID(const char* const sAudioSwitchName) const override;
  66. TAudioSwitchStateID GetAudioSwitchStateID(const TAudioControlID nSwitchID, const char* const sAudioSwitchStateName) const override;
  67. TAudioPreloadRequestID GetAudioPreloadRequestID(const char* const sAudioPreloadRequestName) const override;
  68. TAudioEnvironmentID GetAudioEnvironmentID(const char* const sAudioEnvironmentName) const override;
  69. bool ReserveAudioListenerID(TAudioObjectID& rAudioObjectID) override;
  70. bool ReleaseAudioListenerID(const TAudioObjectID nAudioObjectID) override;
  71. bool SetAudioListenerOverrideID(const TAudioObjectID nAudioObjectID) override;
  72. const char* GetControlsPath() const override;
  73. void UpdateControlsPath() override;
  74. void RefreshAudioSystem(const char* const levelName) override;
  75. IAudioProxy* GetAudioProxy() override;
  76. void RecycleAudioProxy(IAudioProxy* const pIAudioProxy) override;
  77. TAudioSourceId CreateAudioSource(const SAudioInputConfig& sourceConfig) override;
  78. void DestroyAudioSource(TAudioSourceId sourceId) override;
  79. private:
  80. using TAudioProxies = AZStd::vector<CAudioProxy*, Audio::AudioSystemStdAllocator>;
  81. void InternalUpdate();
  82. bool m_bSystemInitialized;
  83. using duration_ms = AZStd::chrono::duration<float, AZStd::milli>;
  84. const duration_ms m_targetUpdatePeriod = AZStd::chrono::milliseconds(4);
  85. CAudioTranslationLayer m_oATL;
  86. CAudioThread m_audioSystemThread;
  87. AudioRequestsQueue m_blockingRequestsQueue;
  88. AudioRequestsQueue m_pendingRequestsQueue;
  89. AudioRequestsQueue m_pendingCallbacksQueue;
  90. AZStd::mutex m_blockingRequestsMutex;
  91. AZStd::mutex m_pendingRequestsMutex;
  92. AZStd::mutex m_pendingCallbacksMutex;
  93. // Synchronization objects
  94. AZStd::binary_semaphore m_mainEvent;
  95. AZStd::binary_semaphore m_processingEvent;
  96. // Audio Proxy containers
  97. TAudioProxies m_apAudioProxies;
  98. TAudioProxies m_apAudioProxiesToBeFreed;
  99. AZStd::string m_controlsPath;
  100. #if !defined(AUDIO_RELEASE)
  101. void DrawGlobalDebugInfo() override;
  102. #endif // !AUDIO_RELEASE
  103. };
  104. } // namespace Audio