-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
64 lines (51 loc) · 1.87 KB
/
app.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
require('dotenv').config();
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const azure = require('microsoft-cognitiveservices-speech-sdk');
const WaveFile = require('wavefile').WaveFile;
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
const azureKey = process.env.AZURE_KEY;
const azureRegion = process.env.AZURE_REGION;
app.get('/', (req, res) => {
res.sendFile(__dirname + '/web.html');
});
io.on('connection', (socket) => {
let audioStream = azure.AudioInputStream.createPushStream();
let speechConfig = null;
let audioConfig = null;
let recognizer = null;
let transcript = ""
socket.on('init', (config) => {
speechConfig = azure.SpeechConfig.fromSubscription(azureKey, azureRegion);
speechConfig.speechRecognitionLanguage = config['language'];
audioConfig = azure.AudioConfig.fromStreamInput(audioStream);
recognizer = new azure.SpeechRecognizer(speechConfig, audioConfig);
socket.on('audio', (data) => {
// Convert whatever wav to 16kHz wav
let wav = new WaveFile(data);
wav.toSampleRate(16000);
audioStream.write(wav.toBuffer());
});
recognizer.recognizing = function (s, e) {
if (e.result.text) {
socket.emit('transcript', `${transcript} ${e.result.text}`);
}
};
recognizer.recognized = function (s, e) {
if (e.result.text) {
transcript = `${transcript} ${e.result.text}`
socket.emit('transcript', transcript);
}
};
socket.on('disconnect', () => {
recognizer.stopContinuousRecognitionAsync();
recognizer = undefined;
});
recognizer.startContinuousRecognitionAsync();
socket.emit('ready');
});
});
server.listen(4000, () => console.log('Listening on port 4000'));