@euirim/microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
176 lines (174 loc) • 5.79 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { FileAudioSource, MicAudioSource, PcmRecorder } from "../../common.browser/Exports";
import { Contracts } from "../Contracts";
import { AudioInputStream, PullAudioInputStreamCallback } from "../Exports";
import { bufferSize, PullAudioInputStreamImpl } from "./AudioInputStream";
/**
* Represents audio input configuration used for specifying what type of input to use (microphone, file, stream).
* @class AudioConfig
*/
export class AudioConfig {
/**
* Creates an AudioConfig object representing the default microphone on the system.
* @member AudioConfig.fromDefaultMicrophoneInput
* @function
* @public
* @returns {AudioConfig} The audio input configuration being created.
*/
static fromDefaultMicrophoneInput() {
const pcmRecorder = new PcmRecorder();
return new AudioConfigImpl(new MicAudioSource(pcmRecorder, bufferSize));
}
/**
* Creates an AudioConfig object representing a microphone with the specified device ID.
* @member AudioConfig.fromMicrophoneInput
* @function
* @public
* @param {string | undefined} deviceId - Specifies the device ID of the microphone to be used.
* Default microphone is used the value is omitted.
* @returns {AudioConfig} The audio input configuration being created.
*/
static fromMicrophoneInput(deviceId) {
const pcmRecorder = new PcmRecorder();
return new AudioConfigImpl(new MicAudioSource(pcmRecorder, bufferSize, deviceId));
}
/**
* Creates an AudioConfig object representing the specified file.
* @member AudioConfig.fromWavFileInput
* @function
* @public
* @param {File} fileName - Specifies the audio input file. Currently, only WAV / PCM with 16-bit
* samples, 16 kHz sample rate, and a single channel (Mono) is supported.
* @returns {AudioConfig} The audio input configuration being created.
*/
static fromWavFileInput(file) {
return new AudioConfigImpl(new FileAudioSource(file));
}
/**
* Creates an AudioConfig object representing the specified stream.
* @member AudioConfig.fromStreamInput
* @function
* @public
* @param {AudioInputStream | PullAudioInputStreamCallback} audioStream - Specifies the custom audio input
* stream. Currently, only WAV / PCM with 16-bit samples, 16 kHz sample rate, and a single channel
* (Mono) is supported.
* @returns {AudioConfig} The audio input configuration being created.
*/
static fromStreamInput(audioStream) {
if (audioStream instanceof PullAudioInputStreamCallback) {
return new AudioConfigImpl(new PullAudioInputStreamImpl(audioStream));
}
if (audioStream instanceof AudioInputStream) {
return new AudioConfigImpl(audioStream);
}
throw new Error("Not Supported Type");
}
}
/**
* Represents audio input stream used for custom audio input configurations.
* @private
* @class AudioConfigImpl
*/
// tslint:disable-next-line:max-classes-per-file
export class AudioConfigImpl extends AudioConfig {
/**
* Creates and initializes an instance of this class.
* @constructor
* @param {IAudioSource} source - An audio source.
*/
constructor(source) {
super();
this.privSource = source;
}
/**
* Format information for the audio
*/
get format() {
return this.privSource.format;
}
/**
* @member AudioConfigImpl.prototype.close
* @function
* @public
*/
close() {
this.privSource.turnOff();
}
/**
* @member AudioConfigImpl.prototype.id
* @function
* @public
*/
id() {
return this.privSource.id();
}
/**
* @member AudioConfigImpl.prototype.turnOn
* @function
* @public
* @returns {Promise<boolean>} A promise.
*/
turnOn() {
return this.privSource.turnOn();
}
/**
* @member AudioConfigImpl.prototype.attach
* @function
* @public
* @param {string} audioNodeId - The audio node id.
* @returns {Promise<IAudioStreamNode>} A promise.
*/
attach(audioNodeId) {
return this.privSource.attach(audioNodeId);
}
/**
* @member AudioConfigImpl.prototype.detach
* @function
* @public
* @param {string} audioNodeId - The audio node id.
*/
detach(audioNodeId) {
return this.detach(audioNodeId);
}
/**
* @member AudioConfigImpl.prototype.turnOff
* @function
* @public
* @returns {Promise<boolean>} A promise.
*/
turnOff() {
return this.privSource.turnOff();
}
/**
* @member AudioConfigImpl.prototype.events
* @function
* @public
* @returns {EventSource<AudioSourceEvent>} An event source for audio events.
*/
get events() {
return this.privSource.events;
}
setProperty(name, value) {
Contracts.throwIfNull(value, "value");
if (undefined !== this.privSource.setProperty) {
this.privSource.setProperty(name, value);
}
else {
throw new Error("This AudioConfig instance does not support setting properties.");
}
}
getProperty(name, def) {
if (undefined !== this.privSource.getProperty) {
return this.privSource.getProperty(name, def);
}
else {
throw new Error("This AudioConfig instance does not support getting properties.");
}
return def;
}
get deviceInfo() {
return this.privSource.deviceInfo;
}
}
//# sourceMappingURL=AudioConfig.js.map