-
Notifications
You must be signed in to change notification settings - Fork 16
/
voiceRecorder.js
297 lines (258 loc) · 9.71 KB
/
voiceRecorder.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
const { Porcupine } = require('@picovoice/porcupine-node');
const { PvRecorder } = require("@picovoice/pvrecorder-node");
const { WaveFile } = require('wavefile');
const voiceHandler = require("./modules/voiceHandler.js");
const readline = require('readline');
const openaiLib = require("./modules/openaiLib.js");
const axios = require('axios');
dotenv.config();
const USE_NODE_VAD = process.env.USE_NODE_VAD === '1';
const enableDebug = process.env.DEBUG_MODE === '1';
const portNumber = process.env.PORT_NUMBER;
const VAD_MODE = process.env.VOICE_ACTIVATION_MODE_LEVEL || "NORMAL";
let VAD, vad;
if (USE_NODE_VAD) {
VAD = require('node-vad');
if (enableDebug) {
console.log(`VAD mode: ${VAD_MODE}`);
}
switch (VAD_MODE) {
case "NORMAL":
vad = new VAD(VAD.Mode.NORMAL);
break;
case "LOW_BITRATE":
vad = new VAD(VAD.Mode.LOW_BITRATE);
break;
case "AGGRESSIVE":
vad = new VAD(VAD.Mode.AGGRESSIVE);
break;
case "VERY_AGGRESSIVE":
vad = new VAD(VAD.Mode.VERY_AGGRESSIVE);
default:
vad = new VAD(VAD.Mode.NORMAL);
break;
}
}
// Error Handling
if (enableDebug) {
process.on('uncaughtException', handleUncaughtException);
process.on('unhandledRejection', handleUnhandledRejection);
}
let MICROPHONE_DEVICE = -1;
const CONFIG_FILE = './config.json';
let SILENCE_THRESHOLD = -1;
const MAX_SILENCE_FRAMES = 48;
let recorder;
let recordingFrames = [];
let isRecording = false;
let showChooseMic = true;
let porcupineHandle;
let isListening = true;
function handleUncaughtException(err, origin) {
console.error('An uncaught exception occurred!');
console.error(err);
console.error('Exception origin:', origin);
}
function handleUnhandledRejection(reason, promise) {
console.error('An unhandled rejection occurred!');
console.error('Reason:', reason);
console.error('Promise:', promise);
}
async function calibrate() {
console.log("Calibrating...");
let framesArray = [];
const calibrationDuration = 5000; // 5 seconds
const startTime = Date.now();
try {
while (Date.now() - startTime < calibrationDuration) {
const frames = await recorder.read();
framesArray.push(...frames);
}
const average = framesArray.reduce((a, b) => a + Math.abs(b), 0) / framesArray.length;
SILENCE_THRESHOLD = average * 1.5;
console.log(`Calibration completed. SILENCE_THRESHOLD set to ${SILENCE_THRESHOLD}`);
fs.writeFileSync(CONFIG_FILE, JSON.stringify({ MICROPHONE_DEVICE, SILENCE_THRESHOLD }));
} catch (error) {
console.error(`Error during calibration: ${error}`);
}
}
async function pressAnyKeyToContinue() {
return new Promise((resolve) => {
console.log("Please turn on microphone. Press any key to start 5 seconds of silence for calibration...");
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.once('keypress', (str, key) => {
process.stdin.setRawMode(false);
resolve();
});
});
}
function readRandomMP3(directory) {
const mp3Files = fs.readdirSync(directory);
const randomMP3 = mp3Files[Math.floor(Math.random() * mp3Files.length)];
console.log(`Playing from ${directory}: ${randomMP3}`);
return voiceHandler.streamMP3FromFile(path.join(directory, randomMP3));
}
function readRandomWakeWordAnswerMP3() {
return readRandomMP3(path.join(__dirname, 'wake_word_answer'));
}
function loadWakeWord() {
try {
const directoryPath = path.resolve(__dirname, 'wake_word');
let files = fs.readdirSync(directoryPath);
let modelFile = files.find(file => file.startsWith('porcupine_params_') && file.endsWith('.pv'));
let keywordFiles = files.filter(file => file.endsWith('.ppn'));
if (!modelFile) throw new Error("No model file found");
if (!keywordFiles.length) throw new Error('No .ppn files found');
let keywordPaths = keywordFiles.map(file => path.resolve(directoryPath, file));
const MY_MODEL_PATH = path.resolve(directoryPath, modelFile);
porcupineHandle = new Porcupine(process.env.PORCUPINE_API_KEY, keywordPaths, new Array(keywordPaths.length).fill(0.5), MY_MODEL_PATH);
console.log("Wake word loaded");
} catch (error) {
console.error(`Error loading wake word: ${error}`);
}
}
function saveRecording() {
try {
let waveFile = new WaveFile();
if (fs.existsSync("recording.wav")) {
fs.unlinkSync("recording.wav");
}
const audioData = new Int16Array(recordingFrames.length * porcupineHandle.frameLength);
for (let i = 0; i < recordingFrames.length; i++) {
audioData.set(recordingFrames[i], i * porcupineHandle.frameLength);
}
waveFile.fromScratch(1, recorder.sampleRate, '16', audioData);
fs.writeFileSync("recording.wav", waveFile.toBuffer());
console.log('Recording saved to recording.wav file');
} catch (error) {
console.error(`Error saving recording: ${error}`);
}
}
async function transcriptRecording() {
try {
console.log("Transcripting recording");
const result = await openaiLib.speechToText("recording.wav");
console.log("Detected sentence: " + result);
console.log("Transcripting recording done");
return result;
} catch (error) {
console.error(`Error during transcription: ${error}`);
}
}
async function startListening() {
console.log("Start listening");
let silenceFramesCount = 0;
while (isListening) {
const frames = await recorder.read();
if (isRecording) {
// processFrames(frames);
recordingFrames.push(frames);
const isSilence = await handleSilenceDetection(frames);
if (isSilence) {
silenceFramesCount++;
if (silenceFramesCount > MAX_SILENCE_FRAMES) {
isRecording = false;
silenceFramesCount = 0;
saveRecording();
const result = await transcriptRecording();
await axios.post(`http://localhost:${portNumber}/transcription`, { transcription: result });
}
} else {
silenceFramesCount = 0;
}
} else {
const index = porcupineHandle.process(frames);
if (index !== -1) {
console.log(`Wake word detected, start recording !`);
readRandomWakeWordAnswerMP3();
recordingFrames = [];
isRecording = true;
}
}
}
console.log("Loop ended");
}
async function handleSilenceDetection(frames) {
if (USE_NODE_VAD) {
const framesBuffer = Buffer.from(frames);
const res = await vad.processAudio(framesBuffer, recorder.sampleRate);
console.log(`VAD result: ${res}`);
switch (res) {
case VAD.Event.VOICE:
return false; // Voice detected, not silence
case VAD.Event.SILENCE:
case VAD.Event.NOISE:
case VAD.Event.ERROR:
default:
return true; // All other cases treated as silence
}
} else {
return frames.filter(frame => Math.abs(frame) < SILENCE_THRESHOLD).length / frames.length >= 0.9;
}
}
function processFrames(frames) {
if (!USE_NODE_VAD) return frames; // If not using node-vad, return frames as-is
const framesBuffer = Buffer.from(frames);
vad.processAudio(framesBuffer, recorder.sampleRate).then(res => {
if (res === VAD.Event.VOICE) {
recordingFrames.push(frames);
}
}).catch(console.error);
}
function saveMicrophoneInput(deviceId) {
MICROPHONE_DEVICE = deviceId;
fs.writeFileSync(CONFIG_FILE, JSON.stringify({ MICROPHONE_DEVICE, SILENCE_THRESHOLD }));
console.log(`Microphone input saved: ${deviceId}`);
}
function readConfig() {
if (fs.existsSync(CONFIG_FILE)) {
const config = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
MICROPHONE_DEVICE = config.MICROPHONE_DEVICE || MICROPHONE_DEVICE;
SILENCE_THRESHOLD = config.SILENCE_THRESHOLD || SILENCE_THRESHOLD;
}
}
function initMicrophone() {
console.log(`Using microphone device: ${recorder.getSelectedDevice()} | Wrong device? Run \`npm run choose-mic\` to select the correct input.`);
recorder.start();
if (USE_NODE_VAD) {
startListening();
} else {
pressAnyKeyToContinue()
.then(calibrate)
.then(startListening);
}
}
async function chooseMicrophone() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const devices = PvRecorder.getAvailableDevices();
console.log('Available microphone devices:');
for (let i = 0; i < devices.length; i++) {
console.log(`Device index: ${i} | Device name: ${devices[i]}`);
}
rl.question('Please enter the device index: ', (deviceId) => {
rl.close();
showChooseMic = false;
saveMicrophoneInput(parseInt(deviceId));
console.log('Please restart the script to use the new microphone input');
process.exit(0);
});
}
async function initialize() {
readConfig();
loadWakeWord();
openaiLib.initVoice();
if (process.argv.includes('--choose-mic')) {
await chooseMicrophone();
} else {
recorder = new PvRecorder(porcupineHandle.frameLength, MICROPHONE_DEVICE);
initMicrophone();
}
}
initialize().catch(error => console.error(`Failed to initialize: ${error}`));