-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (63 loc) · 2.17 KB
/
app.js
File metadata and controls
83 lines (63 loc) · 2.17 KB
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
var express = require('express');
var cors = require('cors')
const DeepSpeech = require('deepspeech');
const Fs = require('fs');
const Sox = require('sox-stream');
const MemoryStream = require('memory-stream');
const Duplex = require('stream').Duplex;
const Wav = require('node-wav');
const BEAM_WIDTH = 1024;
const N_FEATURES = 26;
const N_CONTEXT = 9;
let modelPath = './models/output_graph.pbmm';
let alphabetPath = './models/alphabet.txt';
let model = new DeepSpeech.Model(modelPath, N_FEATURES, N_CONTEXT, alphabetPath, BEAM_WIDTH);
const LM_ALPHA = 0.75;
const LM_BETA = 1.85;
let lmPath = './models/lm.binary';
let triePath = './models/trie';
model.enableDecoderWithLM(alphabetPath, lmPath, triePath, LM_ALPHA, LM_BETA);
function bufferToStream(buffer) {
let stream = new Duplex();
stream.push(buffer);
stream.push(null);
return stream;
}
// const bodyParser= require('body-parser')
const multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var app = express();
app.use(cors())
// app.use(bodyParser.urlencoded({extended: true}))
const execSync = require('child_process').execSync;
app.post('/', upload.single('data'), function(req, res, next){
execSync(`ffmpeg -i ${req.file.path} ${req.file.path + ".wav"}`)
// Fs.renameSync(req.file.path, req.file.path + ".wav")
const buffer = Fs.readFileSync(req.file.path + ".wav")
let audioStream = new MemoryStream();
bufferToStream(buffer).
pipe(Sox({
global: {
'no-dither': true,
},
output: {
bits: 16,
rate: 16000,
channels: 1,
encoding: 'signed-integer',
endian: 'little',
compression: 0.0,
type: 'wav'
}
})).
pipe(audioStream);
audioStream.on('finish', () => {
let audioBuffer = audioStream.toBuffer();
const audioLength = (audioBuffer.length / 2) * ( 1 / 16000);
console.log('audio length', audioLength);
let result = model.stt(audioBuffer.slice(0, audioBuffer.length / 2), 16000);
// console.log('result:', result);
res.json({ result: result })
});
});
app.listen(3000);