AudioSourceManager.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <AzCore/PlatformIncl.h> // This include is needed to include WinSock2.h before including Windows.h
  10. // As AK/SoundEngine/Common/AkTypes.h eventually includes Windows.h
  11. #include <IAudioInterfacesCommonData.h>
  12. #include <AzCore/std/containers/unordered_map.h>
  13. #include <AzCore/std/parallel/mutex.h>
  14. #include <AzCore/std/smart_ptr/unique_ptr.h>
  15. #include <AzCore/IO/FileIO.h>
  16. #include <AudioAllocators.h>
  17. #include <AK/SoundEngine/Common/AkTypes.h>
  18. #include <AK/SoundEngine/Common/IAkPlugin.h>
  19. namespace Audio
  20. {
  21. /**
  22. * Base class for Audio Input Source types.
  23. * Represents an Audio Input Source, which has input/output routines and configuration information.
  24. */
  25. class AudioInputSource
  26. {
  27. public:
  28. AUDIO_IMPL_CLASS_ALLOCATOR(AudioInputSource)
  29. AudioInputSource() = default;
  30. virtual ~AudioInputSource() = default;
  31. virtual void ReadInput(const AudioStreamData& data) = 0;
  32. virtual void WriteOutput(AkAudioBuffer* akBuffer) = 0;
  33. virtual bool IsOk() const = 0;
  34. virtual bool IsFormatValid() const;
  35. virtual void OnActivated() {}
  36. virtual void OnDeactivated() {}
  37. void SetFormat(AkAudioFormat& format);
  38. void SetSourceId(TAudioSourceId sourceId);
  39. TAudioSourceId GetSourceId() const;
  40. protected:
  41. SAudioInputConfig m_config; ///< Configuration information for the source type.
  42. AkPlayingID m_playingId = AK_INVALID_PLAYING_ID; ///< Playing ID of the source.
  43. };
  44. /**
  45. * Manager class for AudioInputSource.
  46. * Manages lifetime of AudioInputSource objects as they are created, activated, deactivated, and destroyed.
  47. * The lifetime of an Audio Input Source:
  48. * CreateSource (loads resources)
  49. * ActivateSource (once you obtain a playing Id)
  50. * (Running, callbacks being received, also async loading input if enabled)
  51. * DeactivateSource (once it's determined to be done playing)
  52. * DestroySource (unloads resources)
  53. */
  54. class AudioSourceManager
  55. {
  56. public:
  57. AudioSourceManager();
  58. ~AudioSourceManager();
  59. static AudioSourceManager& Get();
  60. static void Initialize();
  61. void Shutdown();
  62. /**
  63. * CreateSource a new AudioInputSource.
  64. * Creates an AudioInputSource, based on the SAudioInputConfig and stores it in an inactive state.
  65. * @param sourceConfig Configuration of the AudioInputSource.
  66. * @return True if the source was created successfully, false otherwise.
  67. */
  68. bool CreateSource(const SAudioInputConfig& sourceConfig);
  69. /**
  70. * Activates an AudioInputSource.
  71. * Moves a source from the inactive state to an active state by assigning an AkPlayingID.
  72. * @param sourceId ID of the source (returned by CreateSource).
  73. * @param playingId A playing ID of the source that is now playing in Wwise.
  74. */
  75. void ActivateSource(TAudioSourceId sourceId, AkPlayingID playingId);
  76. /**
  77. * Deactivates an AudioInputSource.
  78. * Moves a source from the active state back to an inactive state, will happen when an end event callback is recieved.
  79. * @param playingId Playing ID of the source that ended.
  80. */
  81. void DeactivateSource(AkPlayingID playingId);
  82. /**
  83. * Destroy an AudioInputSource.
  84. * Destroys an AudioInputSource from the manager when it is no longer needed.
  85. * @param sourceId Source ID of the object to remove.
  86. */
  87. void DestroySource(TAudioSourceId sourceId);
  88. /**
  89. * Find the Playing ID of a source.
  90. * Given a Source ID, check if there are sources in the active state and if so, return their Playing ID.
  91. * @param sourceId Source ID to look for in the active sources.
  92. */
  93. AkPlayingID FindPlayingSource(TAudioSourceId sourceId);
  94. private:
  95. /**
  96. * Wwise Audio Input Plugin "Execute" callback function.
  97. * This will be called whenever a playing Audio Input Source needs to be fed.
  98. * @param playingId The Playing ID of the source.
  99. * @param audioBuffer The buffer to copy samples into.
  100. */
  101. static void ExecuteCallback(AkPlayingID playingId, AkAudioBuffer* audioBuffer);
  102. /**
  103. * Wwise Audio Input Plugin "GetFormat" callback function.
  104. * This will be called once whenever a new Audio Input Source is starting playback.
  105. * @param playingId The Playing ID of the source.
  106. * @param audioFormat The format structure that should be filled with format information.
  107. */
  108. static void GetFormatCallback(AkPlayingID playingId, AkAudioFormat& audioFormat);
  109. AZStd::mutex m_inputMutex; ///< Callbacks will come from the Wwise event processing thread.
  110. template <typename KeyType, typename ValueType>
  111. using AudioInputMap = AZStd::unordered_map<KeyType, AZStd::unique_ptr<ValueType>, AZStd::hash<KeyType>, AZStd::equal_to<KeyType>, Audio::AudioImplStdAllocator>;
  112. AudioInputMap<TAudioSourceId, AudioInputSource> m_inactiveAudioInputs; ///< Sources that haven't started playing yet.
  113. AudioInputMap<AkPlayingID, AudioInputSource> m_activeAudioInputs; ///< Sources that are currently playing.
  114. };
  115. }