使用Electron的gRPC实时转录Google Cloud Speech API

Meh*_* M. 9 javascript electron gcp

我想要实现的是与Web Speech API相同的实时脚本处理流程,但使用的是Google Cloud Speech API.

主要目标是使用gRPC协议通过带有Speech API的Electron应用程序转录实时录制.

这是我实现的简化版本:

const { desktopCapturer } = window.require('electron');
const speech = require('@google-cloud/speech');

const client = speech.v1({
    projectId: 'my_project_id',
    credentials: {
        client_email: 'my_client_email',
        private_key: 'my_private_key',
    },
});

desktopCapturer.getSources(
    { types: ['window', 'screen'] },
    (error, sources) => {
        navigator.mediaDevices
            .getUserMedia({
                audio: true,
            })
            .then((stream) => {
                let fileReader = new FileReader();
                let arrayBuffer;
                fileReader.onloadend = () => {
                    arrayBuffer = fileReader.result;
                    let speechStreaming = client.streamingRecognize({
                        config: {
                           encoding: speech.v1.types.RecognitionConfig.AudioEncoding.LINEAR16,
                           languageCode: 'en-US',
                           sampleRateHertz: 44100,
                        },
                        singleUtterance: true,
                    }).on('data', (response) => response);

                    speechStreaming.write(arrayBuffer);
                }

                fileReader.readAsArrayBuffer(stream);
            })
    }
);
Run Code Online (Sandbox Code Playgroud)

Speech API的错误响应是音频流太慢而我们没有实时发送.

我觉得原因是我在没有任何格式化或对象初始化的情况下传递了流,因此无法执行流识别.

Ven*_*ryx 0

Github 上的这个官方示例项目似乎与您正在寻找的内容相匹配: https: //github.com/googleapis/nodejs-speech/blob/master/samples/infiniteStreaming.js

此应用程序演示了如何使用 StreamingRecognize 操作和 Google Cloud Speech API 来执行无限流式传输。

另请参阅我对 Electron 中替代方案的评论,即使用 OtterAI 的转录服务。(这是我很快就会尝试的方法)