-
Notifications
You must be signed in to change notification settings - Fork 16
/
voice-button.js
1332 lines (1149 loc) · 44.3 KB
/
voice-button.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* // Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License. */
/*
FIXME(polymer-modulizer): the above comments were extracted
from HTML and may be out of place here. Review them and
then delete this comment!
*/
import { PolymerElement } from './node_modules/@polymer/polymer/polymer-element.js';
import './node_modules/@polymer/paper-behaviors/paper-ripple-behavior.js';
const $_documentContainer = document.createElement('template');
$_documentContainer.setAttribute('style', 'display: none;');
$_documentContainer.innerHTML = `<dom-module id="voice-button">
<!--
Note: deprecation warning for styling master document doesn't apply within <template> tags
Read more: https://www.polymer-project.org/blog/2017-10-18-upcoming-changes
-->
<template>
<style>
/* shadow DOM styles */
:host {
/* red active color */
--active-color: #fe4042;
/* background color for button */
--button-color: #FFFFFF;
/* circle viz color (opacity is at 0.2) */
--circle-viz-color:#000000;
/* diameter of button, option can be changed by user */
--button-diameter:100px;
/* mic color*/
--mic-color:#FFFFFF;
/* text color, used for warning messaging*/
--text-color:#666;
display: inline-block;
height: var(--button-diameter);
width: var(--button-diameter);
}
#paper-button{
outline:none;
background:var(--button-color);
border:none;
width:100%;
height:100%;
min-width:0;
border-radius:50%;
cursor:pointer;
transition: all ease 0.25s;
background-size: 100%;
background-repeat: no-repeat;
background-position: center;
box-shadow: 0 2px 5px 0 rgba(0,0,0,.26);
z-index:2;
position:absolute;
opacity:0.8;
margin:0;
left:0;
}
#paper-button.flat{
box-shadow:0px 0px 0px 0px;
}
#paper-button:hover{
box-shadow: 0 4px 7px 0 rgba(0,0,0,.20);
}
#paper-button.disabled{
box-shadow: 0px 0px 0px 0px;
cursor:not-allowed;
opacity:0.5;
}
#paper-button.listening, #paper-button.user-input{
box-shadow: 0px 0px 0px 0px;
opacity:1;
background:var(--active-color);
}
#paper-button.flat:hover{
box-shadow:0px 0px 0px 0px;
}
#paper-button.disabled:hover{
box-shadow: 0px 0px 0px 0px;
}
#circle-viz-container{
position:absolute;
z-index:0;
opacity:0;
}
.svg-container{
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
background:rgba(255, 0, 0, 0);
z-index:3;
pointer-events: none;
}
#mic-svg, #mic-svg-disabled, #mic-loading{
height: 64%;
width:100%;
display: block;
position: absolute;
top: -11%;
left: 0px;
transform:translate(0, 50%);
z-index:3;
pointer-events:none;
}
#mic-loading {
opacity: 0;
top: -14%;
transform: translate(0, 50%) scale(0.8, 0.8);
}
#mic-loading circle:nth-child(1) {
animation: 0.7s micLoading 0s infinite;
}
#mic-loading circle:nth-child(2) {
animation: 0.7s micLoading 0.3s infinite;
}
#mic-loading circle:nth-child(3) {
animation: 0.7s micLoading 0.5s infinite;
}
@keyframes micLoading {
to {
fill-opacity: 0;
}
from {
fill-opacity: 1;
}
}
/* show the disabled mic when in disabled state */
.container #mic-svg-disabled{ display:none; }
.container.disabled #mic-svg-disabled{ display:block; }
.container.disabled #mic-svg{ display:none; }
.container.listening #mic-svg, .container.user-input #mic-svg { fill: #FFFFFF !important; }
.container{
position:absolute;
height: var(--button-diameter);
width: var(--button-diameter);
}
.circle{
width:calc(var(--button-diameter) - 4px);
height:calc(var(--button-diameter) - 4px);
background-color:var(--circle-viz-color);
opacity:0.2;
border-radius:50%;
position:absolute;
transform-origin: center center;
transform:scale(1);
}
#mic-not-allowed{
font-family: 'Noto Sans', sans-serif;
font-size: 1.0em;
-webkit-font-smoothing: antialiased;
color: var(--text-color);
width: 210px;
top: 100%;
display: none;
position: absolute;
margin-left: 0px;
transform: translateX(-50%);
left: 50%;
font-weight: 400;
text-align: center;
vertical-align: bottom;
margin-top: 15px;
}
#mic-not-allowed.show{
display:block;
}
paper-ripple{
position:absolute;
}
</style>
<!-- shadow DOM -->
<div class\$="container [[state]]">
<!-- rings that scale out from button with user input (only available with autodetect) -->
<div id="circle-viz-container"></div>
<!-- clickable button -->
<button id="paper-button" class\$="voice-button [[state]] [[flatStr]] [[autoDetectStr]]"><paper-ripple></paper-ripple></button>
<div class="svg-container">
<!-- mic icon -->
<svg id="mic-svg" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 24 24" fill="#000000">
<path d="M12 14c1.66 0 2.99-1.34 2.99-3L15 5c0-1.66-1.34-3-3-3S9 3.34 9 5v6c0 1.66 1.34 3 3 3zm5.3-3c0 3-2.54 5.1-5.3 5.1S6.7 14 6.7 11H5c0 3.41 2.72 6.23 6 6.72V21h2v-3.28c3.28-.48 6-3.3 6-6.72h-1.7z"></path>
<path d="M0 0h24v24H0z" fill="none"></path>
</svg>
<svg id="mic-svg-disabled" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 24 24" fill="#000000">
<path d="M0 0h24v24H0zm0 0h24v24H0z" fill="none"></path>
<path d="M19 11h-1.7c0 .74-.16 1.43-.43 2.05l1.23 1.23c.56-.98.9-2.09.9-3.28zm-4.02.17c0-.06.02-.11.02-.17V5c0-1.66-1.34-3-3-3S9 3.34 9 5v.18l5.98 5.99zM4.27 3L3 4.27l6.01 6.01V11c0 1.66 1.33 3 2.99 3 .22 0 .44-.03.65-.08l1.66 1.66c-.71.33-1.5.52-2.31.52-2.76 0-5.3-2.1-5.3-5.1H5c0 3.41 2.72 6.23 6 6.72V21h2v-3.28c.91-.13 1.77-.45 2.54-.9L19.73 21 21 19.73 4.27 3z"></path>
</svg>
<svg id="mic-loading" version="1.1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 727 353" fill="#ffffff">
<circle cx="136.5" cy="176.5" r="73.5"></circle>
<circle cx="363.5" cy="176.5" r="73.5"></circle>
<circle cx="590.5" cy="176.5" r="73.5"></circle>
</svg>
</div>
<div id="mic-not-allowed">Cannot access microphone</div>
</div>
</template>
</dom-module>`;
document.head.appendChild($_documentContainer.content);
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
window.AudioContext = window.AudioContext || window.webkitAudioContext;
function VoiceRecorder(autodetect, onStopRecordComplete) {
this.autodetect = autodetect;
this.onStopRecordComplete = onStopRecordComplete;
this.microphone;
this.isRecording = false;
this.isSpeaking = false;
this.audioContext;
this.leftChannel = [];
this.recordingLength = 0;
this.processor;
this.config = {
sampleRate: 44100,
bufferLen: 4096,
numChannels: 1,
mimeType: 'audio/mpeg'
};
this.analyzer = (context) => {
let listener = context.createAnalyser();
this.microphone.connect(listener);
listener.fftSize = 256;
var bufferLength = listener.frequencyBinCount;
let analyserData = new Uint8Array(bufferLength);
}
this.onAudioProcess = (e) => {
var left = e.inputBuffer.getChannelData(0);
this.recordingLength += this.config.bufferLen;
// we clone the samples
this.leftChannel.push(new Float32Array(left));
// Check for pause while speaking
var MIN_SPEAKING_VOLUME = 0.04;
var sum = 0.0;
var i;
var clipcount = 0;
for (i = 0; i < left.length; ++i) {
sum += left[i] * left[i];
if (Math.abs(left[i]) > 0.99) {
clipcount += 1;
}
}
var volume = Math.sqrt(sum / left.length);
if (volume > MIN_SPEAKING_VOLUME) {
this.isSpeaking = true;
clearTimeout(this.speakingTimeout);
} else {
if (this.isSpeaking) {
this.isSpeaking = false;
clearTimeout(this.speakingTimeout);
this.speakingTimeout = setTimeout(() => {
this.stopRecord();
}, 500);
}
}
}
this.logError = (error) => {
console.error(error);
}
this.startRecord = () => {
this.audioContext = new AudioContext();
/**
* Create a ScriptProcessorNode
* */
if (this.audioContext.createJavaScriptNode) {
this.processor = this.audioContext.createJavaScriptNode(this.config.bufferLen, this.config.numChannels, this.config.numChannels);
} else if (this.audioContext.createScriptProcessor) {
this.processor = this.audioContext.createScriptProcessor(this.config.bufferLen, this.config.numChannels, this.config.numChannels);
} else {
console.log('WebAudio API has no support on this browser.');
}
this.processor.connect(this.audioContext.destination);
/**
* ask permission of the user for use this.microphone or camera
* */
navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then(this.gotStreamMethod.bind(this))
.catch(this.logError);
}
this.getBuffers = (event) => {
var buffers = [];
for (var ch = 0; ch < 2; ++ch){
buffers[ch] = event.inputBuffer.getChannelData(ch);
}
return buffers;
}
this.gotStreamMethod = (stream) => {
// audioElement.src = "";
this.isRecording = true;
this.tracks = stream.getTracks();
/**
* Create a MediaStreamAudioSourceNode for the this.microphone
* */
this.microphone = this.audioContext.createMediaStreamSource(stream);
/**
* connect the AudioBufferSourceNode to the gainNode
* */
this.microphone.connect(this.processor);
// encoder = new Mp3LameEncoder(audioContext.sampleRate, 160);
/**
* Give the node a function to process audio events
*/
if(this.autodetect){
this.processor.onaudioprocess = this.onAudioProcess;
}
this.analyzer(this.audioContext);
}
this.clearRecordedData = () => {
this.recordingLength = 0;
this.leftChannel = [];
}
this.stopRecord = () => {
var callback = this.onStopRecordComplete;
this.isRecording = false;
this.audioContext.close();
this.processor.disconnect();
this.tracks.forEach(track => track.stop());
this.mergeLeftRightBuffers({
sampleRate: this.config.sampleRate,
numberOfAudioChannels: this.config.numChannels,
internalInterleavedLength: this.recordingLength,
leftBuffers: this.leftChannel,
rightBuffers: this.config.numChannels === 1 ? [] : rightChannel
}, (buffer, view) => {
self.blob = new Blob([view], {
type: 'audio/wav'
});
self.buffer = new ArrayBuffer(view.buffer.byteLength);
self.view = view;
self.sampleRate = this.config.sampleRate;
self.bufferSize = this.config.bufferLen;
self.length = this.recordingLength;
callback && callback(self.blob);
this.clearRecordedData();
isAudioProcessStarted = false;
});
}
this.mergeLeftRightBuffers = (config, callback) => {
function mergeAudioBuffers(config, cb){
var numberOfAudioChannels = config.numberOfAudioChannels;
// todo: "slice(0)" --- is it causes loop? Should be removed?
var leftBuffers = config.leftBuffers.slice(0);
var rightBuffers = config.rightBuffers.slice(0);
var sampleRate = config.sampleRate;
var internalInterleavedLength = config.internalInterleavedLength;
var desiredSampRate = config.desiredSampRate;
if (numberOfAudioChannels === 2) {
leftBuffers = mergeBuffers(leftBuffers, internalInterleavedLength);
rightBuffers = mergeBuffers(rightBuffers, internalInterleavedLength);
if (desiredSampRate) {
leftBuffers = interpolateArray(leftBuffers, desiredSampRate, sampleRate);
rightBuffers = interpolateArray(rightBuffers, desiredSampRate, sampleRate);
}
}
if (numberOfAudioChannels === 1) {
leftBuffers = mergeBuffers(leftBuffers, internalInterleavedLength);
if (desiredSampRate) {
leftBuffers = interpolateArray(leftBuffers, desiredSampRate, sampleRate);
}
}
// set sample rate as desired sample rate
if (desiredSampRate) {
sampleRate = desiredSampRate;
}
// for changing the sampling rate, reference:
// http://stackoverflow.com/a/28977136/552182
function interpolateArray(data, newSampleRate, oldSampleRate){
var fitCount = Math.round(data.length * (newSampleRate / oldSampleRate));
//var newData = new Array();
var newData = [];
//var springFactor = new Number((data.length - 1) / (fitCount - 1));
var springFactor = Number((data.length - 1) / (fitCount - 1));
newData[0] = data[0]; // for new allocation
for (var i = 1; i < fitCount - 1; i++) {
var tmp = i * springFactor;
//var before = new Number(Math.floor(tmp)).toFixed();
//var after = new Number(Math.ceil(tmp)).toFixed();
var before = Number(Math.floor(tmp)).toFixed();
var after = Number(Math.ceil(tmp)).toFixed();
var atPoint = tmp - before;
newData[i] = linearInterpolate(data[before], data[after], atPoint);
}
newData[fitCount - 1] = data[data.length - 1]; // for new allocation
return newData;
}
function linearInterpolate(before, after, atPoint){
return before + (after - before) * atPoint;
}
function mergeBuffers(channelBuffer, rLength){
var result = new Float64Array(rLength);
var offset = 0;
var lng = channelBuffer.length;
for (var i = 0; i < lng; i++) {
var buffer = channelBuffer[i];
result.set(buffer, offset);
offset += buffer.length;
}
return result;
}
function interleave(leftChannel, rightChannel){
var length = leftChannel.length + rightChannel.length;
var result = new Float64Array(length);
var inputIndex = 0;
for (var index = 0; index < length;) {
result[index++] = leftChannel[inputIndex];
result[index++] = rightChannel[inputIndex];
inputIndex++;
}
return result;
}
function writeUTFBytes(view, offset, string){
var lng = string.length;
for (var i = 0; i < lng; i++) {
view.setUint8(offset + i, string.charCodeAt(i));
}
}
// interleave both channels together
var interleaved;
if (numberOfAudioChannels === 2) {
interleaved = interleave(leftBuffers, rightBuffers);
}
if (numberOfAudioChannels === 1) {
interleaved = leftBuffers;
}
var interleavedLength = interleaved.length;
// create wav file
var resultingBufferLength = 44 + interleavedLength * 2;
var buffer = new ArrayBuffer(resultingBufferLength);
var view = new DataView(buffer);
// RIFF chunk descriptor/identifier
writeUTFBytes(view, 0, 'RIFF');
// RIFF chunk length
view.setUint32(4, 44 + interleavedLength * 2, true);
// RIFF type
writeUTFBytes(view, 8, 'WAVE');
// format chunk identifier
// FMT sub-chunk
writeUTFBytes(view, 12, 'fmt ');
// format chunk length
view.setUint32(16, 16, true);
// sample format (raw)
view.setUint16(20, 1, true);
// stereo (2 channels)
view.setUint16(22, numberOfAudioChannels, true);
// sample rate
view.setUint32(24, sampleRate, true);
// byte rate (sample rate * block align)
view.setUint32(28, sampleRate * 2, true);
// block align (channel count * bytes per sample)
view.setUint16(32, numberOfAudioChannels * 2, true);
// bits per sample
view.setUint16(34, 16, true);
// data sub-chunk
// data chunk identifier
writeUTFBytes(view, 36, 'data');
// data chunk length
view.setUint32(40, interleavedLength * 2, true);
// write the PCM samples
var lng = interleavedLength;
var index = 44;
var volume = 1;
for (var i = 0; i < lng; i++) {
view.setInt16(index, interleaved[i] * (0x7FFF * volume), true);
index += 2;
}
if (cb) {
return cb({
buffer: buffer,
view: view
});
}
postMessage({
buffer: buffer,
view: view
});
}
var webWorker = this.processInWebWorker(mergeAudioBuffers);
webWorker.onmessage = function(event) {
callback(event.data.buffer, event.data.view);
// release memory
URL.revokeObjectURL(webWorker.workerURL);
};
webWorker.postMessage(config);
}
this.processInWebWorker = (_function) => {
var workerURL = URL.createObjectURL(new Blob([_function.toString(),
';this.onmessage = function (e) {' + _function.name + '(e.data);}'
], {
type: 'application/javascript'
}));
var worker = new Worker(workerURL);
worker.workerURL = workerURL;
return worker;
}
}// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
Circle animation behind the voice button
*/
function CircleViz(containerEl, onNoMicAccess){
this.containerEl = containerEl;
this.stopped = true;
this.circle = document.createElement('div');
this.circle.classList.add('circle');
this.containerEl.appendChild(this.circle);
this.isMobile = window.mobileAndTabletcheck();
this.onNoMicAccess = onNoMicAccess;
this.attachedUserMediaStream = false;
// create one audio context
var AudioContext = window.AudioContext || window.webkitAudioContext;
if(typeof AudioContext !== 'undefined'){
this.audioContext = new AudioContext();
}
this.attachUserMediaStream = () => {
// if it has been stopped, let's open it back up
if(!this.attachedUserMediaStream || (this.audioStream && !this.audioStream.active)){
if(this.isMobile){
this.doDraw();
}else{
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia){
navigator.mediaDevices
.getUserMedia({audio:true})
.then(this.soundAllowed.bind(this))
.catch(this.soundNotAllowed.bind(this));
}else{ // deprecated approach: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getUserMedia
navigator.getUserMedia = navigator['getUserMedia'] || navigator['webkitGetUserMedia'] || navigator['mozGetUserMedia'] || null;
if (navigator.getUserMedia) navigator.getUserMedia({ audio: true }, (stream) => this.soundAllowed(stream), (e) => this.soundNotAllowed(e));
}
}
this.attachedUserMediaStream = true;
}
}
this.soundNotAllowed = (e) => {
// circle viz graceful degrade, but let know main component know
this.onNoMicAccess();
}
this.soundAllowed = (stream) => {
this.audioStream = stream;
if(this.audioContext){
var audioStream = this.audioContext.createMediaStreamSource(stream);
this.analyser = this.audioContext.createAnalyser();
audioStream.connect(this.analyser);
this.analyser.fftSize = 1024;
this.frequencyArray = new Uint8Array(this.analyser.frequencyBinCount);
}
this.doDraw();
}
this.getCircleScale = () => {
let scale = 1;
if(this.analyser){
this.analyser.getByteFrequencyData(this.frequencyArray);
// todo: frequency array is all zeros for android chrome
let freqSum = 0;
for (let i = 0; i < 255; i++) {
freqSum += this.frequencyArray[i]
}
let freqAvg = freqSum / 255
scale = 1 + ((freqAvg / 255) * 1.2)
}else{
// don't have FFT spectrum, so fake it
let currScale = this.getCurrentScale();
if(currScale){
var rand = 1 + Math.random();
// smooth it
scale = currScale + ((rand - currScale) / 8);
}
}
return scale;
}
this.doDraw = () => {
if(!this.stopped){
let newScale = this.getCircleScale();
this.circle.style.transform = 'scale(' + newScale + ') translate(2px, 2px)';
}else{
// stopped, slowly scale down
let currScale = this.getCurrentScale();
if(currScale){
if(currScale > 0){
currScale = Math.max(0, currScale - 0.05);
this.circle.style.transform = 'scale(' + currScale + ') translate(2px, 2px)';
}
}
}
this.animation = window.requestAnimationFrame(this.doDraw.bind(this));
}
this.getCurrentScale = () => {
let transformProp = (this.circle.style.transform && this.circle.style.transform !== '') ? this.circle.style.transform : 'scale(1)'
let splitProperty = transformProp.split(new RegExp(/\(|\)/, 'g'))
if(splitProperty.length > 2){
let currScale = parseFloat(splitProperty[1]);
return currScale;
}
return null;
}
this.startAnimation = () => {
if(this.stopped){
this.containerEl.style.opacity = 1;
this.stopped = false;
this.attachUserMediaStream();
}
}
this.stopAnimation = () => {
this.stopped = true;
this.containerEl.style.opacity = 0;
if(this.audioStream && this.audioStream.getAudioTracks().length > 0){
this.audioStream.getAudioTracks()[0].stop();
}
}
}window.componentTranslations = {
"micNotAllowed": {
"af": "Kan nie toegang tot mikrofoon hê nie",
"sq": "Nuk mund të hyni në mikrofon",
"am": "ማይክሮፎን መድረስ አልተቻለም",
"ar": "لا يمكن الدخول إلى الميكروفون",
"hy": "Հնարավոր չէ մուտք գործել խոսափող",
"az": "Mikrofona daxil ola bilmir",
"eu": "Ezin da mikrofonoa atzitu",
"be": "Не ўдаецца атрымаць доступ да мікрафону",
"bs": "Ne mogu pristupiti mikrofonu",
"bg": "Няма достъп до микрофона",
"ca": "No es pot accedir al micròfon",
"ceb": "Dili maka-access sa mikropono",
"ny": "Sangathe kulumikiza maikolofoni",
"zh": "无法访问麦克风",
"zh-TW": "無法訪問麥克風",
"co": "Ùn pudete micca accede à u micrufonu",
"hr": "Nije moguće pristupiti mikrofonu",
"cs": "Nemá přístup k mikrofonu",
"da": "Kan ikke få adgang til mikrofonen",
"nl": "Kan geen microfoon openen",
"en": "Cannot access microphone",
"eo": "Ne povas aliri mikrofonon",
"et": "Ei saa mikrofoni juurde pääseda",
"tl": "Hindi ma-access ang mikropono",
"fi": "Mikrofonia ei voi käyttää",
"fr": "Impossible d'accéder au microphone",
"fy": "Kin gjin mikrofoan tagong krije",
"gl": "Non se pode acceder ao micrófono",
"ka": "მიკროფონი ვერ წვდომა",
"de": "Kann nicht auf Mikrofon zugreifen",
"el": "Δεν είναι δυνατή η πρόσβαση στο μικρόφωνο",
"gu": "માઇક્રોફોનને ઍક્સેસ કરી શકતા નથી",
"ht": "Pa ka jwenn mikwofòn",
"ha": "Ba za a iya shiga makirufo ba",
"haw": "ʻAʻole hiki ke hoʻokomo i ka microphone",
"iw": "לא ניתן לגשת למיקרופון",
"hi": "माइक्रोफ़ोन तक नहीं पहुंच सकते",
"hmn": "Mus saib tsis tau microphone",
"hu": "Nem lehet hozzáférni a mikrofonhoz",
"is": "Get ekki fengið aðgang að hljóðnemanum",
"ig": "Enweghị ike ịnweta igwe okwu",
"id": "Tidak dapat mengakses mikrofon",
"ga": "Ní féidir rochtain a fháil ar mhicreafón",
"it": "Impossibile accedere al microfono",
"ja": "マイクにアクセスできない",
"jw": "Ora bisa ngakses mikropon",
"kn": "ಮೈಕ್ರೊಫೋನ್ ಪ್ರವೇಶಿಸಲು ಸಾಧ್ಯವಿಲ್ಲ",
"kk": "Микрофонға кіру мүмкін емес",
"km": "មិនអាចចូលដំណើរការមីក្រូហ្វូន",
"ko": "마이크에 액세스 할 수 없습니다.",
"ku": "Mirov dikare mîkrofon nikare",
"ky": "коелу кире албайт",
"lo": "ບໍ່ສາມາດເຂົ້າເຖິງໄມໂຄໂຟນ",
"la": "Access potest non tortor ligula, facilisis",
"lv": "Nevar piekļūt mikrofonam",
"lt": "Negalite pasiekti mikrofono",
"lb": "Kann net op den Mikrofon goen",
"mk": "Не можам да пристапам на микрофон",
"mg": "Tsy afaka mikitika mikrôfo",
"ms": "Tidak boleh mengakses mikrofon",
"ml": "മൈക്രോഫോൺ ആക്സസ്സുചെയ്യാനാവില്ല",
"mt": "Ma tistax taċċessa mikrofonu",
"mi": "Kaore e taea te uru ki te hopuorooro",
"mn": "Микрофон руу нэвтэрч чадахгүй байна",
"my": "မိုက်ခရိုဖုန်းကိုရယူမနိုင်",
"no": "Kan ikke få tilgang til mikrofonen",
"ps": "مایکروفون ته لاسرسی نشي کولی",
"fa": "می توانید به میکروفون دسترسی پیدا کنید",
"pl": "Nie można uzyskać dostępu do mikrofonu",
"pt": "Não é possível acessar o microfone",
"pa": "ਮਾਈਕ੍ਰੋਫੋਨ ਤੱਕ ਪਹੁੰਚ ਨਹੀਂ ਕਰ ਸਕਦਾ",
"ro": "Nu puteți accesa microfonul",
"ru": "Не удается получить доступ к микрофону",
"sm": "E le mafai ona maua le masini faaleotele leo",
"gd": "Cha ghabh mi lorg air microfòn",
"sr": "Не могу приступити микрофону",
"st": "Ha e khone ho finyella microphone",
"sn": "Haikwanise kuwana maikorofoni",
"sd": "مائڪروفون تائين رسائي نه ٿي سگھي",
"si": "මයික්රෆෝනය වෙත පිවිසිය නොහැක",
"sk": "Nemáte prístup k mikrofónu",
"sl": "Ne morem dostopati do mikrofona",
"so": "Ma heli kartid makarafoon",
"es": "No se puede acceder al micrófono",
"su": "moal bisa ngakses mikropon",
"sw": "Haiwezi kufikia kipaza sauti",
"sv": "Kan inte komma åt mikrofonen",
"tg": "Микрофонро ба кор андохта намешавад",
"ta": "மைக்ரோஃபோனை அணுக முடியவில்லை",
"te": "మైక్రోఫోన్ను ప్రాప్యత చేయలేరు",
"th": "ไม่สามารถเข้าถึงไมโครโฟนได้",
"tr": "Mikrofona erişilemiyor",
"uk": "Не вдається отримати доступ до мікрофона",
"ur": "مائیکروفون تک رسائی حاصل نہیں",
"uz": "Mikrofonga kirib bo'lmadi",
"vi": "Không thể truy cập micrô",
"cy": "Methu â chael mynediad i feicroffon",
"xh": "Ayikwazi ukufikelela kwimakrofoni"
}
}
/*
Note: deprecation warning for styling master document doesn't apply within <template> tags
Read more: https://www.polymer-project.org/blog/2017-10-18-upcoming-changes
*/
/*
FIXME(polymer-modulizer): the above comments were extracted
from HTML and may be out of place here. Review them and
then delete this comment!
*/
const ListeningState = Object.freeze({
IDLE: 'idle',
LISTENING: 'listening',
USER_INPUT: 'user-input',
DISABLED: 'disabled'
});
const KeyboardTriggers = Object.freeze({
ALL_KEYS: 'all-keys',
SPACE_BAR: 'space-bar',
NONE: 'none'
})
// maintain one audio stream, immediately, to work in safari
// see here: https://forums.developer.apple.com/thread/86286
var audioStream;
class VoiceButton extends PolymerElement {
static get is() {
return 'voice-button';
}
static get properties(){
return {
flat: {
type: Boolean,
reflectToAttribute: true,
value: false
},
autodetect: {
type: Boolean,
notify: false,
reflectToAttribute: true,
value: false
},
clickForPermission: {
type: Boolean,
notify: false,
reflectToAttribute: true,
value: false
},
hidePermissionText: {
type: Boolean,
notify: false,
reflectToAttribute: true,
value: false
},
language: {
type: String,
reflectToAttribute: true,
value: 'en-US'
},
disabled: {
type: Boolean,
notify: true,
reflectToAttribute: true,
value: false,
observer: '_onEnabledStateChange'
},
keyboardTrigger: {
type: String,
reflectToAttribute: true,
value: KeyboardTriggers.SPACE_BAR
},
pressed: {
type: Boolean,
notify: false,
reflectToAttribute: true,
readOnly: true,
value: false
},
cloudSpeechApiKey: {
type:String,
readOnly: false,
reflectToAttribute: false,
value: null
},
state: {
type: String,
notify: false,
readOnly: true,
reflectToAttribute: true,
value: ListeningState.IDLE,
observer: '_onStateChange'
},
supported: {
type: Boolean,
readOnly: true,
reflectToAttribute: true,
value: true
},
flatStr: String,
autoDetectStr: String
}
}
constructor() {
super();
this._setState(ListeningState.IDLE)
}
ready() {
super.ready();
this.useCloudSpeech = false;
// warn host if a cloud speech api isn't provided
if(typeof this.cloudSpeechApiKey !== 'string'){
console.warn('For fallback support, it is recommended to provide a cloud-speech-api-key.')
}
// Get the SpeechRecognition object
// only allow prefixed webkitSpeechRecognition for Chrome, Opera has support but doesn't work
// As test: https://www.google.com/intl/en/chrome/demos/speech.html
// Support table: https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition#Browser_compatibility
// Opera 8.0+
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
const SpeechRecognition = window['webkitSpeechRecognition'];
if(typeof SpeechRecognition === 'undefined' || isOpera){
// make sure you have a Cloud Speech API key
if(typeof this.cloudSpeechApiKey !== 'string'){
console.error('Need to provide a cloud-speech-api-key.')