Skip to content

AudioListener

Chuck Walbourn edited this page Aug 13, 2024 · 24 revisions
DirectXTK Audio

AudioListener is a helper object derived from X3DAUDIO_LISTENER that can be used by the Apply3D method of SoundEffectInstance, SoundStreamInstance, and DynamicSoundEffectInstance.

See AudioEmitter.

Related tutorial: Using positional audio

classDiagram
direction LR
class AudioListener{
   +ListenerCone
   +SetPosition()
   +SetVelocity()
   +SetOrientation()
   +Update()
   +IsValid()
}
class X3DAUDIO_LISTENER
X3DAUDIO_LISTENER <|-- AudioListener
Loading

Header

#include <Audio.h>

Initialization

The default constructor creates an omnidirectional listener located at 0,0,0 facing -z and an up vector of +y.

When using left-handed coordinates, be sure to set OrientFront

Helpers

In addition to setting the members of X3DAUDIO_LISTENER directly, these helper functions are provided:

  • SetPosition (XMVECTOR) or (XMFLOAT3): Sets the Position of the listener.

  • SetVelocity (XMVECTOR) or (XMFLOAT3): Sets the Velocity of the listener.

  • SetOrientation (XMVECTOR forward, XMVECTOR up) or (XMFLOAT3 forward, XMFLOAT3 up)

  • SetOrientationFromQuaternion (XMVECTOR): Sets the OrientFront/OrientTop of the listener.

  • Update (XMVECTOR newPos, XMVECTOR upDir, float dt): Computes a direction and velocity for the listener based on the current Position value, the new position, and the provided delta time (Δt). This updates the OrientFront/OrientTop to match, and then sets the Position to the new position. If dt is 0, the update is skipped.

  • IsValid: Validates the member values to ensure they are in a proper range and that all floating-point values are in the 'real' range (i.e. not NAN or INF which are unsupported by X3DAudio). This is useful for debugging and asserts in 'checked' builds.

Directional Listener

AudioListener defaults to an omnidirectional listener. To create a sound-cone, set the pCone member to point to a X3DAUDIO_CONE structure. The pointer must point to valid memory at the time Apply3D is called.

  • SetOmnidirectional sets the pCone value to nullptr indicating an omnidirectional listener (the default).

  • SetCone (X3DAUDIO_CONE) sets the pCone value to point to ListenerCone and copies the passed in cone parameters.

Example

float dt = static_cast<float>(timer.GetElapsedSeconds());

emitter.Update(soundPosition, Vector3::Up, dt);
listener.Update(playerPosiiton, Vector3::Up, dt);
if (soundSource)
{
    soundSource->Apply3D(listener, emitter);
}

Further reading

X3DAudio
X3DAudioCalculate

GameFest 2010: The (3D) Sound of Success: X3DAudio and Sound Positioning

For Use

  • Universal Windows Platform apps
  • Windows desktop apps
  • Windows 11
  • Windows 10
  • Windows 8.1
  • Xbox One

Architecture

  • x86
  • x64
  • ARM64

For Development

  • Visual Studio 2022
  • Visual Studio 2019 (16.11)
  • clang/LLVM v12 - v18
  • MinGW 12.2, 13.2
  • CMake 3.20

Related Projects

DirectX Tool Kit for DirectX 12

DirectXMesh

DirectXTex

DirectXMath

Win2D

Tools

Test Suite

Model Viewer

Content Exporter

DxCapsViewer

Clone this wiki locally