版本
menu

Wwise SDK 2025.1.4
初始化声音引擎模块

集成 Wwise 声音引擎的第一步是在游戏启动时正确初始化构成声音引擎的各种模块。

在本示例工程中,定义了以下函数来执行与声音引擎初始化相关的一切操作:

bool InitSoundEngine()
{
... 在此执行初始化...
return true;
}

在主函数的开头调用它。让我们查看一下需要初始化的各项:

初始化 Memory Manager

第一个您必须初始化的组件是 Memory Manager(内存管理器)。您可以使用以下代码来创建默认的 Memory Manager:

#include AK/SoundEngine/Common/AkMemoryMgr.h> // Memory Manager interface
#include AK/SoundEngine/Common/AkMemoryMgrModule.h> // Default memory manager
(...)
bool InitSoundEngine()
{
AkMemSettings memSettings;
if ( AK::MemoryMgr::Init( &memSettings ) != AK_Success )
{
assert( !"Could not create the memory manager." );
return false;
}
(...)
}
备注: 您可以覆盖 Memory Manager,在这种情况下,必须根据需要修改上述代码。请参阅 取代内存管理器 了解更多信息。

请参阅 Memory Manager 了解有关 Memory Manager 的更多详情。

初始化 Streaming Manager

Memory Manager 一旦初始化,我们就可以继续初始化 Streaming Manager 了。

以下代码对默认 Streaming Manager 进行初始化。它需要 AK::StreamMgr::IAkFileLocationResolver 实例,并会创建流播放设备。这需要 AK::StreamMgr::IAkLowLevelIOHook 实例。此接口在 AkStreamMgrModule.h 中定义。其中包含所有特定于 SDK 中默认 Stream Manager 实现的定义。

对于磁盘 I/O,集成声音引擎的推荐方式是使用默认 Stream Manager 实现,并实现 AkStreamMgrModule.h 中定义的接口。这些接口构成了 Low-Level I/O 子模块。请参阅 流播放/流管理器 了解更多信息。

本例中按原样使用了 CAkFilePackageLowLevelIODeferred 示例。此类用于实现 AK::StreamMgr::IAkFileLocationResolverAK::StreamMgr::IAkLowLevelIOHook 接口,并能加载通过 File Packager 实用程序生成的文件包(如需详细了解 File Packager 及其在 Low-Level I/O 中的运作方式,请参阅 文件包底层 I/O 实现File Packager 实用程序 章节)。

请参阅 Low-Level I/O 了解有关 Low-Level I/O 的更多信息。

#include AK/SoundEngine/Common/IAkStreamMgr.h> // Streaming Manager
#include AK/Tools/Common/AkPlatformFuncs.h> // 线程定义
#include // Low-Level I/O 实现示例
(...)
// 我们使用作为 SDK 代码示例一部分的
// 默认 Low-Level I/O 实现。文件包扩展名为
CAkFilePackageLowLevelIODeferred g_lowLevelIO;
(...)
bool InitSoundEngine()
{
(...)
//
// Create and initialize an instance of the default streaming manager. 注意,
// 可以使用您自己的流管理器覆盖默认流管理器。
//
AkStreamMgrSettings stmSettings;
// Customize the Stream Manager settings here.
if ( !AK::StreamMgr::Create( stmSettings ) )
{
assert( !"Could not create the Streaming Manager" );
return false;
}
//
// Create a streaming device.
// 注意,可以使用您自己的 Low-Level I/O 模块覆盖默认的 Low-Level I/O 模块。
//
AkDeviceSettings deviceSettings;
// Customize the streaming device settings here.
// CAkFilePackageLowLevelIODeferred::Init() 在 Stream Manager 中
// 创建流播放设备,并将其自身注册为 File Location Resolver。
if ( g_lowLevelIO.Init( deviceSettings ) != AK_Success )
{
assert( !"Could not create the streaming device and Low-Level I/O system" );
return false;
}
(...)
}

有关默认 Stream Manager 和流播放设备初始化设置的更多信息,请参阅 Audiokinetic Stream Manager 初始化设置

注意: 某些初始化设置使用针对特定平台的成员结构,例如 AkThreadProperties。

如果您决定覆盖默认的 Low-Level I/O 实现或者整个 Streaming Manager,需要相应地修改此代码。请参阅 流播放/流管理器 了解更多信息。

初始化声音引擎

现在基本模块已经初始化,下面我们可以对声音引擎本身进行初始化了:

#include AK/SoundEngine/Common/AkSoundEngine.h> // 声音引擎
(...)
bool InitSoundEngine()
{
(...)
//
// Create the Sound Engine
// Using default initialization parameters
//
AkInitSettings initSettings;
AkPlatformInitSettings platformInitSettings;
if ( AK::SoundEngine::Init( &initSettings, &platformInitSettings ) != AK_Success )
{
assert( !"Could not initialize the Sound Engine." );
return false;
}
(...)
}

有关如何初始化声音引擎的更多信息,请参阅 高级声音引擎集成

初始化 Spatial Audio

若您的工程使用 Wwise Spatial Audio,则在声音引擎之后还须初始化 Spatial Audio 库。有关如何使用 Spatial Audio 的基本信息,请参阅 API 设置 章节。如需了解可用于配置 Spatial Audio 的各项参数,请参阅 AkSpatialAudioInitSettings 章节。

备注: 若要使用 Spatial Audio,必须链接 Spatial Audio 模块。
#include AK/SpatialAudio/Common/AkSpatialAudio.h> // Spatial Audio
(...)
bool InitSoundEngine()
{
(...)
//
// 初始化 Spatial Audio
// 使用默认的初始化参数
//
AkSpatialAudioInitSettings settings; // 构造函数将建议的默认设置赋予 AkSpatialAudioInitSettings。
if ( AK::SpatialAudio::Init( &settings ) != AK_Success )
{
assert( !"Could not initialize the Spatial Audio." );
return false;
}
(...)
}

通信初始化

If you want to use Wwise Authoring to connect to your game and perform in-game mixing, profiling, and troubleshooting, you must initialize communications. We recommend that you do so because in-game mixing, profiling, and troubleshooting are powerful ways for sound designers to work on your game's audio efficiently.

To use communications, you need to link to the CommunicationCentral module.

To use CommunicationCentral on Windows, you must also link to the external Winsock 2 library (ws2_32.lib).

Communications are only available in the Debug and Profile sound engine configurations. They are not needed in the retail version of your game, so they are not part of the sound engine Release build. The following code uses the AK_OPTIMIZED symbol to omit communication-specific code from the release build.

// 为允许 Wwise 和游戏之间进行通信,包含以下代码——发布版本不需要
#ifndef AK_OPTIMIZED
#include AK/Comm/AkCommunication.h>
#endif // AK_OPTIMIZED
(...)
bool InitSoundEngine()
{
(...)
#ifndef AK_OPTIMIZED
//
// 对通信进行初始化(在发布版本中无此步骤!)
//
AkCommSettings commSettings;
if ( AK::Comm::Init( commSettings ) != AK_Success )
{
assert( !"Could not initialize communication." );
return false;
}
#endif // AK_OPTIMIZED
(...)
}

现在您已将声音引擎的所有模块初始化。然后应注册插件(请参阅 插件示例 )。请参阅 创建重复调用来执行音频处理 详细了解您在游戏循环中用于处理音频的、必须执行的调用。

控制台相关通信库

有些控制台通信库不能正确地平衡初始化/终止调用;因此,调用 AK::Comm::Term 将终止控制台的底层通信库。这将关闭游戏的所有 TCP/IP 通信。如果您的游戏需要在终止后使通信库保持启用状态,AkCommSettings(结合 Ak::Comm:Init 使用)具有可供选择是否将系统库初始化的参数。如果您选择自己将通信库初始化,则请参照 Wwise 声音引擎的以下代码。游戏中应执行类似的代码。

Windows 套接字初始化:

WSAData wsaData = { 0 };
::WSAStartup( MAKEWORD( 2, 2 ), &wsaData )

终止:

::WSACleanup();

通信端口

AkCommSettings 结构的 AkCommSettings::ports 成员代表 Wwise 创作应用程序与声音引擎之间进行通信所使用的网络端口。当 Wwise 通信功能启用时,所有这些端口在游戏中都会打开。

一个固定端口:Discovery Broadcast

其中 AkCommSettings::Ports::uDiscoveryBroadcast 端口不是动态的(不能设置为 0)。Wwise 创作应用程序需知道该端口才能在网络上发现游戏。另外,此处的指定值必须是创作应用程序 Project Settings 的 Network 选项卡中指定的值。

技巧: 如果您的团队正在使用 Wwise 制作多款游戏,可以为每款游戏使用不同的 Discovery Broadcast 端口。通过这种方法,在 Wwise 创作应用程序中打开 Remote Connections 窗口时,只有对应当前 Wwise 工程的游戏才会被列出。更改此端口时,请确保在创作应用程序的 Project Settings 中游戏中传递给 AK::Comm::Init()AkCommSettings 结构中一起更改。

一个动态端口

结构中的另一端口可以是动态的(或临时的)。这意味着,操作系统使用的不是固定端口,而是自动选择一个端口:

此端口在默认情况下是动态的。建议将其保留为动态端口以尽量降低与其他应用程序发生冲突的机率。

若其与游戏的其他组件所用的端口发生冲突,请在调用 AK::Comm::Init() 前在 AkCommSettings 结构中加以更改。

备注: 在多平台游戏中,可在不同平台上对 AkCommSettings::Ports::uCommand 使用不同的端口。

技巧: 有关动态/临时端口的详细信息,请参阅以下文章:

另一个端口(仅限于创作应用程序)

Wwise 通信中还涉及另一个端口,但它只在创作应用程序中打开,因此不是 AkCommSettings::Ports 结构的一部分。

创作应用程序使用该端口来接收针对 Discovery Broadcast 消息的响应,这些消息用于检测网络上运行的游戏。它在默认情况下是动态的(设置为 0),但可以在创作应用程序的 Project Settings 中进行更改。

AKSOUNDENGINE_API AKRESULT Init(AkInitSettings *in_pSettings, AkPlatformInitSettings *in_pPlatformSettings)
AKSOUNDENGINE_API AKRESULT Init(const AkSpatialAudioInitSettings &in_initSettings)
Initialize the SpatialAudio API.
AKSOUNDENGINE_API AKRESULT Init(AkMemSettings *in_pSettings)
AKSOUNDENGINE_API void GetDefaultSettings(AkStreamMgrSettings &out_settings)
AKSOUNDENGINE_API AKRESULT Init(const AkCommSettings &in_settings)
AKSOUNDENGINE_API void GetDefaultInitSettings(AkInitSettings &out_settings)
AKSOUNDENGINE_API void GetDefaultSettings(AkMemSettings &out_pMemSettings)
Obtain the default initialization settings for the default implementation of the Memory Manager.
AKSOUNDENGINE_API void GetDefaultInitSettings(AkCommSettings &out_settings)
Initialization settings of the spatial audio module.
AKSOUNDENGINE_API void GetDefaultPlatformInitSettings(AkPlatformInitSettings &out_platformSettings)
AKSOUNDENGINE_API IAkStreamMgr * Create(const AkStreamMgrSettings &in_settings)
AKSOUNDENGINE_API void GetDefaultDeviceSettings(AkDeviceSettings &out_settings)
@ AK_Success
The operation was successful.
Definition: AkEnums.h:34

此页面对您是否有帮助?

需要技术支持?

仍有疑问?或者问题?需要更多信息?欢迎联系我们,我们可以提供帮助!

查看我们的“技术支持”页面

介绍一下自己的项目。我们会竭力为您提供帮助。

来注册自己的项目,我们帮您快速入门,不带任何附加条件!

开始 Wwise 之旅