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AAE,iBAAiB,EAE1C;IAEM,KAAK,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI;IAIhC,KAAK,IAAI,IAAI;IAIb,EAAE,IAAI,MAAM;IAIZ,WAAW,IAAI,IAAI;IAInB,WAAW,IAAI,MAAM;CAG/B","file":"AudioConfig.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\n/* eslint-disable max-classes-per-file */\r\n\r\nimport { PathLike } from \"fs\";\r\nimport {\r\n FileAudioSource,\r\n MicAudioSource,\r\n PcmRecorder,\r\n} from \"../../common.browser/Exports.js\";\r\nimport { ISpeechConfigAudioDevice } from \"../../common.speech/Exports.js\";\r\nimport {\r\n AudioSourceEvent,\r\n EventSource,\r\n IAudioDestination,\r\n IAudioSource,\r\n IAudioStreamNode\r\n} from \"../../common/Exports.js\";\r\nimport { Contracts } from \"../Contracts.js\";\r\nimport {\r\n AudioInputStream,\r\n AudioOutputStream,\r\n AudioStreamFormat,\r\n IPlayer,\r\n PullAudioInputStreamCallback,\r\n PullAudioOutputStream,\r\n PushAudioOutputStream,\r\n PushAudioOutputStreamCallback,\r\n SpeakerAudioDestination\r\n} from \"../Exports.js\";\r\nimport { AudioFileWriter } from \"./AudioFileWriter.js\";\r\nimport { PullAudioInputStreamImpl, PushAudioInputStreamImpl } from \"./AudioInputStream.js\";\r\nimport { PullAudioOutputStreamImpl, PushAudioOutputStreamImpl } from \"./AudioOutputStream.js\";\r\nimport { AudioStreamFormatImpl } from \"./AudioStreamFormat.js\";\r\n\r\n/**\r\n * Represents audio input configuration used for specifying what type of input to use (microphone, file, stream).\r\n * @class AudioConfig\r\n * Updated in version 1.11.0\r\n */\r\nexport abstract class AudioConfig {\r\n /**\r\n * Creates an AudioConfig object representing the default microphone on the system.\r\n * @member AudioConfig.fromDefaultMicrophoneInput\r\n * @function\r\n * @public\r\n * @returns {AudioConfig} The audio input configuration being created.\r\n */\r\n public static fromDefaultMicrophoneInput(): AudioConfig {\r\n const pcmRecorder = new PcmRecorder(true);\r\n return new AudioConfigImpl(new MicAudioSource(pcmRecorder));\r\n }\r\n\r\n /**\r\n * Creates an AudioConfig object representing a microphone with the specified device ID.\r\n * @member AudioConfig.fromMicrophoneInput\r\n * @function\r\n * @public\r\n * @param {string | undefined} deviceId - Specifies the device ID of the microphone to be used.\r\n * Default microphone is used the value is omitted.\r\n * @returns {AudioConfig} The audio input configuration being created.\r\n */\r\n public static fromMicrophoneInput(deviceId?: string): AudioConfig {\r\n const pcmRecorder = new PcmRecorder(true);\r\n return new AudioConfigImpl(new MicAudioSource(pcmRecorder, deviceId));\r\n }\r\n\r\n /**\r\n * Creates an AudioConfig object representing the specified file.\r\n * @member AudioConfig.fromWavFileInput\r\n * @function\r\n * @public\r\n * @param {File} fileName - Specifies the audio input file. Currently, only WAV / PCM is supported.\r\n * @returns {AudioConfig} The audio input configuration being created.\r\n */\r\n public static fromWavFileInput(file: File | Buffer, name: string = \"unnamedBuffer.wav\"): AudioConfig {\r\n return new AudioConfigImpl(new FileAudioSource(file, name));\r\n }\r\n\r\n /**\r\n * Creates an AudioConfig object representing the specified stream.\r\n * @member AudioConfig.fromStreamInput\r\n * @function\r\n * @public\r\n * @param {AudioInputStream | PullAudioInputStreamCallback | MediaStream} audioStream - Specifies the custom audio input\r\n * stream. Currently, only WAV / PCM is supported.\r\n * @returns {AudioConfig} The audio input configuration being created.\r\n */\r\n public static fromStreamInput(audioStream: AudioInputStream | PullAudioInputStreamCallback\r\n | MediaStream): AudioConfig {\r\n if (audioStream instanceof PullAudioInputStreamCallback) {\r\n return new AudioConfigImpl(new PullAudioInputStreamImpl(audioStream));\r\n }\r\n\r\n if (audioStream instanceof AudioInputStream) {\r\n return new AudioConfigImpl(audioStream as PushAudioInputStreamImpl);\r\n }\r\n if (typeof MediaStream !== \"undefined\" && audioStream instanceof MediaStream) {\r\n const pcmRecorder = new PcmRecorder(false);\r\n return new AudioConfigImpl(new MicAudioSource(pcmRecorder, null, null, audioStream));\r\n }\r\n\r\n throw new Error(\"Not Supported Type\");\r\n }\r\n\r\n /**\r\n * Creates an AudioConfig object representing the default speaker.\r\n * @member AudioConfig.fromDefaultSpeakerOutput\r\n * @function\r\n * @public\r\n * @returns {AudioConfig} The audio output configuration being created.\r\n * Added in version 1.11.0\r\n */\r\n public static fromDefaultSpeakerOutput(): AudioConfig {\r\n return new AudioOutputConfigImpl(new SpeakerAudioDestination());\r\n }\r\n\r\n /**\r\n * Creates an AudioConfig object representing the custom IPlayer object.\r\n * You can use the IPlayer object to control pause, resume, etc.\r\n * @member AudioConfig.fromSpeakerOutput\r\n * @function\r\n * @public\r\n * @param {IPlayer} player - the IPlayer object for playback.\r\n * @returns {AudioConfig} The audio output configuration being created.\r\n * Added in version 1.12.0\r\n */\r\n public static fromSpeakerOutput(player?: IPlayer): AudioConfig {\r\n if (player === undefined) {\r\n return AudioConfig.fromDefaultSpeakerOutput();\r\n }\r\n if (player instanceof SpeakerAudioDestination) {\r\n return new AudioOutputConfigImpl(player);\r\n }\r\n\r\n throw new Error(\"Not Supported Type\");\r\n }\r\n\r\n /**\r\n * Creates an AudioConfig object representing a specified output audio file\r\n * @member AudioConfig.fromAudioFileOutput\r\n * @function\r\n * @public\r\n * @param {PathLike} filename - the filename of the output audio file\r\n * @returns {AudioConfig} The audio output configuration being created.\r\n * Added in version 1.11.0\r\n */\r\n public static fromAudioFileOutput(filename: PathLike): AudioConfig {\r\n return new AudioOutputConfigImpl(new AudioFileWriter(filename));\r\n }\r\n\r\n /**\r\n * Creates an AudioConfig object representing a specified audio output stream\r\n * @member AudioConfig.fromStreamOutput\r\n * @function\r\n * @public\r\n * @param {AudioOutputStream | PushAudioOutputStreamCallback} audioStream - Specifies the custom audio output\r\n * stream.\r\n * @returns {AudioConfig} The audio output configuration being created.\r\n * Added in version 1.11.0\r\n */\r\n public static fromStreamOutput(audioStream: AudioOutputStream | PushAudioOutputStreamCallback): AudioConfig {\r\n if (audioStream instanceof PushAudioOutputStreamCallback) {\r\n return new AudioOutputConfigImpl(new PushAudioOutputStreamImpl(audioStream));\r\n }\r\n\r\n if (audioStream instanceof PushAudioOutputStream) {\r\n return new AudioOutputConfigImpl(audioStream as PushAudioOutputStreamImpl);\r\n }\r\n\r\n if (audioStream instanceof PullAudioOutputStream) {\r\n return new AudioOutputConfigImpl(audioStream as PullAudioOutputStreamImpl);\r\n }\r\n\r\n throw new Error(\"Not Supported Type\");\r\n }\r\n\r\n /**\r\n * Explicitly frees any external resource attached to the object\r\n * @member AudioConfig.prototype.close\r\n * @function\r\n * @public\r\n */\r\n public abstract close(): void;\r\n\r\n /**\r\n * Sets an arbitrary property.\r\n * @member SpeechConfig.prototype.setProperty\r\n * @function\r\n * @public\r\n * @param {string} name - The name of the property to set.\r\n * @param {string} value - The new value of the property.\r\n */\r\n public abstract setProperty(name: string, value: string): void;\r\n\r\n /**\r\n * Returns the current value of an arbitrary property.\r\n * @member SpeechConfig.prototype.getProperty\r\n * @function\r\n * @public\r\n * @param {string} name - The name of the property to query.\r\n * @param {string} def - The value to return in case the property is not known.\r\n * @returns {string} The current value, or provided default, of the given property.\r\n */\r\n public abstract getProperty(name: string, def?: string): string;\r\n\r\n}\r\n\r\n/**\r\n * Represents audio input stream used for custom audio input configurations.\r\n * @private\r\n * @class AudioConfigImpl\r\n */\r\nexport class AudioConfigImpl extends AudioConfig implements IAudioSource {\r\n private privSource: IAudioSource;\r\n\r\n /**\r\n * Creates and initializes an instance of this class.\r\n * @constructor\r\n * @param {IAudioSource} source - An audio source.\r\n */\r\n public constructor(source: IAudioSource) {\r\n super();\r\n this.privSource = source;\r\n }\r\n\r\n /**\r\n * Format information for the audio\r\n */\r\n public get format(): Promise<AudioStreamFormatImpl> {\r\n return this.privSource.format;\r\n }\r\n\r\n /**\r\n * @member AudioConfigImpl.prototype.close\r\n * @function\r\n * @public\r\n */\r\n public close(cb?: () => void, err?: (error: string) => void): void {\r\n this.privSource.turnOff().then((): void => {\r\n if (!!cb) {\r\n cb();\r\n }\r\n }, (error: string): void => {\r\n if (!!err) {\r\n err(error);\r\n }\r\n });\r\n }\r\n\r\n /**\r\n * @member AudioConfigImpl.prototype.id\r\n * @function\r\n * @public\r\n */\r\n public id(): string {\r\n return this.privSource.id();\r\n }\r\n\r\n /**\r\n * @member AudioConfigImpl.prototype.turnOn\r\n * @function\r\n * @public\r\n * @returns {Promise<void>} A promise.\r\n */\r\n public turnOn(): Promise<void> {\r\n return this.privSource.turnOn();\r\n }\r\n\r\n /**\r\n * @member AudioConfigImpl.prototype.attach\r\n * @function\r\n * @public\r\n * @param {string} audioNodeId - The audio node id.\r\n * @returns {Promise<IAudioStreamNode>} A promise.\r\n */\r\n public attach(audioNodeId: string): Promise<IAudioStreamNode> {\r\n return this.privSource.attach(audioNodeId);\r\n }\r\n\r\n /**\r\n * @member AudioConfigImpl.prototype.detach\r\n * @function\r\n * @public\r\n * @param {string} audioNodeId - The audio node id.\r\n */\r\n public detach(audioNodeId: string): void {\r\n return this.privSource.detach(audioNodeId);\r\n }\r\n\r\n /**\r\n * @member AudioConfigImpl.prototype.turnOff\r\n * @function\r\n * @public\r\n * @returns {Promise<void>} A promise.\r\n */\r\n public turnOff(): Promise<void> {\r\n return this.privSource.turnOff();\r\n }\r\n\r\n /**\r\n * @member AudioConfigImpl.prototype.events\r\n * @function\r\n * @public\r\n * @returns {EventSource<AudioSourceEvent>} An event source for audio events.\r\n */\r\n public get events(): EventSource<AudioSourceEvent> {\r\n return this.privSource.events;\r\n }\r\n\r\n public setProperty(name: string, value: string): void {\r\n Contracts.throwIfNull(value, \"value\");\r\n\r\n if (undefined !== this.privSource.setProperty) {\r\n this.privSource.setProperty(name, value);\r\n } else {\r\n throw new Error(\"This AudioConfig instance does not support setting properties.\");\r\n }\r\n\r\n }\r\n\r\n public getProperty(name: string, def?: string): string {\r\n if (undefined !== this.privSource.getProperty) {\r\n return this.privSource.getProperty(name, def);\r\n } else {\r\n throw new Error(\"This AudioConfig instance does not support getting properties.\");\r\n }\r\n\r\n return def;\r\n }\r\n\r\n public get deviceInfo(): Promise<ISpeechConfigAudioDevice> {\r\n return this.privSource.deviceInfo;\r\n }\r\n}\r\n\r\nexport class AudioOutputConfigImpl extends AudioConfig implements IAudioDestination {\r\n private privDestination: IAudioDestination;\r\n\r\n /**\r\n * Creates and initializes an instance of this class.\r\n * @constructor\r\n * @param {IAudioDestination} destination - An audio destination.\r\n */\r\n public constructor(destination: IAudioDestination) {\r\n super();\r\n this.privDestination = destination;\r\n }\r\n\r\n public set format(format: AudioStreamFormat) {\r\n this.privDestination.format = format;\r\n }\r\n\r\n public write(buffer: ArrayBuffer): void {\r\n this.privDestination.write(buffer);\r\n }\r\n\r\n public close(): void {\r\n this.privDestination.close();\r\n }\r\n\r\n public id(): string {\r\n return this.privDestination.id();\r\n }\r\n\r\n public setProperty(): void {\r\n throw new Error(\"This AudioConfig instance does not support setting properties.\");\r\n }\r\n\r\n public getProperty(): string {\r\n throw new Error(\"This AudioConfig instance does not support getting properties.\");\r\n }\r\n}\r\n"]}