-
Notifications
You must be signed in to change notification settings - Fork 5
/
mic.js
257 lines (217 loc) · 7.21 KB
/
mic.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
import { PorcupineWorkerFactory } from "@picovoice/porcupine-web-en-worker";
import { WebVoiceProcessor } from "@picovoice/web-voice-processor";
import { browser } from "webextension-polyfill-ts";
import { GOOGLE_SEARCH_LANGUAGES } from "./google_search_languages.js";
import {
ACCESS_KEY,
OK_GOOGLE,
HEY_GOOGLE,
PICOVOICE,
PORCUPINE
} from "./constants.js";
for (const [language, value] of Object.entries(GOOGLE_SEARCH_LANGUAGES)) {
const optGroup = document.createElement("optgroup");
optGroup.label = language; // e.g. "English"
for (const [country, code] of value) {
const option = document.createElement("option");
option.value = code;
option.label = `${country} (${code})` ; // e.g. "USA (en-US)"
optGroup.appendChild(option);
}
document.getElementById("select-search-language").appendChild(optGroup);
}
const DEFAULT_SENSITIVITY = 50; // Sensitivity meter ranges from 0 to 100
const DEFAULT_WAKE_WORD = "Picovoice";
const DEFAULT_GOOGLE_VOICE_SEARCH_LANGUAGE = "en-US";
const STRINGS_READY = "Porcupine is ready and listening for the wake word.";
const STRINGS_INITIALIZING = "Initializing Porcupine...";
const KEYWORD_MAP = {
"Ok Google": OK_GOOGLE,
"Hey Google": HEY_GOOGLE,
"Picovoice": PICOVOICE,
"Porcupine": PORCUPINE
}
const chime = new Audio(browser.runtime.getURL("audio/click.ogg"));
let ppnStatus = "unknown";
async function init() {
await initializeUiValues();
await startPorcupine();
}
/**
* Set the values on the UI from data from browser storage,
* instead of from HTML.
*/
async function initializeUiValues() {
const { wakeWord, sensitivity, searchLanguage } =
await getOptionsFromStorage();
document.getElementById("select-wake-word").value = wakeWord;
document.getElementById("input-sensitivity").value = sensitivity;
document.getElementById("select-search-language").value = searchLanguage;
}
/**
* Options are peristed to browser storage. If they've never been set,
* give them defaults and set them.
*/
async function getOptionsFromStorage() {
const data1 = await browser.storage.local.get("wakeWord");
let wakeWord;
if (data1.wakeWord === undefined || KEYWORD_MAP[data1.wakeWord] === undefined) {
await browser.storage.local.set({ wakeWord: DEFAULT_WAKE_WORD });
wakeWord = DEFAULT_WAKE_WORD;
} else {
wakeWord = data1.wakeWord;
}
const data2 = await browser.storage.local.get("sensitivity");
let sensitivity;
if (data2.sensitivity === undefined) {
sensitivity = DEFAULT_SENSITIVITY;
await browser.storage.local.set({ sensitivity: DEFAULT_SENSITIVITY });
} else {
sensitivity = data2.sensitivity;
}
const data3 = await browser.storage.local.get("searchLanguage");
let searchLanguage;
if (data3.searchLanguage === undefined) {
searchLanguage = DEFAULT_GOOGLE_VOICE_SEARCH_LANGUAGE;
await browser.storage.local.set({
searchLanguage: DEFAULT_GOOGLE_VOICE_SEARCH_LANGUAGE,
});
} else {
searchLanguage = data3.searchLanguage;
}
return { wakeWord, sensitivity, searchLanguage };
}
// Options
document.getElementById("select-wake-word").onchange = async (event) => {
const newWakeWord = event.target.value;
await browser.storage.local.set({ wakeWord: newWakeWord });
await startPorcupine();
};
document.getElementById("input-sensitivity").onchange = async (event) => {
const newSensitivity = event.target.value;
await browser.storage.local.set({ sensitivity: newSensitivity });
await startPorcupine();
};
document.getElementById("select-search-language").onchange = async (event) => {
const newSearchLanguage = event.target.value;
await browser.storage.local.set({ searchLanguage: newSearchLanguage });
await startPorcupine();
};
function setPpnStatus(status, message) {
let alertElement = document.getElementById("alert-status");
switch (status) {
case "error":
alertElement.innerHTML = "Error: " + message;
alertElement.className = "alert alert-danger";
document.getElementById("input-sensitivity").disabled = true;
document.getElementById("select-wake-word").disabled = true;
break;
case "init":
alertElement.className = "alert alert-info";
alertElement.innerHTML = STRINGS_INITIALIZING;
document.getElementById("input-sensitivity").disabled = true;
document.getElementById("select-wake-word").disabled = true;
break;
case "ready":
alertElement.className = "alert alert-success";
alertElement.innerHTML = STRINGS_READY;
document.getElementById("input-sensitivity").disabled = false;
document.getElementById("select-wake-word").disabled = false;
document.getElementById("select-search-language").disabled = false;
break;
case "keyword":
alertElement.className = "alert alert-warning";
alertElement.innerHTML = message ?? "[Say your search query]";
break;
}
ppnStatus = status;
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
let ppnWorker = null, webVp = null;
async function startPorcupine() {
const { wakeWord, sensitivity, searchLanguage } =
await getOptionsFromStorage();
const sensitivityNormalized = sensitivity / 100.0;
setPpnStatus("init");
console.log(wakeWord)
if (ppnWorker !== null) {
ppnWorker.terminate();
ppnWorker = null;
}
if (webVp !== null) {
webVp.release();
webVp = null;
}
try {
ppnWorker = await PorcupineWorkerFactory.create(
ACCESS_KEY,
{
custom: wakeWord,
sensitivity: sensitivityNormalized,
base64: KEYWORD_MAP[wakeWord]
}
);
} catch (error) {
setPpnStatus("error", error);
console.error(error);
return;
}
// Add a very small delay; initialization is async, but it's typically quite fast,
// making the GUI confusing and appear glitchy.
await sleep(500);
console.log("Porcupine is ready!");
setPpnStatus("ready");
browser.runtime.sendMessage({
command: "ready",
});
try {
webVp = await WebVoiceProcessor.init({
engines: [ppnWorker],
start: true,
});
} catch (error) {
setPpnStatus("error", error);
browser.runtime.sendMessage({
command: "error",
});
console.error(error);
}
ppnWorker.onmessage = (messageEvent) => {
if (messageEvent.data.command === "ppn-keyword") {
// If already in keyword state, ignore until transcription is finished
if (ppnStatus === "keyword") {
return;
}
chime.play();
setPpnStatus("keyword");
browser.runtime.sendMessage({ ...messageEvent.data });
const speech = new webkitSpeechRecognition();
let transcript;
speech.lang = searchLanguage;
speech.interimResults = true;
speech.continuous = false;
speech.onend = (event) => {
setPpnStatus("ready");
browser.runtime.sendMessage({
command: "wsr-onend",
transcript: transcript,
});
};
speech.onresult = (event) => {
transcript = "";
for (const line of event.results) {
transcript += line[0].transcript.trim() + " ";
}
setPpnStatus("keyword", transcript);
browser.runtime.sendMessage({
command: "wsr-onresult",
transcript: transcript.trim(),
});
};
speech.start();
}
};
}
init();