Class WavSound
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
Duration
Playing time.
public TimeSpan Duration { get; }
Property Value
FrameCount
Frames in the sound: samples per channel.
public int FrameCount { get; }
Property Value
SampleRate
Samples per second per channel.
public int SampleRate { get; }
Property Value
Samples
The decoded samples, interleaved by channel.
public ReadOnlyMemory<short> Samples { get; }
Property Value
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
spatializedboolWhether the instance can be positioned with Apply3D(AudioEmitter). Mono sounds only.
useHrtfboolHead-related transfer function spatialisation; needs
AudioEngineSettings.HrtfSupportand works on Windows only.listenerAudioListenerThe listener the instance is heard by; the engine's default listener when omitted.
Returns
Exceptions
- InvalidOperationException
The sound was decoded with Read(Stream) rather than loaded through the audio system.
- ArgumentException
spatializedon 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
streamStream
Returns
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
pathstring
Returns
Exceptions
- InvalidDataException
The file is not a WAVE file, or holds a format this class does not decode.