1

我正在 Google App Engine 的节点/快速环境中设置 google 的语音到文本。我有一个通过 websockets 与服务器通信的 Angular 应用程序。

这一切都可以在本地主机上完美运行,但是当我的 Angular 应用程序指向 App Engine 实例时,它不会。

它可以很好地连接 - 来回发送连接消息。它运行我的谷歌语音连接很好。

但是,我在尝试访问麦克风流时遇到错误。错误消息没有多大用处:(ERROR with recorder.record sox has exited with error code 2.下面的完整代码)

错误似乎与recorder.record(由. 提供const recorder = require('node-record-lpcm16');。或者可能是我的 GOOGLE_APPLICATION_CREDENTIALS 身份验证未设置(尽管我预计会出现错误)。或者我缺少其他东西?我已将我的 package.json 和我的 Dockerfile 附加在底部供参考。

我在 npm 和机器上安装了 SoX(使用 Flexible env 和 Dockerfile 进行设置)。

知道什么可能导致此错误/如何调试/如何修复?

这是我设置连接的代码;您可以在调用的 recorder.record 上看到错误处理程序。

var constants = require('../conf/consts');

function GCPSpeechToTextService(socket) {

  console.log('GCPSpeechToTextService() called');
  const msg = 'Hello from server. GCP Speech Started!';
  socket.emit(constants.SOCKET_NEW_MSG_STRING, msg);

  const recorder = require('node-record-lpcm16');
  const speech = require('@google-cloud/speech');
  const encoding = 'LINEAR16';
  const sampleRateHertz = 16000;
  const languageCode = 'en-GB'; // en-US etc. This really improves recognition!

  const request = {
    config: {
      encoding: encoding,
      sampleRateHertz: sampleRateHertz,
      languageCode: languageCode,
      single_utterance: true, // Sends voice immediately after speaking, ideal for clear/not clear prompts
    },
    interimResults: true,
  };

  // // Create a recognize stream
  const client = new speech.SpeechClient();
  const recognizeStream = client
    .streamingRecognize(request)
    .on('error', (error) => console.log('ERROR with streamingRecognize', error))
    .on('data', data => {
      console.log('>> data: ', data); //.results[0].alternatives);
      var msg = data.results[0] && data.results[0].alternatives[0]
          ? data.results[0].alternatives[0].transcript
          : 'ERROR:TIMELIMIT Reached transcription time limit';
      process.stdout.write(msg);
    
      // Send it to the client
      socket.emit(constants.SOCKET_NEW_MSG_STRING, msg);
    });

  // Start recording and send the microphone input to the Speech API.
  console.log('>set recorder...');
  recorder
    .record({
      sampleRateHertz: sampleRateHertz,
      threshold: 0.1,
      verbose: false,
      recordProgram: 'sox',
      recordProgram: 'sox',
      silence: '10.0',
    })
    .stream()
    .on('error', (error) => console.log('ERROR with recorder.record', error))
    .on('end', () => console.log('> END recorder.record'))
    .pipe(recognizeStream);
}

module.exports = GCPSpeechToTextService;

package.json 依赖:

"dependencies": {
    "@google-cloud/speech": "^4.1.4",
    "cookie-parser": "~1.4.4",
    "cpx": "^1.5.0",
    "debug": "~2.6.9",
    "express": "^4.16.4",
    "express-ws": "^4.0.0",
    "http-errors": "~1.6.3",
    "jade": "^1.11.0",
    "morgan": "~1.9.1",
    "node-record-lpcm16": "^1.0.1",
    "pug": "^3.0.0",
    "socket.io": "^3.0.3",
    "sox": "^0.1.0"
  }

和我的 Dockerfile:

WORKDIR /app
COPY package.json /app/package.json
RUN apt-get update \
 && apt-get install -y sox libsox-fmt-all
RUN npm install
COPY . /app
EXPOSE 8080
CMD ["npm", "start"]
4

0 回答 0