Skip to content

Audio Output

An IO class for audio output devices, can be synchronous or asynchronous.

Creates a new synchronous AudioOut object instance.

AudioOut(options)

Creates a new asynchronous AudioOut object instance.

AudioOut.Async(options)

options

An object of properties used to construct the class.

bitsPerSample - A number indicating the number of bits per audio sample when outputting uncompressed audio. Allowed values are 8 and 16. This property is optional and defaults to a host defined value.

channels - A number indicating the number of audio channels provided. Allowed values are 1 and 2. This property is optional and defaults to a host defined value.

sampleRate - A number indicating the sample rate of the audio. This property is optional and defaults to a host defined value.

audioType - A string indicating the encoding of the audio. The allowed value is "LPCM". This property is optional and defaults to a host defined value.

onWritable (optional) - A callback function invoked when there is space available to write more audio samples.

Includes properties of the IO Class Pattern. Specific to this class:

A number indicating the number of bits per sample. This property is read-only.

A number indicating the number of channels. This property is read-only.

A number indicating the sample rate. This property is read-only.

A string indicating the audio encoding. This property is read-only.

A number indicating the volume level to be applied to the audio output. Full volume is 1.0 and fully muted is 0.0.

Always returns "buffer".

Sends Byte Buffer data to the IO instance.

write(buffer)
write(buffer, callbackFn)

buffer

A Byte Buffer of audio data to output.

callbackFn

callbackFn(error)

For asynchronous classes, a function that executes when the data has been written.

Begins outputting audio.

start()

Suspends audio output.

stop(options)

options

An optional object with a single defined property:

flush - A boolean value. If true, any unplayed audio should be flushed immediately. If false, the unplayed audio will be output before stopping.

Invoked when space has been made available to write audio samples.

onWritable(byteLength, sampleCount)

byteLength

The maximum number of bytes that may be written.

sampleCount

The maximum number of samples that may be written.

import AudioOut from "embedded:io/audio/out";
const output = new AudioOut({
sampleRate: 44100,
channels: 2,
bitsPerSample: 16
});
output.volume = 0.5;
output.start();
const buffer = new Uint8Array(1024); // fill with audio data
output.write(buffer);

Audio Output - synchronous IO

Audio Output - asynchronous IO