-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcribe.js
644 lines (566 loc) · 20 KB
/
transcribe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
const spawn = require('child_process').spawn;
const stream = require('stream');
const express = require('@cisl/express');
const BinaryRingBuffer = require('@cisl/binary-ring-buffer');
const io = require('@cisl/io')();
const logger = require('@cisl/logger');
const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
const { BearerTokenAuthenticator } = require("ibm-cloud-sdk-core");
const app = express();
let publish = true;
const config = Object.assign(
{
channels: [
{} // Create a channel that uses all defaults
],
default_driver: 'ffmpeg',
default_device: 'default',
default_language: 'en-US',
default_model: 'broad',
default_language_model: 'generic',
language_models: {},
default_acoustic_model: null,
acoustic_models: {},
sample_rate: 16000,
buffer_size: 512000,
speaker_id_duration: 5 * 6000,
max_alternatives: 3,
publish_interim: false,
},
io.config.get('transcribe')
);
let channels = config.channels
let languages;
let models;
let model_names;
let currentLanguageModel;
let languageModels;
let currentAcousticModel;
let acousticModels;
let environmentUUID = null;
let pauseForSpeakerWorker = false;
if (!io.rabbit) {
logger.warn('Only printing to console, could not find RabbitMQ.');
}
else {
io.rabbit.onTopic('speaker.speak.begin', () => {
pauseForSpeakerWorker = true;
});
io.rabbit.onTopic('speaker.speak.end', () => {
pauseForSpeakerWorker = false;
})
}
if (!(['broad', 'narrow'].includes(config.default_model))) {
logger.error(`Unsupported model (broad or narrow): ${config.default_model}`);
process.exit();
}
// let stt_url =
// "https://cpd-cpd-instance.sbx-cpd-speech-01-e648741016b5b16f9b585588dcd0ed80-0000.us-south.containers.appdomain.cloud/speech-to-text/cpd-instance-speech-prod-cr/instances/1635259471651757/api";
// const bearerToken =
// "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkhLRXVtd2RyWUpEXzM1VzB3ZzhlSHh0eFV2dmpKeGF3VjFtODFzLTczcnMifQ.eyJ1aWQiOiIxMDAwMzMwOTk5IiwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOiJBZG1pbiIsInBlcm1pc3Npb25zIjpbImFkbWluaXN0cmF0b3IiLCJjYW5fcHJvdmlzaW9uIl0sImdyb3VwcyI6WzEwMDAwXSwic3ViIjoiYWRtaW4iLCJpc3MiOiJLTk9YU1NPIiwiYXVkIjoiRFNYIiwiaWF0IjoxNjM1MjU5NDcyfQ.sBg3PUj7i_AmIb7jzQqr67pI8R6Zebexbf1gvIciT_TPYSUmy6yeFKIosTqmV_tTFet9f_UhchLa5OuprrYU2xX7x8IOB5tP-c11pQC9Oo4Gm8QHrTto56jyH53HWoM18hqwrzMaXSWZ2mK2FNtC8-iZ1xCjoSrJRmNJM8zg5wMfNGOpqEaZz2CHdvHm4DQU8P_U-xDb89HHm6YGYP6az5K-Z6Z-BnM_LzolLLlRI8Af8KoftIebmcqNrswMhzT_ORkw1ncCbgWiWL2Eamss0VHWChc6ERa2IXnDQqSGIz05wgMuZ1BiucRU6R8an7j2rUm-oE-JzruJMcfZwWKoXA";
// const speechToText = new SpeechToTextV1({
// authenticator: new BearerTokenAuthenticator({
// bearerToken: bearerToken,
// }),
// disableSslVerification: true,
// serviceUrl: stt_url,
// });
let watson_stt;
const watsonInfo = io.config.get('watson') || null;
if (watsonInfo && watsonInfo.environment == 'cloudpak') {
console.log("watsonInfo is: ");
console.log(JSON.stringify(watsonInfo, null, 2));
let sttUrl = watsonInfo.sttURL;
let bearerToken = watsonInfo.bearerToken;
watson_stt = new SpeechToTextV1({
authenticator: new BearerTokenAuthenticator({
bearerToken: bearerToken,
}),
disableSslVerification: true,
serviceUrl: sttUrl,
});
}
else {
watson_stt = new SpeechToTextV1({});
}
function getModels() {
return new Promise((resolve, reject) => {
watson_stt.listModels(null, (err, res) => {
if (err) {
reject(err);
}
else {
resolve(res.result.models);
}
});
});
}
async function initializeWatson() {
models = await getModels(watson_stt);
model_names = [];
languages = [];
for (let model of models) {
if (!(languages.includes(model.language))) {
languages.push(model.language);
}
model_names.push(model.name);
}
languages.sort();
/*
currentLanguageModel = io.config.get('transcribe:default_language_model');
languageModels = io.config.get('transcribe:language_models');
currentAcousticModel = io.config.get('transcribe:default_acoustic_model');
acousticModels = io.config.get('transcribe:acoustic_models');
*/
currentLanguageModel = config.default_language_model;
languageModels = config.language_models;
currentAcousticModel = config.default_acoustic_model;
acousticModels = config.acoustic_models;
}
function getModelName(language, model) {
return language + '_' + model.substr(0, 1).toUpperCase() + model.substr(1) + 'bandModel';
}
function getDeviceInfo(device_name) {
let device_interface, device;
switch (process.platform) {
case 'darwin':
device_interface = 'avfoundation';
device = `none:${device_name}`;
break;
case 'win32':
device_interface = 'dshow';
device = `audio=${device_name}`;
break;
default:
device_interface = 'alsa';
device = device_name;
break;
}
return {
interface: device_interface,
device: device
};
}
function last(array) {
const length = array === null ? 0 : array.length;
return length ? array[length - 1] : undefined;
}
function resetSpeaker(idx) {
channels[idx].speaker = undefined;
notifyClientsChannel(channels[idx]);
}
function publishTranscript(idx, channel, data) {
if (data.results && data.results[0] && data.results[0].alternatives && publish && !channel.paused) {
let result = data.results[0];
// we always output our first interim result, which we know it is if speechStartTime is null
if (!channel.speechStartTime) {
channel.speechStartTime = (new Date()).getTime() / 100;
}
else if (!config.publish_interim && !result.final) {
return;
}
let transcript = result.alternatives[0];
transcript.transcript = transcript.transcript.trim();
let total_time = 0;
if (transcript.timestamps) {
total_time = Math.round((last(last(transcript.timestamps)) - transcript.timestamps[0][1]) * 100) / 100;
}
if (channel.speaker) {
if (channel.speaker_timeout) {
clearTimeout(channel.speaker_timeout);
}
channel.speaker_timeout = setTimeout(() => resetSpeaker(idx), config.speaker_id_duration);
}
channel.last_message_timestamp = new Date();
let msg = {
worker_id: config.id || 'transcript-worker',
message_id: io.generateUuid(),
timestamp: channel.last_message_timestamp,
channel_idx: idx,
speaker: channel.speaker,
language: channel.language,
transcript: transcript.transcript,
total_time: total_time,
result: result,
speechStartTime: channel.speechStartTime,
speechEndTime: 0,
environmentUUID,
};
if (result.final) {
msg.speechEndTIme = (new Date()).getTime() / 100;
channel.speechStartTime = null;
}
if (result.final) {
logger.info(`Transcript (Channel ${msg.channel_idx}): ${msg.transcript}`);
if (channel.extract_requested && io.rabbit) {
channel.extract_requested = false;
let start_time = transcript.timestamps[0][1];
let end_time = last(last(transcript.timestamps));
let start_index = (config.sample_rate * 2 * start_time);
let end_index = (config.sample_rate * 2 * end_time);
logger.info(` > Extracted for analysis`);
io.rabbit.publishTopic('transcript.pitchtone.audio', channel.raw_buffer.slice(start_index, end_index));
}
logger.debug(`Transcript (Channel ${msg.channel_idx}): ${JSON.stringify(msg, null, 2)}`);
app.wsServer.clients.forEach((client) => {
client.send(JSON.stringify({
type: 'transcript',
data: {
idx: msg.channel_idx,
transcript: msg.transcript, timestamp: msg.timestamp.toLocaleTimeString(undefined, {hourCycle:'h24'})
}
}));
});
}
if (io.rabbit) {
io.rabbit.publishTopic(`transcript.result.${result.final ? 'final' : 'interim'}`, msg);
// LEGACY
io.rabbit.publishTopic(`far.${result.final ? 'final' : 'interim'}.transcript`, msg);
// END LEGACY
}
}
}
function notifyClientsChannel(channel) {
app.wsServer.clients.forEach((client) => {
client.send(JSON.stringify({type: 'channel', data: channel}));
});
}
function transcribeChannel(idx, channel) {
logger.info(`opening channel ${idx}: ${channel.driver} - ${channel.device}`);
if (channel.driver === 'fake') {
return;
}
if (channel.stt_stream) {
channel.stt_stream.stop();
}
let params = {
objectMode: true,
model: getModelName(channel.language, channel.model),
customizationId: languageModels[currentLanguageModel],
contentType: `audio/l16; rate=${config.sample_rate}; channels=1`,
inactivityTimeout: -1,
timestamps: true,
smartFormatting: true,
interimResults: true,
maxAlternatives: config.max_alternatives,
};
if (currentAcousticModel) {
params.acousticCustomizationId = acousticModels[currentAcousticModel];
}
logger.info(` -> model: ${params.model}`);
if (params.customizationId) {
logger.info(` -> customizationId: ${params.customizationId}`);
}
if (params.acousticCustomizationId) {
logger.info(` -> acousticCustomizationId: ${params.acousticCustomizationId}`);
}
channel.stt_stream = watson_stt.recognizeUsingWebSocket(params);
channel.stream.pipe(channel.stt_stream);
channel.stt_stream.on('data', (data) => {
publishTranscript(idx, channel, data);
});
}
async function startTranscriptWorker() {
logger.info(`Starting ${channels.length} channel(s)`);
for (let idx = 0; idx < channels.length; idx++) {
let channel = channels[idx];
channel.transcript = [];
channel.idx = channel.idx || idx;
channel.driver = channel.driver || config.default_driver;
channel.device = channel.device || config.default_device;
channel.language = channel.language || config.default_language;
if (!(languages.includes(channel.language))) {
logger.error(`Invalid language for channel ${idx}: ${channel.language}`);
return;
}
channel.model = channel.model || config.default_model;
let model_full = getModelName(channel.language, channel.model);
if (!model_names.includes(model_full)) {
logger.error(`Invalid model for channel ${idx}: ${channel.model} (${model_full})`);
return;
}
channel.paused = false;
channel.last_message_timestamp = null;
channel.speaker = undefined;
channel.speaker_timeout = null;
channel.extract_requested = false;
channel.raw_buffer = new BinaryRingBuffer(config.buffer_size);
if (channel.driver === 'ffmpeg') {
let device_info = getDeviceInfo(channel.device);
let args = [
'-v', 'error',
'-f', device_info.interface,
'-i', device_info.device,
'-map_channel', `0.0.${channel.idx}`,
'-acodec', 'pcm_s16le', '-ar', `${config.sample_rate}`,
'-f', 'wav', '-'
];
channel.process = spawn('ffmpeg', args);
channel.process.stderr.on('data', data => {
logger.error(data.toString());
process.exit(1);
});
channel.stream = channel.process.stdout;
channel.stream.on('data', (data) => {
channel.raw_buffer.write(data);
});
const pausable = stream.Transform();
pausable._transform = function(chunk, encoding, callback) {
if (!(channel.paused || pauseForSpeakerWorker)) {
this.push(chunk);
}
callback();
};
channel.stream = channel.stream.pipe(pausable);
}
}
if (io.rabbit) {
io.rabbit.onTopic('transcript.command.switch_language', msg => {
msg.content.channel_idx = msg.content.channel_idx || msg.content.channelIndex;
changeLanguage(msg);
});
io.rabbit.onTopic('transcript.command.controlAudioCapture', msg => {
const content = msg.content || {};
changeChannelPauseState({
channel_idx: content.channelIndex || content.channel_idx || null,
}, msg.content.command === 'pause');
});
io.rabbit.onTopic('transcript.command.pause', msg => {
msg.content.channel_idx = msg.content.channel_idx || msg.content.channelIndex;
changeChannelPauseState(msg.content, true);
});
io.rabbit.onTopic('transcript.command.unpause', msg => {
msg.content.channel_idx = msg.content.channel_idx || msg.content.channelIndex;
changeChannelPauseState(msg.content, false);
});
io.rabbit.onTopic('transcript.command.stop_publish', () => {
logger.info('Stopping publishing');
publish = false;
});
io.rabbit.onTopic('transcript.command.start_publish', () => {
logger.info('Starting publishing');
publish = true;
});
io.rabbit.onTopic('transcript.command.extract_pitchtone', msg => {
let idx = msg.content.channel_idx;
if (channels[idx]) {
logger.info(`Extract requested for channel ${idx}`);
channels[idx].extract_requested = true;
}
});
io.rabbit.onTopic('transcript.command.tag_channel', msg => {
if (msg.content.channel_idx && msg.content.speaker && channels[msg.content.channel_idx]) {
logger.info(`Tagging channel ${msg.content.channel_idx}: ${msg.content.speaker}`);
channels[msg.content.channel_idx].speaker = msg.content.speaker;
}
});
// LEGACY
io.rabbit.onTopic('controlAudioCapture.transcript.command', {contentType: 'application/json'}, (msg) => {
const content = msg.content || {};
changeChannelPauseState({
channel_idx: content.channelIndex || content.channel_idx || null,
}, msg.content.command === 'pause');
});
io.rabbit.onTopic('transcript.command.switchLanguageModel', {contentType: 'text/string'}, (msg) => {
const model = msg.content;
if (!languageModels[model]) {
logger.info(`Cannot find the ${model} model. Not switching.`);
return;
}
logger.info(`Switching to the ${model} model.`);
currentLanguageModel = model;
for (let idx = 0; idx < channels.length; idx++) {
transcribeChannel(idx, channels[idx]);
}
});
io.rabbit.onTopic('transcript.command.switchAcousticModel', {contentType: 'text/string'}, (msg) => {
const model = msg.content;
if (!acousticModels[model]) {
logger.info(`Cannot find the ${model} acoustic model. Not switching.`);
return;
}
logger.info(`Switching to the ${model} acoustic model.`);
currentAcousticModel = model;
for (let idx = 0; idx < channels.length; idx++) {
transcribeChannel(idx, channels[idx]);
}
});
io.rabbit.onTopic('transcript.command.setEnvironmentUUID', {contentType: 'application/json'}, (msg) => {
if (msg.content.environmentUUID) {
environmentUUID = msg.content.environmentUUID;
}
else {
environmentUUID = null;
}
logger.info(`Set environmentUUID to ${environmentUUID}`);
});
io.rabbit.onRpc(`rpc-transcript-${io.config.get('id')}-tagChannel`, {contentType: 'application/json'}, (msg, reply) => {
const input = msg.content;
if (input.channelIndex >= channels.length) {
return reply('ignored');
}
logger.info(
`Tagging channel ${input.channelIndex} with name: ${input.speaker}`
);
channels[input.channelIndex].speaker = input.speaker;
reply('done');
});
// END LEGACY
io.rabbit.onTopic('transcript.command.restart_channel', (msg) => {
if (msg.content.channel_idx) {
transcribeChannel(msg.channel_idx, channels[msg.channel_idx]);
}
else {
for (let i = 0; i < channels.length; i++) {
transcribeChannel(i, channels[i]);
}
}
});
}
logger.info(`Starting to transcribe...`);
for (let idx = 0; idx < channels.length; idx++) {
transcribeChannel(idx, channels[idx]);
}
}
function changeLanguage(msg) {
logger.info(`Switching languages for ${(!msg.content.channel_idx || isNaN(parseInt(msg.content.channel_idx))) ? 'all' : msg.content.channel_idx} to ${msg.content.language}`);
if (!msg.content.channel_idx || isNaN(parseInt(msg.content.channel_idx))) {
for (let idx = 0; idx < channels.length; idx++) {
if (!model_names.includes(getModelName(msg.content.language, channels[idx].model))) {
logger.warn(`Invalid model for channel ${msg.content.channel_idx}: ${getModelName(msg.content.language, channels[idx].model)}`);
continue;
}
channels[idx].language = msg.content.language;
notifyClientsChannel(channels[idx]);
transcribeChannel(idx, channels[idx]);
}
}
else if (!isNaN(parseInt(msg.content.channel_idx))) {
const idx = parseInt(msg.content.channel_idx);
if (!model_names.includes(getModelName(msg.content.language, channels[idx].model))) {
logger.warn(`Invalid model for channel ${idx}: ${getModelName(msg.content.language, channels[idx].model)}`);
}
else {
channels[idx].language = msg.content.language
notifyClientsChannel(channels[idx]);
transcribeChannel(idx, channels[idx]);
}
}
}
function changeChannelPauseState(msg, pause) {
logger.info((pause ? 'Pausing' : 'Unpausing') + ` channel ${(!msg.channel_idx || isNaN(parseInt(msg.channel_idx))) ? 'all' : msg.channel_idx}`);
if (!msg.channel_idx || isNaN(parseInt(msg.channel_idx))) {
for (let idx = 0; idx < channels.length; idx++) {
channels[idx].paused = pause;
notifyClientsChannel(channels[idx]);
}
}
else if (!isNaN(parseInt(msg.channel_idx))) {
channels[parseInt(msg.channel_idx)].paused = true;
notifyClientsChannel(channels[parseInt(msg.channel_idx)]);
}
}
function stopTranscriptWorker() {
logger.info('Stopping all channels.');
for (let channel of config.channels) {
if (channel.process) {
channel.process.kill();
channel.process = null;
}
if (channel.stt_stream) {
channel.stt_stream.stop();
}
channel.stream = null;
channel.raw_buffer = null;
}
}
function exitHandler(options, err) {
if (options.cleanup) {
stopTranscriptWorker();
}
if (err && err.stack) {
console.log(err.stack);
}
if (options.exit) {
process.exit();
}
}
function get_channel(channel) {
const disallowed_keys = ['raw_buffer', 'process', 'stream', 'stt_stream', 'socket', 'speaker_timeout'];
return Object.keys(channel)
.filter(key => !disallowed_keys.includes(key))
.reduce((obj, key) => {
obj[key] = channel[key];
return obj;
}, {});
}
function get_channels() {
let filtered_channels = [];
for (const channel of channels) {
filtered_channels.push(get_channel(channel));
}
return filtered_channels;
}
// do something when app is closing
process.on('exit', exitHandler.bind(null, { cleanup: true }));
// catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, { exit: true }));
process.on('SIGTERM', exitHandler.bind(null, { exit: true }));
// catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, { exit: true }));
initializeWatson().then(() => {
startTranscriptWorker();
});
app.get('/', (req, res) => {
res.render('index', {
channels: channels
});
});
app.use(express.static('static'));
app.wsServer.on('connection', (socket) => {
socket.onmessage = (event) => {
let msg;
try {
msg = JSON.parse(event.data);
}
catch (ex) {
socket.send(JSON.stringify({type: 'error', message: `Invalid JSON object sent: ${ex}`}));
return;
}
if (msg.type === 'get_channels') {
socket.send(JSON.stringify({
type: 'channels',
data: get_channels()
}));
}
else if (msg.type === 'get_languages') {
socket.send(JSON.stringify({
type: 'languages',
data: languages
}));
}
else if (msg.type === 'save_channel') {
channels[msg.data.idx] = msg.data;
app.wsServer.clients.forEach((client) => {
client.send(JSON.stringify({type: 'channel', data: get_channel(channels[msg.data.idx])}));
});
}
else if (msg.type === 'transcript') {
let data = {
results: [
{
alternatives: [
{
confidence: 1,
transcript: msg.data.transcript
}
],
final: true
},
]
};
publishTranscript(msg.data.idx, channels[msg.data.idx], data);
}
}
});
app.listen();