This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
/
index.js
659 lines (549 loc) · 21.3 KB
/
index.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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// $(function(){
// setTimeout(function(){ $('#onboardingModal [data-dismiss="modal"]').click(); }, 0);
// setTimeout(function(){ $('#sendToVmixSettings').click(); }, 0);
// });
function clear_saved() {
ga('send', 'event', 'user', 'clearButtonClick');
if (confirm('Clear saved transcript?')) {
$('#final_span').text('');
// final_transcript = '';
//window.localStorage.setItem("transcript", final_transcript);
}
}
var webSocketPath;
if (window.location.protocol === "https:") {
webSocketPath = "wss:";
} else {
webSocketPath = "ws:";
}
webSocketPath += "//" + window.location.host;
var socket = new WebSocket(webSocketPath);
var chromeExtensionId = 'fckappdcgnijafmmjkcmicdidflhelfe'; // production
// var chromeExtensionId = 'ipngpifbnlijigdmhaoiepdlfjpfnajd'; // local
var recognizing = false;
var restartingDueToFailure = false;
var lastStartTimestamp;
var wordCount = 0;
var shouldStartOnStop = false;
var final_span = document.getElementById('final_span');
var interim_span = document.getElementById('interim_span');
var recognition;
var lastResultTime = (new Date()).getTime() / 1000;
var startStopButtonLastPressedTime;
var microphoneAccessAllowed = false;
var showMicrophonePermissionModalTimeout;
var transcriptionSequenceNum = 0;
var translatedTextBuffer = '';
window._wc = { // set defaults
language: {
from: null,
to: null,
},
};
// Load from local storage
var settings = window.localStorage.getItem("webcaptioner-settings");
if (settings) {
window._wc = JSON.parse(settings);
}
loadWordReplacementSettings();
if (!('webkitSpeechRecognition' in window)) {
upgrade();
} else {
recognition = initRecognition();
}
function shouldTranslateText() {
return false;
//return window._wc.language.from !== window._wc.language.to;
}
function throttle(fn, wait) {
var time = Date.now();
return function() {
if ((time + wait - Date.now()) < 0) {
fn();
time = Date.now();
}
}
}
var vmixInputGUID;
function getVmixInputGUID() {
if (window.vmixInputGUID) {
// Already cached
return Promise.resolve(window.vmixInputGUID);
}
// Fetch it from /API the first time
return new Promise(function(resolve, reject) {
chrome.runtime.sendMessage(
chromeExtensionId,
{
path: window._wc.vmix.address + '/API',
},
function(response) {
if (!response) return;
var $xml = $(response.text.replace(/<input /gi,'<webcaptioner-vmix-input ').replace(/\<\/input\>/gi,'</webcaptioner-vmix-input>'));
/*
Looks like
<inputs>
<input key="e5e760c3-36df-48b5-93ce-0d6e14616fa2" number="1" type="Xaml" title="web-captioner-title.xaml" state="Paused" position="0" duration="0" loop="False" selectedIndex="0">
web-captioner-title.xaml
<text index="0" name="WebCaptionerCaptions">...</text>
</input>
<input key="83047dd8-c2c7-4e38-8085-0f85ab3de925" number="2" type="Blank" title="Blank" state="Paused" position="0" duration="0" loop="False">Blank</input>
</inputs>
The parent() or parents() methods both go to <inputs> and not <input> for some reason.
*/
window.vmixInputGUID = $xml.find('text[name="WebCaptionerCaptions"]').parent('webcaptioner-vmix-input').first().attr('key');
resolve(window.vmixInputGUID);
}
);
});
}
var sendToVmixThrottled = throttle(function(){
var text = document.getElementsByClassName('caption-wrap-real')[0].innerText;
getVmixInputGUID()
.then(function(inputGUID) {
chrome.runtime.sendMessage(
chromeExtensionId,
{
path: window._wc.vmix.address + '/API/?Function=SetText&Input='+ inputGUID +'&SelectedName=WebCaptionerCaptions&Value='+encodeURIComponent(text.slice(-1000)),
},
function(response) {
// console.log(response);
}
);
// fetch(window._wc.vmix.address + '/API/?Function=SetText&Input='+ inputGUID +'&SelectedName=WebCaptionerCaptions&Value='+encodeURIComponent(text.slice(-1000)));
});
}, 200);
function initRecognition() {
recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
loadLanguageSettings();
recognition.lang = window._wc.language.from;
recognition.onstart = function () {
lastStartTimestamp = (new Date()).getTime() / 1000;
recognizing = true;
// If we are here, permission was granted
if ($('#microphonePermissionModal').is(':visible')) {
$('#microphonePermissionModal').modal('hide');
}
if (shouldTranslateText()) {
printTranslatedTextBuffered();
}
// Warn before leaving
window.onbeforeunload = function() { return true; }
$('.caption-wrap-real').scrollTop($('.caption-wrap-real')[0].scrollHeight);
initMediaLevelMonitoring();
// If onstart is running, then permission was granted. Don't show this modal.
clearTimeout(showMicrophonePermissionModalTimeout);
// If the current final text doesn't end in a space, add one. Every time this starts we can't guarantee
// that the last transcription ended with a space.
if (final_span.innerHTML.slice(-1) !== ' ') {
final_span.insertAdjacentHTML('beforeend', ' ');
}
};
recognition.onerror = function (event) {
// And recognition has stopped
// Don't show this modal if it was scheduled to show.
clearTimeout(showMicrophonePermissionModalTimeout);
$('#now_listening').attr('hidden',true);
if (event.error == 'no-speech') {
shouldStartOnStop = true;
// recognition.stop();
// recognition.start();
ga('send', 'event', 'recognition', 'errorNoSpeech');
window.onbeforeunload = null; // Don't show warning on leave
return;
}
if (event.error == 'audio-capture') {
$('#noMicrophoneModal').modal('show');
ga('send', 'event', 'recognition', 'errorNoMicrophone');
}
if (event.error == 'not-allowed') {
$('#microphonePermissionBlockedModal').modal('show');
ga('send', 'event', 'recognition', 'errorNotAllowed');
}
$('#startButton').text('Start Captioning');
$('#audioLevelWrap').attr('hidden','true'); // hide any error messages
clippingReadings = [];
lowLevelReadings = [];
clearInterval(levelCheckLoopInterval);
window.onbeforeunload = null; // Don't show warning on leave
return;
};
recognition.onend = function () {
if ($('#microphonePermissionModal').is(':visible')) {
$('#microphonePermissionModal').modal('hide');
}
$('#now_listening').attr('hidden',true);
recognizing = false;
var now = (new Date()).getTime() / 1000;
if (restartingDueToFailure || now - startStopButtonLastPressedTime > 1) {
// Button wasn't pressed in the last second, so recognition
// must have stopped on its own
ga('send', 'event', 'recognition', 'restartingDueToPossibleFailure');
recognition.start();
restartingDueToFailure = false;
startStopButtonPressed = false;
return;
}
else {
// Button was pressed and it really should stop
recognition = null;
clearInterval(levelCheckLoopInterval);
audioLevelWrap.setAttribute('hidden','true');
if (mediaStream) {
(mediaStream.getAudioTracks() || []).forEach(function(audioTrack) {
audioTrack.stop();
});
}
if (final_span.innerHTML.length <= 1) {
// No transcription; don't show warning on leave
window.onbeforeunload = null;
}
}
};
recognition.onresult = function (event) {
lastResultTime = (new Date()).getTime() / 1000;
$('#now_listening').attr('hidden',true);
var interim_transcript = '';
var final_transcript = '';
if (typeof (event.results) == 'undefined') {
recognition.onend = null;
recognition.stop();
upgrade();
return;
}
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript;
} else {
interim_transcript += event.results[i][0].transcript;
}
}
final_transcript = makeReplacements(final_transcript);
interim_transcript = makeReplacements(interim_transcript);
if (final_transcript) {
if (shouldTranslateText()) {
// Translate
fetch('/translate', {
method: 'POST',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
text: final_transcript,
target: window._wc.language.to,
}),
})
.then(function(response) {
return response.json();
})
.then(function(json) {
translatedTextBuffer += json.translation + ' ';
});
}
else {
final_span.insertAdjacentHTML('beforeend', final_transcript);
}
ga(
'send',
'event',
'recognition',
'recognizingSpeech',
'wordCount:' + final_transcript.split(' ').length
);
// Clear interim span
while (interim_span.firstChild) {
interim_span.removeChild(interim_span.firstChild);
}
interim_transcript = '';
}
if (interim_transcript) {
if (shouldTranslateText()) {
// Instead of showing interim text, show spinner
// $('#captioning-spinner').removeAttr('hidden');
}
else {
// Not translating
interim_span.innerHTML = interim_transcript;
// $('#captioning-spinner').attr('hidden', true);
}
}
else {
$('#captioning-spinner').attr('hidden', true);
}
if (interim_transcript || final_transcript) {
// Scroll to the bottom of the div
var captionWrapReal = document.getElementsByClassName('caption-wrap-real')[0];
captionWrapReal.scrollTop = captionWrapReal.scrollHeight;
// remove any current text selection
window.getSelection().removeAllRanges();
if (window._wc && window._wc.vmix && window._wc.vmix.on && window._wc.vmix.address) {
sendToVmixThrottled();
}
}
// Send to socket
if (socket) {
socket.send(JSON.stringify({
interim: interim_transcript,
final: final_transcript
}));
}
};
return recognition;
}
function printTranslatedTextBuffered() {
if ((translatedTextBuffer || '').length > 0) {
$('#captioning-spinner').attr('hidden', true);
console.log(translatedTextBuffer);
var words = translatedTextBuffer.split(' ');
console.log(words);
final_span.insertAdjacentHTML('beforeend', words[0] + ' ');
words.shift(); // remove first word
translatedTextBuffer = words.join(' ').trim();
}
setTimeout(printTranslatedTextBuffered, 500);
}
function makeReplacements(transcript) {
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
window._wc.wordReplacements.forEach(function(rewritePair) {
rewritePair.find.split(',').forEach(function(findString) {
transcript = transcript.replace(new RegExp(escapeRegExp(findString), 'gi'), rewritePair.replacement);
})
});
return transcript;
}
// Temp fix for issue where recognition stops when on another tab
document.addEventListener('visibilitychange', function(){
if ($('#startButton').text() == 'Stop') {
$('#audioLevelWrap').attr('hidden','true');
// It should be running right now
try {
recognition.start();
}
catch (e) {
// If the service was already running it throws an error.
// Can't figure out a way to check if the service is running
// before calling start().
}
}
})
setInterval(function () {
if (recognizing) {
var now = (new Date()).getTime() / 1000;
if (now - lastResultTime >= 1.5 && now - lastStartTimestamp > 3 && !showLowLevelmessage && !showClippingMessage) {
restartingDueToFailure = true;
recognition.stop();
}
}
}, 1000);
function upgrade() {
$('#onboardingModal .modal-footer').hide();
$('#upgrade-alert').show();
}
var audioContext = null;
var meter = null;
var WIDTH=500;
var HEIGHT=50;
var rafID = null;
var meterWrapWidth;
var levelCheckLoopInterval;
function initMediaLevelMonitoring() {
// monkeypatch Web Audio
if (!audioContext) {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
// grab an audio context
audioContext = new AudioContext();
}
// Attempt to get audio input
try {
// monkeypatch getUserMedia
navigator.getUserMedia =
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
// ask for an audio input
navigator.getUserMedia(
{
"audio": {
"mandatory": {
"googEchoCancellation": "false",
"googAutoGainControl": "false",
"googNoiseSuppression": "false",
"googHighpassFilter": "false"
},
"optional": []
},
}, gotStream, didntGetStream);
} catch (e) {
alert('getUserMedia threw exception :' + e);
}
clearInterval(levelCheckLoopInterval);
levelCheckLoopInterval = setInterval(levelCheckLoop, 400);
}
function didntGetStream() {
console.log('Stream generation failed.');
}
var mediaStreamSource = null;
var mediaStream = null;
function gotStream(stream) {
mediaStream = stream;
// Create an AudioNode from the stream.
mediaStreamSource = audioContext.createMediaStreamSource(stream);
// Hide the "give us permission to use your microphone" dialog (if it's showing)
// Create a new volume meter and connect it.
meter = createAudioMeter(audioContext, 1);
mediaStreamSource.connect(meter);
// kick off the visual updating
drawLoop();
}
function drawLoop( time ) {
var canvas = document.getElementById('meter');
// draw a bar based on the current volume
canvas.style.width = meterWrapWidth * Math.min(meter.volume * 4, 1) + 'px';
// check if we're currently clipping
if (meter.checkClipping() || meter.checkLowLevel()) {
canvas.classList.add('bg-danger');
canvas.classList.remove('bg-success');
if (meter.checkClipping()) window.audioIsClipping = true;
if (meter.checkLowLevel()) window.audioLevelIsLow = true;
}
else {
canvas.classList.add('bg-success');
canvas.classList.remove('bg-danger');
window.audioIsClipping = false;
window.audioLevelIsLow = false;
}
// set up the next visual callback
rafID = window.requestAnimationFrame(drawLoop);
}
// level check loop
var clippingReadings = [],
lowLevelReadings = [],
showClippingMessage = false,
showLowLevelmessage = false,
clippingMessage = document.getElementById('clippingMessage'),
lowLevelMessage = document.getElementById('lowLevelMessage'),
audioLevelWrap = document.getElementById('audioLevelWrap');
function levelCheckLoop() {
if (!meter) {
return;
}
clippingReadings.push(meter.checkClipping());
lowLevelReadings.push(meter.checkLowLevel());
// save just the past x readings
clippingReadings = clippingReadings.slice(-10);
lowLevelReadings = lowLevelReadings.slice(-10);
if ((clippingReadings.length > 0) && clippingReadings.filter(function (isClipping) {return isClipping;}).length / clippingReadings.length > .6) {
[clippingMessage, audioLevelWrap].forEach(function(element) {
element.removeAttribute('hidden');
});
if (!meterWrapWidth) {
meterWrapWidth = document.getElementById('meterWrap').offsetWidth;
}
showClippingMessage = true;
}
else {
clippingMessage.setAttribute('hidden','true');
showClippingMessage = false;
}
if ((lowLevelReadings.length > 0) && lowLevelReadings.filter(function(isLowLevel){return isLowLevel;}).length / lowLevelReadings.length > .6) {
[lowLevelMessage, audioLevelWrap].forEach(function(element) {
element.removeAttribute('hidden');
});
if (!meterWrapWidth) {
meterWrapWidth = document.getElementById('meterWrap').offsetWidth;
}
showLowLevelmessage = true;
// Don't show both messages at once
clippingMessage.setAttribute('hidden','true');
}
else {
lowLevelMessage.setAttribute('hidden','true');
showLowLevelmessage = false;
}
if (!showClippingMessage && !showLowLevelmessage) {
audioLevelWrap.setAttribute('hidden','true');
}
}
$(function () {
if ($('#onboardingModal').length && !$('body[data-view-only-mode]').length) {
$('#onboardingModal').modal();
}
//document.getElementById('final_span').innerHTML = window.localStorage.getItem("transcript");
if ($('.caption-wrap-real').length) {
$('.caption-wrap-real').scrollTop($('.caption-wrap-real')[0].scrollHeight);
$('.caption-wrap-real').parents('.caption-wrap-row').css({'margin-bottom': $('#main-navbar').outerHeight()});
}
if ($('#final_span').length) {
// $('#final_span').on('keyup', function (event) {
// final_transcript = $('#final_span').text();
//window.localStorage.setItem("transcript", final_transcript);
// });
}
});
$('#startButton').on('click', function(){
// if recognition ends for some unknown reason (it likes to just stop)
// and the button wasn't pressed, we will use this to check if that
// happened and start it again
startStopButtonLastPressedTime = (new Date()).getTime() / 1000;
if (recognizing) {
// Currently recognizing, so stop it.
ga('send', 'event', 'user', 'stopButtonClick');
recognition.stop();
$('#startButton').text('Start Captioning');
$('#audioLevelWrap').attr('hidden','true'); // hide any error messages
clippingReadings = [];
lowLevelReadings = [];
return;
}
else {
// Start recognition
if (!recognition) {
recognition = initRecognition();
}
ga('send', 'event', 'user', 'startButtonClick');
$('#startButton').text('Stop');
$('#now_listening').removeAttr('hidden');
recognition.start();
interim_span.innerHTML = '';
// Schedule the "allow the microphone" modal to appear. This scheduling gets
// canceled if the recognition service starts - this means that we did indeed
// have permission.
showMicrophonePermissionModalTimeout = setTimeout(function(){
$('#microphonePermissionModal').modal('show');
}, 500);
}
});
$('#saveTranscriptToFileButton').on('click', function() {
ga('send', 'event', 'settings', 'saveTranscriptToFile');
var a = document.createElement('a');
a.href = 'data:text/plain;base64,' + btoa(document.getElementById('results').innerText);
a.textContent = 'download';
a.download = 'web-captioner-'+ moment().format('YYYY-MM-DD-HH-mm-ss') +'.txt';
a.click();
});
// Init tooltips
$('[data-toggle="tooltip"]').tooltip();
$('#settingsDropdownContainer').on('show.bs.dropdown', function () {
if (document.getElementById('results').innerText.length > 0) {
// Transcription exists; show save option
$('#saveTranscriptToFileButton').removeAttr('hidden');
$('#saveTranscriptToFileDisabledButton').attr('hidden', true);
}
else {
// No transcription yet; hide save option
$('#saveTranscriptToFileButton').attr('hidden', true);
$('#saveTranscriptToFileDisabledButton').removeAttr('hidden');
}
});
$('#clearTranscriptButton').on('click', function() {
ga('send', 'event', 'settings', 'clearTranscriptStart');
$('#clearTranscriptModal').modal('show');
});
$('#clearTranscriptConfirmButton').on('click', function() {
ga('send', 'event', 'settings', 'clearTranscriptConfirm');
final_span.innerHTML = '';
});