-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
120 lines (92 loc) · 3.94 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
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
'use strict'
// Setting the requirements for the app to function properly
const express = require('express'); // const bodyParser = require('body-parser'); // const path = require('path');
const fs = require('fs');
const environmentVars = require('dotenv').config();
// Loading the Google Cloud Speech API
const speech = require('@google-cloud/speech');
const speechClient = new speech.SpeechClient(); // Creates a client
const app = express();
const port = process.env.PORT || 1337;
const server = require('http').createServer(app);
const io = require('socket.io')(server); // Connecting with Socket IO
app.use('/assets', express.static(__dirname + '/public'));
app.use('/session/assets', express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
// =========================== ROUTERS ================================ //
app.get('/', function (req, res) {
res.render('index', {});
});
app.use('/', function (req, res, next) {
next(); // console.log(`Request Url: ${req.url}`);
});
// =========================== SOCKET.IO ================================ //
io.on('connection', function (client) {
console.log('Client Connected to server');
let recognizeStream = null;
client.on('join', function (data) {
client.emit('messages', 'Socket Connected to Server');
});
client.on('messages', function (data) {
client.emit('broad', data);
});
client.on('startGoogleCloudStream', function (data) {
startRecognitionStream(this, data);
});
client.on('endGoogleCloudStream', function (data) {
stopRecognitionStream();
});
client.on('binaryData', function (data) {
// console.log(data); //log binary data
if (recognizeStream !== null) {
recognizeStream.write(data);
}
});
function startRecognitionStream(client, data) {
// Getting the results from Google Cloud API
recognizeStream = speechClient.streamingRecognize(request)
.on('error', console.error)
.on('data', (data) => {
process.stdout.write(
(data.results[0] && data.results[0].alternatives[0])
? `Transcription: ${data.results[0].alternatives[0].transcript}\n`
: `\n\nReached transcription time limit, press Ctrl+C\n`);
client.emit('speechData', data);
// if end of utterance, let's restart stream
// this is a small hack. After 65 seconds of silence, the stream will still throw an error for speech length limit
if (data.results[0] && data.results[0].isFinal) {
stopRecognitionStream();
startRecognitionStream(client);
// console.log('restarted stream serverside');
}
});
}
function stopRecognitionStream() {
if (recognizeStream) {
recognizeStream.end();
}
recognizeStream = null;
}
});
// =========================== GOOGLE CLOUD SETTINGS ================================ //
// The encoding of the audio file, e.g. 'LINEAR16'
// The sample rate of the audio file in hertz, e.g. 16000
// The BCP-47 language code to use, e.g. 'en-US'
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'yue-Hant-HK'; //en-US
const request = {
config: {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
profanityFilter: false,
enableWordTimeOffsets: true
},
interimResults: true // If you want interim results, set this to true
};
// =========================== START SERVER ================================ //
server.listen(port, "127.0.0.1", function () { //http listen, to make socket work
// app.address = "127.0.0.1";
console.log('Server started on port:' + port)
});