Skip to content
Tone.js

UserMedia

class Source ↳ extends ToneAudioNode<UserMediaOptions>

UserMedia uses MediaDevices.getUserMedia to open up and external microphone or audio input. Check MediaDevices API Support to see which browsers are supported. Access to an external input is limited to secure (HTTPS) connections.

const meter = new Tone.Meter();
const mic = new Tone.UserMedia().connect(meter);
mic.open().then(() => {
	// promise resolves when input is available
	console.log("mic open");
	// print the incoming mic levels in decibels
	setInterval(() => console.log(meter.getValue()), 100);
}).catch(e => {
	// promise is rejected when the user doesn't have or allow mic access
	console.log("mic not open");
});
new UserMedia(volume?: number)

Parameters

  • volume number optional

    The level of the input in decibels

new UserMedia(options?: Partial<UserMediaOptions>)

Parameters

read-only

input: undefined

The input node or nodes. If the object is a source, it does not have any input and this.input is undefined.

read-only

name: string

The name of the class

Default: "UserMedia"

read-only

output: OutputNode

The output nodes. If the object is a sink, it does not have any output and this.output is undefined.

read-only

volume: Param<"decibels">

The volume of the output in decibels.

read-only

deviceId: string | undefined

Returns an identifier for the represented device that is persisted across sessions. It is un-guessable by other applications and unique to the origin of the calling application. It is reset when the user clears cookies (for Private Browsing, a different identifier is used that is not persisted across sessions). Returns undefined when the device is not open.

read-only

groupId: string | undefined

Returns a group identifier. Two devices have the same group identifier if they belong to the same physical device. Returns null when the device is not open.

read-only

label: string | undefined

Returns a label describing this device (for example “Built-in Microphone”). Returns undefined when the device is not open or label is not available because of permissions.

mute: boolean

Mute the output.

const mic = new Tone.UserMedia();
mic.open().then(() => {
	// promise resolves when input is available
});
// mute the output
mic.mute = true;

read-only

state: "started" | "stopped"

Returns the playback state of the source, “started” when the microphone is open and “stopped” when the mic is closed.

static read-only

supported: boolean

If getUserMedia is supported by the browser.

close(): this

Close the media stream

Returns this

dispose(): this

Dispose and disconnect

Returns this

open(labelOrIdOrConstraints?: string | number | MediaStreamConstraints): Promise<UserMedia>

Open the media stream. If a string is passed in, it is assumed to be the label or id of the stream, if a number is passed in, it is the input number of the stream.

Parameters

  • labelOrIdOrConstraints string | number | MediaStreamConstraints optional

    The label or id of the audio input media device, or a getUserMedia constraints object. With no argument, the default stream is opened.

Returns Promise<UserMedia> The promise is resolved when the stream is open.

static

enumerateDevices(): Promise<MediaDeviceInfo[]>

Returns a promise which resolves with the list of audio input devices available.

Tone.UserMedia.enumerateDevices().then((devices) => {
	// print the device labels
	console.log(devices.map(device => device.label));
});

Returns Promise<MediaDeviceInfo[]> The promise that is resolved with the devices

static

getDefaults(): UserMediaOptions

Returns all of the default options belonging to the class.

Returns UserMediaOptions