-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (98 loc) · 2.84 KB
/
index.js
File metadata and controls
107 lines (98 loc) · 2.84 KB
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
let listening
let listeningToggle = false
//Set options
function setOptions(newOptions) {
// const window = window
//Call browser API for Web Speech
window.SpeechRecognition =
window.SpeechRecognition || window.webkitSpeechRecognition
listening = new SpeechRecognition()
//Options defaults
let options = {
persistentListening: false,
listenOnLoad: false,
language: 'en-US',
}
options = { ...options, ...newOptions }
if (options.persistentListening) {
listening.start()
listening.addEventListener('end', listening.start)
}
if (options.listenOnLoad) {
listening.start()
}
if (options.language) {
listening.lang = options.language
}
}
//Get all voice inputs
function results(callback) {
console.log(listening)
listening.onresult = function (e) {
const results = e.results[0]
const transcripts = results[0].transcript.split(' ')
return callback(transcripts)
}
}
//Start and stop listening using one or two buttons
function startStop(numberOfButtons, button) {
if (numberOfButtons === 1) {
listeningToggle = !listeningToggle
if (listeningToggle) {
listening.start()
listening.addEventListener('end', listening.start)
} else {
listening.removeEventListener('end', listening.start)
listening.stop()
}
} else if (numberOfButtons === 2) {
const buttonClicked = button
if (buttonClicked === 'start') {
listening.start()
listening.addEventListener('end', listening.start)
} else if (buttonClicked === 'stop') {
listening.removeEventListener('end', listening.start)
listening.stop()
}
} else {
console.log(
`Error startStopTrigger was passed more than two numberOfButtons`
)
}
}
//Get results that match word list
function wordList(words, withActions, callback) {
let actions = []
listening.onresult = function (e) {
const results = e.results[0]
const transcripts = results[0].transcript.split(' ')
if (transcripts.length > 0) {
//return a single word said after the actionWord
if (withActions) {
actions = words
for (const action of actions) {
if (action.actionWord.name === transcripts[0]) {
actions = action.actionWord
for (const word of actions.options) {
if (word.word === transcripts[1]) {
return callback(word.word)
}
}
}
}
}
//returns an array of all words that match the list of words
else {
let filteredTranscript = []
for (const word of words) {
filteredTranscript = [
...filteredTranscript,
...transcripts.filter((match) => match === word.word.name),
]
}
return callback(filteredTranscript)
}
}
}
}
module.exports = { setOptions, results, startStop, wordList }