AkAudioInputManager.cpp 14 KB

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