https://cloud.google.com/speech-to-text/docs/streaming-recognize
我一直在尝试执行“在音频流上执行流式语音识别”下的示例google语音API代码
以下是我一直在尝试执行的代码:
'use strict';
const record = require('node-record-lpcm16');
const speech = require('@google-cloud/speech');
const exec = require('child_process').exec;
//const speech = 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: true // If you want interim results, set this to true
};
const recognizeStream = client.streamingRecognize(request)
.on('error', console.error)
.on('data', (data) =>
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`)
);
record.start({
sampleRateHertz: sampleRateHertz,
threshold: 0.5,
verbose: true,
recordProgram: 'arecord', // Try also "arecord" or "sox"
silence: '10.0'
}).on('error', console.error)
.pipe(recognizeStream);
console.log('Listening, press Ctrl+C to stop.');
终端输出: the output in the terminal:
我意识到来自arecord的输出流的编码存在问题,即它不符合程序中指定的配置,但我不知道该怎么做才能纠正这个