-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (115 loc) · 3.5 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
const { networkInterfaces, hostname } = require('os');
const port = 3000;
const express = require('express')
const app = express()
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const { PvRecorder } = require("@picovoice/pvrecorder-node");
const { WaveFile } = require("wavefile");
const fs = require("fs");
const nets = networkInterfaces();
const hostnames = ["localhost", "*"];
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
// 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4
if (net.family === familyV4Value && !net.internal) {
hostnames.push(net.address);
}
}
}
server.listen(port, () => {
console.log(`listening on:`);
hostnames.forEach(hostname => {
console.log(`- http://${hostname}:${port}`);
})
});
app.use('/static', express.static('static'))
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/parler', function (req, res) {
res.sendFile(__dirname + '/parler.html');
});
app.get('/ecrire', function (req, res) {
res.sendFile(__dirname + '/ecrire.html');
});
app.get('/lire', function (req, res) {
res.sendFile(__dirname + '/lire.html');
});
let readSocket;
const devices = PvRecorder.getAvailableDevices();
let deviceIndex = 0;
io.on('connection', (socket) => {
socket.emit('devicesList', devices);
socket.on('selectDevice', (index) => {
deviceIndex = index;
console.log(`device : ${devices[deviceIndex]}`);
});
socket.on('start', () => {
isInterrupted = false;
record();
});
socket.on('stop', () => {
isInterrupted = true;
});
socket.on('save', (data) => {
saveText(data);
if (readSocket) {
readSocket.emit('new', `<b>${data.author}</b> : ${data.text}`)
}
})
socket.on('read', (data) => {
readSocket = socket;
sendAllText(readSocket);
});
});
const outputTextDir = "texts";
async function sendAllText() {
fs.readdir(outputTextDir, (err, files) => {
files.forEach((file) => {
fs.readFile(`${outputTextDir}/${file}`, 'utf8', (err, data) =>{
readSocket.emit('new', data);
});
});
});
}
async function saveText({text, author}) {
fs.readdir(outputTextDir, (err, files) => {
fs.writeFileSync(`${outputTextDir}/${files.length}-${author}.txt`, `<b>${author}</b> : ${text}`);
});
}
let isInterrupted = false;
const outputWavDir = "records";
async function record() {
const wav = new WaveFile();
const frames = [];
const frameLength = 512;
const recorder = new PvRecorder(frameLength, deviceIndex);
let outputWavFile;
console.log(`Using PvRecorder version: ${recorder.version}`);
recorder.start();
console.log(`Using device: ${recorder.getSelectedDevice()}`);
fs.readdir(outputWavDir, (err, files) => {
outputWavFile = `${outputWavDir}/record-${files.length}.wav`;
});
while (!isInterrupted) {
const frame = await recorder.read();
if (outputWavFile) {
frames.push(frame);
}
}
if (outputWavFile) {
const audioData = new Int16Array(recorder.frameLength * frames.length);
for (let i = 0; i < frames.length; i++) {
audioData.set(frames[i], i * recorder.frameLength);
}
wav.fromScratch(1, recorder.sampleRate, '16', audioData);
fs.writeFileSync(outputWavFile, wav.toBuffer());
}
console.log("Stopping...");
recorder.release();
}