-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
160 lines (125 loc) · 3.41 KB
/
server.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
const http = require("http");
const socketIO = require("socket.io");
const DeepSpeech = require("deepspeech");
let DEEPSPEECH_MODEL = __dirname + "/deepspeech-0.9.3-models"; // path to deepspeech english model directory
const SERVER_PORT = 4000; // websocket server port
function createModel(modelDir) {
let modelPath = modelDir + ".pbmm";
let scorerPath = modelDir + ".scorer";
let model = new DeepSpeech.Model(modelPath);
model.enableExternalScorer(scorerPath);
return model;
}
let englishModel = createModel(DEEPSPEECH_MODEL);
let modelStream;
let recordedChunks = 0;
let endInterval = null;
let silenceBuffers = [];
let firstTime = true;
function processAudioStream(data, callback) {
processVoice(data);
if (firstTime) {
endInterval = setInterval(function () {
let results = intermediateDecode();
if (results && callback) callback(results);
}, 500);
firstTime = false;
}
}
function endAudioStream(callback) {
console.log("[end]");
let results = intermediateDecode();
if (results && callback) callback(results);
}
function endConnection() {
console.log("[end connection]");
if (endInterval) clearInterval(endInterval);
if (modelStream) modelStream.finishStream(); // ignore results
endInterval = null;
firstTime = true;
recordedChunks = 0;
modelStream = null;
}
function resetAudioStream() {
console.log("[reset]");
clearTimeout(endInterval);
createStream(); // ignore results
firstTime = true;
recordedChunks = 0;
silenceStart = null;
}
function addBufferedSilence(data) {
let audioBuffer;
if (silenceBuffers.length) {
silenceBuffers.push(data);
let length = 0;
silenceBuffers.forEach(function (buf) {
length += buf.length;
});
audioBuffer = Buffer.concat(silenceBuffers, length);
silenceBuffers = [];
} else audioBuffer = data;
return audioBuffer;
}
function processVoice(data, callback) {
silenceStart = null;
if (recordedChunks === 0) {
console.log("");
process.stdout.write("[start]"); // recording started
} else process.stdout.write("="); // still recording
recordedChunks++;
data = addBufferedSilence(data);
feedAudioContent(data);
}
function createStream() {
modelStream = englishModel.createStream();
recordedChunks = 0;
}
function intermediateDecode() {
let results;
if (modelStream) {
let text = modelStream.intermediateDecode();
if (text) {
console.log("\nRecognized Text:", text);
results = {
text,
};
}
}
return results;
}
function feedAudioContent(chunk) {
modelStream.feedAudioContent(chunk);
}
const app = http.createServer(function (req, res) {
res.writeHead(200);
res.write("generate-transcription");
res.end();
});
const io = socketIO(app, {});
io.set("origins", "*:*");
io.on("connection", function (socket) {
console.log("client connected");
socket.once("disconnect", () => {
console.log("client disconnected");
endConnection();
});
createStream();
socket.on("stream-data", function (data) {
processAudioStream(data, (results) => {
socket.emit("recognize", results);
});
});
socket.on("stream-end", function () {
endAudioStream((results) => {
socket.emit("recognize", results);
});
});
socket.on("stream-reset", function () {
resetAudioStream();
});
});
app.listen(SERVER_PORT, "localhost", () => {
console.log("Socket server listening on:", SERVER_PORT);
});
module.exports = app;