x01.Game.CubeRun: XACT3 播放声音

时间:2022-10-10 04:39:32

1.使用 Xact3 工具

在 DXSDK 安装目录 => Uilities => bin => x86 里,运行 Xact3.exe 程序,新建波形库(Wave Bank)、声音库(Sound Bank) 项目,将 clip.wav 或类似的文件复制到 Wave Bank 工作区,再将其拖入 Sound Bank 工作区,选择 Build 菜单,便可生成 XBox 和 Win 目录,里面即为待用的声音文件。

2.添加 XactAudio 类

#pragma once

#include <xact3.h>

class XACT3Audio
{
public:
XACT3Audio(void);
~XACT3Audio(void); bool InitEngine();
bool LoadWaveBank(LPCWSTR path);
bool LoadSoundBank(LPCWSTR path);
void* MapFileData(LPCWSTR path, DWORD& size);
void Clearup(); IXACT3Engine* XactEngine;
IXACT3WaveBank* WaveBank;
IXACT3SoundBank* SoundBank; private:
void* m_pWaveData;
void* m_pSoundData;
};

XactAudio.h

#include <iostream>
#include "XACT3Audio.h" XACT3Audio::XACT3Audio(void) :
WaveBank(nullptr), SoundBank(nullptr), m_pWaveData(nullptr), m_pSoundData(nullptr)
{
} XACT3Audio::~XACT3Audio(void)
{
Clearup();
} bool XACT3Audio::InitEngine()
{
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hr))
{
return false;
} hr = XACT3CreateEngine(XACT_FLAG_API_AUDITION_MODE, &XactEngine);
if (FAILED(hr))
{
return false;
} XACT_RUNTIME_PARAMETERS rp = { };
rp.lookAheadTime = XACT_ENGINE_LOOKAHEAD_DEFAULT; hr = XactEngine->Initialize(&rp);
if (FAILED(hr))
{
return false;
} return true;
} bool XACT3Audio::LoadWaveBank(LPCWSTR path)
{
DWORD size = ;
m_pWaveData = MapFileData(path, size); HRESULT hr = XactEngine->CreateInMemoryWaveBank(m_pWaveData, size, , , &WaveBank);
if (FAILED(hr))
{
return false;
} return true;
} bool XACT3Audio::LoadSoundBank(LPCWSTR path)
{
DWORD size = ;
m_pSoundData = MapFileData(path, size); HRESULT hr = XactEngine->CreateSoundBank(m_pSoundData, size, , , &SoundBank);
if (FAILED(hr))
{
return false;
} return true;
} void* XACT3Audio::MapFileData(LPCWSTR path, DWORD& size)
{
HANDLE hFile = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, , nullptr);
if (hFile == INVALID_HANDLE_VALUE)
{
return nullptr;
} size = GetFileSize(hFile, nullptr);
if (size == -)
{
CloseHandle(hFile);
return nullptr;
} HANDLE hMap = CreateFileMapping(hFile, nullptr, PAGE_READONLY, , size, nullptr);
if (!hMap)
{
CloseHandle(hFile);
return nullptr;
} void* data = MapViewOfFile(hMap, FILE_MAP_READ, , , ); CloseHandle(hMap);
CloseHandle(hFile); return data;
} void XACT3Audio::Clearup()
{
if (WaveBank)
{
WaveBank->Destroy();
WaveBank = nullptr;
} if (SoundBank)
{
SoundBank->Destroy();
SoundBank = nullptr;
} if (m_pWaveData)
{
UnmapViewOfFile(m_pWaveData);
m_pWaveData = nullptr;
} if (m_pSoundData)
{
UnmapViewOfFile(m_pSoundData);
m_pSoundData = nullptr;
} if (XactEngine)
{
XactEngine->ShutDown();
XactEngine->Release();
XactEngine = nullptr;
} CoUninitialize();
}

XactAudio.cpp

3.使用 XactAudio 类

基本都是程式化的东西,只需在 Cube.cpp 的 LoadContent() 最后添加如下代码即可:

    if (!m_audio.InitEngine())
{
return false;
} if (!m_audio.LoadWaveBank(L"Win/Wave Bank.xwb"))
{
return false;
} if (!m_audio.LoadSoundBank(L"Win/Sound Bank.xsb"))
{
return false;
} XACTINDEX cue = m_audio.SoundBank->GetCueIndex("clip");
m_audio.SoundBank->Play(cue, , , nullptr);

完整代码可到我的博客 x01.Lab.Download 中下载获取。