-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (50 loc) · 1.54 KB
/
index.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
var execSync = require("child_process").execSync;
var fs = require('fs');
const path = require("path");
const parserSHPATH = path.join(__dirname, "parse.sh");
let AipSpeechClient = require("baidu-aip-sdk").speech;
let client;
// ASR
// 根据文件FILE的后缀填写:pcm / wav / amr
let FORMAT = "pcm";
// 根据文档填写PID,1537 表示识别普通话,使用输入法模型。1536表示识别普通话,使用搜索模型
let DEV_PID = "1537";
let methods = {
init(config) {
// 新建一个对象,建议只保存一个对象调用服务接口
client = new AipSpeechClient(config.APP_ID, config.API_KEY, config.SECRET_KEY);
return this;
},
start(audioPath) {
let convertWav = this.adoptVoice(audioPath);
if (convertWav) {
let voice = fs.readFileSync(audioPath + '.pcm');
let voiceBuffer = new Buffer(voice);
// 识别本地文件,附带参数
let result;
console.log('Start to recognize: ', audioPath)
await client.recognize(voiceBuffer, FORMAT, 16000, { dev_pid: DEV_PID, ctp: 1 })
.then(res => {
return result = res;
console.log('<recognize>: ' + JSON.stringify(res));
}, err => {
console.log(err);
});
return result;
}
return {
code: 601,
msg: 'Convert wav error'
};
},
adoptVoice(audioPath) {
console.info("Start generate wav audio: ", audioPath);
if (fs.existsSync(audioPath)) {
// Do something
let result = execSync(parserSHPATH + ' ' + audioPath);
console.log(result);
return result;
}
return new Error(audioPath + ' is not existed.')
}
}