Class AudioBuffer
- Namespace
- Sigil.Sound
- Assembly
- Sigil.Sound.dll
Stereo audio held in memory: a rendered sound, or a sample decoded from a WAV file for Sigil.Sound.Sounds.Sample(Sigil.Sound.AudioBuffer,System.Nullable{Sigil.Sound.Param},Sigil.Sound.PlaybackMode,System.Double,System.Nullable{System.Double}).
public sealed class AudioBufferInheritance
Inherited Members
Constructors
AudioBuffer(StereoFrame[], int)
Wraps frames that are already rendered or decoded.
public AudioBuffer(StereoFrame[] frames, int sampleRate)Parameters
framesStereoFrame[]-
The audio, left and right per frame. The buffer keeps this array, so do not change it afterwards.
sampleRateint-
Frames per second of
frames.
Properties
Duration
The length in seconds.
public double Duration { get; }Property Value
FrameCount
The number of stereo frames.
public int FrameCount { get; }Property Value
Frames
All frames, read-only.
public ReadOnlySpan<StereoFrame> Frames { get; }Property Value
SampleRate
The sample rate of the frames, in Hz.
public int SampleRate { get; }Property Value
this[int]
The frame at index.
public StereoFrame this[int index] { get; }Property Value
Methods
FromWav(string)
Decodes a WAV file from disk into memory, ready for Sigil.Sound.Sounds.Sample(Sigil.Sound.AudioBuffer,System.Nullable{Sigil.Sound.Param},Sigil.Sound.PlaybackMode,System.Double,System.Nullable{System.Double}). Reads 8, 16, 24 and 32-bit PCM and 32-bit float, mono or stereo. The whole file is read before this returns.
public static AudioBuffer FromWav(string path)Parameters
pathstring-
Path to a .wav file, absolute or relative to the current directory.
Returns
- AudioBuffer
-
The decoded audio at the file's own sample rate.
Examples
using Sigil.Sound;
// Read a file from disk, then play it.
var kick = AudioBuffer.FromWav("samples/kick.wav");
Remarks
The browser REPL does not allow this call in snippets, because a snippet must not read files on the host. Hosts that load samples from disk can call it directly.
Exceptions
- FileNotFoundException
-
No file exists at
path. - InvalidDataException
-
The file is not a supported WAV file.
FromWav(Stream)
Decodes a WAV file from a stream into memory, ready for Sigil.Sound.Sounds.Sample(Sigil.Sound.AudioBuffer,System.Nullable{Sigil.Sound.Param},Sigil.Sound.PlaybackMode,System.Double,System.Nullable{System.Double}). Reads 8, 16, 24 and 32-bit PCM and 32-bit float, mono or stereo.
public static AudioBuffer FromWav(Stream stream)Parameters
streamStream-
A readable stream holding a complete WAV file. It is read to its end.
Returns
- AudioBuffer
-
The decoded audio at the file's own sample rate.
Examples
using Sigil.Sound;
using var stream = File.OpenRead("pad.wav");
var pad = AudioBuffer.FromWav(stream);
Exceptions
- InvalidDataException
-
The data is not a supported WAV file.
FromWav(ReadOnlySpan<byte>)
Decodes a WAV file that is already in memory into a buffer for Sigil.Sound.Sounds.Sample(Sigil.Sound.AudioBuffer,System.Nullable{Sigil.Sound.Param},Sigil.Sound.PlaybackMode,System.Double,System.Nullable{System.Double}).
public static AudioBuffer FromWav(ReadOnlySpan<byte> bytes)Parameters
bytesReadOnlySpan<byte>-
The complete contents of a .wav file, such as an embedded resource.
Returns
- AudioBuffer
-
The decoded audio at the file's own sample rate.
Examples
using Sigil.Sound;
// The bytes of a .wav file that came from a resource or the network.
byte[] bytes = File.ReadAllBytes("kick.wav");
var kick = AudioBuffer.FromWav(bytes);
Exceptions
- InvalidDataException
-
The data is not a supported WAV file.
Left()
The left channel as a new array.
public float[] Left()Returns
- float[]
Right()
The right channel as a new array.
public float[] Right()Returns
- float[]
ToInterleaved()
Left/right interleaved samples as a new array, the layout most audio APIs expect.
public float[] ToInterleaved()Returns
- float[]
ToMono()
The center-mixed signal as a new array.
public float[] ToMono()Returns
- float[]