forked from mozilla/vaani.client
/
index.js
164 lines (135 loc) · 4.27 KB
/
index.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
"use strict";
var Wakeword = require('wakeword');
var MemoryStream = require('memorystream');
var WebSocket = require('ws');
const child_process = require('child_process');
const fs = require('fs');
const MAX_LISTEN_TIME = 7500;
const VAD_BYTES = 640;
const MAX_SIL_TIME = 2500;
const url = require('url');
const ssldir = './resources/ssl/';
var ws;
var streamServer = new MemoryStream();
const call = (command, params) => child_process.spawn(command, params.split(' '));
const callSync = (command, params) => child_process.spawnSync(command, params.split(' '));
var lastvadStatus = 0;
var dtStartSilence, totalSilencetime;
const config = JSON.parse(process.env.VAANI_CONFIG || fs.readFileSync("config.json"));
// If an environment variable defines the oauth token, use that one
// instead of the one in the config file. The vaani.setup server sets this
// environment variable in /lib/systemd/system/vaani.service.d/evernote.conf.
// When vaani.setup starts us with systemd, we'll get the user's current
// oauth token that way.
if (process.env.EVERNOTE_OAUTH_TOKEN) {
config.evernote.authtoken = process.env.EVERNOTE_OAUTH_TOKEN;
}
function connectServer(){
var server = config.vaaniserver;
server = url.parse(server, true, false);
server.query.authtoken = config.evernote.authtoken;
server.pathname = '/';
var secure = server.protocol == 'wss:';
server = url.format(server);
var options = {
rejectUnauthorized: false
};
if (secure) {
options.key = fs.readFileSync(ssldir + 'client-key.pem');
options.cert = fs.readFileSync(ssldir + 'client-crt.pem');
options.ca = fs.readFileSync(ssldir + 'ca-crt.pem');
options.passphrase = config.passphrase;
}
ws = new WebSocket(server, null, options);
var wstream;
var isWav;
ws.on('open', () => {
isWav = false;
wstream = fs.createWriteStream('output.wav');
});
ws.on('message', (data, flags) => {
if (!isWav){
console.log(data);
isWav = true;
Wakeword.resume();
Wakeword.pause();
} else {
wstream.write(data);
}
});
ws.on('close', () => {
wstream.end();
var play = call('play', 'output.wav');
play.stdout.on('close', () => {
Wakeword.resume();
});
});
}
function streamToServer(data) {
streamServer.write(data);
var samples;
if (ws.readyState == ws.OPEN) {
while ((samples = streamServer.read(VAD_BYTES))) {
ws.send(samples);
}
}
}
function endStreamToServer() {
if (ws.readyState == ws.OPEN) {
ws.send('EOS');
}
}
function vad(data) {
if (!data) {
return MAX_SIL_TIME;
}
var vadStatus = Wakeword.decoder.processWebrtcVad(data);
if (lastvadStatus == 1 && vadStatus == 0){
dtStartSilence = Date.now();
} else if (lastvadStatus == 0 && vadStatus == 0 && dtStartSilence){
totalSilencetime = Date.now() - dtStartSilence;
} else if (lastvadStatus == 0 && vadStatus == 1) {
totalSilencetime = 0;
}
lastvadStatus = vadStatus;
return totalSilencetime;
}
function listen() {
var streamvad = null;
var wakeTime = 0;
var secsSilence = 0;
let logFilename;
let logStream;
Wakeword.listen([config.wakeword], 0.83, (data, word) => {
let samples;
// this block is executed only the first time after the kw get spotted per iteration
if (!streamvad) {
Wakeword.mic.pause();
callSync('play', 'resources/hi.wav');
Wakeword.mic.resume();
connectServer();
streamvad = new MemoryStream();
wakeTime = Date.now();
dtStartSilence = totalSilencetime = null;
logStream = fs.createWriteStream("logs/" + new Date().getTime(), {'flags': 'a'});
}
streamvad.write(data);
while ((samples = streamvad.read(VAD_BYTES))) {
secsSilence = vad(samples);
if (config.logaudios){
logStream.write(samples);
}
streamToServer(samples);
}
if ((Date.now() - wakeTime > MAX_LISTEN_TIME) || (secsSilence >= MAX_SIL_TIME)) {
logStream.close();
var play = call('play', 'resources/end_spot.wav');
streamvad.end();
streamvad = null;
Wakeword.resume();
Wakeword.pause();
endStreamToServer();
}
}, () => { callSync('play', 'resources/start.wav'); } );
}
listen();