Table of Contents

Class WavSound

Namespace
Stride.CommunityToolkit.Audio
Assembly
Stride.CommunityToolkit.dll

A .wav file decoded to 16-bit PCM and held in memory, from which any number of overlapping SoundInstances can be created.

public sealed class WavSound
Inheritance
WavSound

Remarks

The counterpart of the asset pipeline's Sound for a code-only game: read the file at runtime, no compilation step. Supported: RIFF/WAVE with PCM 8, 16, 24 or 32-bit integer samples or 32-bit IEEE float, mono or stereo, any sample rate - the audio engine creates each source at the file's own rate, so nothing is resampled. Anything else in the file (ADPCM, MP3 in a WAV container, more than two channels) is rejected with a message that says what it found.

Everything is held in memory: three minutes of 44.1 kHz stereo is about 32 MB. That is the right trade for effects and short loops; long music would want a disk-streaming source, which this class does not provide.

Load through game.GetAudioSystem().LoadWav(...) to get a sound that can create instances; the static Read(Stream) and Read(string) decode the data only, for inspection or for tests, and a sound obtained that way cannot create instances.

Properties

Channels

1 for mono, 2 for stereo.

public int Channels { get; }

Property Value

int

Duration

Playing time.

public TimeSpan Duration { get; }

Property Value

TimeSpan

FrameCount

Frames in the sound: samples per channel.

public int FrameCount { get; }

Property Value

int

SampleRate

Samples per second per channel.

public int SampleRate { get; }

Property Value

int

Samples

The decoded samples, interleaved by channel.

public ReadOnlyMemory<short> Samples { get; }

Property Value

ReadOnlyMemory<short>

Methods

CreateInstance(bool, bool, AudioListener?)

Creates a new instance of this sound. Each instance plays independently, so several can overlap - the usual case for a sound effect.

public SoundInstance CreateInstance(bool spatialized = false, bool useHrtf = false, AudioListener? listener = null)

Parameters

spatialized bool

Whether the instance can be positioned with Apply3D(AudioEmitter). Mono sounds only.

useHrtf bool

Head-related transfer function spatialisation; needs AudioEngineSettings.HrtfSupport and works on Windows only.

listener AudioListener

The listener the instance is heard by; the engine's default listener when omitted.

Returns

SoundInstance

A stopped instance. Set IsLooping before Play() for a loop.

Exceptions

InvalidOperationException

The sound was decoded with Read(Stream) rather than loaded through the audio system.

ArgumentException

spatialized on a stereo sound.

Read(Stream)

Decodes a .wav from a stream. Data only: the result cannot create instances - use game.GetAudioSystem().LoadWav for that.

public static WavSound Read(Stream stream)

Parameters

stream Stream

Returns

WavSound

Exceptions

InvalidDataException

The stream is not a WAVE file, or holds a format this class does not decode.

Read(string)

Decodes a .wav file. Data only: the result cannot create instances - use game.GetAudioSystem().LoadWav for that.

public static WavSound Read(string path)

Parameters

path string

Returns

WavSound

Exceptions

InvalidDataException

The file is not a WAVE file, or holds a format this class does not decode.