Audio Output
An IO class for audio output devices, can be synchronous or asynchronous.
Constructor
Section titled “Constructor”AudioOut
Section titled “AudioOut”Creates a new synchronous AudioOut object instance.
AudioOut(options)AudioOut.Async
Section titled “AudioOut.Async”Creates a new asynchronous AudioOut object instance.
AudioOut.Async(options)Parameters
Section titled “Parameters”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 are8and16. This property is optional and defaults to a host defined value.
channels- A number indicating the number of audio channels provided. Allowed values are1and2. 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.
Instance Properties
Section titled “Instance Properties”Includes properties of the IO Class Pattern. Specific to this class:
bitsPerSample
Section titled “bitsPerSample”A number indicating the number of bits per sample. This property is read-only.
channels
Section titled “channels”A number indicating the number of channels. This property is read-only.
sampleRate
Section titled “sampleRate”A number indicating the sample rate. This property is read-only.
audioType
Section titled “audioType”A string indicating the audio encoding. This property is read-only.
volume
Section titled “volume”A number indicating the volume level to be applied to the audio output. Full volume is 1.0 and fully muted is 0.0.
format
Section titled “format”Always returns "buffer".
Instance Methods
Section titled “Instance Methods”Sends Byte Buffer data to the IO instance.
write(buffer)write(buffer, callbackFn)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”options
An optional object with a single defined property:
flush- A boolean value. Iftrue, any unplayed audio should be flushed immediately. Iffalse, the unplayed audio will be output before stopping.
Callbacks
Section titled “Callbacks”onWritable
Section titled “onWritable”Invoked when space has been made available to write audio samples.
onWritable(byteLength, sampleCount)Parameters
Section titled “Parameters”byteLength
The maximum number of bytes that may be written.
sampleCount
The maximum number of samples that may be written.
Examples
Section titled “Examples”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 dataoutput.write(buffer);