-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstt.js
223 lines (176 loc) · 4.4 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* @module stt.js
*
* @See STT API documentation
* https://stt.readthedocs.io/en/latest/NodeJS-API.html
*
* @See STT NodeJs binding code
* https://github.com/coqui-ai/STT/tree/main/native_client/javascript
*
* @See STT NodeJs examples
* https://github.com/coqui-ai/STT-examples#javascript
*/
const fs = require('fs').promises
const path = require('path')
const STT = require('stt')
/**
* loadmodel
*
* load the model
* with specified pbmm and scorer files
*
* @function
* @async
*
* @param {String} modelPath
* @param {String} scorerPath
*
* @return {STTModelObject} STT Model object
*
* TODO
* add metadata object as parameter
*
* WARNING
* In facts the STT Model creation is a syncronous elaboration.
*
*/
function loadModel(modelPath, scorerPath) {
let model
try {
model = new STT.Model(modelPath)
}
catch (error) {
throw (`model.stt: ${error}`)
}
try {
model.enableExternalScorer(scorerPath)
}
catch (error) {
throw `model.enableExternalScorer: ${error}`
}
return model
}
/**
* freeModel
*
* Frees associated resources and destroys model object.
*
* @param {STTMemoryModelObject} model
*
*/
function freeModel(model) {
try {
STT.FreeModel(model)
}
catch (error) {
throw `STT.FreeModel error: ${error}`
}
}
/**
* transcriptBuffer
*
* return the speech to text (transcript)
* of the audio contained in the specified audio PCM buffer
*
* The function is async to avoid the caller thread is blocked
* - during audio file reading
* - but especially during the STT engine processing.
*
* @param {Buffer} audioBuffer
* @param {STTMemoryModelObject} model
*
* @return {String} text transcript
*/
function transcriptBuffer(audioBuffer, model) {
// WARNING:
// no audioBuffer validation is done.
// The audio fle must be a WAV audio in raw format.
try {
return model.stt(audioBuffer)
}
catch (error) {
throw `model.stt error: ${error}`
}
}
/**
* transcriptFile
*
* return the speech to text (transcript)
* of the audio contained in the specified filename
*
* The function is async to avoid the caller thread is blocked
* - during audio file reading
* - but especially during the STT engine processing.
*
* @param {String} audioFile
* @param {STTMemoryModelObject} model
*
* @return {Promise<String>} text transcript
*/
async function transcriptFile(audioFile, model) {
let audioBuffer
try {
audioBuffer = await fs.readFile(audioFile)
}
catch (error) {
throw `readFile error: ${error}`
}
// WARNING:
// no audioBuffer validation is done.
// The audio fle must be a WAV audio in raw format.
try {
return model.stt(audioBuffer)
}
catch (error) {
throw `model.stt error: ${error}`
}
}
async function main() {
const scriptName = path.basename(__filename, '.js')
if (process.argv.length < 5) {
console.log()
console.log(`usage : node ${scriptName} <model pbmm file> <model scorer file> <audio file>`)
console.log(`example: node ${scriptName} models/coqui-stt-0.9.3-models.pbmm models/coqui-stt-0.9.3-models.scorer audio/4507-16021-0012.wav`)
console.log()
process.exit(1)
}
const modelPath = process.argv[2]
const scorerPath = process.argv[3]
const audioFile = process.argv[4]
const startModel = new Date()
//
// load STT model
//
const model = loadModel(modelPath, scorerPath)
const stopModel = new Date()
console.log()
console.log(`pbmm : ${modelPath}`)
console.log(`scorer : ${scorerPath}`)
console.log(`elapsed : ${stopModel - startModel}ms\n`)
//console.dir(model)
//
// transcript an audio file
//
const startTranscript = new Date()
const result = await transcriptFile(audioFile, model)
const stopTranscript = new Date()
console.log(`audio file : ${audioFile}`)
console.log(`transcript : ${result}`)
console.log(`elapsed : ${stopTranscript - startTranscript}ms\n`)
//
// free model memory
//
const startFreeModel = new Date()
freeModel(model)
const stopFreeModel = new Date()
console.log(`free model elapsed : ${stopFreeModel - startFreeModel}ms\n`)
}
if (require.main === module)
main()
module.exports = {
loadModel,
transcriptBuffer,
transcriptFile,
transcript: transcriptFile, // alias
freeModel
}