我正在尝试将这个示例代码生成的脚本从 google 发送到 nodejs 服务器并显示给用户。
var http = require('http');
const recorder = require('node-record-lpcm16');
const speech = require('@google-cloud/speech');
const client = new speech.SpeechClient();
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'en-US';
const request = {
config: {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
},
interimResults: false, // if you want interim results, set this to true
};
// create a recognize stream
const recognizeStream = client
.streamingRecognize(request)
.on('error', console.error)
.on('data', data =>
//console.log('test');
process.stdout.write(
data.results[0] && data.results[0].alternatives[0]
? `Transcription: ${data.results[0].alternatives[0].transcript}\n`
: '\n\nReached transcription time limit, press Ctrl+C\n'
)
writeToServer(data.results[0].alternatives[0].transcript);
);
function writeToServer(data){
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(data);
res.end();
}).listen(8080);
}
// start recording and send the microphone input to the speech api.
// ensure SoX is installed, see https://www.npmjs.com/package/node-record-lpcm16#dependencies
recorder
.record({
sampleRateHertz: sampleRateHertz,
threshold: 0,
// other options, see https://www.npmjs.com/package/node-record-lpcm16#options
verbose: false,
recordProgram: 'rec', // try also "arecord" or "sox"
silence: '1.0',
})
.stream()
.on('error', console.error)
.pipe(recognizeStream);
console.log('listening, press Ctrl+C to stop.');
我很难从谷歌语音客户端中访问成绩单。还可以参考如何从 webapp 而不是本地麦克风传递麦克风输入,因为目标是通过浏览器从用户那里获取麦克风输入并传递给 google-speech-to-text api。