Skip to content
CShift0.45

AudioOutput

Struct · namespace System.Audio · Since 0.38

struct AudioOutput : IDisposable

The 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)); // A4
output.Write(samples);
while (output.Queued() > 0) ... // the program goes on while it plays

A 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).

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

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

Panics when sampleRate, channels or bufferMilliseconds is out of range.

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.
int SampleRate()

The frames a second.

int Channels()

The channels (1 or 2).

bool IsOpen()

True until AudioOutput.Close.

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.

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).

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.

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.

void Dispose()

Stops the output.