-
Notifications
You must be signed in to change notification settings - Fork 519
Audio
DirectXTK | DirectXTK12 |
---|
The DirectXTK for Audio components implement a low-level audio API similar to the XNA Game Studio 4 (Microsoft.Xna.Framework.Audio
) design. This consists of the following classes all declared in the Audio.h
header (in the Inc folder of the distribution):
- AudioEngine - This class represents an XAudio2 audio graph, device, and mastering voice.
-
SoundEffect - A container class for sound resources which can be loaded from
.wav
files. These can be played as 'one-shots' managed by the engine, or used to create a SoundEffectInstance. - SoundEffectInstance - Provides a single playing, looped, paused, or stopped instance of a sound. These support 3D positional audio and optionally reverb effects.
-
SoundStreamInstance - SoundEffectInstance for playing waves from a streaming XACT-style
.xwb
wave bank. - DynamicSoundEffectInstance - SoundEffectInstance where the application provides the audio data on demand.
-
WaveBank - A container class for sound resources packaged into an XACT-style
.xwb
wave bank, with support for directly playing one-shots and creating SoundEffectInstance / SoundStreamInstance objects that refer to entries in the wave bank. -
AudioListener, AudioEmitter - Utility classes used with
SoundEffectInstance::Apply3D
.
DirectXTK for Audio uses XAudio 2. It does not make use of the legacy XACT Engine, XACT Cue, or XACT SoundBank.
Related tutorials: Adding the DirectX Tool Kit for Audio, Adding audio to your project, Creating and playing sounds, Making use of wave banks, Using positional audio
classDiagram
class AudioEngine{
+SetMasterVolume
+SetReverb
+SetMasteringLimit
+GetStatistics
+Update()
+Suspend()
+Resume()
}
class SoundEffect{
+GetFormat
+GetSampleDuration
+Play()
+CreateInstance()
}
class WaveBank{
+GetFormat
+GetSampleDuration
+IsStreamingBank
+Play()
+CreateInstance()
+CreateStreamInstance()
}
class SoundEffectInstance{
+SetVolume
+SetPitch
+SetPan
+Play()
+Stop()
+Pause()
+Resume()
+Apply3D(AudioListener,AudioEmitter)
}
class SoundStreamInstance{
+SetVolume
+SetPitch
+SetPan
+Play()
+Stop()
+Pause()
+Resume()
+Apply3D(AudioListener,AudioEmitter)
}
class DynamicSoundEffectInstance{
+SetVolume
+SetPitch
+SetPan
+Play()
+Stop()
+Pause()
+Resume()
+SubmitBuffer()
+Apply3D(AudioListener,AudioEmitter)
}
AudioEngine -- SoundEffect
AudioEngine -- WaveBank
AudioEngine -- DynamicSoundEffectInstance
SoundEffect --> SoundEffectInstance
WaveBank --> SoundEffectInstance
WaveBank --> SoundStreamInstance
classDiagram
class AudioListener{
+ListenerCone
+SetPosition()
+SetVelocity()
+SetOrientation()
+Update()
}
class X3DAUDIO_LISTENER
class AudioEmitter{
+EmitterCone
+EmitterAzimuths
+SetPosition()
+SetVelocity()
+SetOrientation()
+Update()
}
class X3DAUDIO_EMITTER
X3DAUDIO_LISTENER <|-- AudioListener
X3DAUDIO_EMITTER <|-- AudioEmitter
#include <Audio.h>
The first step in using DirectXTK for Audio is to create the AudioEngine, which creates an XAudio2 interface, an XAudio2 mastering voice, and other global resources.
// This is only needed in Windows desktop apps
hr = CoInitializeEx( nullptr, COINIT_MULTITHREADED );
if (FAILED(hr))
// error
...
std::unique_ptr<AudioEngine> audEngine;
...
AUDIO_ENGINE_FLAGS eflags = AudioEngine_Default;
#ifdef _DEBUG
eflags |= AudioEngine_Debug;
#endif
audEngine = std::make_unique<AudioEngine>( eflags );
The application should call Update
every frame to allow for per-frame engine updates, such as one-shot voice management. This could also be done in a worker thread rather than on the main rendering thread.
if ( !audEngine->Update() )
{
// No audio device is active
if ( audEngine->IsCriticalError() )
{
...
}
}
Update
returns false if no audio is actually playing (either due to there being no audio device on the system at the time AudioEngine was created, or because XAudio2 encountered a Critical Error--typically due to speakers being unplugged). Calls to various DirectXTK for Audio methods can still be made in this state but no actual audio processing will take place. See AudioEngine for more information.
Creating SoundEffectInstances allows full control over the playback, and are provided with a dedicated XAudio2 source voice. This allows control of playback, looping, volume control, panning, and pitch-shifting.
std::unique_ptr<SoundEffect> soundEffect;
soundEffect = std::make_unique<SoundEffect>( audEngine.get(), L"Sound.wav" );
auto effect = soundEffect->CreateInstance();
...
effect->Play( true );
A common way to play sounds is to trigger them in a 'fire-and-forget' mode. This is done by calling SoundEffect::Play
rather than creating a SoundEffectInstance. These use XAudio2 source voices managed by AudioEngine, are cleaned up automatically when they finish playing, and can overlap in time. One-shot sounds cannot be looped or have positional 3D effects.
soundEffect = std::make_unique<SoundEffect>( audEngine.get(), L"Explosion.wav" );
soundEffect->Play();
...
soundEffect->Play();
DirectXTK for Audio supports positional 3D audio with optional environmental reverb effects using X3DAudio.
AUDIO_ENGINE_FLAGS eflags = AudioEngine_EnvironmentalReverb
| AudioEngine_ReverbUseFilters;
#ifdef _DEBUG
eflags = eflags | AudioEngine_Debug;
#endif
audEngine = std::make_unique<AudioEngine>( eflags );
audEngine->SetReverb( Reverb_ConcertHall );
...
soundEffect = std::make_unique<SoundEffect>( audEngine.get(), L"Sound.wav" );
auto effect = soundEffect->CreateInstance( SoundEffectInstance_Use3D
| SoundEffectInstance_ReverbUseFilters );
...
effect->Play(true);
...
AudioListener listener;
listener.SetPosition( ... );
AudioEmitter emitter;
emitter.SetPosition( ... );
effect->Apply3D( listener, emitter );
Note: A C++ exception is thrown if you call Apply3D for a SoundEffectInstance that was not created with SoundEffectInstance_Use3D
Apply3D
assumes the emitter and listener are using right-handed coordinates. You can pass 'false' for the rhcoords parameter which defaults to 'true' if using left-handed coordinates.
Rather than loading individual .wav
files, a more efficient method is to package them into a "wave bank". This allows for more efficient loading and memory organization. DirectXTK for Audio's WaveBank class can be used to play one-shots or to create SoundEffectInstances from 'in-memory' wave banks.
std::unique_ptr<WaveBank> wb;
wb = std::make_unique<WaveBank>( audEngine.get(), L"wavebank.xwb" ) );
A SoundEffectInstance can be created from a wavebank referencing a particular wave in the bank:
auto effect = wb->CreateInstance( 10 );
if ( !effect )
// Error (invalid index for wave bank)
...
effect->Play( true );
One-shot sounds can also be played directly from the wave bank.
wb->Play( 2 );
wb->Play( 6 );
DirectXTK for Audio's WaveBank class can also be used to create SoundStreamInstances for streaming wave banks.
auto stream = wb->CreateStreamInstance( 10 );
if ( !stream )
// Error (invalid index for wave bank)
...
stream->Play( true );
XACT3-style "wave banks" can be created by using the XWBTool command-line tool, or they can be authored using XACT3 in the DirectX SDK. Note that the XWBTool will not perform any format conversions or compression, so more full-featured options are better handled with the XACT3 GUI or XACTBLD, or it can be used on .wav
files already compressed by adpcmencode.exe
, xwmaencode.exe
, xma2encode.exe
, etc.
xwbtool -o wavebank.xwb Sound.wav Explosion.wav Music.wav
xwbtool -s -o streamingwb.xwb Track1.wav Track2.wav Track3.wav Track4.wav
DirectXTK for Audio does not make use of the XACT engine, nor does it make use of XACT "sound banks" .xsb
or "cues". We only use .xwb
wave banks as a method for packing .wav
data.
Each instance of a SoundEffectInstance will allocate it's own source voice when played, which won't be released until it is destroyed. Each time a one-shot sound is played from a SoundEffect or a WaveBank, a voice will be created or a previously used one-shot voice will be reused if possible.
See AudioEngine for more information.
XAudio 2.9 for Windows 10 & Windows 11 | DirectXTK_Desktop_2019_Win10 DirectXTK_Desktop_2022_Win10 DirectXTK_Windows10_2022 |
XAudio 2.8 for Windows 8.1 or later (no xWMA support) | DirectXTK_Desktop_2019 DirectXTK_Desktop_2022 |
XAudio 2.9 for Xbox with XMA2 support | DirectXTK_GDK_2019 DirectXTK_GDK_2022 |
XAudio 2.7 support was retired in June 2010. Support for XAudio2Redist is still present in the CMake projects, but is not supported by the Visual Studio projects or NuGet packages as of March 2025.
See the Adding the DirectX Tool Kit for Audio.
Use of XAudio 2.7 and the legacy DirectX SDK is not recommended, and support for this configuration is removed as of the June 2020 release.
The DirectX Tool Kit for Audio is also available through the vcpkg C++ Library Manager.
For Windows 8.1 using XAudio 2.8:
vcpkg install directxtk[xaudio2-8]
For Windows 10 or Windows 11, use XAudio 2.9 support:
vcpkg install directxtk[xaudio2-9]
For XAudio 2.9 on Windows 8.1 or later support via the XAudio2Redist, use:
vcpkg install directxtk[xaudio2redist]
For the 64-bit version of the library, use:
vcpkg install directxtk[xaudio2redist]:x64-windows
Audio support is already included when building for any
*-uwp
orxbox-*
triplet.
When building with the MinGW toolset, you will need to use xaudio2redist for DirectX Tool Kit for Audio and should explicitly link to
ksguid.lib
as well.
Note: When adding .xwb
files to your Universal Windows Platform app or Xbox One XDK project, you need to manually set the file properties to "Content: Yes" for all configurations to have these files included in your AppX package. .wav
files are automatically detected as a media file and are included as content by default.
Real-time data about the audio system is provided by GetStatistics
.
auto stats = m_audEngine->GetStatistics();
wchar_t buff[256] = {};
swprintf_s(buff, L"Playing: %zu / %zu; Instances %zu; Voices %zu / %zu / %zu / %zu; %zu audio bytes",
stats.playingOneShots, stats.playingInstances,
stats.allocatedInstances, stats.allocatedVoices, stats.allocatedVoices3d,
stats.allocatedVoicesOneShot, stats.allocatedVoicesIdle,
stats.audioBytes);
The DirectXTK for Audio methods assume it is always called from a single thread. This is generally either the main thread or a worker thread dedicated to audio processing. The XAudio2 engine itself makes use of lock-free mechanism to make it 'thread-safe'.
Note that IVoiceNotify::OnBufferEnd
is called from XAudio2's thread, so the callback must be very fast and use thread-safe operations.
All content and source code for this package are subject to the terms of the MIT License.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
- Universal Windows Platform apps
- Windows desktop apps
- Windows 11
- Windows 10
- Windows 8.1
- Xbox One
- x86
- x64
- ARM64
- Visual Studio 2022
- Visual Studio 2019 (16.11)
- clang/LLVM v12 - v19
- MinGW 12.2, 13.2
- CMake 3.20