Skip to content

Commit 49dac3d

Browse files
Made speech recognition work
1 parent 3a582ff commit 49dac3d

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules/
33
*.log
44
haters/
55
.idea/
6+
package-lock.json

20 - Speech Detection/index-START.html

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,88 @@
1010
<div class="words" contenteditable>
1111
</div>
1212

13+
<button class="start-button">Start</button>
14+
1315
<script>
16+
// We grab the SpeechRecognition object from the window
17+
// We use the webkit prefix for any browsers that don't support the unprefixed version
1418
window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
1519

20+
// We create a new instance of SpeechRecognition
21+
const recognition = new SpeechRecognition();
22+
23+
// If we want to get continues results while we are speaking, we set the continuous property to true
24+
// Otherswise, we will only get a result when we stop speaking
25+
recognition.interimResults = true;
26+
27+
let stopped = false;
28+
29+
// Create a new paragraph element and add it to the '.words' div
30+
let p = document.createElement('p');
31+
const words = document.querySelector('.words');
32+
words.appendChild(p);
33+
34+
// We listen for the 'result' event on the recognition object
35+
// Which is fired when we get a result from the speech recognition
36+
// The event object contains a results property
37+
// Which is an array of all the results we have received so far
38+
recognition.addEventListener( 'result', event => {
39+
const transcript = Array.from( event.results )
40+
.map( result => result[0] )
41+
.map( result => result.transcript )
42+
.join('');
43+
44+
// We set the text content of the paragraph to the transcript
45+
p.textContent = transcript;
46+
47+
// If the result is final, we create a new paragraph element and add it to the '.words' div
48+
// Final means that the speech recognition has finished processing the audio and has a final result for us
49+
if ( event.results[0].isFinal ) {
50+
p = document.createElement('p');
51+
words.appendChild(p);
52+
}
53+
54+
// If the transcript includes the word 'stop', we set the stopped variable to true and stop the recognition
55+
if ( transcript.includes('stop') ) {
56+
stopped = true;
57+
recognition.stop();
58+
}
59+
60+
} );
61+
62+
// When we stop speaking, we want to start the recognition again so we can keep getting results
63+
// Otherwise, it will only work once and then stop
64+
recognition.addEventListener( 'end', () => {
65+
if ( false === stopped ) {
66+
recognition.start();
67+
}
68+
} );
69+
70+
// We grab the start button and listen for a click event on it
71+
const startButton = document.querySelector('.start-button');
72+
73+
// On click, we set the stopped variable to false and start the recognition again
74+
// We also disable the start button so we can't click it again while the recognition is running
75+
startButton.addEventListener( 'click', () => {
76+
stopped = false;
77+
recognition.start();
78+
startButton.disabled = true;
79+
} );
80+
81+
// We disable the start button when the recognition starts
82+
recognition.addEventListener( 'start', () => startButton.disabled = true );
83+
84+
// When the recognition ends, we check if the stopped variable is true
85+
// If it is, we enable the start button again so we can start the recognition again
86+
// So if the user has said 'stop', the recognition will stop and the start button will be enabled again
87+
recognition.addEventListener( 'end', () => {
88+
if ( true === stopped ) {
89+
startButton.disabled = false;
90+
}
91+
} );
92+
93+
// We start the recognition for the first time when the page loads
94+
recognition.start();
1695

1796
</script>
1897

0 commit comments

Comments
 (0)