Google语音转文本实时流,single_utterance不起作用

时间:2019-07-10 12:58:33

标签: node.js google-speech-api

我正在尝试使用Google进行实时流语音转换为文本。我已将节点安装到服务器中。

我已经成功实现了它,但是我希望Google能够识别用户何时停止讲话。 Google解释了如何使用single_utterance = true做到这一点,但没有生效。您能否说明以下代码中存在什么问题。谢谢!

var request = {
config: {
    encoding: encoding,
    sampleRateHertz: sampleRateHertz,
    languageCode: languageCode,
    //profanityFilter: false,
    enableWordTimeOffsets: true,
    //single_utterance: true
    // speechContexts: [{
    //     phrases: ["hoful","shwazil"]
    //    }] // add your own speech context for better recognition
},
interimResults: true, // If you want interim results, set this to true
singleUtterance: true
};


function startRecognitionStream(client, data) {
    console.log(request);
    recognizeStream = speechClient.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`);
            client.emit('speechData', data);

            // if end of utterance, let's restart stream
            // this is a small hack. After 65 seconds of silence, the stream will still throw an error for speech length limit
            if (data.results[0] && data.results[0].isFinal) {
                stopRecognitionStream();
                startRecognitionStream(client);
                // console.log('restarted stream serverside');
            }
        })
        .on('end_of_single_utterance', (data) => {
            process.stdout.write('data ended');
            console.log('data ended');
        })
        ;
}

提前谢谢!

0 个答案:

没有答案