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AO,GAAG,IAAI;IAe3C;;;;;;OAMG;oBACe,gBAAgB,EAAI,OAAO;IAI7C;;;;;;;;;OASG;WAEW,eAAe,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAUrD,SAAS,CAAC,QAAQ,CAAC,sBAAsB,CAAC,YAAY,EAAE,mBAAmB,GAAG,gBAAgB;IAG9F,SAAS,CAAC,QAAQ,CAAC,uBAAuB,CACtC,cAAc,EAAE,eAAe,EAC/B,iBAAiB,EAAE,kBAAkB,EACrC,WAAW,EAAE,WAAW,EACxB,gBAAgB,EAAE,gBAAgB,GAAG,qBAAqB;IAG9D,SAAS,CAAC,yBAAyB,IAAI,IAAI;IAqC3C,SAAS,CAAC,mBAAmB,CACzB,eAAe,EAAE,eAAe,EAChC,eAAe,EAAE,CAAC,CAAC,EAAE,uBAAuB,KAAK,IAAI,EACrD,aAAa,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI;IAY7C,SAAS,CAAC,kBAAkB,IAAI,IAAI;CAKvC","file":"Recognizer.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license.\n\nimport {\n CognitiveSubscriptionKeyAuthentication,\n CognitiveTokenAuthentication,\n Context,\n IAuthentication,\n IConnectionFactory,\n OS,\n RecognitionMode,\n RecognizerConfig,\n ServiceRecognizerBase,\n SpeechServiceConfig,\n} from \"../common.speech/Exports\";\nimport { Promise, PromiseHelper } from \"../common/Exports\";\nimport { Contracts } from \"./Contracts\";\nimport {\n AudioConfig,\n Connection,\n PropertyCollection,\n PropertyId,\n RecognitionEventArgs,\n SessionEventArgs,\n SpeechRecognitionResult,\n} from \"./Exports\";\n\n/**\n * Defines the base class Recognizer which mainly contains common event handlers.\n * @class Recognizer\n */\nexport abstract class Recognizer {\n private privDisposed: boolean;\n protected audioConfig: AudioConfig;\n protected privReco: ServiceRecognizerBase;\n protected privProperties: PropertyCollection;\n private privConnectionFactory: IConnectionFactory;\n\n /**\n * Creates and initializes an instance of a Recognizer\n * @constructor\n * @param {AudioConfig} audioInput - An optional audio input stream associated with the recognizer\n */\n protected constructor(audioConfig: AudioConfig, properties: PropertyCollection, connectionFactory: IConnectionFactory) {\n this.audioConfig = (audioConfig !== undefined) ? audioConfig : AudioConfig.fromDefaultMicrophoneInput();\n this.privDisposed = false;\n this.privProperties = properties.clone();\n this.privConnectionFactory = connectionFactory;\n this.implCommonRecognizerSetup();\n }\n\n /**\n * Defines event handler for session started events.\n * @member Recognizer.prototype.sessionStarted\n * @function\n * @public\n */\n public sessionStarted: (sender: Recognizer, event: SessionEventArgs) => void;\n\n /**\n * Defines event handler for session stopped events.\n * @member Recognizer.prototype.sessionStopped\n * @function\n * @public\n */\n public sessionStopped: (sender: Recognizer, event: SessionEventArgs) => void;\n\n /**\n * Defines event handler for speech started events.\n * @member Recognizer.prototype.speechStartDetected\n * @function\n * @public\n */\n public speechStartDetected: (sender: Recognizer, event: RecognitionEventArgs) => void;\n\n /**\n * Defines event handler for speech stopped events.\n * @member Recognizer.prototype.speechEndDetected\n * @function\n * @public\n */\n public speechEndDetected: (sender: Recognizer, event: RecognitionEventArgs) => void;\n\n /**\n * Dispose of associated resources.\n * @member Recognizer.prototype.close\n * @function\n * @public\n */\n public close(): void {\n Contracts.throwIfDisposed(this.privDisposed);\n\n this.dispose(true);\n }\n\n /**\n * @Internal\n * Internal data member to support fromRecognizer* pattern methods on other classes.\n * Do not use externally, object returned will change without warning or notice.\n */\n public get internalData(): object {\n return this.privReco;\n }\n\n /**\n * This method performs cleanup of resources.\n * The Boolean parameter disposing indicates whether the method is called\n * from Dispose (if disposing is true) or from the finalizer (if disposing is false).\n * Derived classes should override this method to dispose resource if needed.\n * @member Recognizer.prototype.dispose\n * @function\n * @public\n * @param {boolean} disposing - Flag to request disposal.\n */\n protected dispose(disposing: boolean): void {\n if (this.privDisposed) {\n return;\n }\n\n if (disposing) {\n if (this.privReco) {\n this.privReco.audioSource.turnOff();\n this.privReco.dispose();\n }\n }\n\n this.privDisposed = true;\n }\n\n /**\n * This method returns the current state of the telemetry setting.\n * @member Recognizer.prototype.telemetryEnabled\n * @function\n * @public\n * @returns true if the telemetry is enabled, false otherwise.\n */\n public static get telemetryEnabled(): boolean {\n return ServiceRecognizerBase.telemetryDataEnabled;\n }\n\n /**\n * This method globally enables or disables telemetry.\n * @member Recognizer.prototype.enableTelemetry\n * @function\n * @public\n * @param enabled - Global setting for telemetry collection.\n * If set to true, telemetry information like microphone errors,\n * recognition errors are collected and sent to Microsoft.\n * If set to false, no telemetry is sent to Microsoft.\n */\n /* tslint:disable:member-ordering */\n public static enableTelemetry(enabled: boolean): void {\n ServiceRecognizerBase.telemetryDataEnabled = enabled;\n }\n\n //\n // ################################################################################################################\n // IMPLEMENTATION.\n // Move to independent class\n // ################################################################################################################\n //\n protected abstract createRecognizerConfig(speecgConfig: SpeechServiceConfig): RecognizerConfig;\n\n // Creates the correct service recognizer for the type\n protected abstract createServiceRecognizer(\n authentication: IAuthentication,\n connectionFactory: IConnectionFactory,\n audioConfig: AudioConfig,\n recognizerConfig: RecognizerConfig): ServiceRecognizerBase;\n\n // Does the generic recognizer setup that is common across all recognizer types.\n protected implCommonRecognizerSetup(): void {\n\n let osPlatform = (typeof window !== \"undefined\") ? \"Browser\" : \"Node\";\n let osName = \"unknown\";\n let osVersion = \"unknown\";\n\n if (typeof navigator !== \"undefined\") {\n osPlatform = osPlatform + \"/\" + navigator.platform;\n osName = navigator.userAgent;\n osVersion = navigator.appVersion;\n }\n\n const recognizerConfig = this.createRecognizerConfig(\n new SpeechServiceConfig(\n new Context(new OS(osPlatform, osName, osVersion))));\n\n const subscriptionKey = this.privProperties.getProperty(PropertyId.SpeechServiceConnection_Key, undefined);\n const authentication = (subscriptionKey && subscriptionKey !== \"\") ?\n new CognitiveSubscriptionKeyAuthentication(subscriptionKey) :\n new CognitiveTokenAuthentication(\n (authFetchEventId: string): Promise<string> => {\n const authorizationToken = this.privProperties.getProperty(PropertyId.SpeechServiceAuthorization_Token, undefined);\n return PromiseHelper.fromResult(authorizationToken);\n },\n (authFetchEventId: string): Promise<string> => {\n const authorizationToken = this.privProperties.getProperty(PropertyId.SpeechServiceAuthorization_Token, undefined);\n return PromiseHelper.fromResult(authorizationToken);\n });\n\n this.privReco = this.createServiceRecognizer(\n authentication,\n this.privConnectionFactory,\n this.audioConfig,\n recognizerConfig);\n }\n\n // Start the recognition\n protected implRecognizerStart(\n recognitionMode: RecognitionMode,\n successCallback: (e: SpeechRecognitionResult) => void,\n errorCallback: (e: string) => void): void {\n this.privReco.recognize(recognitionMode, successCallback, errorCallback).on(\n /* tslint:disable:no-empty */\n (result: boolean): void => { },\n (error: string): void => {\n if (!!errorCallback) {\n // Internal error with service communication.\n errorCallback(\"Runtime error: \" + error);\n }\n });\n }\n\n protected implRecognizerStop(): void {\n if (this.privReco) {\n this.privReco.stopRecognizing();\n }\n }\n}\n"]}