major refactoring

This commit is contained in:
Dave Horton
2020-01-25 11:47:33 -05:00
parent 621ea8c0f5
commit 4a1ea4e091
25 changed files with 947 additions and 933 deletions

View File

@@ -1,54 +1,50 @@
const Task = require('./task');
const {TaskName, TaskPreconditions, TranscriptionEvents} = require('../utils/constants');
const assert = require('assert');
class TaskTranscribe extends Task {
constructor(logger, opts) {
constructor(logger, opts, parentTask) {
super(logger, opts);
this.preconditions = TaskPreconditions.Endpoint;
this.action = this.data.action;
this.language = this.data.language || 'en-US';
this.vendor = this.data.vendor;
this.interim = this.data.interim === true;
this.mixType = this.data.mixType;
this.earlyMedia = this.data.earlyMedia === true;
this._completionPromise = new Promise((resolve) => this._completionResolver = resolve);
this.transcriptionCallback = this.data.transcriptionCallback;
this.earlyMedia = this.data.earlyMedia === true || (parentTask && parentTask.earlyMedia);
if (this.data.recognizer) {
this.language = this.data.recognizer.language || 'en-US';
this.vendor = this.data.recognizer.vendor;
this.interim = this.data.recognizer.interim === true;
this.dualChannel = this.data.recognizer.dualChannel === true;
}
}
get name() { return TaskName.Transcribe; }
async exec(cs, ep, parentTask) {
super.exec(cs);
this.ep = ep;
this.actionHook = ep.cs.actionHook;
this.transcribeInProgress = true;
try {
await this._initSpeech(ep);
await this._startTranscribing(ep);
await this._completionPromise;
await this.awaitTaskDone();
} catch (err) {
this.logger.info(err, 'TaskTranscribe:exec - error');
}
this.transcribeInProgress = true;
ep.removeCustomEventListener(TranscriptionEvents.Transcription);
ep.removeCustomEventListener(TranscriptionEvents.NoAudioDetected);
ep.removeCustomEventListener(TranscriptionEvents.MaxDurationExceeded);
}
async kill() {
super.kill();
if (this.ep.connected && this.transcribeInProgress) {
if (this.ep.connected) {
this.ep.stopTranscription().catch((err) => this.logger.info(err, 'Error TaskTranscribe:kill'));
// hangup after 1 sec if we don't get a final transcription
this._timer = setTimeout(() => this._completionResolver(), 1000);
this._timer = setTimeout(() => this.notifyTaskDone(), 1000);
}
else {
this._completionResolver();
}
await this._completionPromise;
else this.notifyTaskDone();
await this.awaitTaskDone();
}
async _initSpeech(ep) {
async _startTranscribing(ep) {
const opts = {
GOOGLE_SPEECH_USE_ENHANCED: true,
GOOGLE_SPEECH_MODEL: 'phone_call'
@@ -56,43 +52,48 @@ class TaskTranscribe extends Task {
if (this.hints) {
Object.assign(opts, {'GOOGLE_SPEECH_HINTS': this.hints.join(',')});
}
if (this.profanityFilter === true) {
if (this.profanityFilter) {
Object.assign(opts, {'GOOGLE_SPEECH_PROFANITY_FILTER': true});
}
if (this.dualChannel) {
Object.assign(opts, {'GOOGLE_SPEECH_SEPARATE_RECOGNITION_PER_CHANNEL': true});
}
await ep.set(opts)
.catch((err) => this.logger.info(err, 'TaskTranscribe:_initSpeech error setting fs vars'));
.catch((err) => this.logger.info(err, 'TaskTranscribe:_startTranscribing'));
ep.addCustomEventListener(TranscriptionEvents.Transcription, this._onTranscription.bind(this, ep));
ep.addCustomEventListener(TranscriptionEvents.NoAudioDetected, this._onNoAudio.bind(this, ep));
ep.addCustomEventListener(TranscriptionEvents.MaxDurationExceeded, this._onMaxDurationExceeded.bind(this, ep));
await this._transcribe(ep);
}
async _startTranscribing(ep) {
await ep.startTranscription({
async _transcribe(ep) {
await this.ep.startTranscription({
interim: this.interim ? true : false,
language: this.language
language: this.language || this.callSession.speechRecognizerLanguage,
channels: this.dualChannel ? 2 : 1
});
}
_onTranscription(ep, evt) {
this.logger.debug(evt, 'TaskTranscribe:_onTranscription');
this.actionHook(this.action, 'POST', {
Speech: evt
});
this.notifyHook(this.transcriptionCallback, 'POST', {speech: evt});
if (this.killed) {
this.logger.debug('TaskTranscribe:_onTranscription exiting after receiving final transcription');
this._clearTimer();
this._completionResolver();
this.notifyTaskDone();
}
}
_onNoAudio(ep) {
this.logger.debug('TaskTranscribe:_onNoAudio restarting transcription');
this._startTranscribing(ep);
this._transcribe(ep);
}
_onMaxDurationExceeded(ep) {
this.logger.debug('TaskTranscribe:_onMaxDurationExceeded restarting transcription');
this._startTranscribing(ep);
this._transcribe(ep);
}
_clearTimer() {