AudioMixerInputComponent.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*******************************************************************************
  2. The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
  3. Technology released in source code form as part of the game integration package.
  4. The content of this file may not be used without valid licenses to the
  5. AUDIOKINETIC Wwise Technology.
  6. Note that the use of the game engine is subject to the Unreal(R) Engine End User
  7. License Agreement at https://www.unrealengine.com/en-US/eula/unreal
  8. License Usage
  9. Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
  10. this file in accordance with the end user license agreement provided with the
  11. software or, alternatively, in accordance with the terms contained
  12. in a written agreement between you and Audiokinetic Inc.
  13. Copyright (c) 2023 Audiokinetic Inc.
  14. *******************************************************************************/
  15. #include "AudioMixerInputComponent.h"
  16. #include "AkAudioDevice.h"
  17. #include "AkAudioInputManager.h"
  18. #include "AkAudioEvent.h"
  19. #define AK_AUDIO_INPUT_EVENT_NAME "Play_UnrealAudio"
  20. FAudioMixerInputComponent::FAudioMixerInputComponent() :
  21. PlayingID(AK_INVALID_PLAYING_ID)
  22. {
  23. auto Device = FAkAudioDevice::Get();
  24. if (Device != nullptr)
  25. {
  26. Device->RegisterComponent(GetAkGameObjectID());
  27. }
  28. }
  29. FAudioMixerInputComponent::~FAudioMixerInputComponent()
  30. {
  31. if (OnNextBuffer.IsBound())
  32. {
  33. OnNextBuffer.Unbind();
  34. }
  35. auto Device = FAkAudioDevice::Get();
  36. Device->UnregisterComponent(GetAkGameObjectID());
  37. }
  38. bool FAudioMixerInputComponent::FillSamplesBuffer(uint32 NumChannels, uint32 NumSamples, float** BufferToFill)
  39. {
  40. if (OnNextBuffer.IsBound())
  41. {
  42. OnNextBuffer.Execute(NumChannels, NumSamples, BufferToFill);
  43. }
  44. return true;
  45. }
  46. /** This callback is used to provide the Wwise sound engine with the required audio format. */
  47. void FAudioMixerInputComponent::GetChannelConfig(AkAudioFormat& OutAudioFormat)
  48. {
  49. const int sampleRate = 48000;
  50. OutAudioFormat.uSampleRate = sampleRate;
  51. OutAudioFormat.channelConfig.SetStandard(AK_SPEAKER_SETUP_STEREO);
  52. UE_LOG(LogAkAudio, Log, TEXT("Wwise Channel configuration:"));
  53. UE_LOG(LogAkAudio, Log, TEXT("Wwise Input Sample Rate: %d"), OutAudioFormat.uSampleRate);
  54. UE_LOG(LogAkAudio, Log, TEXT("Wwise Channel num: %d"), 2);
  55. }
  56. AkPlayingID FAudioMixerInputComponent::PostAssociatedAudioInputEvent(UAkAudioEvent* InputEvent)
  57. {
  58. PlayingID = FAkAudioInputManager::PostAudioInputEvent(
  59. InputEvent,
  60. OnNextBuffer,
  61. FAkGlobalAudioFormatDelegate::CreateRaw(this, &FAudioMixerInputComponent::GetChannelConfig),
  62. EAkAudioContext::AlwaysActive);
  63. return PlayingID;
  64. }
  65. void FAudioMixerInputComponent::PostUnregisterGameObject()
  66. {
  67. auto Device = FAkAudioDevice::Get();
  68. if (Device != nullptr)
  69. {
  70. if (PlayingID != AK_INVALID_PLAYING_ID)
  71. {
  72. Device->StopPlayingID(PlayingID);
  73. PlayingID = AK_INVALID_PLAYING_ID;
  74. }
  75. }
  76. }
  77. AkGameObjectID FAudioMixerInputComponent::GetAkGameObjectID() const
  78. {
  79. return (AkGameObjectID)this;
  80. }