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