-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspreek.html
258 lines (192 loc) · 7.7 KB
/
spreek.html
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
<!doctype html>
<html>
<head>
<style type="text/css">
body > div{
text-align: center;
}
textarea{
width:80%;
height:12rem;
}
</style>
</head>
<body>
<div>
<textarea></textarea>
<a href="javascript:leesVoor()">Spreek</a>
</div>
<script>
var recognition = null,
recognizedText = [],
current = 0,
running = false,
recognizedText2 = ''
function canvasBlack(){
var cnvs = document.querySelector( 'canvas' ),
ctx = cnvs.getContext( '2d' )
ctx.fillStyle = 'lime'
ctx.fillRect( 0, 0, cnvs.width, cnvs.height)
}
function recognize(){
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition,
//SpeechRecognitionResultList = SpeechRecognitionResultList || webkitSpeechRecognitionResultList,
SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
//SpeechRecognitionResultList.prototype.forEach = Array.prototype.forEach
running = true
recognition = new SpeechRecognition();
recognition.lang = 'en-GB';
var grammar = '#JSGF V1.0; grammar colors; public <color> = aqua | azure | beige | bisque | black | blue | brown | chocolate | coral | crimson | cyan | fuchsia | ghostwhite | gold | goldenrod | gray | green | indigo | ivory | khaki | lavender | lime | linen | magenta | maroon | moccasin | navy | olive | orange | orchid | peru | pink | plum | purple | red | salmon | sienna | silver | snow | tan | teal | thistle | tomato | turquoise | violet | white | yellow ;'
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
recognition.continuous = true
recognition.interimResults = true
recognition.maxAlternatives = 2
recognition.onend = function(){
if( running )
recognition.start()
}
recognition.onresult = function(event) {
if( ! recognizedText[ current ] ){
recognizedText.push('')
}
var color = event.results;
//console.log( color, event.results )
//console.log('Confidence: ' + event.results[0][0].confidence);
recognizedText[current] = ''
color.forEach = Array.prototype.forEach
color.forEach( ( result ) => {
recognizedText[ current ] += result[ 0 ].transcript
})
}
recognition.onspeechend = function( event ) {
current++
recognition.stop()
}
recognition.start();
}
function stopListening(){
recognition.stop();
running = false
current = 0
setTimeout( () => {
var p = document.createElement('p')
p.innerText = recognizedText.join( ' ' )
document.body.appendChild( p )
recognizedText = []
}, 1000)
}
function getVoices(){
return new Promise( ( resolve, reject ) => {
function _getVoices(){
return window.speechSynthesis.getVoices()
}
var voices = _getVoices()
if( typeof voices == 'undefined' || voices.length == 0){
var timer = setInterval( () => {
voices = _getVoices()
if( voices.length > 0){
clearInterval( timer )
resolve( voices )
}
}, 1000)
}
else
resolve( voices )
})
}
function speak (message, volume, male) {
//msg.pitch = 1/volume
//msg.pitch = 5
//msg.voice = new SpeechSynthesisVoice()
getVoices()
.catch( ( e ) => {
console.log( e )
})
.then( ( voices ) => {
console.log( voices )
var msg = new SpeechSynthesisUtterance(message)
if(volume)
msg.rate = 1/volume
msg.lang = 'en-GB'
msg.lang = 'nl-NL'
if( male )
msg.voice = voices[ 16 ]
window.speechSynthesis.speak(msg)
})
}
function leesVoor(){
var text = document.querySelector('textarea') ? document.querySelector('textarea').value : ''
speak(text)
}
function spreek(){
if( recognizedText != ''){
speak( recognizedText, 1, false )
return
}
speak( 'Hoi, hoe gaat het?', 1, true );
return;
speak( 'You are not your brain', 1, true )
return
speak( 'hello Joey.', 0.5, false )
speak( 'Ha, ha, ha, ha, mmmmmmm', 1, false )
return
setTimeout( () => {
speak( 'I repeat: HELLO Joey', 3, false )
}, 5000)
speak( `The Great Google Revolt - The New York Timeswww.nytimes.com › interactive › 2020/02/18 › magazine
Feb 18, 2020 - Rebecca Rivers, a Google engineer and activist, was fired by the company in November. Bobby Doherty for The New York Times ...`, 1, true)
setTimeout( () => {
speak( 'A cookie associated with a cross-site resource at http://youtube.com/ was set without the "SameSite" attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with "SameSite=None" and Secure. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.', 1, true )
}, 8000)
return
}
</script>
<script>
var recorder = null,
audio = document.querySelector( 'audio' ),
video = document.querySelector( 'video' ),
what = document.querySelector( 'select' ) ? document.querySelector( 'select' ).value : '',
chunks = [],
audioStream = null,
videoStream = null
function start(){
navigator.mediaDevices.getUserMedia( { audio: true } )
.then( (stream) => {
canvasBlack()
audioStream = stream
videoStream = document.querySelector( 'canvas' ).captureStream( 100 )
if( what == 'audio')
recorder = new MediaRecorder( stream )
else
recorder = new MediaRecorder( new MediaStream( [ videoStream.getVideoTracks()[0], stream.getAudioTracks()[0] ]), {mimeType: 'video/webm'} )
if( what != 'audio')
setInterval( () => {
canvasBlack()
}, 5)
recorder.ondataavailable = (event) => {
if (event.data && event.data.size > 0) {
chunks.push(event.data);
}
}
recorder.onstop = evt => {
let blob = new Blob(chunks, { type: 'video/webm' });
if( what == 'audio')
audio.src = URL.createObjectURL(blob);
else{
video.src = URL.createObjectURL(blob);
video.play()
}
};
recorder.start( 100 )
})
}
function stop(){
recorder.stop()
audioStream.getAudioTracks()[0].stop()
videoStream.getVideoTracks()[0].stop()
}
</script>
</body>
</html>