简而言之,这就是我正在尝试做的事情:
\n浏览器/WebRTc音频==>服务器端(节点js)socket.io服务器==>谷歌云
\nI\xe2\x80\x99m 在浏览器中使用 webRTC 从浏览器麦克风捕获音频。该音频在传入时作为带有 base64 字符串的对象发送到 socket.io 服务器。这部分工作是因为我在记录传入数据时可以看到它。
\n我陷入困境的是将此流发送到谷歌云语音API以将其转录。
\n谷歌云语音文档中有一个快速入门应用程序,用于将麦克风数据流式传输到谷歌语音并获取实时转录。我设法让它工作,但它使用计算机\xe2\x80\x99s 麦克风。该应用程序使用节点node-record-lpcm16和SoX来访问computer\xe2\x80\x99s麦克风并将流传输到google cloud api。
\n音频通过 SpeechClient 上的 StreamingRecognize 方法发送到谷歌云。请求对象被传递给该方法。请求对象有一个名为audio_content的字段,这是我认为传入的音频流应该去的地方(???)。
\n下面是包含 socket.io 实例和来自与 node-record-lpcm16 包一起使用的 google cloud Quickstart 应用程序的代码的服务器文件。
\nlet io = require(\'socket.io\')(3000, {\n cors: {origin: [\'http://localhost:8080\']},\n})\n\nconst speech = require(\'@google-cloud/speech\');\n \n// Create a speech client\nconst client = new speech.SpeechClient();\n\n\nconst encoding = \'LINEAR16\';\nconst sampleRateHertz = 16000;\nconst languageCode = \'en-US\';\n\n//speech client request header\nconst request = {\n config: {\n encoding: encoding,\n sampleRateHertz: sampleRateHertz,\n languageCode: languageCode,\n …Run Code Online (Sandbox Code Playgroud)