首页 > 解决方案 > Google 的语音到文本流 API 在 Electron-Vue 中不起作用

问题描述

由于这是我在这里的第一个问题,因此我很乐意获得有关如何更好地提出问题的提示。

我目前正在 Electron-Vue 中开发一个应用程序,我们想要添加 Google Speech-To-Text 流。我可以使它适用于音频文件,但不适用于来自麦克风的流。这些是重现我的问题的步骤。

npm install -g vue-cli
vue init simulatedgreg/electron-vue my-project

设置 GCP 控制台项目并按此处所述获取凭据: https ://cloud.google.com/speech-to-text/docs/quickstart-client-libraries

按照此处所述安装和初始化 Cloud SDK: https ://cloud.google.com/sdk/docs/

安装 SoX 并将其添加到 PATH 变量 https://sourceforge.net/projects/sox/files/latest/download

专门为目标电子版本安装 GRPC。Simulatedgreg 使用 2.0.14:

npm install --runtime=electron --target=2.0.14 --disturl=https://atom.io/download/electron

安装谷歌云语音和node-record-lpcm-16

npm install @google-cloud/speech
npm install node-record-lpcm16

现在,对于任何无法在 NodeJS 上使用 Google Speech to Text api 的人来说,这一步很重要。转到 node_modules 并查找 node-record-lpcm16 并打开 index.js。将 cmdArgs 更改为:

var cmd = 'sox';
var cmdArgs = [
'-q',                                     // show no progress
'-t', 'waveaudio',                        // input-type
'-d',                                     // use default recording device
'-r', options.sampleRate.toString(),      // sample rate
'-c', '1',                                // channels
'-e', 'signed-integer',                   // sample encoding
'-b', '16',                               // precision (bits)
'-t', 'raw',                              // output-type
'-'                                       // pipe
];

将您从 API 生成的 credentials.json 文件放在项目文件夹中。

最后是 Vue 本身的代码:

<template>
  <div id="wrapper">
          <button @click="record">Test</button>
  </div>
</template>

<script>
  export default {
    name: 'landing-page',
    methods: {
      record(){
          this.version = process.versions.electron;
          const record = require('node-record-lpcm16');

// Imports the Google Cloud client library
          const speech = require('@google-cloud/speech');

// Creates a client
          const client = new speech.SpeechClient({
              keyFilename: './credentials.json'
          });

          const encoding = 'LINEAR16';
          const sampleRateHertz = 16000;
          const languageCode = 'nl-NL';

          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 =>
                  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`
                  )
              );

// Start recording and send the microphone input to the Speech API
          record
              .start({
                  sampleRateHertz: sampleRateHertz,
                  threshold: 0,
                  // Other options, see https://www.npmjs.com/package/node-record-lpcm16#options
                  verbose: false,
                  recordProgram: 'sox', // Try also "arecord" or "sox"
                  silence: '10.0',
              })
              .on('error', console.error)
              .pipe(recognizeStream);

          console.log('Listening, press Ctrl+C to stop.');
      }
    }
  }
</script>

我确实意识到,由于我让它在 NodeJS 本身中工作,我可以使用与电子应用程序捆绑在一起的单独的 NodeJS 服务器并通过 websocket 进行通信,但我想让最终用户尽可能简单。提前致谢。

标签: node.jsvue.jsgoogle-apielectron

解决方案


推荐阅读