AudioOutput
Struct · namespace System.Audio · Since 0.38
struct AudioOutput : IDisposableThe sound output: signed 16-bit samples at a sample rate, with 1 (mono) or 2 (stereo, left and right interleaved) channels. One output at a time.
using var output = try AudioOutput.Open(44100, 1);var samples = new int16[output.Writable()];for (var i = 0; i < samples.Length; i += 1) samples[i] = (int16)(8000.0 * Math.Sin(2.0 * Math.PI * 440.0 * (double)i / 44100.0)); // A4output.Write(samples);while (output.Queued() > 0) ... // the program goes on while it playsA frame is one sample of each channel. AudioOutput.Writable says how many frames fit into the buffer now:
writing more ahead does not wait, it only takes what fits. When the buffer runs empty, the output plays silence
until the next write. AudioOutput.Queued is the number of frames written but not heard yet, so the frame being
heard is the number of frames written minus that (to show where a song is).
Static methods
Section titled “Static methods”| Name | Description |
|---|---|
Open |
Opens the output for sampleRate frames a second (8000 to 192000; 44100 and 48000 work everywhere) and channels channels (1 or 2), with a buffer of about a quarter of a second. |
static AudioError<AudioOutput> Open(int sampleRate, int channels)Opens the output for sampleRate frames a second (8000 to 192000; 44100 and 48000 work everywhere) and
channels channels (1 or 2), with a buffer of about a quarter of a second.
Errors
AudioError.NotSupported: the target has no sound output.AudioError.NoDevice: there is no sound device.AudioError.CannotOpen: the device does not take the format, or another output is open.
Panics when sampleRate or channels is out of range.
static AudioError<AudioOutput> Open(int sampleRate, int channels, int bufferMilliseconds)Opens the output with a buffer of bufferMilliseconds (20 to 2000): a longer buffer needs to be refilled less
often, a shorter one is heard sooner after a write.
Errors
AudioError.NotSupported: the target has no sound output.AudioError.NoDevice: there is no sound device.AudioError.CannotOpen: the device does not take the format, or another output is open.
Panics when sampleRate, channels or bufferMilliseconds is out of range.
Methods
Section titled “Methods”| Name | Description |
|---|---|
SampleRate |
The frames a second. |
Channels |
The channels (1 or 2). |
IsOpen |
True until AudioOutput.Close. |
Writable |
The number of frames that can be written now (0 while paused). |
Write |
Writes samples (channels per frame, interleaved): as many frames as AudioOutput.Writable allows now. |
WriteSigned8 |
Writes 8-bit signed samples (a byte from -128 to 127 each, as on the Amiga): like AudioOutput.Write, they become 16-bit samples (times 256). |
Queued |
The number of frames written but not heard yet. |
Pause |
Pauses the output: what is queued is kept and plays on with AudioOutput.Resume. |
Resume |
Goes on after AudioOutput.Pause. |
Clear |
Drops the frames that are queued: the output is silent at once (to stop a song). |
Close |
Stops the output; another one can be opened then. |
Dispose |
Stops the output. |
SampleRate
Section titled “SampleRate”int SampleRate()The frames a second.
Channels
Section titled “Channels”int Channels()The channels (1 or 2).
IsOpen
Section titled “IsOpen”bool IsOpen()True until AudioOutput.Close.
Writable
Section titled “Writable”int Writable()The number of frames that can be written now (0 while paused).
int Write(ReadOnlySlice<int16> samples)Writes samples (channels per frame, interleaved): as many frames as AudioOutput.Writable allows now. Returns
the number of frames written; the rest of the samples were not taken.
WriteSigned8
Section titled “WriteSigned8”int WriteSigned8(ReadOnlySlice<uint8> samples)Writes 8-bit signed samples (a byte from -128 to 127 each, as on the Amiga): like AudioOutput.Write, they
become 16-bit samples (times 256).
Queued
Section titled “Queued”int Queued()The number of frames written but not heard yet.
void Pause()Pauses the output: what is queued is kept and plays on with AudioOutput.Resume.
Resume
Section titled “Resume”void Resume()Goes on after AudioOutput.Pause.
void Clear()Drops the frames that are queued: the output is silent at once (to stop a song). It also ends a pause.
void Close()Stops the output; another one can be opened then. The same as AudioOutput.Dispose.
Dispose
Section titled “Dispose”void Dispose()Stops the output.