Module: audio

Module to access audio hardware on the host. This module defines three key classes, Player, ClipPlayer, and Capture. The Player class accepts audio data in a variety of formats and plays them through the default audio output device on the host platform. The ClipPlayer accepts a URL specifying an audio source file and plays that. The Capture class records audio from the default audio input device, such as a microphone, on the host platform.

Author:
  • Edward A. Lee and Beth Osyk
Source:

Methods

(static) byteFormats()

Return an array of byte formats currently available for audio capture on the current host. This includes at least 'raw', which is whatever byte format the host audio hardware happens to provide when capturing audio from the microphone.

Source:
Returns:

An array of capture formats.

(static) Capture(captureTime, outputFormat)

Construct an instance of a Capture object type. This should be instantiated in your JavaScript code as

    var audio = require("audio");
    var capture = new audio.Capture();
 
An instance of this object type implements the following functions:
  • capture.start(): Start capturing.
  • stop(): Stop capturing.
This is an event emitter that will emit an event "capture" when audio data has been captured. The argument passed to a listener for that event will be audio data in a format given by the outputFormat argument, which is one of the following strings:

  • "raw": The argument is a byte array representing audio data exactly as captured in the specified capture format.
  • "array": The audio data is converted into arrays of numbers (one per channel), where each number is in the range from -1.0 to 1.0. The argument passed to the "capture" event is an array of arrays, where the first index specifies the channel number.
  • "aiff": The audio data is converted into the AIFF file format historically associated with Apple computers.
  • "aifc": The audio data is converted into the AIFF-C, a compressed version of AIFF.
  • "au": The audio data is converted into the AU file format historically associated with Sun Microsystems and Unix computers.
  • "wav": The audio data is converted into the WAVE file format historically associated with Windows PCs.

    The optional captureOptions argument is an object with the following properties, all of which are optional:

  • bitsPerSample: The number of bits per sample. This is an integer that defaults to 16.

  • channels: The number of channels. This defaults to 1.
  • sampleRate: The sample rate. This is an integer that defaults to 8000.
Parameters:
Name Type Description
captureTime

The length of time for each audio capture (in milliseconds). This is an integer that defaults to 1000, capturing 1 second of audio at a time.

outputFormat

The format of the data passed to the "capture" event listeners as specified above.

Source:

(static) ClipPlayer()

Create a ClipPlayer to play the specified URL.

Source:

(static) Player(playbackFormat, playbackOptions)

Construct an instance of an Player object type. This should be instantiated in your JavaScript code as

    var audio = require("audio");
    var player = new audio.Player();
 
An instance of this object type implements the following functions:
  • play(data, callback): Play the specified array, and call the specified callback function when the data has been accepted.
  • stop(): Stop playback and free the audio resources.

The callback function should be used to construct the next batch of audio data to be played. The callback function will be called as soon as the specified data has been queued to the audio system. You should not call the play() function again until the callback has been called or you might overwrite the audio data.

The playbackFormat argument specifies the form in which the audio data will be provided. The available formats include:

  • "raw": The data is a byte array representing audio data exactly as captured by default on the host.
  • "array": The audio input data is an array of arrays of numbers, where each number is in the range from -1.0 to 1.0. The first index of the input specifies the channel number.
  • "encoded": The audio input data is a byte array containing audio data encoded in one of the file format standards such as AIFF (historically associated with Apple computers), AIFF-C (a compressed version of AIFF), AU (historically associated with Sun Microsystems and Unix computers), or WAVE (historically associated with Windows PCs).

    The optional playbackOptions argument is an object with the following properties, all of which are optional:

  • bitsPerSample: The number of bits per sample. This is an integer that defaults to 16.

  • channels: The number of channels. This defaults to 1.
  • sampleRate: The sample rate. This is an integer that defaults to 8000. Typical supported sample rates are 8000, 11025, 22050, 44100, and 48000.

    The audio data will be converted to this specified format before being sent to the audio hardware.

Parameters:
Name Type Description
playbackFormat

The format of the input data.

playbackOptions

The playback options.

Source: