UNPKG

@voice-ping/cognitive-services-speech

Version:

VoicePing Cognitive Services Speech SDK for JavaScript forked from Microsoft

354 lines (352 loc) 12.7 kB
// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. import { RecognizerConfig } from "../../common.speech/Exports"; import { Contracts } from "../../sdk/Contracts"; import { Recognizer } from "../../sdk/Exports"; import { ConversationConnectionFactory } from "./ConversationConnectionFactory"; import { ConversationServiceAdapter } from "./ConversationServiceAdapter"; import { ConversationTranslatorCommandTypes, ConversationTranslatorMessageTypes } from "./ConversationTranslatorInterfaces"; import { PromiseToEmptyCallback } from "./ConversationUtils"; /** * Sends messages to the Conversation Translator websocket and listens for incoming events containing websocket messages. * Based off the recognizers in the SDK folder. */ export class ConversationTranslatorRecognizer extends Recognizer { constructor(speechConfig, audioConfig) { const serviceConfigImpl = speechConfig; Contracts.throwIfNull(serviceConfigImpl, "speechConfig"); super(audioConfig, serviceConfigImpl.properties, new ConversationConnectionFactory()); this.privIsDisposed = false; this.privProperties = serviceConfigImpl.properties.clone(); } set conversation(value) { this.privRoom = value; } /** * Return the speech language used by the recognizer */ get speechRecognitionLanguage() { return this.privSpeechRecognitionLanguage; } /** * Return the properties for the recognizer */ get properties() { return this.privProperties; } isDisposed() { return this.privIsDisposed; } /** * Connect to the recognizer * @param token */ connect(token, cb, err) { try { Contracts.throwIfDisposed(this.privIsDisposed); Contracts.throwIfNullOrWhitespace(token, "token"); this.privReco.conversationTranslatorToken = token; this.privReco.connectAsync(cb, err); } catch (error) { if (!!err) { if (error instanceof Error) { const typedError = error; err(typedError.name + ": " + typedError.message); } else { err(error); } } } } /** * Disconnect from the recognizer */ disconnect(cb, err) { try { Contracts.throwIfDisposed(this.privIsDisposed); this.privRoom = undefined; this.privReco.disconnectAsync(cb, err); } catch (error) { if (!!err) { if (error instanceof Error) { const typedError = error; err(typedError.name + ": " + typedError.message); } else { err(error); } } // Destroy the recognizer. this.dispose(true); } } /** * Send the text message command to the websocket * @param conversationId * @param participantId * @param message */ sendMessageRequest(message, cb, err) { try { Contracts.throwIfDisposed(this.privIsDisposed); Contracts.throwIfNullOrWhitespace(this.privRoom.roomId, "conversationId"); Contracts.throwIfNullOrWhitespace(this.privRoom.participantId, "participantId"); Contracts.throwIfNullOrWhitespace(message, "message"); const command = { // tslint:disable-next-line: object-literal-shorthand participantId: this.privRoom.participantId, roomId: this.privRoom.roomId, text: message, type: ConversationTranslatorMessageTypes.instantMessage }; this.sendMessage(JSON.stringify(command), cb, err); } catch (error) { if (!!err) { if (error instanceof Error) { const typedError = error; err(typedError.name + ": " + typedError.message); } else { err(error); } } // Destroy the recognizer. this.dispose(true); } } /** * Send the lock conversation command to the websocket * @param conversationId * @param participantId * @param isLocked */ sendLockRequest(isLocked, cb, err) { try { Contracts.throwIfDisposed(this.privIsDisposed); Contracts.throwIfNullOrWhitespace(this.privRoom.roomId, "conversationId"); Contracts.throwIfNullOrWhitespace(this.privRoom.participantId, "participantId"); Contracts.throwIfNullOrUndefined(isLocked, "isLocked"); const command = { command: ConversationTranslatorCommandTypes.setLockState, // tslint:disable-next-line: object-literal-shorthand participantId: this.privRoom.participantId, roomid: this.privRoom.roomId, type: ConversationTranslatorMessageTypes.participantCommand, value: isLocked }; this.sendMessage(JSON.stringify(command), cb, err); } catch (error) { if (!!err) { if (error instanceof Error) { const typedError = error; err(typedError.name + ": " + typedError.message); } else { err(error); } } // Destroy the recognizer. this.dispose(true); } } /** * Send the mute all participants command to the websocket * @param conversationId * @param participantId * @param isMuted */ sendMuteAllRequest(isMuted, cb, err) { try { Contracts.throwIfDisposed(this.privIsDisposed); Contracts.throwIfNullOrWhitespace(this.privRoom.roomId, "conversationId"); Contracts.throwIfNullOrWhitespace(this.privRoom.participantId, "participantId"); Contracts.throwIfNullOrUndefined(isMuted, "isMuted"); const command = { command: ConversationTranslatorCommandTypes.setMuteAll, // tslint:disable-next-line: object-literal-shorthand participantId: this.privRoom.participantId, roomid: this.privRoom.roomId, type: ConversationTranslatorMessageTypes.participantCommand, value: isMuted }; this.sendMessage(JSON.stringify(command), cb, err); } catch (error) { if (!!err) { if (error instanceof Error) { const typedError = error; err(typedError.name + ": " + typedError.message); } else { err(error); } } // Destroy the recognizer. this.dispose(true); } } /** * Send the mute participant command to the websocket * @param conversationId * @param participantId * @param isMuted */ sendMuteRequest(participantId, isMuted, cb, err) { try { Contracts.throwIfDisposed(this.privIsDisposed); Contracts.throwIfNullOrWhitespace(this.privRoom.roomId, "conversationId"); Contracts.throwIfNullOrWhitespace(participantId, "participantId"); Contracts.throwIfNullOrUndefined(isMuted, "isMuted"); const command = { command: ConversationTranslatorCommandTypes.setMute, // tslint:disable-next-line: object-literal-shorthand participantId: participantId, roomid: this.privRoom.roomId, type: ConversationTranslatorMessageTypes.participantCommand, value: isMuted }; this.sendMessage(JSON.stringify(command), cb, err); } catch (error) { if (!!err) { if (error instanceof Error) { const typedError = error; err(typedError.name + ": " + typedError.message); } else { err(error); } } // Destroy the recognizer. this.dispose(true); } } /** * Send the eject participant command to the websocket * @param conversationId * @param participantId */ sendEjectRequest(participantId, cb, err) { try { Contracts.throwIfDisposed(this.privIsDisposed); Contracts.throwIfNullOrWhitespace(this.privRoom.roomId, "conversationId"); Contracts.throwIfNullOrWhitespace(participantId, "participantId"); const command = { command: ConversationTranslatorCommandTypes.ejectParticipant, // tslint:disable-next-line: object-literal-shorthand participantId: participantId, roomid: this.privRoom.roomId, type: ConversationTranslatorMessageTypes.participantCommand, }; this.sendMessage(JSON.stringify(command), cb, err); if (!!cb) { try { cb(); } catch (e) { if (!!err) { err(e); } } } } catch (error) { if (!!err) { if (error instanceof Error) { const typedError = error; err(typedError.name + ": " + typedError.message); } else { err(error); } } // Destroy the recognizer. this.dispose(true); } } /** * Send the mute participant command to the websocket * @param conversationId * @param participantId * @param isMuted */ sendChangeNicknameRequest(nickname, cb, err) { try { Contracts.throwIfDisposed(this.privIsDisposed); Contracts.throwIfNullOrWhitespace(this.privRoom.roomId, "conversationId"); Contracts.throwIfNullOrWhitespace(nickname, "nickname"); const command = { command: ConversationTranslatorCommandTypes.changeNickname, nickname, // tslint:disable-next-line: object-literal-shorthand participantId: this.privRoom.participantId, roomid: this.privRoom.roomId, type: ConversationTranslatorMessageTypes.participantCommand, value: nickname }; this.sendMessage(JSON.stringify(command), cb, err); } catch (error) { if (!!err) { if (error instanceof Error) { const typedError = error; err(typedError.name + ": " + typedError.message); } else { err(error); } } // Destroy the recognizer. this.dispose(true); } } /** * Close and dispose the recognizer */ close() { Contracts.throwIfDisposed(this.privIsDisposed); this.dispose(true); } /** * Dispose the recognizer * @param disposing */ dispose(disposing) { if (this.privIsDisposed) { return; } if (disposing) { this.privIsDisposed = true; super.dispose(disposing); } } /** * Create the config for the recognizer * @param speechConfig */ createRecognizerConfig(speechConfig) { return new RecognizerConfig(speechConfig, this.privProperties); } /** * Create the service recognizer. * The audio source is redundnant here but is required by the implementation. * @param authentication * @param connectionFactory * @param audioConfig * @param recognizerConfig */ createServiceRecognizer(authentication, connectionFactory, audioConfig, recognizerConfig) { const audioSource = audioConfig; return new ConversationServiceAdapter(authentication, connectionFactory, audioSource, recognizerConfig, this); } sendMessage(msg, cb, err) { const withAsync = this.privReco; PromiseToEmptyCallback(withAsync.sendMessageAsync(msg), cb, err); } } //# sourceMappingURL=ConversationTranslatorRecognizer.js.map