UNPKG

4.34 kBTypeScriptView Raw
1import { OutputNode, ToneAudioNode, ToneAudioNodeOptions } from "../core/context/ToneAudioNode.js";
2import { Decibels } from "../core/type/Units.js";
3import { Param } from "../core/context/Param.js";
4export interface UserMediaOptions extends ToneAudioNodeOptions {
5 volume: Decibels;
6 mute: boolean;
7}
8/**
9 * UserMedia uses MediaDevices.getUserMedia to open up and external microphone or audio input.
10 * Check [MediaDevices API Support](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia)
11 * to see which browsers are supported. Access to an external input
12 * is limited to secure (HTTPS) connections.
13 * @example
14 * const meter = new Tone.Meter();
15 * const mic = new Tone.UserMedia().connect(meter);
16 * mic.open().then(() => {
17 * // promise resolves when input is available
18 * console.log("mic open");
19 * // print the incoming mic levels in decibels
20 * setInterval(() => console.log(meter.getValue()), 100);
21 * }).catch(e => {
22 * // promise is rejected when the user doesn't have or allow mic access
23 * console.log("mic not open");
24 * });
25 * @category Source
26 */
27export declare class UserMedia extends ToneAudioNode<UserMediaOptions> {
28 readonly name: string;
29 readonly input: undefined;
30 readonly output: OutputNode;
31 /**
32 * The MediaStreamNode
33 */
34 private _mediaStream?;
35 /**
36 * The media stream created by getUserMedia.
37 */
38 private _stream?;
39 /**
40 * The open device
41 */
42 private _device?;
43 /**
44 * The output volume node
45 */
46 private _volume;
47 /**
48 * The volume of the output in decibels.
49 */
50 readonly volume: Param<"decibels">;
51 /**
52 * @param volume The level of the input in decibels
53 */
54 constructor(volume?: Decibels);
55 constructor(options?: Partial<UserMediaOptions>);
56 static getDefaults(): UserMediaOptions;
57 /**
58 * Open the media stream. If a string is passed in, it is assumed
59 * to be the label or id of the stream, if a number is passed in,
60 * it is the input number of the stream.
61 * @param labelOrId The label or id of the audio input media device.
62 * With no argument, the default stream is opened.
63 * @return The promise is resolved when the stream is open.
64 */
65 open(labelOrId?: string | number): Promise<this>;
66 /**
67 * Close the media stream
68 */
69 close(): this;
70 /**
71 * Returns a promise which resolves with the list of audio input devices available.
72 * @return The promise that is resolved with the devices
73 * @example
74 * Tone.UserMedia.enumerateDevices().then((devices) => {
75 * // print the device labels
76 * console.log(devices.map(device => device.label));
77 * });
78 */
79 static enumerateDevices(): Promise<MediaDeviceInfo[]>;
80 /**
81 * Returns the playback state of the source, "started" when the microphone is open
82 * and "stopped" when the mic is closed.
83 */
84 get state(): "started" | "stopped";
85 /**
86 * Returns an identifier for the represented device that is
87 * persisted across sessions. It is un-guessable by other applications and
88 * unique to the origin of the calling application. It is reset when the
89 * user clears cookies (for Private Browsing, a different identifier is
90 * used that is not persisted across sessions). Returns undefined when the
91 * device is not open.
92 */
93 get deviceId(): string | undefined;
94 /**
95 * Returns a group identifier. Two devices have the
96 * same group identifier if they belong to the same physical device.
97 * Returns null when the device is not open.
98 */
99 get groupId(): string | undefined;
100 /**
101 * Returns a label describing this device (for example "Built-in Microphone").
102 * Returns undefined when the device is not open or label is not available
103 * because of permissions.
104 */
105 get label(): string | undefined;
106 /**
107 * Mute the output.
108 * @example
109 * const mic = new Tone.UserMedia();
110 * mic.open().then(() => {
111 * // promise resolves when input is available
112 * });
113 * // mute the output
114 * mic.mute = true;
115 */
116 get mute(): boolean;
117 set mute(mute: boolean);
118 dispose(): this;
119 /**
120 * If getUserMedia is supported by the browser.
121 */
122 static get supported(): boolean;
123}