-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstt.js
157 lines (129 loc) · 4.65 KB
/
stt.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
/*
Yandex speechkit cloud :: recognize client
Install:
* npm install grpc
* npm install @grpc/proto-loader
* npm install yargs
* npm install sprintf-js
*/
sprintf = require("sprintf-js").sprintf;
const PROTO_PATH = [__dirname + '/stt_service.proto'];
const FORMAT_PCM = 'LINEAR16_PCM';
function ndate() {
var u = new Date();
return sprintf("%02s:%02s:%02s.%03s", u.getHours(), u.getMinutes(), u.getSeconds(), u.getMilliseconds());
}
const args = require('yargs')
.options({
'folder_id': {
describe: 'folder ID',
demandOption: true,
},
'token': {
describe: 'IAM token',
demandOption: true,
},
'path': {
describe: 'audio file name',
demandOption: false,
default: ''
},
'stderr_log_file': {
describe: 'stderr log file path',
demandOption: false,
default: ''
}
})
.argv;
main(args.path, args.token, args.folder_id, args.stderr_log_file);
function createSttClient(iam_token, folder_id) {
var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
PROTO_PATH,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
metadata = new grpc.Metadata();
metadata.set('authorization', 'Bearer ' + iam_token);
var sttProto = grpc.loadPackageDefinition(packageDefinition).yandex.cloud.ai.stt.v2;
var sslCreds = grpc.credentials.createSsl();
var client = new sttProto.SttService('stt.api.cloud.yandex.net:443', sslCreds);
var sttService = client.StreamingRecognize(metadata);
// send config to server
var config = {
config: {
folder_id: folder_id,
specification: {
language_code: "ru-RU",
sample_rate_hertz: 16000,
audio_encoding: FORMAT_PCM,
profanity_filter: false,
partial_results: false
}
},
};
sttService.on('error', function (error) {
console.error("[" + ndate() + "] sttService event: error. Error: code " + error.code + " [" + error.message + "]. Closing client.");
client.close();
process.exit(1);
});
sttService.on('end', function() {
console.error("[" + ndate() + "] sttService event: end. Closing client.");
client.close();
});
sttService.write(config);
return sttService;
}
function main(audio_file_name, token, folder_id, stderr_log_file) {
var fs = require('fs');
Writable = require('stream').Writable;
const { pipeline } = require('stream');
/* mirror stderr to file */
if (stderr_log_file != "") {
var fn = process.stderr.write;
var sttStderrLog = fs.createWriteStream(stderr_log_file)
function write() {
fn.apply(process.stderr, arguments);
sttStderrLog.write.apply(sttStderrLog, arguments);
}
process.stderr.write = write;
}
var sttService = createSttClient(token, folder_id);
/* --- */
var transformStream = Writable();
transformStream._write = function (chunk, enc, next) {
// send audio chunks
console.error("[" + ndate() + "] send chunk of size " + chunk.length + " bytes")
if (!sttService.write({audio_content: chunk}, next)) {
console.error("[" + ndate() + "] sttService.write returned false.");
}
// next()
};
transformStream.on('finish', () => {
sttService.end();
console.error("[" + ndate() + "] transformStream event: finished. All chunks are sent now.")
});
var outStream = process.stdout;
outStream.on('error', function (error) {
console.error("[" + ndate() + "] output stream error, so exit. Error code: " + error.code + " Error message: " + error.message);
process.exit(1);
});
/* --- */
sttService.on('data', function (response) {
if (response.chunks.length > 0) {
console.error("[" + ndate() + "] received chunk of response")
outStream.write("Recognized message: " + response.chunks[0].alternatives[0].text + "\n");
outStream.write("Is final: " + response.chunks[0].final + "\n")
}
});
var inputStream = (audio_file_name == "") ? process.stdin : fs.createReadStream(audio_file_name);
inputStream.on('error', function (error) {
console.error("[" + ndate() + "] inputStream event: error. Error: code " + error.code + " [" + error.message + "]");
});
inputStream.pipe(transformStream);
}