WwiseGlobalCallbacks.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #pragma once
  16. #include "Wwise/WwiseDeferredQueue.h"
  17. #include "AkInclude.h"
  18. #include "WwiseProcessingModule.h"
  19. #include "Async/Future.h"
  20. class WWISEPROCESSING_API FWwiseGlobalCallbacks
  21. {
  22. public:
  23. using FCompletionPromise = TPromise<void>;
  24. using FAsyncFunction = FWwiseDeferredQueue::FFunction;
  25. using FGameFunction = FWwiseDeferredQueue::FFunction;
  26. using FSyncFunction = FWwiseDeferredQueue::FSyncFunction;
  27. using FGameThreadDelegate = FWwiseDeferredQueue::FGameThreadDelegate;
  28. using FThreadSafeDelegate = FWwiseDeferredQueue::FThreadSafeDelegate;
  29. protected:
  30. bool bInitialized = false;
  31. FWwiseDeferredQueue RegisterQueue;
  32. FWwiseDeferredQueue BeginQueue;
  33. FWwiseDeferredQueue PreProcessMessageQueueForRenderQueue;
  34. FWwiseDeferredQueue PostMessagesProcessedQueue;
  35. FWwiseDeferredQueue BeginRenderQueue;
  36. FWwiseDeferredQueue EndRenderQueue;
  37. FWwiseDeferredQueue EndQueue;
  38. FWwiseDeferredQueue TermQueue;
  39. FWwiseDeferredQueue MonitorQueue;
  40. FWwiseDeferredQueue MonitorRecapQueue;
  41. FWwiseDeferredQueue InitQueue;
  42. FWwiseDeferredQueue SuspendQueue;
  43. FWwiseDeferredQueue WakeupFromSuspendQueue;
  44. public:
  45. static FWwiseGlobalCallbacks* Get()
  46. {
  47. const auto ProcessingModule = IWwiseProcessingModule::GetModule();
  48. if (UNLIKELY(!ProcessingModule))
  49. {
  50. return nullptr;
  51. }
  52. return ProcessingModule->GetGlobalCallbacks();
  53. }
  54. FWwiseGlobalCallbacks();
  55. virtual ~FWwiseGlobalCallbacks();
  56. virtual bool Initialize();
  57. virtual void Terminate();
  58. bool IsInitialized() const { return bInitialized; }
  59. // AkGlobalCallbackLocation_Register: Right after successful registration of callback/plugin. Typically used by plugins along with AkGlobalCallbackLocation_Term for allocating memory for the lifetime of the sound engine.
  60. void RegisterAsync(FAsyncFunction&& InFunction) { RegisterQueue.AsyncDefer(MoveTemp(InFunction)); }
  61. void RegisterGame(FGameFunction&& InFunction) { RegisterQueue.GameDefer(MoveTemp(InFunction)); }
  62. void RegisterSync(FSyncFunction&& InFunction);
  63. void RegisterCompletion(FCompletionPromise&& Promise);
  64. void WaitForRegister();
  65. FGameThreadDelegate& OnRegister { RegisterQueue.OnGameRun };
  66. FThreadSafeDelegate& OnRegisterTS { RegisterQueue.OnSyncRunTS };
  67. // AkGlobalCallbackLocation_Begin: Start of audio processing. The number of frames about to be rendered depends on the sink/end-point and can be zero.
  68. void BeginAsync(FAsyncFunction&& InFunction) { BeginQueue.AsyncDefer(MoveTemp(InFunction)); }
  69. void BeginGame(FGameFunction&& InFunction) { BeginQueue.GameDefer(MoveTemp(InFunction)); }
  70. void BeginSync(FSyncFunction&& InFunction);
  71. void BeginCompletion(FCompletionPromise&& Promise);
  72. void WaitForBegin();
  73. FGameThreadDelegate& OnBegin { BeginQueue.OnGameRun };
  74. FThreadSafeDelegate& OnBeginTS { BeginQueue.OnSyncRunTS };
  75. // AkGlobalCallbackLocation_PreProcessMessageQueueForRender: Start of frame rendering, before having processed game messages.
  76. void PreProcessMessageQueueForRenderAsync(FAsyncFunction&& InFunction) { PreProcessMessageQueueForRenderQueue.AsyncDefer(MoveTemp(InFunction)); }
  77. void PreProcessMessageQueueForRenderGame(FGameFunction&& InFunction) { PreProcessMessageQueueForRenderQueue.GameDefer(MoveTemp(InFunction)); }
  78. void PreProcessMessageQueueForRenderSync(FSyncFunction&& InFunction);
  79. void PreProcessMessageQueueForRenderCompletion(FCompletionPromise&& Promise);
  80. void WaitForPreProcessMessageQueueForRender();
  81. FGameThreadDelegate& OnPreProcessMessageQueueForRender { PreProcessMessageQueueForRenderQueue.OnGameRun };
  82. FThreadSafeDelegate& OnPreProcessMessageQueueForRenderTS { PreProcessMessageQueueForRenderQueue.OnSyncRunTS };
  83. // AkGlobalCallbackLocation_PostMessagesProcessed: After one or more messages have been processed, but before updating game object and listener positions internally.
  84. void PostMessagesProcessedAsync(FAsyncFunction&& InFunction) { PostMessagesProcessedQueue.AsyncDefer(MoveTemp(InFunction)); }
  85. void PostMessagesProcessedGame(FGameFunction&& InFunction) { PostMessagesProcessedQueue.GameDefer(MoveTemp(InFunction)); }
  86. void PostMessagesProcessedSync(FSyncFunction&& InFunction);
  87. void PostMessagesProcessedCompletion(FCompletionPromise&& Promise);
  88. void WaitForPostMessagesProcessed();
  89. FGameThreadDelegate& OnPostMessagesProcessed { PostMessagesProcessedQueue.OnGameRun };
  90. FThreadSafeDelegate& OnPostMessagesProcessedTS { PostMessagesProcessedQueue.OnSyncRunTS };
  91. // AkGlobalCallbackLocation_BeginRender: Start of frame rendering, after having processed game messages.
  92. void BeginRenderAsync(FAsyncFunction&& InFunction) { BeginRenderQueue.AsyncDefer(MoveTemp(InFunction)); }
  93. void BeginRenderGame(FGameFunction&& InFunction) { BeginRenderQueue.GameDefer(MoveTemp(InFunction)); }
  94. void BeginRenderSync(FSyncFunction&& InFunction);
  95. void BeginRenderCompletion(FCompletionPromise&& Promise);
  96. void WaitForBeginRender();
  97. FGameThreadDelegate& OnBeginRender { BeginRenderQueue.OnGameRun };
  98. FThreadSafeDelegate& OnBeginRenderTS { BeginRenderQueue.OnSyncRunTS };
  99. // AkGlobalCallbackLocation_EndRender: End of frame rendering.
  100. void EndRenderAsync(FAsyncFunction&& InFunction) { EndRenderQueue.AsyncDefer(MoveTemp(InFunction)); }
  101. void EndRenderGame(FGameFunction&& InFunction) { EndRenderQueue.GameDefer(MoveTemp(InFunction)); }
  102. void EndRenderSync(FSyncFunction&& InFunction);
  103. void EndRenderCompletion(FCompletionPromise&& Promise);
  104. void WaitForEndRender();
  105. FGameThreadDelegate& OnEndRender { EndRenderQueue.OnGameRun };
  106. FThreadSafeDelegate& OnEndRenderTS { EndRenderQueue.OnSyncRunTS };
  107. // AkGlobalCallbackLocation_End: End of audio processing.
  108. void EndAsync(FAsyncFunction&& InFunction) { EndQueue.AsyncDefer(MoveTemp(InFunction)); }
  109. void EndGame(FGameFunction&& InFunction) { EndQueue.GameDefer(MoveTemp(InFunction)); }
  110. void EndSync(FSyncFunction&& InFunction);
  111. void EndCompletion(FCompletionPromise&& Promise, int Count = 1);
  112. void WaitForEnd();
  113. FGameThreadDelegate& OnEnd { EndQueue.OnGameRun };
  114. FThreadSafeDelegate& OnEndTS { EndQueue.OnSyncRunTS };
  115. // AkGlobalCallbackLocation_Term: Sound engine termination.
  116. void TermAsync(FAsyncFunction&& InFunction) { TermQueue.AsyncDefer(MoveTemp(InFunction)); }
  117. void TermGame(FGameFunction&& InFunction) { TermQueue.GameDefer(MoveTemp(InFunction)); }
  118. void TermSync(FSyncFunction&& InFunction);
  119. void TermCompletion(FCompletionPromise&& Promise);
  120. void WaitForTerm();
  121. FGameThreadDelegate& OnTerm { TermQueue.OnGameRun };
  122. FThreadSafeDelegate& OnTermTS { TermQueue.OnSyncRunTS };
  123. // AkGlobalCallbackLocation_Monitor: Send monitor data
  124. void MonitorAsync(FAsyncFunction&& InFunction) { MonitorQueue.AsyncDefer(MoveTemp(InFunction)); }
  125. void MonitorGame(FGameFunction&& InFunction) { MonitorQueue.GameDefer(MoveTemp(InFunction)); }
  126. void MonitorSync(FSyncFunction&& InFunction);
  127. void MonitorCompletion(FCompletionPromise&& Promise);
  128. void WaitForMonitor();
  129. FGameThreadDelegate& OnMonitor { MonitorQueue.OnGameRun };
  130. FThreadSafeDelegate& OnMonitorTS { MonitorQueue.OnSyncRunTS };
  131. // AkGlobalCallbackLocation_MonitorRecap: Send monitor data connection to recap.
  132. void MonitorRecapAsync(FAsyncFunction&& InFunction) { MonitorRecapQueue.AsyncDefer(MoveTemp(InFunction)); }
  133. void MonitorRecapGame(FGameFunction&& InFunction) { MonitorRecapQueue.GameDefer(MoveTemp(InFunction)); }
  134. void MonitorRecapSync(FSyncFunction&& InFunction);
  135. void MonitorRecapCompletion(FCompletionPromise&& Promise);
  136. void WaitForMonitorRecap();
  137. FGameThreadDelegate& OnMonitorRecap { MonitorRecapQueue.OnGameRun };
  138. FThreadSafeDelegate& OnMonitorRecapTS { MonitorRecapQueue.OnSyncRunTS };
  139. // AkGlobalCallbackLocation_Init: Sound engine initialization.
  140. void InitAsync(FAsyncFunction&& InFunction) { InitQueue.AsyncDefer(MoveTemp(InFunction)); }
  141. void InitGame(FGameFunction&& InFunction) { InitQueue.GameDefer(MoveTemp(InFunction)); }
  142. void InitSync(FSyncFunction&& InFunction);
  143. void InitCompletion(FCompletionPromise&& Promise);
  144. void WaitForInit();
  145. FGameThreadDelegate& OnInit { InitQueue.OnGameRun };
  146. FThreadSafeDelegate& OnInitTS { InitQueue.OnSyncRunTS };
  147. // AkGlobalCallbackLocation_Suspend: Sound engine suspension through AK::SoundEngine::Suspend
  148. void SuspendAsync(FAsyncFunction&& InFunction) { SuspendQueue.AsyncDefer(MoveTemp(InFunction)); }
  149. void SuspendGame(FGameFunction&& InFunction) { SuspendQueue.GameDefer(MoveTemp(InFunction)); }
  150. void SuspendSync(FSyncFunction&& InFunction);
  151. void SuspendCompletion(FCompletionPromise&& Promise);
  152. void WaitForSuspend();
  153. FGameThreadDelegate& OnSuspend { SuspendQueue.OnGameRun };
  154. FThreadSafeDelegate& OnSuspendTS { SuspendQueue.OnSyncRunTS };
  155. // AkGlobalCallbackLocation_WakeupFromSuspend: Sound engine awakening through AK::SoundEngine::WakeupFromSuspend
  156. void WakeupFromSuspendAsync(FAsyncFunction&& InFunction) { WakeupFromSuspendQueue.AsyncDefer(MoveTemp(InFunction)); }
  157. void WakeupFromSuspendGame(FGameFunction&& InFunction) { WakeupFromSuspendQueue.GameDefer(MoveTemp(InFunction)); }
  158. void WakeupFromSuspendSync(FSyncFunction&& InFunction);
  159. void WakeupFromSuspendCompletion(FCompletionPromise&& Promise);
  160. void WaitForWakeupFromSuspend();
  161. FGameThreadDelegate& OnWakeupFromSuspend { WakeupFromSuspendQueue.OnGameRun };
  162. FThreadSafeDelegate& OnWakeupFromSuspendTS { WakeupFromSuspendQueue.OnSyncRunTS };
  163. protected:
  164. virtual void OnRegisterCallback(AK::IAkGlobalPluginContext* in_pContext);
  165. virtual void OnBeginCallback(AK::IAkGlobalPluginContext* in_pContext);
  166. virtual void OnPreProcessMessageQueueForRenderCallback(AK::IAkGlobalPluginContext* in_pContext);
  167. virtual void OnPostMessagesProcessedCallback(AK::IAkGlobalPluginContext* in_pContext);
  168. virtual void OnBeginRenderCallback(AK::IAkGlobalPluginContext* in_pContext);
  169. virtual void OnEndRenderCallback(AK::IAkGlobalPluginContext* in_pContext);
  170. virtual void OnEndCallback(AK::IAkGlobalPluginContext* in_pContext);
  171. virtual void OnTermCallback(AK::IAkGlobalPluginContext* in_pContext);
  172. virtual void OnMonitorCallback(AK::IAkGlobalPluginContext* in_pContext);
  173. virtual void OnMonitorRecapCallback(AK::IAkGlobalPluginContext* in_pContext);
  174. virtual void OnInitCallback(AK::IAkGlobalPluginContext* in_pContext);
  175. virtual void OnSuspendCallback(AK::IAkGlobalPluginContext* in_pContext);
  176. virtual void OnWakeupFromSuspendCallback(AK::IAkGlobalPluginContext* in_pContext);
  177. private:
  178. static void OnRegisterCallbackStatic(
  179. AK::IAkGlobalPluginContext * in_pContext, ///< Engine context.
  180. AkGlobalCallbackLocation in_eLocation, ///< Location where this callback is fired.
  181. void * in_pCookie ///< User cookie passed to AK::SoundEngine::RegisterGlobalCallback().
  182. );
  183. static void OnBeginCallbackStatic(
  184. AK::IAkGlobalPluginContext * in_pContext, ///< Engine context.
  185. AkGlobalCallbackLocation in_eLocation, ///< Location where this callback is fired.
  186. void * in_pCookie ///< User cookie passed to AK::SoundEngine::RegisterGlobalCallback().
  187. );
  188. static void OnPreProcessMessageQueueForRenderCallbackStatic(
  189. AK::IAkGlobalPluginContext * in_pContext, ///< Engine context.
  190. AkGlobalCallbackLocation in_eLocation, ///< Location where this callback is fired.
  191. void * in_pCookie ///< User cookie passed to AK::SoundEngine::RegisterGlobalCallback().
  192. );
  193. static void OnPostMessagesProcessedCallbackStatic(
  194. AK::IAkGlobalPluginContext * in_pContext, ///< Engine context.
  195. AkGlobalCallbackLocation in_eLocation, ///< Location where this callback is fired.
  196. void * in_pCookie ///< User cookie passed to AK::SoundEngine::RegisterGlobalCallback().
  197. );
  198. static void OnBeginRenderCallbackStatic(
  199. AK::IAkGlobalPluginContext * in_pContext, ///< Engine context.
  200. AkGlobalCallbackLocation in_eLocation, ///< Location where this callback is fired.
  201. void * in_pCookie ///< User cookie passed to AK::SoundEngine::RegisterGlobalCallback().
  202. );
  203. static void OnEndRenderCallbackStatic(
  204. AK::IAkGlobalPluginContext * in_pContext, ///< Engine context.
  205. AkGlobalCallbackLocation in_eLocation, ///< Location where this callback is fired.
  206. void * in_pCookie ///< User cookie passed to AK::SoundEngine::RegisterGlobalCallback().
  207. );
  208. static void OnEndCallbackStatic(
  209. AK::IAkGlobalPluginContext * in_pContext, ///< Engine context.
  210. AkGlobalCallbackLocation in_eLocation, ///< Location where this callback is fired.
  211. void * in_pCookie ///< User cookie passed to AK::SoundEngine::RegisterGlobalCallback().
  212. );
  213. static void OnTermCallbackStatic(
  214. AK::IAkGlobalPluginContext * in_pContext, ///< Engine context.
  215. AkGlobalCallbackLocation in_eLocation, ///< Location where this callback is fired.
  216. void * in_pCookie ///< User cookie passed to AK::SoundEngine::RegisterGlobalCallback().
  217. );
  218. static void OnMonitorCallbackStatic(
  219. AK::IAkGlobalPluginContext * in_pContext, ///< Engine context.
  220. AkGlobalCallbackLocation in_eLocation, ///< Location where this callback is fired.
  221. void * in_pCookie ///< User cookie passed to AK::SoundEngine::RegisterGlobalCallback().
  222. );
  223. static void OnMonitorRecapCallbackStatic(
  224. AK::IAkGlobalPluginContext * in_pContext, ///< Engine context.
  225. AkGlobalCallbackLocation in_eLocation, ///< Location where this callback is fired.
  226. void * in_pCookie ///< User cookie passed to AK::SoundEngine::RegisterGlobalCallback().
  227. );
  228. static void OnInitCallbackStatic(
  229. AK::IAkGlobalPluginContext * in_pContext, ///< Engine context.
  230. AkGlobalCallbackLocation in_eLocation, ///< Location where this callback is fired.
  231. void * in_pCookie ///< User cookie passed to AK::SoundEngine::RegisterGlobalCallback().
  232. );
  233. static void OnSuspendCallbackStatic(
  234. AK::IAkGlobalPluginContext * in_pContext, ///< Engine context.
  235. AkGlobalCallbackLocation in_eLocation, ///< Location where this callback is fired.
  236. void * in_pCookie ///< User cookie passed to AK::SoundEngine::RegisterGlobalCallback().
  237. );
  238. static void OnWakeupFromSuspendCallbackStatic(
  239. AK::IAkGlobalPluginContext * in_pContext, ///< Engine context.
  240. AkGlobalCallbackLocation in_eLocation, ///< Location where this callback is fired.
  241. void * in_pCookie ///< User cookie passed to AK::SoundEngine::RegisterGlobalCallback().
  242. );
  243. };