AkSubmixInputComponent.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /*=============================================================================
  16. AkSubmixInputComponent.cpp:
  17. =============================================================================*/
  18. #include "AkSubmixInputComponent.h"
  19. #include "AkAudioDevice.h"
  20. #include "AudioMixerDevice.h"
  21. #include <inttypes.h>
  22. UAkSubmixInputComponent::UAkSubmixInputComponent(const class FObjectInitializer& ObjectInitializer) :
  23. UAkAudioInputComponent(ObjectInitializer)
  24. {}
  25. Audio::FMixerDevice* UAkSubmixInputComponent::GetAudioMixerDevice()
  26. {
  27. UWorld* ThisWorld = GetWorld();
  28. if (!ThisWorld || !ThisWorld->bAllowAudioPlayback || ThisWorld->GetNetMode() == NM_DedicatedServer)
  29. {
  30. return nullptr;
  31. }
  32. if (FAudioDevice* AudioDevice = ThisWorld->GetAudioDevice().GetAudioDevice())
  33. {
  34. if (!AudioDevice->bAudioMixerModuleLoaded)
  35. {
  36. return nullptr;
  37. }
  38. else
  39. {
  40. return static_cast<Audio::FMixerDevice*>(AudioDevice);
  41. }
  42. }
  43. return nullptr;
  44. }
  45. int32 UAkSubmixInputComponent::PostAssociatedAudioInputEvent()
  46. {
  47. if (PlayingID == AK_INVALID_PLAYING_ID)
  48. {
  49. Audio::FMixerDevice* AudioMixerDevice = GetAudioMixerDevice();
  50. if (AudioMixerDevice)
  51. {
  52. NumChannels = AudioMixerDevice->GetNumDeviceChannels();
  53. SampleRate = AudioMixerDevice->GetDeviceSampleRate();
  54. BufferLength = AudioMixerDevice->GetBufferLength();
  55. SampleBuffer.SetCapacity(2 * BufferLength * NumChannels);
  56. PoppedSamples.Empty();
  57. PoppedSamples.AddUninitialized(BufferLength * NumChannels);
  58. AudioMixerDevice->RegisterSubmixBufferListener(this, SubmixToRecord);
  59. }
  60. else
  61. {
  62. UE_LOG(LogAkAudio, Warning, TEXT("AkSubmixInputComponent::PostAssociatedAudioInputEvent (%s): No Audio Mixer Device available, AkSubMixInputComponent cannot work."), *GetOwner()->GetName());
  63. return PlayingID;
  64. }
  65. PlayingID = Super::PostAssociatedAudioInputEvent();
  66. }
  67. else
  68. {
  69. UE_LOG(LogAkAudio, Log, TEXT("AkSubmixInputComponent (%s): Event was already posted."), *GetOwner()->GetName());
  70. }
  71. return PlayingID;
  72. }
  73. void UAkSubmixInputComponent::Stop()
  74. {
  75. Audio::FMixerDevice* AudioMixerDevice = GetAudioMixerDevice();
  76. if (AudioMixerDevice)
  77. {
  78. AudioMixerDevice->UnregisterSubmixBufferListener(this, SubmixToRecord);
  79. }
  80. Super::Stop();
  81. PlayingID = AK_INVALID_PLAYING_ID;
  82. }
  83. bool UAkSubmixInputComponent::FillSamplesBuffer(uint32 InNumChannels, uint32 InNumSamples, float** InOutBufferToFill)
  84. {
  85. check(InNumChannels == NumChannels);
  86. if (SampleBuffer.Num() >= (InNumChannels * InNumSamples))
  87. {
  88. auto NumPopped = SampleBuffer.Pop(PoppedSamples.GetData(), InNumChannels * InNumSamples);
  89. if (NumPopped == InNumChannels * InNumSamples)
  90. {
  91. for (uint32 Channel = 0; Channel < InNumChannels; Channel++)
  92. {
  93. for (uint32 Sample = 0; Sample < InNumSamples; Sample++)
  94. {
  95. InOutBufferToFill[Channel][Sample] = PoppedSamples[((NumChannels * Sample) + Channel)];
  96. }
  97. }
  98. return true;
  99. }
  100. }
  101. for (uint32 Channel = 0; Channel < InNumChannels; Channel++)
  102. {
  103. FMemory::Memset(InOutBufferToFill[Channel], 0, InNumSamples * sizeof(float));
  104. }
  105. return true;
  106. }
  107. void UAkSubmixInputComponent::GetChannelConfig(AkAudioFormat& AudioFormat)
  108. {
  109. Audio::FMixerDevice* AudioMixerDevice = GetAudioMixerDevice();
  110. if (!AudioMixerDevice)
  111. {
  112. UE_LOG(LogAkAudio, Error, TEXT("AkSubmixInputComponent::GetChannelConfig (%s): No Audio Mixer Device available, AkSubMixInputComponent cannot work."), *GetOwner()->GetName());
  113. return;
  114. }
  115. AudioFormat.uSampleRate = SampleRate;
  116. switch (NumChannels)
  117. {
  118. case 8:
  119. AudioFormat.channelConfig.SetStandard(AK_SPEAKER_SETUP_7POINT1);
  120. break;
  121. case 6:
  122. AudioFormat.channelConfig.SetStandard(AK_SPEAKER_SETUP_5POINT1);
  123. break;
  124. case 2:
  125. AudioFormat.channelConfig.SetStandard(AK_SPEAKER_SETUP_STEREO);
  126. break;
  127. case 1:
  128. AudioFormat.channelConfig.SetStandard(AK_SPEAKER_SETUP_MONO);
  129. break;
  130. default:
  131. UE_LOG(LogAkAudio, Error, TEXT("AkSubmixInputComponent::GetChannelConfig (%s): Unknown number of channels (%" PRIu32 ")"), *GetOwner()->GetName(), NumChannels);
  132. }
  133. }
  134. void UAkSubmixInputComponent::OnNewSubmixBuffer(
  135. const USoundSubmix* InOwningSubmix,
  136. float* InAudioData,
  137. int32 InNumSamples,
  138. int32 InNumChannels,
  139. const int32 InSampleRate,
  140. double InAudioClock)
  141. {
  142. check(InNumChannels == NumChannels);
  143. check(InSampleRate == SampleRate);
  144. SampleBuffer.Push(InAudioData, InNumSamples);
  145. }