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");
});Constructor
Section titled “Constructor”new UserMedia(volume?: number)Parameters
-
volume
numberoptionalThe level of the input in decibels
new UserMedia(options?: Partial<UserMediaOptions>)Parameters
- options
Partial<UserMediaOptions>optional
Properties
Section titled “Properties”read-only
input: undefinedThe input node or nodes. If the object is a source, it does not have any input and this.input is undefined.
output
Section titled “output”read-only
output: OutputNodeThe output nodes. If the object is a sink, it does not have any output and this.output is undefined.
volume
Section titled “volume”read-only
volume: Param<"decibels">The volume of the output in decibels.
deviceId
Section titled “deviceId”read-only
deviceId: string | undefinedReturns 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.
groupId
Section titled “groupId”read-only
groupId: string | undefinedReturns 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 | undefinedReturns 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: booleanMute 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.
supported
Section titled “supported”static read-only
supported: booleanIf getUserMedia is supported by the browser.
Methods
Section titled “Methods”open()
Section titled “open()”open(labelOrId?: string | number): 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
-
labelOrId
string | numberoptionalThe label or id of the audio input media device. With no argument, the default stream is opened.
Returns Promise<UserMedia> The promise is resolved when the stream is open.
enumerateDevices()
Section titled “enumerateDevices()”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
getDefaults()
Section titled “getDefaults()”static
getDefaults(): UserMediaOptionsReturns all of the default options belonging to the class.
Returns UserMediaOptions
Inherited members
Section titled “Inherited members”- From
ToneWithContext: context, blockTime, sampleTime, get(), immediate(), now(), set(), toFrequency(), toSeconds(), toTicks() - From
Tone: debug, version, disposed, toString() - From
ToneAudioNode: channelCount, channelCountMode, channelInterpretation, numberOfInputs, numberOfOutputs, chain(), connect(), disconnect(), fan(), toDestination(), toMaster()