WwiseSound.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #include "WwiseSound.h"
  2. #include "BFApp.h"
  3. #if (_MSC_VER != 1800)
  4. #undef BF_WWISE_ENABLED
  5. #endif
  6. #ifdef BF_WWISE_ENABLED
  7. #if _DEBUG
  8. #define BF_IS_WWISE_COMM_ENABLED 1
  9. #endif
  10. #include "AK/SoundEngine/Common/AkTypes.h"
  11. #include <AK/SoundEngine/Common/AkMemoryMgr.h> // Memory Manager
  12. #include <AK/SoundEngine/Common/AkModule.h> // Default memory and stream managers
  13. #include <AK/SoundEngine/Common/IAkStreamMgr.h> // Streaming Manager
  14. #include <AK/SoundEngine/Common/AkSoundEngine.h> // Sound engine
  15. #include <AK/MusicEngine/Common/AkMusicEngine.h> // Music Engine
  16. #include <AK/SoundEngine/Common/AkStreamMgrModule.h> // AkStreamMgrModule
  17. #include <AK/Plugin/AllPluginsFactories.h>
  18. #include "Win32/AkDefaultIOHookBlocking.h"
  19. #ifndef AK_OPTIMIZED
  20. #include <AK/Comm/AkCommunication.h> // Communication between Wwise and the game (excluded in release build)
  21. #endif
  22. /*
  23. AkMusicEngine.lib
  24. AkHarmonizerFX.lib
  25. AkPitchShifterFX.lib
  26. AkStereoDelayFX.lib
  27. AkTimeStretchFX.lib
  28. AkMeterFX.lib
  29. AkConvolutionReverbFX.lib
  30. AkAudioInputSource.lib
  31. AkSilenceSource.lib
  32. AkPeakLimiterFX.lib
  33. AkRoomVerbFX.lib
  34. AkParametricEQFX.lib
  35. AkGainFX.lib
  36. AkDelayFX.lib
  37. AkMatrixReverbFX.lib
  38. AkExpanderFX.lib
  39. AkCompressorFX.lib
  40. AkSineSource.lib
  41. AkGuitarDistortionFX.lib
  42. AkTremoloFX.lib
  43. AkFlangerFX.lib
  44. AkToneSource.lib
  45. AkSoundSeedImpactFX.lib
  46. McDSPLimiterFX.lib McDSPFutzBoxFX.lib AkSoundSeedWoosh.lib AkSoundSeedWind.lib AkStreamMgr.lib AkVorbisDecoder.lib ;%(AdditionalDependencies)*/
  47. //iZHybridReverbFX.lib iZTrashBoxModelerFX.lib iZTrashDelayFX.lib iZTrashDistortionFX.lib iZTrashDynamicsFX.lib iZTrashFiltersFX.lib iZTrashMultibandDistortionFX.lib McDSPLimiterFX.lib
  48. #pragma comment(lib, "AkSoundEngine.lib")
  49. #pragma comment(lib, "AkMemoryMgr.lib")
  50. #pragma comment(lib, "AkStreamMgr.lib")
  51. #pragma comment(lib, "AkMusicEngine.lib")
  52. #pragma comment(lib, "AkVorbisDecoder.lib")
  53. #pragma comment(lib, "AkAudioInputSource.lib")
  54. #pragma comment(lib, "AkToneSource.lib")
  55. #pragma comment(lib, "AkSilenceSource.lib")
  56. #pragma comment(lib, "AkSineSource.lib")
  57. //#pragma comment(lib, "AkMP3Source.lib")
  58. #pragma comment(lib, "AkMatrixReverbFX.lib")
  59. #pragma comment(lib, "AkMeterFX.lib")
  60. #pragma comment(lib, "AkParametricEQFX.lib")
  61. #pragma comment(lib, "AkGainFX.lib")
  62. #pragma comment(lib, "AkDelayFX.lib")
  63. #pragma comment(lib, "AkCompressorFX.lib")
  64. #pragma comment(lib, "AkExpanderFX.lib")
  65. #pragma comment(lib, "AkPeakLimiterFX.lib")
  66. #pragma comment(lib, "AkRoomVerbFX.lib")
  67. #pragma comment(lib, "AkGuitarDistortionFX.lib")
  68. #pragma comment(lib, "AkFlangerFX.lib")
  69. #pragma comment(lib, "AkStereoDelayFX.lib")
  70. #pragma comment(lib, "AkTimeStretchFX.lib")
  71. #pragma comment(lib, "AkHarmonizerFX.lib")
  72. #pragma comment(lib, "AkPitchShifterFX.lib")
  73. #pragma comment(lib, "AkConvolutionReverbFX.lib")
  74. #pragma comment(lib, "AkTremoloFX.lib")
  75. #pragma comment(lib, "AkRumble.lib")
  76. #pragma comment(lib, "AkMotionGenerator.lib")
  77. #pragma comment(lib, "AkSoundSeedWind.lib")
  78. #pragma comment(lib, "AkSoundSeedWoosh.lib")
  79. #pragma comment(lib, "AkSoundSeedImpactFX.lib")
  80. #pragma comment(lib, "McDSPFutzBoxFX.lib")
  81. #pragma comment(lib, "dxguid.lib")
  82. #ifdef BF_IS_WWISE_COMM_ENABLED
  83. #pragma comment(lib, "CommunicationCentral.lib")
  84. #endif
  85. #pragma comment(lib, "wsock32.lib")
  86. static bool gWwiseInitialized = false;
  87. USING_NS_BF;
  88. #define DEMO_DEFAULT_POOL_SIZE 2*1024*1024
  89. #define DEMO_LENGINE_DEFAULT_POOL_SIZE 1*1024*1024
  90. CAkDefaultIOHookBlocking* m_pLowLevelIO = NULL;
  91. namespace AK
  92. {
  93. void * AllocHook( size_t in_size )
  94. {
  95. return malloc( in_size );
  96. }
  97. void FreeHook( void * in_ptr )
  98. {
  99. free( in_ptr );
  100. }
  101. #ifdef _WINDOWS
  102. // Note: VirtualAllocHook() may be used by I/O pools of the default implementation
  103. // of the Stream Manager, to allow "true" unbuffered I/O (using FILE_FLAG_NO_BUFFERING
  104. // - refer to the Windows SDK documentation for more details). This is NOT mandatory;
  105. // you may implement it with a simple malloc().
  106. void * VirtualAllocHook(
  107. void * in_pMemAddress,
  108. size_t in_size,
  109. DWORD in_dwAllocationType,
  110. DWORD in_dwProtect
  111. )
  112. {
  113. return VirtualAlloc( in_pMemAddress, in_size, in_dwAllocationType, in_dwProtect );
  114. }
  115. void VirtualFreeHook(
  116. void * in_pMemAddress,
  117. size_t in_size,
  118. DWORD in_dwFreeType
  119. )
  120. {
  121. VirtualFree( in_pMemAddress, in_size, in_dwFreeType );
  122. }
  123. #endif
  124. }
  125. static bool WWise_Init()
  126. {
  127. m_pLowLevelIO = new CAkDefaultIOHookBlocking();
  128. AkMemSettings memSettings;
  129. AkStreamMgrSettings stmSettings;
  130. AkDeviceSettings deviceSettings;
  131. AkInitSettings initSettings;
  132. AkPlatformInitSettings platformInitSettings;
  133. AkMusicSettings musicInit;
  134. // Get default settings
  135. memSettings.uMaxNumPools = 20;
  136. AK::StreamMgr::GetDefaultSettings( stmSettings );
  137. AK::StreamMgr::GetDefaultDeviceSettings( deviceSettings );
  138. AK::SoundEngine::GetDefaultInitSettings( initSettings );
  139. initSettings.uDefaultPoolSize = DEMO_DEFAULT_POOL_SIZE;
  140. #if defined( INTEGRATIONDEMO_ASSERT_HOOK )
  141. initSettings.pfnAssertHook = INTEGRATIONDEMO_ASSERT_HOOK;
  142. #endif // defined( INTEGRATIONDEMO_ASSERT_HOOK )
  143. AK::SoundEngine::GetDefaultPlatformInitSettings( platformInitSettings );
  144. platformInitSettings.uLEngineDefaultPoolSize = DEMO_LENGINE_DEFAULT_POOL_SIZE;
  145. AK::MusicEngine::GetDefaultInitSettings( musicInit );
  146. //
  147. // Create and initialize an instance of the default memory manager. Note
  148. // that you can override the default memory manager with your own. Refer
  149. // to the SDK documentation for more information.
  150. //
  151. AKRESULT res = AK::MemoryMgr::Init( &memSettings );
  152. if ( res != AK_Success )
  153. {
  154. return false;
  155. }
  156. //
  157. // Create and initialize an instance of the default streaming manager. Note
  158. // that you can override the default streaming manager with your own. Refer
  159. // to the SDK documentation for more information.
  160. //
  161. // Customize the Stream Manager settings here.
  162. if ( !AK::StreamMgr::Create( stmSettings ) )
  163. {
  164. return false;
  165. }
  166. //
  167. // Create a streaming device with blocking low-level I/O handshaking.
  168. // Init() creates a streaming device in the Stream Manager, and registers itself as the File Location Resolver.
  169. deviceSettings.uSchedulerTypeFlags = AK_SCHEDULER_BLOCKING;
  170. res = m_pLowLevelIO->Init( deviceSettings );
  171. if ( res != AK_Success )
  172. {
  173. return false;
  174. }
  175. // Set the path to the SoundBank Files.
  176. /*#ifdef HOST_WINDOWS
  177. std::wstring wFullPath = StringToWString(m_filePath);
  178. m_pLowLevelIO->SetBasePath( wFullPath.c_str() );
  179. #else
  180. m_pLowLevelIO->SetBasePath( m_filePath.c_str() );
  181. #endif */
  182. //
  183. // Create the Sound Engine
  184. // Using default initialization parameters
  185. //
  186. res = AK::SoundEngine::Init( &initSettings, &platformInitSettings );
  187. if ( res != AK_Success )
  188. {
  189. return false;
  190. }
  191. //
  192. // Initialize the music engine
  193. // Using default initialization parameters
  194. //
  195. res = AK::MusicEngine::Init( &musicInit );
  196. if ( res != AK_Success )
  197. {
  198. return false;
  199. }
  200. #if defined HOST_MACOSX || defined HOST_IPHONEOS
  201. // Register the AAC codec for Mac/iOS.
  202. AK::SoundEngine::RegisterCodec(
  203. AKCOMPANYID_AUDIOKINETIC,
  204. AKCODECID_AAC,
  205. CreateAACFilePlugin,
  206. CreateAACBankPlugin );
  207. #endif
  208. #if BF_IS_WWISE_COMM_ENABLED
  209. //
  210. // Initialize communications (not in release build!)
  211. //
  212. AkCommSettings commSettings;
  213. AK::Comm::GetDefaultInitSettings( commSettings );
  214. res = AK::Comm::Init( commSettings );
  215. if ( res != AK_Success )
  216. {
  217. }
  218. #endif // BF_IS_WWISE_COMM_ENABLED
  219. gWwiseInitialized = true;
  220. return true;
  221. }
  222. void Beefy::WWiseUpdate()
  223. {
  224. if (gWwiseInitialized)
  225. AK::SoundEngine::RenderAudio();
  226. }
  227. BF_EXPORT void BF_CALLTYPE Wwise_Shutdown()
  228. {
  229. if (!gWwiseInitialized)
  230. return;
  231. #if BF_IS_WWISE_COMM_ENABLED
  232. //
  233. // Terminate Communication Services (not in release build!)
  234. //
  235. AK::Comm::Term();
  236. #endif // BF_IS_WWISE_COMM_ENABLED
  237. //
  238. // Terminate the music engine
  239. //
  240. AK::MusicEngine::Term();
  241. //
  242. // Terminate the sound engine
  243. //
  244. AK::SoundEngine::Term();
  245. // Terminate the streaming device and streaming manager
  246. // SexyIOHookBlocking::Term() destroys its associated streaming device
  247. // that lives in the Stream Manager, and unregisters itself as the File Location Resolver.
  248. m_pLowLevelIO->Term();
  249. delete m_pLowLevelIO;
  250. if ( AK::IAkStreamMgr::Get() )
  251. AK::IAkStreamMgr::Get()->Destroy();
  252. // Terminate the Memory Manager
  253. AK::MemoryMgr::Term();
  254. gWwiseInitialized = false;
  255. }
  256. //
  257. BF_EXPORT void* BF_CALLTYPE Wwise_LoadBankByName(const uint16* name)
  258. {
  259. if (!gWwiseInitialized)
  260. WWise_Init();
  261. std::wstring bankFileName = gBFApp->mInstallDir + UTF16Decode(name) + L".bnk";
  262. AkBankID bankID;
  263. AKRESULT result = AK::SoundEngine::LoadBank(bankFileName.c_str(), AK_DEFAULT_POOL_ID, bankID);
  264. if (result != AK_Success)
  265. return NULL;
  266. return (void*) bankID;
  267. }
  268. BF_EXPORT uint32 BF_CALLTYPE Wwise_SendEvent(uint32 eventId, void* gameObject)
  269. {
  270. uint32 playingSound;
  271. playingSound = AK::SoundEngine::PostEvent(eventId, (AkGameObjectID) gameObject);
  272. return playingSound;
  273. }
  274. BF_EXPORT int BF_CALLTYPE Wwise_SetRTPCValue(uint32 paramId, float value, void* gameObject)
  275. {
  276. if (gameObject == 0)
  277. {
  278. if (AK::SoundEngine::SetRTPCValue(paramId, value, (AkGameObjectID) AK_INVALID_GAME_OBJECT) == AK_Success)
  279. return true;
  280. }
  281. else
  282. {
  283. if (AK::SoundEngine::SetRTPCValue(paramId, value, (AkGameObjectID) gameObject) == AK_Success)
  284. return true;
  285. }
  286. return false;
  287. }
  288. #else
  289. void Beefy::WWiseUpdate()
  290. {
  291. }
  292. BF_EXPORT void* BF_CALLTYPE Wwise_LoadBankByName(const uint16* name)
  293. {
  294. return NULL;
  295. }
  296. BF_EXPORT uint32 BF_CALLTYPE Wwise_SendEvent(uint32 eventId, void* gameObject)
  297. {
  298. return 0;
  299. }
  300. BF_EXPORT int BF_CALLTYPE Wwise_SetRTPCValue(uint32 paramId, float value, void* gameObject)
  301. {
  302. return false;
  303. }
  304. BF_EXPORT void BF_CALLTYPE Wwise_Shutdown()
  305. {
  306. }
  307. #endif