AkAudioInputManager.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 "AkAudioInputManager.h"
  16. #include "AkAudioDevice.h"
  17. #include "AkAudioEvent.h"
  18. #if WITH_EDITOR
  19. #include "Editor.h"
  20. #endif
  21. #include "Wwise/API/WwiseSoundEngineAPI.h"
  22. #include "Misc/ScopeLock.h"
  23. #include <inttypes.h>
  24. #include "AkComponent.h"
  25. /*------------------------------------------------------------------------------------
  26. FAudioInputDelegates
  27. Helper struct that contains an audio samples delegate and an audio format delegate
  28. ------------------------------------------------------------------------------------*/
  29. struct FAudioInputDelegates
  30. {
  31. FAkGlobalAudioInputDelegate AudioSamplesDelegate;
  32. FAkGlobalAudioFormatDelegate AudioFormatDelegate;
  33. };
  34. /*------------------------------------------------------------------------------------
  35. FAkAudioInputHelpers
  36. ------------------------------------------------------------------------------------*/
  37. namespace FAkAudioInputHelpers
  38. {
  39. static FCriticalSection MapSection;
  40. static TArray<float*> AudioData = TArray<float*>();
  41. /* A Map of playing ids to input delegates */
  42. static TMap<uint32, FAudioInputDelegates> AudioInputDelegates = TMap<uint32, FAudioInputDelegates>();
  43. static AkSampleType* GetChannel(AkAudioBuffer* Buffer, AkUInt32 in_uIndex)
  44. {
  45. check(Buffer);
  46. check( in_uIndex < Buffer->NumChannels() );
  47. return (AkSampleType*)((AkUInt8*)(Buffer->GetInterleavedData()) + ( in_uIndex * sizeof(AkSampleType) * Buffer->MaxFrames() ));
  48. }
  49. static void UpdateDataPointers(AkAudioBuffer* BufferToFill)
  50. {
  51. AkUInt32 NumChannels = BufferToFill->NumChannels();
  52. for (AkUInt32 c = 0; c < NumChannels; ++c)
  53. {
  54. AudioData[c] = GetChannel(BufferToFill, c);
  55. }
  56. }
  57. // This is the equivalent of AkAudioBuffer::ZeroPadToMaxFrames, but doesn't use global variables.
  58. static void ZeroPadToMaxFrames(AkAudioBuffer* Buffer)
  59. {
  60. check(Buffer);
  61. auto pData = Buffer->GetInterleavedData();
  62. check(pData || Buffer->MaxFrames() == 0)
  63. // The following members MUST be copied locally due to multi-core calls to this function.
  64. const AkUInt32 uNumChannels = Buffer->NumChannels();
  65. const AkUInt32 uNumCurrentFrames = FMath::Min(Buffer->uValidFrames, Buffer->MaxFrames());
  66. const AkUInt32 uNumZeroFrames = Buffer->MaxFrames() - uNumCurrentFrames;
  67. if ( uNumZeroFrames )
  68. {
  69. check(pData);
  70. for ( AkUInt32 i = 0; i < uNumChannels; ++i )
  71. {
  72. FPlatformMemory::Memset( GetChannel(Buffer, i) + uNumCurrentFrames, 0, uNumZeroFrames * sizeof(AkSampleType) );
  73. }
  74. Buffer->uValidFrames = Buffer->MaxFrames();
  75. }
  76. }
  77. /* The global audio samples callback that searches AudioInputDelegates for
  78. the key PlayingID and executes the corresponding delegate*/
  79. static void GetAudioSamples(AkPlayingID PlayingID, AkAudioBuffer* BufferToFill)
  80. {
  81. if (!BufferToFill)
  82. {
  83. return;
  84. }
  85. BufferToFill->eState = AK_NoMoreData;
  86. AkUInt32 NumChannels = BufferToFill->NumChannels();
  87. const AkUInt16 NumFrames = BufferToFill->MaxFrames();
  88. BufferToFill->uValidFrames = NumFrames;
  89. FAkGlobalAudioInputDelegate SamplesCallback;
  90. {
  91. FScopeLock MapLock(&MapSection);
  92. auto Delegates = AudioInputDelegates.Find((uint32)PlayingID);
  93. if (Delegates)
  94. {
  95. SamplesCallback = Delegates->AudioSamplesDelegate;
  96. }
  97. }
  98. if (SamplesCallback.IsBound())
  99. {
  100. UpdateDataPointers(BufferToFill);
  101. if(auto AudioDataPtr = AudioData.GetData())
  102. {
  103. if (SamplesCallback.Execute((int)NumChannels, (int)NumFrames, AudioDataPtr))
  104. {
  105. BufferToFill->eState = AK_DataReady;
  106. }
  107. }
  108. }
  109. else
  110. {
  111. ZeroPadToMaxFrames(BufferToFill);
  112. }
  113. }
  114. /* The global audio format callback that searches AudioInputDelegates for
  115. the key PlayingID and executes the corresponding delegate*/
  116. static void GetAudioFormat(AkPlayingID PlayingID, AkAudioFormat& AudioFormat)
  117. {
  118. FAkGlobalAudioFormatDelegate FormatCallback;
  119. {
  120. FScopeLock MapLock(&MapSection);
  121. auto Delegates = AudioInputDelegates.Find((uint32)PlayingID);
  122. if (Delegates)
  123. {
  124. FormatCallback = Delegates->AudioFormatDelegate;
  125. }
  126. }
  127. if (FormatCallback.IsBound())
  128. {
  129. FormatCallback.Execute(AudioFormat);
  130. }
  131. const uint32 NumChannels = AudioFormat.channelConfig.uNumChannels;
  132. if (AudioData.Max() < (int32)NumChannels)
  133. {
  134. AudioData.Reserve(NumChannels);
  135. AudioData.AddUninitialized(AudioData.GetSlack());
  136. }
  137. }
  138. /**
  139. * Sets the main callbacks for the Wwise engine audio input plugin.
  140. *
  141. */
  142. static void SetAkAudioInputCallbacks()
  143. {
  144. auto* SoundEngine = IWwiseSoundEngineAPI::Get();
  145. if (UNLIKELY(!SoundEngine)) return;
  146. SoundEngine->AudioInputPlugin->SetAudioInputCallbacks(
  147. &FAkAudioInputHelpers::GetAudioSamples,
  148. &FAkAudioInputHelpers::GetAudioFormat,
  149. nullptr);
  150. }
  151. /* Protects against calling Wwise sound engine SetAudioInputCallbacks function more than once */
  152. static bool bIsInitialized = false;
  153. /* Calls the Wwise sound engine SetAudioInputCallbacks function*/
  154. static void TryInitialize()
  155. {
  156. if (!bIsInitialized)
  157. {
  158. SetAkAudioInputCallbacks();
  159. bIsInitialized = true;
  160. }
  161. #if WITH_EDITOR
  162. FEditorDelegates::EndPIE.AddLambda([](const bool bIsSimulating)
  163. {
  164. bIsInitialized = false;
  165. });
  166. #endif
  167. }
  168. static void AddAudioInputPlayingID(AkPlayingID PlayingID,
  169. FAkGlobalAudioInputDelegate AudioSamplesDelegate,
  170. FAkGlobalAudioFormatDelegate AudioFormatDelegate)
  171. {
  172. FScopeLock MapLock(&MapSection);
  173. AudioInputDelegates.Add((uint32)PlayingID, { AudioSamplesDelegate, AudioFormatDelegate });
  174. }
  175. /* Posts an event and associates the AudioSamplesDelegate and AudioFormatDelegate delegates with the resulting playing id. */
  176. AkPlayingID PostAudioInputEvent(TFunction<AkPlayingID(FAkAudioDevice* AkDevice)> PostEventCall,
  177. FAkGlobalAudioInputDelegate AudioSamplesDelegate,
  178. FAkGlobalAudioFormatDelegate AudioFormatDelegate)
  179. {
  180. TryInitialize();
  181. AkPlayingID PlayingID = AK_INVALID_PLAYING_ID;
  182. FAkAudioDevice* AkDevice = FAkAudioDevice::Get();
  183. if (AkDevice != nullptr)
  184. {
  185. PlayingID = PostEventCall(AkDevice);
  186. if (PlayingID != AK_INVALID_PLAYING_ID)
  187. {
  188. AddAudioInputPlayingID(PlayingID, AudioSamplesDelegate, AudioFormatDelegate);
  189. }
  190. }
  191. return PlayingID;
  192. }
  193. static void EventCallback(AkCallbackType CallbackType, AkCallbackInfo *CallbackInfo)
  194. {
  195. if (CallbackType == AkCallbackType::AK_EndOfEvent)
  196. {
  197. AkEventCallbackInfo* EventInfo = (AkEventCallbackInfo*)CallbackInfo;
  198. if (EventInfo != nullptr)
  199. {
  200. uint32 PlayingID = (uint32)EventInfo->playingID;
  201. {
  202. FScopeLock MapLock(&MapSection);
  203. AudioInputDelegates.Remove(PlayingID);
  204. }
  205. }
  206. }
  207. }
  208. }
  209. /*------------------------------------------------------------------------------------
  210. FAkAudioInputManager
  211. ------------------------------------------------------------------------------------*/
  212. AkPlayingID FAkAudioInputManager::PostAudioInputEvent(
  213. UAkAudioEvent * Event,
  214. AActor * Actor,
  215. FAkGlobalAudioInputDelegate AudioSamplesDelegate,
  216. FAkGlobalAudioFormatDelegate AudioFormatDelegate,
  217. EAkAudioContext AudioContext
  218. )
  219. {
  220. if (!IsValid(Event))
  221. {
  222. UE_LOG(LogAkAudio, Warning, TEXT("FAkAudioInputManager::PostAudioInputEvent: Invalid AkEvent."))
  223. return AK_INVALID_PLAYING_ID;
  224. }
  225. if (!IsValid(Actor))
  226. {
  227. UE_LOG(LogAkAudio, Warning, TEXT("FAkAudioInputManager::PostAudioInputEvent: Invalid Actor playing AkEvent %s."), *Event->GetName())
  228. return AK_INVALID_PLAYING_ID;
  229. }
  230. return FAkAudioInputHelpers::PostAudioInputEvent([Event, Actor, AudioContext](FAkAudioDevice* AkDevice)
  231. {
  232. const auto Result = Event->PostOnActor(
  233. Actor,
  234. nullptr,
  235. &FAkAudioInputHelpers::EventCallback,
  236. nullptr,
  237. AkCallbackType::AK_EndOfEvent,
  238. nullptr,
  239. false,
  240. AudioContext);
  241. UE_CLOG(UNLIKELY(Result == AK_INVALID_PLAYING_ID), LogAkAudio, Warning,
  242. TEXT("FAkAudioInputManager::PostAudioInputEvent: Failed posting input event %s to actor %s."), *Event->GetName(), *Actor->GetName());
  243. UE_CLOG(LIKELY(Result != AK_INVALID_PLAYING_ID), LogAkAudio, VeryVerbose,
  244. TEXT("FAkAudioInputManager::PostAudioInputEvent: Posted input event %s to actor %s. PlayId=%" PRIu32), *Event->GetName(), *Actor->GetName(), Result);
  245. return Result;
  246. }, AudioSamplesDelegate, AudioFormatDelegate);
  247. }
  248. AkPlayingID FAkAudioInputManager::PostAudioInputEvent(
  249. UAkAudioEvent* Event,
  250. UAkComponent* Component,
  251. FAkGlobalAudioInputDelegate AudioSamplesDelegate,
  252. FAkGlobalAudioFormatDelegate AudioFormatDelegate,
  253. EAkAudioContext AudioContext)
  254. {
  255. if (!IsValid(Event))
  256. {
  257. UE_LOG(LogAkAudio, Warning, TEXT("FAkAudioInputManager::PostAudioInputEvent: Invalid AkEvent."))
  258. return AK_INVALID_PLAYING_ID;
  259. }
  260. if (!Component)
  261. {
  262. UE_LOG(LogAkAudio, Warning, TEXT("FAkAudioInputManager::PostAudioInputEvent: Invalid Component playing AkEvent %s."), *Event->GetName())
  263. return AK_INVALID_PLAYING_ID;
  264. }
  265. return FAkAudioInputHelpers::PostAudioInputEvent([Event, Component, AudioContext](FAkAudioDevice* AkDevice)
  266. {
  267. const auto Result = Event->PostOnComponent(
  268. Component,
  269. nullptr,
  270. &FAkAudioInputHelpers::EventCallback,
  271. nullptr,
  272. AkCallbackType::AK_EndOfEvent,
  273. nullptr,
  274. false,
  275. AudioContext);
  276. UE_CLOG(UNLIKELY(Result == AK_INVALID_PLAYING_ID), LogAkAudio, Warning,
  277. TEXT("FAkAudioInputManager::PostAudioInputEvent: Failed posting input event %s to component %s."), *Event->GetName(), *Component->GetName());
  278. UE_CLOG(LIKELY(Result != AK_INVALID_PLAYING_ID), LogAkAudio, VeryVerbose,
  279. TEXT("FAkAudioInputManager::PostAudioInputEvent: Posted input event %s to component %s. PlayId=%" PRIu32), *Event->GetName(), *Component->GetName(), Result);
  280. return Result;
  281. }, AudioSamplesDelegate, AudioFormatDelegate);
  282. }
  283. AkPlayingID FAkAudioInputManager::PostAudioInputEvent(
  284. UAkAudioEvent* Event,
  285. AkGameObjectID GameObject,
  286. FAkGlobalAudioInputDelegate AudioSamplesDelegate,
  287. FAkGlobalAudioFormatDelegate AudioFormatDelegate,
  288. EAkAudioContext AudioContext)
  289. {
  290. if (!IsValid(Event))
  291. {
  292. UE_LOG(LogAkAudio, Warning, TEXT("FAkAudioInputManager::PostAudioInputEvent: Invalid AkEvent."))
  293. return AK_INVALID_PLAYING_ID;
  294. }
  295. if (GameObject == AK_INVALID_GAME_OBJECT)
  296. {
  297. UE_LOG(LogAkAudio, Warning, TEXT("FAkAudioInputManager::PostAudioInputEvent: Invalid GameObject playing AkEvent %s."), *Event->GetName())
  298. return AK_INVALID_PLAYING_ID;
  299. }
  300. return FAkAudioInputHelpers::PostAudioInputEvent([Event, GameObject, AudioContext](FAkAudioDevice* AkDevice)
  301. {
  302. const auto Result = Event->PostOnGameObjectID(
  303. GameObject,
  304. nullptr,
  305. &FAkAudioInputHelpers::EventCallback,
  306. nullptr,
  307. AkCallbackType::AK_EndOfEvent,
  308. nullptr,
  309. AudioContext);
  310. UE_CLOG(UNLIKELY(Result == AK_INVALID_PLAYING_ID), LogAkAudio, Warning,
  311. TEXT("FAkAudioInputManager::PostAudioInputEvent: Failed posting input event %s to %" PRIu64 "."), *Event->GetName(), GameObject);
  312. UE_CLOG(LIKELY(Result != AK_INVALID_PLAYING_ID), LogAkAudio, VeryVerbose,
  313. TEXT("FAkAudioInputManager::PostAudioInputEvent: Posted input event %s to %" PRIu64 ". PlayId=%" PRIu32), *Event->GetName(), GameObject, Result);
  314. return Result;
  315. }, AudioSamplesDelegate, AudioFormatDelegate);
  316. }
  317. AkPlayingID FAkAudioInputManager::PostAudioInputEvent(UAkAudioEvent* Event,
  318. FAkGlobalAudioInputDelegate AudioSamplesDelegate, FAkGlobalAudioFormatDelegate AudioFormatDelegate,
  319. EAkAudioContext AudioContext)
  320. {
  321. if (!IsValid(Event))
  322. {
  323. UE_LOG(LogAkAudio, Warning, TEXT("FAkAudioInputManager::PostAudioInputEvent: Invalid AkEvent."))
  324. return AK_INVALID_PLAYING_ID;
  325. }
  326. return FAkAudioInputHelpers::PostAudioInputEvent([Event, AudioContext](FAkAudioDevice* AkDevice)
  327. {
  328. const auto Result = Event->PostAmbient(
  329. nullptr,
  330. &FAkAudioInputHelpers::EventCallback,
  331. nullptr,
  332. AkCallbackType::AK_EndOfEvent,
  333. nullptr,
  334. AudioContext);
  335. UE_CLOG(UNLIKELY(Result == AK_INVALID_PLAYING_ID), LogAkAudio, Warning,
  336. TEXT("FAkAudioInputManager::PostAudioInputEvent: Failed posting ambient input event %s."), *Event->GetName());
  337. UE_CLOG(LIKELY(Result != AK_INVALID_PLAYING_ID), LogAkAudio, VeryVerbose,
  338. TEXT("FAkAudioInputManager::PostAudioInputEvent: Posted ambient input event %s. PlayId=%" PRIu32), *Event->GetName(), Result);
  339. return Result;
  340. }, AudioSamplesDelegate, AudioFormatDelegate);
  341. }