-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.js
123 lines (113 loc) · 3.34 KB
/
stream.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
'use strict';
var auth = require('./auth');
var rec = require('node-record-lpcm16');
var net = require('net');
var Promise = require('bluebird');
var uuid = require('node-uuid');
var bsplit = require('buffer-split');
var ProtoBuf = require("protobufjs");
var ByteBuffer = ProtoBuf.ByteBuffer;
var Protobuf = ProtoBuf.loadProtoFile({
root: __dirname + '/protobuf',
file: 'voiceproxy.proto'
}).build();
var STATE = {
inited: false,
ready: false,
_queue: 0,
_lastChunkCb: function() {},
queue: function(delta) {
this._queue+=delta;
if (this._queue === 0) {
this._lastChunkCb();
}
},
waitForLastChunk: function(cb) {
this._lastChunkCb = cb;
}
};
function sendMessage(msg, socket) {
var payload = msg.toBuffer();
var buffer = Buffer.concat([
new Buffer(payload.length.toString(16)),
new Buffer('\r\n'),
payload
]);
return socket.write(buffer);
}
function init(socket) {
var msg = new Protobuf.VoiceProxyProtobuf.ConnectionRequest({
speechkitVersion: '1',
serviceName: 'asr_dictation',
uuid: uuid.v4().replace(/-/g,''),
apiKey: auth.speechKit,
applicationName: 'escaton-test',
device: 'desktop',
coords: '0, 0',
topic: 'freeform',
lang: 'ru-RU',
format: 'audio/x-pcm;bit=16;rate=16000',
punctuation: true,
// advancedASROptions: {
// // utterance_silence: 120,
// // cmn_latency: 150
// }
});
sendMessage(msg, socket);
var recStream = rec.start({
threshold: 0.01
})
.on('readable', function() {
var chunk;
while (null !== (chunk = recStream.read(32768))) {
var msg = new Protobuf.VoiceProxyProtobuf.AddData({
audioData: chunk,
lastChunk: false
});
STATE.queue(1);
sendMessage(msg, socket);
}
})
.on('end', function() {
STATE.waitForLastChunk(function() {
socket.end();
});
});
}
var client = new net.Socket();
client.connect(80, 'voice-stream.voicetech.yandex.net', function() {
console.log('connected');
client.write([
'GET /asr_partial HTTP/1.1\r\n',
'User-Agent:KeepAliveClient\r\n',
'Host: voice-stream.voicetech.yandex.net:80\r\n',
'Upgrade: dictation\r\n\r\n',
].join(''));
});
client.on('data', function(data) {
if (STATE.inited) {
var delim = new Buffer('\r\n');
var parts = bsplit(data, delim);
if (STATE.ready) {
try {
var data = Protobuf.VoiceProxyProtobuf.AddDataResponse.decode(parts[1]);
STATE.queue(-data.messagesCount);
if (data.responseCode === 200 && data.recognition[0]) {
console.log("\u001b[2J\u001b[0;0H" + data.recognition[0].normalized)
}
} catch (e) {
var data = Protobuf.BasicProtobuf.ConnectionResponse.decode(parts[1]);
console.error(data);
}
} else {
var data = Protobuf.BasicProtobuf.ConnectionResponse.decode(parts[1]);
STATE.ready = true;
}
} else {
init(client);
STATE.inited = true;
}
});
client.on('close', function() {
console.log('Connection closed');
});