Reference p5.AudioIn

p5.AudioIn

Get audio from an input, i.e. your computer's microphone.

Turn the mic on/off with the start() and stop() methods. When the mic is on, its volume can be measured with getLevel or by connecting an FFT object.

If you want to hear the AudioIn, use the .connect() method. AudioIn does not connect to p5.sound output by default to prevent feedback.

Note: This uses the getUserMedia/ Stream API, which is not supported by certain browsers. Access in Chrome browser is limited to localhost and https, but access over http may be limited.

Examples

Syntax

p5.AudioIn([errorCallback])

Parameters

errorCallback
Function:

A function to call if there is an error accessing the AudioIn. For example, Safari and iOS devices do not currently allow microphone access.

Fields

enabled

Client must allow browser to access their microphone / audioin source. Default: false. Will become true when the client enables access.

amplitude

Input amplitude, connect to it by default but not to master out

Methods

start

Start processing audio input. This enables the use of other AudioIn methods like getLevel(). Note that by default, AudioIn is not connected to p5.sound's output. So you won't hear anything unless you use the connect() method.

Certain browsers limit access to the user's microphone. For example, Chrome only allows access from localhost and over https. For this reason, you may want to include an errorCallback—a function that is called in case the browser won't provide mic access.

stop

Turn the AudioIn off. If the AudioIn is stopped, it cannot getLevel(). If re-starting, the user may be prompted for permission access.

connect

Connect to an audio unit. If no parameter is provided, will connect to the main output (i.e. your speakers).

disconnect

Disconnect the AudioIn from all audio units. For example, if connect() had been called, disconnect() will stop sending signal to your speakers.

getLevel

Read the Amplitude (volume level) of an AudioIn. The AudioIn class contains its own instance of the Amplitude class to help make it easy to get a microphone's volume level. Accepts an optional smoothing value (0.0 < 1.0). NOTE: AudioIn must .start() before using .getLevel().

amp

Set amplitude (volume) of a mic input between 0 and 1.0.

getSources

Returns a list of available input sources. This is a wrapper for MediaDevices.enumerateDevices() - Web APIs | MDN and it returns a Promise.

setSource

Set the input source. Accepts a number representing a position in the array returned by getSources(). This is only available in browsers that support navigator.mediaDevices.enumerateDevices()

Notice any errors or typos? Please let us know. Please feel free to edit lib/addons/p5.sound.js and open a pull request!

Related References