10#include <glm/vec3.hpp>
13#include <AK/SoundEngine/Common/AkSoundEngine.h>
14#include <AK/SoundEngine/Common/AkMemoryMgr.h>
15#include <AK/SoundEngine/Common/IAkStreamMgr.h>
16#include <AK/SoundEngine/Common/AkMemoryMgrModule.h>
19#include <AK/SpatialAudio/Common/AkSpatialAudio.h>
20#include <AK/Tools/Common/AkPlatformFuncs.h>
24#include <AK/Comm/AkCommunication.h>
29#include <AK/Plugin/AkVorbisDecoderFactory.h>
32#include <AK/Plugin/AkOpusDecoderFactory.h>
37#define BANKNAME_INIT L"Init.bnk"
41 WwiseSoundEngine* WwiseSoundEngine::Get()
46 bool WwiseSoundEngine::LoadBank(
const wchar_t* bankName)
49 AKRESULT res = AK::SoundEngine::LoadBank(bankName, bankID);
50 bool success = res == AK_Success;
54 std::string nameAsStr = std::filesystem::path(bankName).string();
55 EE_CORE_CERROR(
true,
"ERROR: Failed to load SoundBank : {}", nameAsStr);
59 bool WwiseSoundEngine::LoadBank(
const std::wstring& bankName)
61 return LoadBank(bankName.c_str());
63 bool WwiseSoundEngine::LoadBank(
const char* bankName)
66 AKRESULT res = AK::SoundEngine::LoadBank(bankName, bankID);
67 bool success = res == AK_Success;
68 EE_CORE_CERROR(!success,
"ERROR: Failed to load SoundBank : {}", bankName);
71 bool WwiseSoundEngine::LoadBank(std::string& bankName)
73 return LoadBank(bankName.c_str());
76 bool WwiseSoundEngine::InitImpl()
78 EE_CORE_INFO(
"Initializing Wwise Sound Engine...");
81 AkMemSettings memorySettings;
82 AK::MemoryMgr::GetDefaultSettings(memorySettings);
83 if (AK::MemoryMgr::Init(&memorySettings) != AK_Success)
85 EE_CORE_ASSERT(
false,
"ERROR: Failed to initialize the Wwise memory manager.");
90 EE_CORE_TRACE(
"Wwise memory manager initialized!");
94 AkStreamMgrSettings streamSettings;
95 AK::StreamMgr::GetDefaultSettings(streamSettings);
96 if (!AK::StreamMgr::Create(streamSettings))
98 EE_CORE_ASSERT(
false,
"ERROR: Failed to create the Wwise stream manager.");
103 EE_CORE_TRACE(
"Wwise stream manager initialized!");
107 AkDeviceSettings deviceSettings;
108 AK::StreamMgr::GetDefaultDeviceSettings(deviceSettings);
110 m_lowLevelIO = std::make_unique<CAkFilePackageLowLevelIODeferred>();
111 if (m_lowLevelIO->Init(deviceSettings) != AK_Success)
113 EE_CORE_ASSERT(
false,
"ERROR: Failed to create the Wwise streaming device and low-level I/O system.");
118 EE_CORE_TRACE(
"Wwise streaming device and low-level I/O initialized!");
122 AkInitSettings initSettings;
123 AK::SoundEngine::GetDefaultInitSettings(initSettings);
125 AkPlatformInitSettings platformInitSettings;
126 AK::SoundEngine::GetDefaultPlatformInitSettings(platformInitSettings);
128 if (AK::SoundEngine::Init(&initSettings, &platformInitSettings) != AK_Success)
130 EE_CORE_ASSERT(
false,
"ERROR: Failded to initialize the Sound Engine.");
135 EE_CORE_TRACE(
"Wwise SoundEngine initialized!");
139 AkSpatialAudioInitSettings spatialAudioSettings;
140 if (AK::SpatialAudio::Init(spatialAudioSettings) != AK_Success)
142 EE_CORE_ASSERT(
false,
"ERROR: Failded to initialize Spatial Audio.");
147 EE_CORE_TRACE(
"Wwise Spatial Audio initialized!");
151 AkCommSettings communicationSettings;
152 AK::Comm::GetDefaultInitSettings(communicationSettings);
153 if (AK::Comm::Init(communicationSettings) != AK_Success)
155 EE_CORE_ASSERT(
false,
"ERROR: Failded to initialize Wwise communication.");
160 EE_CORE_TRACE(
"Wwise communication initialized!");
164 EE_CORE_INFO(
"Wwise Initialized!");
168#ifdef EE_EDITOR_BUILD
171 std::string wwiseProjectPath =
"./WwiseProject";
172 std::string rootOutputPath = wwiseProjectPath +
"/GeneratedSoundBanks";
173 std::string currentPlatform =
"Windows";
175 m_mergedDataSource.reset(
new WwiseMergedDataSource(wwiseProjectPath, rootOutputPath, currentPlatform));
176 m_mergedDataSource->InitializeSource();
185 void WwiseSoundEngine::PrepareAudio()
187 m_lowLevelIO->SetBasePath(AKTEXT(
"./WwiseProject/GeneratedSoundBanks/Windows/"));
188 AK::StreamMgr::SetCurrentLanguage(AKTEXT(
"English(US)"));
190 LoadBank(BANKNAME_INIT);
191 LoadBank(L
"Sandbox.bnk");
194 void WwiseSoundEngine::RenderAudioImpl()
196 AK::SoundEngine::RenderAudio();
199 void WwiseSoundEngine::TerminateImpl()
205 AK::SoundEngine::Term();
207 m_lowLevelIO->Term();
209 if (AK::IAkStreamMgr::Get())
211 AK::IAkStreamMgr::Get()->Destroy();
214 AK::MemoryMgr::Term();
217 void WwiseSoundEngine::WakeUpImpl()
220 AK::SoundEngine::WakeupFromSuspend();
221 AK::SoundEngine::RenderAudio();
224 void WwiseSoundEngine::SuspendImpl(
bool renderAnyway,
bool fadeOut)
226 AK::SoundEngine::Suspend(renderAnyway, fadeOut);
229 void WwiseSoundEngine::SetDefaultListenerImpl(GameObject* obj)
233 m_currentListenerID = obj->GetObjectId();
234 AK::SoundEngine::SetDefaultListeners(&m_currentListenerID, 1);
238 void WwiseSoundEngine::SetDistanceProbeImpl(GameObject* obj)
242 AK::SoundEngine::SetDistanceProbe(m_currentListenerID, obj->GetObjectId());
246 void WwiseSoundEngine::UnsetDistanceProbeImpl()
248 AK::SoundEngine::SetDistanceProbe(m_currentListenerID, m_currentListenerID);
251 void WwiseSoundEngine::RegisterGameObjectImpl(GameObject* obj)
255 AkGameObjectID
id = obj->GetObjectId();
256 if (AK::SoundEngine::RegisterGameObj(
id, obj->GetName().c_str()) != AK_Success)
258 EE_CORE_ERROR(
"SoundEngine Error : Unable to register gO => (name : {}), (id : {})", obj->GetObjectId(), obj->GetName().c_str());
262 EE_CORE_TRACE(
"SoundEngine registed gO => (name : {}), (id : {})", obj->GetObjectId(), obj->GetName().c_str());
267 void WwiseSoundEngine::UnregisterGameObjectImpl(GameObject* obj)
271 AkGameObjectID
id = obj->GetObjectId();
272 AK::SoundEngine::UnregisterGameObj(
id);
276 void WwiseSoundEngine::UpdateObjectPositionImpl(GameObject* obj)
280 const glm::vec3 objectPosition = obj->GetPosition();
281 AkGameObjectID
id = obj->GetObjectId();
283 const Transform& transform = obj->GetTransform();
284 glm::vec3 top = glm::normalize(transform.GetUp());
285 glm::vec3 front = glm::normalize(transform.GetForward());
287 AkSoundPosition soundPosition;
288 soundPosition.SetPosition(objectPosition.x, objectPosition.y, objectPosition.z);
289 soundPosition.SetOrientation(front.x, front.y, front.z, top.x, top.y, top.z);
291 if (AK::SoundEngine::SetPosition(
id, soundPosition) == AK_InvalidParameter)
293 EE_CORE_ERROR(
"SoundEngine Error : could not set the transform values for {} with values [{}, {}, {}]", obj->GetName(), objectPosition.x, objectPosition.y, objectPosition.z);
298 void WwiseSoundEngine::PostEventImpl(
const char* eventName, GameObject* obj)
302 AkGameObjectID
id = obj->GetObjectId();
303 AK::SoundEngine::PostEvent(eventName,
id);
307 EE_CORE_ERROR(
"Error (WwiseSoundEngine::PostEventImpl) : Cannot play an event on a null GameObject.");
311 void WwiseSoundEngine::PostEventImpl(uint32_t eventId, GameObject* obj)
315 AkGameObjectID
id = obj->GetObjectId();
316 AK::SoundEngine::PostEvent(eventId,
id);
320 EE_CORE_ERROR(
"Error (WwiseSoundEngine::PostEventImpl) : Cannot play an event on a null GameObject.");
324 void WwiseSoundEngine::PostEventImpl(
const char* eventName)
326 AK::SoundEngine::PostEvent(eventName, m_currentListenerID);
329 void WwiseSoundEngine::PostEventImpl(uint32_t eventId)
331 AK::SoundEngine::PostEvent(eventId, m_currentListenerID);
334#ifdef EE_EDITOR_BUILD
335 std::shared_ptr<WwiseDataSource> WwiseSoundEngine::GetDataSource()
337 return m_mergedDataSource;
static SoundEngine * GetImpl()
static WAAPIClient & Get()