Skip to content

Audio Input

An IO class for audio input sources, can be synchronous or asynchronous.

Creates a new synchronous AudioIn object instance.

AudioIn(options)

Creates a new asynchronous AudioIn object instance.

AudioIn.Async(options)

options

An object of properties used to construct the class.

bitsPerSample - A number indicating the number of bits per audio sample when using 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 returned. 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 captured audio. The allowed value is "LPCM". This property is optional and defaults to a host defined value.

onReadable (optional) - A callback function invoked when audio samples are available to be read.

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

A number indicating the number of bits per sample when using an uncompressed audioType. This property is read-only.

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

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

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

Always returns "buffer".

Returns Byte Buffer data from the IO instance.

read(byteLength)
read(buffer)
read(byteLength, callbackFn)
read(buffer, callbackFn)

byteLength

The number of bytes to read into the returned Byte Buffer.

buffer

A pre-allocated Byte Buffer for the instance to fill.

callbackFn

callbackFn(error, buffer)
callbackFn(error, byteLength)

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

Begins capturing audio.

start()

Suspends audio capture.

stop(options)

options

An optional object with a single defined property:

flush - A boolean value. If true, any unread audio should be flushed immediately. If false, the unread audio may still be read after calling stop.

Invoked when audio samples are available to be read.

onReadable(byteLength, sampleCount)

byteLength

The number of bytes available to read.

sampleCount

The maximum number of samples that may be read.

import AudioIn from "embedded:io/audio/in";
const input = new AudioIn({
sampleRate: 16000,
channels: 1,
bitsPerSample: 16,
onReadable(byteLength) {
const data = this.read(byteLength);
// process audio data
}
});
input.start();

Audio Input - synchronous IO

Audio Input - asynchronous IO