mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 16:50:39 +00:00
feat: queue play audio (#290)
* feat: queue play audio * fix: typo * fix: typo * fix: typo * fix: typo * fix: typo * fix: typo * fix: typo * fix: typo * fix: typo * fix: typo
This commit is contained in:
@@ -2,6 +2,7 @@ const Task = require('./task');
|
|||||||
const {TaskName, TaskPreconditions, ListenEvents, ListenStatus} = require('../utils/constants');
|
const {TaskName, TaskPreconditions, ListenEvents, ListenStatus} = require('../utils/constants');
|
||||||
const makeTask = require('./make_task');
|
const makeTask = require('./make_task');
|
||||||
const moment = require('moment');
|
const moment = require('moment');
|
||||||
|
const MAX_PLAY_AUDIO_QUEUE_SIZE = 10;
|
||||||
|
|
||||||
class TaskListen extends Task {
|
class TaskListen extends Task {
|
||||||
constructor(logger, opts, parentTask) {
|
constructor(logger, opts, parentTask) {
|
||||||
@@ -20,6 +21,8 @@ class TaskListen extends Task {
|
|||||||
this.nested = parentTask instanceof Task;
|
this.nested = parentTask instanceof Task;
|
||||||
|
|
||||||
this.results = {};
|
this.results = {};
|
||||||
|
this.playAudioQueue = [];
|
||||||
|
this.isPlayingAudioFromQueue = false;
|
||||||
|
|
||||||
if (this.transcribe) this.transcribeTask = makeTask(logger, {'transcribe': opts.transcribe}, this);
|
if (this.transcribe) this.transcribeTask = makeTask(logger, {'transcribe': opts.transcribe}, this);
|
||||||
}
|
}
|
||||||
@@ -58,6 +61,7 @@ class TaskListen extends Task {
|
|||||||
super.kill(cs);
|
super.kill(cs);
|
||||||
this.logger.debug(`TaskListen:kill endpoint connected? ${this.ep && this.ep.connected}`);
|
this.logger.debug(`TaskListen:kill endpoint connected? ${this.ep && this.ep.connected}`);
|
||||||
this._clearTimer();
|
this._clearTimer();
|
||||||
|
this.playAudioQueue = [];
|
||||||
if (this.ep && this.ep.connected) {
|
if (this.ep && this.ep.connected) {
|
||||||
this.logger.debug('TaskListen:kill closing websocket');
|
this.logger.debug('TaskListen:kill closing websocket');
|
||||||
try {
|
try {
|
||||||
@@ -184,16 +188,36 @@ class TaskListen extends Task {
|
|||||||
this.notifyTaskDone();
|
this.notifyTaskDone();
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onPlayAudio(ep, evt) {
|
async _playAudio(ep, evt, logger) {
|
||||||
this.logger.info(`received play_audio event: ${JSON.stringify(evt)}`);
|
|
||||||
try {
|
try {
|
||||||
const results = await ep.play(evt.file);
|
const results = await ep.play(evt.file);
|
||||||
this.logger.debug(`Finished playing file, result: ${JSON.stringify(results)}`);
|
logger.debug(`Finished playing file, result: ${JSON.stringify(results)}`);
|
||||||
ep.forkAudioSendText({type: 'playDone', data: Object.assign({id: evt.id}, results)});
|
ep.forkAudioSendText({type: 'playDone', data: Object.assign({id: evt.id}, results)});
|
||||||
|
} catch (err) {
|
||||||
|
logger.error({err}, 'Error playing file');
|
||||||
}
|
}
|
||||||
catch (err) {
|
|
||||||
this.logger.error({err}, 'Error playing file');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async _onPlayAudio(ep, evt) {
|
||||||
|
this.logger.info(`received play_audio event: ${JSON.stringify(evt)}`);
|
||||||
|
if (!evt.queuePlay) {
|
||||||
|
this.playAudioQueue = [];
|
||||||
|
this._playAudio(ep, evt, this.logger);
|
||||||
|
this.isPlayingAudioFromQueue = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.playAudioQueue.length <= MAX_PLAY_AUDIO_QUEUE_SIZE) {
|
||||||
|
this.playAudioQueue.push(evt);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.isPlayingAudioFromQueue) return;
|
||||||
|
|
||||||
|
this.isPlayingAudioFromQueue = true;
|
||||||
|
while (this.playAudioQueue.length > 0) {
|
||||||
|
await this._playAudio(ep, this.playAudioQueue.shift(), this.logger);
|
||||||
|
}
|
||||||
|
this.isPlayingAudioFromQueue = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_onKillAudio(ep) {
|
_onKillAudio(ep) {
|
||||||
|
|||||||
Reference in New Issue
Block a user