forked from Picovoice/porcupine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoiceControlDemo.js
125 lines (113 loc) · 6.65 KB
/
voiceControlDemo.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
let VoiceControlDemo = (function(){
let keywordIDs = {
'ok lamp': new Uint8Array([
0xac, 0x24, 0x75, 0x21, 0x14, 0x3d, 0x2a, 0xe7, 0x0a, 0x85, 0x75, 0x4c,
0x48, 0x31, 0x5b, 0x44, 0x4b, 0xb6, 0xe8, 0xc3, 0x77, 0x30, 0xd5, 0xac,
0xca, 0x54, 0x06, 0x29, 0xbd, 0x15, 0xca, 0x90, 0x55, 0x81, 0xae, 0x21,
0x6a, 0x04, 0x1e, 0x5a, 0x9d, 0x64, 0x83, 0x0c, 0x04, 0x03, 0x6b, 0xe8,
0x22, 0x2e, 0x19, 0xbf, 0x7e, 0x2b, 0x4d, 0x8c, 0x50, 0x27, 0xb6, 0x11,
0xf3, 0x17, 0xc3, 0xf9, 0xe3, 0x69, 0x19, 0x26, 0xbe, 0x0d, 0xad, 0x78,
0x74, 0x61, 0x4b, 0xb8, 0xde, 0x83, 0x1c, 0xb9, 0xa1, 0x06, 0x27, 0x77,
0x03, 0xb2, 0x24, 0x82]),
'yellow': new Uint8Array([
0x6f, 0x7d, 0x2c, 0x10, 0xcb, 0xf0, 0x76, 0x1b, 0xee, 0xeb, 0x80, 0xaa,
0xe1, 0xa5, 0xfa, 0x53, 0xe3, 0xce, 0x4b, 0x59, 0x7d, 0xeb, 0xe3, 0x11,
0x13, 0x87, 0x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e1, 0x7c, 0x5e, 0xd8,
0x08, 0x3a, 0xbf, 0x9f, 0x7f, 0xe7, 0x79, 0x13, 0xed, 0xbc, 0xa3, 0x0b,
0xf4, 0xd2, 0xfc, 0xf2]),
'navy blue': new Uint8Array([
0xd9, 0x36, 0xb2, 0xcc, 0x5d, 0xbb, 0x2b, 0x66, 0xae, 0xbb, 0x39, 0xf3,
0x24, 0xf4, 0x02, 0xf2, 0xb9, 0x5a, 0xf7, 0xd7, 0x8d, 0x02, 0xbc, 0x7b,
0xa3, 0x04, 0xb3, 0xfd, 0x2c, 0x0b, 0x9c, 0x10, 0x2c, 0x28, 0x6f, 0x65,
0x3f, 0xb9, 0x39, 0x08, 0x44, 0x62, 0x47, 0x3a, 0xd8, 0x6d, 0xe7, 0x4a,
0xd9, 0x64, 0x50, 0x6b, 0xd0, 0x39, 0x7e, 0x43, 0x05, 0xeb, 0xf8, 0xc2,
0xe7, 0xab, 0xf5, 0x39, 0x88, 0xd4, 0x99, 0x3c, 0x2d, 0x2a, 0xf2, 0xeb,
0x69, 0x5b, 0x34, 0x7b, 0x51, 0x61, 0x83, 0x82, 0x08, 0x76, 0xdf, 0x86,
0xab, 0xe8, 0x83, 0x48]),
'white': new Uint8Array([
0x5e, 0x52, 0xf1, 0xab, 0x90, 0xae, 0x36, 0x5f, 0x98, 0xd2, 0x96, 0xba,
0xd8, 0x13, 0x16, 0xf1, 0x66, 0x73, 0xa5, 0xbb, 0x95, 0x0d, 0xfa, 0x9e,
0x47, 0x05, 0xc7, 0x3d, 0x36, 0xaf, 0x97, 0x14, 0x98, 0xbc, 0x15, 0xfb,
0xa0, 0xdc, 0xb1, 0x60, 0xa2, 0x60, 0x1d, 0x0a, 0x66, 0x5f, 0x47, 0xb8,
0xfd, 0x71, 0x3b, 0xe3, 0x06, 0x8d, 0xf4, 0xbd, 0xc6, 0x02, 0xbe, 0x47,
0xde, 0x7f, 0xa4, 0x12, 0x9f, 0x07, 0x93, 0xc4, 0xc7, 0x97, 0xd8, 0x9a]),
};
let sensitivities = new Float32Array([0.5, 1, 1, 1, 1, 1]);
let keywordNames = Object.keys(keywordIDs);
let currentTimeSeconds = function() { return new Date().getTime() / 1000 };
let is_listening = false;
let listeningStartSeconds;
let processCallback = function(keywordIndex) {
if (keywordIndex === -1) {
if (is_listening && (currentTimeSeconds() - listeningStartSeconds) > 5) {
document.querySelector("#light_bulb").setAttribute("src", "light_bulb_blue.svg");
is_listening = false;
}
return;
}
let keyword = keywordNames[keywordIndex];
if (is_listening) {
if (keyword === "yellow") {
document.querySelector("#light_bulb").setAttribute("style", "background-color:yellow")
}
if (keyword === "orange") {
document.querySelector("#light_bulb").setAttribute("style", "background-color:orange")
}
if (keyword === "purple") {
document.querySelector("#light_bulb").setAttribute("style", "background-color:purple")
}
if (keyword === "navy blue") {
document.querySelector("#light_bulb").setAttribute("style", "background-color:blue")
}
if (keyword === "white") {
document.querySelector("#light_bulb").setAttribute("style", "background-color:white")
}
document.querySelector("#light_bulb").setAttribute("src", "light_bulb_blue.svg");
is_listening = false;
}
if (keyword === "ok lamp") {
document.querySelector("#light_bulb").setAttribute("src", "light_bulb_red.svg");
is_listening = true;
listeningStartSeconds = currentTimeSeconds();
}
};
let audioManager;
let audioManagerErrorCallback = function(ex) {
alert(ex.toString());
document.querySelector("#light_bulb").setAttribute("style", "background-color:white");
document.querySelector("#light_bulb").setAttribute("src", "light_bulb_blue.svg");
document.querySelector("#demo_button").setAttribute("onclick", "VoiceControlDemo.start()");
document.querySelector("#demo_button").innerText = "Start Demo";
};
let start = function() {
audioManager = new PicovoiceAudioManager();
audioManager.start(Porcupine.create(Object.values(keywordIDs), sensitivities), processCallback, audioManagerErrorCallback);
document.querySelector("#demo_button").setAttribute("onclick", "VoiceControlDemo.stop()");
document.querySelector("#demo_button").innerText = "Stop Demo";
};
let stop = function() {
audioManager.stop();
document.querySelector("#light_bulb").setAttribute("style", "background-color:white");
document.querySelector("#light_bulb").setAttribute("src", "light_bulb_blue.svg");
document.querySelector("#demo_button").setAttribute("onclick", "VoiceControlDemo.start()");
document.querySelector("#demo_button").innerText = "Start Demo";
};
return {start: start, stop: stop}
})();