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
| 'use strict'; var record = require('node-record-lpcm16'); var fs = require('fs'); var HotwordDetector = require('node-hotworddetector'); var quetystring = require("querystring"); var http = require("http"); var AipSpeechClient = require("baidu-aip-sdk").speech; var BAIDU_APP_ID = "替换成你自己的key"; var BAIDU_API_KEY = "替换成你自己的key"; var BAIDU_SECRET_KEY = "替换成你自己的key"; var TULING_APIKEY = "替换成你自己的key"; var HttpClient = require("baidu-aip-sdk").HttpClient; var isrecording = false; var {execFile} = require('child_process');
var hotwordConfiguration = { detector: { resource: "./node_modules/snowboy/resources/common.res" }, models: [{ file: "./hello.pmdl", hotwords: "你好", sensitivity: '0.6', }, { file: "./node_modules/snowboy/resources/models/snowboy.umdl", hotwords: "snowboy", sensitivity: '0.8', } ], recorder: {} };
var hotwordDetector = new HotwordDetector(hotwordConfiguration.detector, hotwordConfiguration.models, hotwordConfiguration.recorder); hotwordDetector.on('hotword', function (index, hotword, buffer) { if (hotword === "你好") { if (isrecording === false) { recordsound(); } } else { console.log(hotword); } });
async function recordsound() { var file = fs.createWriteStream('output.raw', { encoding: 'binary' }); record.start({ sampleRate: 16000, threshold: 0.5, thresholdStart: null, thresholdEnd: null, silence: '1.0', verbose: true, recordProgram: 'rec', device: null }) .pipe(file); setTimeout(async () => { await record.stop(); await speaking(); }, 3000); }
async function speaking() { let voice = fs.readFileSync('output.raw'); let voiceBase64 = new Buffer(voice); var baiduspeechclient = new AipSpeechClient(BAIDU_APP_ID, BAIDU_API_KEY, BAIDU_SECRET_KEY); console.log('开始识别:'); await baiduspeechclient.recognize(voiceBase64, 'pcm', 16000).then(function (result) { console.log('语音识别本地音频文件结果: ' + JSON.stringify(result)); if (result.err_no === 0) { var post_data = quetystring.stringify({ key: TULING_APIKEY, info: result.result[0], userid: "" }); var options = { host: 'www.tuling123.com', port: 80, path: '/openapi/api', method: 'POST', rejectUnauthorized: false, headers: { "Content-Type": 'application/x-www-form-urlencoded', } }; var req = http.request(options, function (res) { console.log('STATUS: ' + res.statusCode); console.log('HEADERS: ' + JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', function (chunk) { console.log('BODY: ' + chunk); var obj = JSON.parse(chunk); baiduspeechclient.text2audio(obj.text).then(function (result) { if (result.data) { fs.writeFileSync('tmp.mp3', result.data); var play = execFile('play', ['tmp.mp3'], (error, stdout, stderr) => { if (error) { throw error; } console.log(stdout); }); play.on('exit', (code) => { isrecording = false; console.log("play结束于:" + code); }); } }, function (err) { console.log("百度没有给出语音,错误代码为:" + err); isrecording = false; }); }); }); req.write(post_data); req.end(); } else { console.log("语音识别出错"); isrecording = false; } }, function (err) { console.log("没识别,错误代码为:" + err); isrecording = false; }); }
HttpClient.setRequestOptions({ timeout: 5000 });
HttpClient.setRequestInterceptor(function (requestOptions) { requestOptions.timeout = 5000; return requestOptions; });
hotwordDetector.start();
hotwordDetector.on('error', function (error) { console.error('hotwordDetector: ' + error); });
|