版本
menu_open
Wwise SDK 2023.1.3
初始化声音引擎模块

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

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

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

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

初始化 Memory Manager

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

#include <AK/SoundEngine/Common/AkMemoryMgr.h> // Memory Manager interface
#include <AK/SoundEngine/Common/AkModule.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 实例,并会创建流播放设备。This requires an instance of AK::StreamMgr::IAkLowLevelIOHook. This interface is defined in AkStreamMgrModule.h, which contains all definitions that are specific to the default Stream Manager implementation provided with the SDK.

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

This example uses the sample CAkFilePackageLowLevelIODeferred as is. This class implements both AK::StreamMgr::IAkFileLocationResolver and AK::StreamMgr::IAkLowLevelIOHook interfaces, and is able to load file packages generated with the File Packager utility (refer to 文件包底层 I/O 实现 and File Packager 实用程序 for more information about the File Packager and how it works in the Low-Level I/O).

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

#include <AK/SoundEngine/Common/IAkStreamMgr.h> // Streaming Manager
#include <AK/Tools/Common/AkPlatformFuncs.h> // Thread defines
#include <AkFilePackageLowLevelIODeferred.h> // Sample low-level I/O implementation
(...)
// 我们使用作为 SDK 代码示例一部分的
// of the SDK's sample code, with the file package extension
CAkFilePackageLowLevelIODeferred g_lowLevelIO;
(...)
bool InitSoundEngine()
{
(...)
//
// 创建并初始化默认 Streaming Manager 实例。注意,
// 可以使用您自己的流管理器覆盖默认流管理器。
//
AkStreamMgrSettings stmSettings;
// 在此自定义 Stream Manager 设置。
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;
// 在此自定义流播放设备设置。
// CAkFilePackageLowLevelIODeferred::Init() creates a streaming device
// in the Stream Manager, and registers itself as the 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()
{
(...)
//
// 使用默认初始化参数
// 创建声音引擎
//
AkInitSettings initSettings;
AkPlatformInitSettings platformInitSettings;
if ( AK::SoundEngine::Init( &initSettings, &platformInitSettings ) != AK_Success )
{
assert( !"Could not initialize the Sound Engine." );
return false;
}
(...)
}

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

初始化音乐引擎

如果您的工程使用互动音乐功能,在声音引擎之后必须对音乐引擎进行初始化。

#include <AK/MusicEngine/Common/AkMusicEngine.h> // 音乐引擎
(...)
bool InitSoundEngine()
{
(...)
//
// 使用默认初始化参数
// 对音乐引擎初始化
//
AkMusicSettings musicInit;
if ( AK::MusicEngine::Init( &musicInit ) != AK_Success )
{
assert( !"Could not initialize the Music 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;
}
(...)
}

通信初始化

如果您希望使用 Wwise 创作应用程序来连接游戏并进行游戏内混音、性能分析和故障排除,下一步就要对通信功能进行初始化。强烈建议这样做,因为游戏内混音、性能分析和故障排除是声音设计师高效处理游戏音频的强大武器。

备注: 要使用通信功能,您需要与 CommunicationCentral 模块建立链接。
备注: 通信功能只有在声音引擎的 Debug 和 Profile 配置中才可用。游戏的零售版中不需要通信功能,因此声音引擎的发布版本中没有这一功能。以下代码使用 AK_OPTIMIZED 符号将通信相关代码排除在发布版本之外。
// 为允许 Wwise 和游戏之间进行通信,包含以下代码——发布版本不需要
#ifndef AK_OPTIMIZED
#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(AkMusicSettings &out_settings)
@ AK_Success
The operation was successful.
Definition: AkTypes.h:215
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)
AKSOUNDENGINE_API AKRESULT Init(AkMusicSettings *in_pSettings)

此页面对您是否有帮助?

需要技术支持?

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

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

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

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

开始 Wwise 之旅