-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
111 lines (88 loc) · 2.75 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
const speech = require('@google-cloud/speech');
const client = new speech.SpeechClient();
const fetch = require('node-fetch');
const app = require('express')()
const server = require('http').Server(app)
const io = require('socket.io')(server)
var port = process.env.PORT || 3000;
server.listen(port)
console.log('Server up!')
const streamBuffers = require('stream-buffers')
const encoding = 'LINEAR16';
const sampleRateHertz = 48000;
const languageCode = 'en-US';
const audio = {
content: null
}
const config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
}
const request = {
config: config,
audio: audio
}
app.get('/', (req, res) => {
res.sendFile(__dirname + '/www/index.html')
})
io.on('connection', function (socket) {
console.log('Connection made!')
function getReply(text, textToSpeech = false) {
fetch('http://localhost:5000', {
method: 'POST',
body: text
}).then(response => response.text())
.then((data) => {
socket.emit('reply', data)
if (textToSpeech) {
fetch('http://localhost:5000/tts', {
method: 'POST',
body: data
}).then(response => response.arrayBuffer()).then((data) => {
socket.emit('reply-tts', data)
})
}
}).catch(err => console.error(err))
}
async function getSTT(data) {
try {
const [response] = await client.recognize({
audio: { content: data.toString('base64') },
config: config
})
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
socket.emit('speechToText', transcription)
} catch(err) {
console.log(`${err.code}: ${err.message}`)
socket.emit('speechToText', false)
}
}
const recognizeStream = client
.streamingRecognize(request)
.on('error', console.error)
.on('data', (data) => {
socket.emit('speechToText', data.results[0].alternatives[0].transcript)
});
const audioStream = new streamBuffers.ReadableStreamBuffer({
frequency: 10,
chunkSize: 1024
})
socket.on('audio-start', function () {
audioStream.pipe(recognizeStream)
});
socket.on('audio-stop', function () {
audioStream.unpipe()
})
socket.on('audio-stream', function (data) {
audioStream.put(data)
})
socket.on('audio', function (data) {
getSTT(data)
})
socket.on('question', function (data) {
getReply(data.text, data.textToSpeech)
})
});