-
Notifications
You must be signed in to change notification settings - Fork 2
/
voice_send.js
69 lines (46 loc) · 1.78 KB
/
voice_send.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
var express = require('express');
var app = express();
var AipSpeechClient = require("baidu-aip-sdk").speech;
// 设置APPID/AK/SK
var APP_ID = "15147521";
var API_KEY = "NXQYhBSGYb6xve8m3FmwOrkU";
var SECRET_KEY = "Zio57yIahYxXLMPmV7FSkG4ahfe0zbXg";
// 新建一个对象,建议只保存一个对象调用服务接口
var client = new AipSpeechClient(APP_ID, API_KEY, SECRET_KEY);
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
var fs = require('fs');
var path = require('path');
app.post('/process_post', multipartMiddleware, function (req, res) {
var source = fs.createReadStream(req.files.audioData.path);
streamToBuffer(source).then(buff => {
console.log(buff);
console.log("正在识别");
// 识别本地语音文件
client.recognize(buff, 'wav', 16000).then(result => {
console.log('return json: ' + JSON.stringify(result));
console.log('在线语音识别结果: ' + result.result);
res.setHeader('Content-Type', 'application/json;charset=utf-8');
res.end(JSON.stringify(result));
}, function (err) {
console.log(err);
});
});
//res.end(JSON.stringify(source));
})
function streamToBuffer(stream) {
return new Promise((resolve, reject) => {
let buffers = [];
stream.on('error', reject);
stream.on('data', (data) => buffers.push(data))
stream.on('end', () => resolve(Buffer.concat(buffers)))
});
}
app.use(express.static('public'));
app.get('/', function (req, res) {
res.sendFile( __dirname + "/public/" + "wave.html" );
})
var server = app.listen(8081, function () {
var port = server.address().port
console.log("应用实例,访问地址为 http://localhost:%s", port)
})