Audio Input
An IO class for audio input sources, can be synchronous or asynchronous.
Constructor
Section titled “Constructor”AudioIn
Section titled “AudioIn”Creates a new synchronous AudioIn object instance.
AudioIn(options)AudioIn.Async
Section titled “AudioIn.Async”Creates a new asynchronous AudioIn object instance.
AudioIn.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 using 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 returned. 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 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.
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 when using an uncompressed audioType. 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 of the audio. This property is read-only.
audioType
Section titled “audioType”A string indicating the audio encoding. This property is read-only.
format
Section titled “format”Always returns "buffer".
Instance Methods
Section titled “Instance Methods”Returns Byte Buffer data from the IO instance.
read(byteLength)read(buffer)
read(byteLength, callbackFn)read(buffer, callbackFn)Parameters
Section titled “Parameters”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)Parameters
Section titled “Parameters”options
An optional object with a single defined property:
flush- A boolean value. Iftrue, any unread audio should be flushed immediately. Iffalse, the unread audio may still be read after callingstop.
Callbacks
Section titled “Callbacks”onReadable
Section titled “onReadable”Invoked when audio samples are available to be read.
onReadable(byteLength, sampleCount)Parameters
Section titled “Parameters”byteLength
The number of bytes available to read.
sampleCount
The maximum number of samples that may be read.
Examples
Section titled “Examples”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();