Electron - 无法使用 Google 的 Speech to Text 引擎

Electron - Cannot use Google's Speech to Text engine

我想 link Google Speech to Text engine 用我的麦克风。
我找到 this 页面,将代码复制到我的 renderer.ts 文件(取消注释 const 的行),但是当 运行ning - 由于第 7 行出现以下错误(const client = new speech.SpeechClient();):

是的,我确实尝试 运行 yarn install --force(因为我主要使用 Yarn)和 npm rebuild,以及 yarn add grpc,但是问题依旧。

renderer.ts:

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();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'en-US';

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: 'rec', // Try also "arecord" or "sox"
        silence: '10.0',
    })
    .on('error', console.error)
    .pipe(recognizeStream);

console.log('Listening, press Ctrl+C to stop.');

感谢您的帮助!

为了在 Electron 上使用这个库,你必须添加额外的安装参数来专门为 Electron 安装。 Electron 具有 using native Node modules.

的通用指令

特别是对于 gRPC,使用您拥有的 Electron 版本,您应该能够在 运行

之前获得它
npm rebuild --runtime=electron --target=4.0.0

特别针对 Electron 4 的警告:由于 Electron 4.0.4 中引入的重大更改,grpc 将无法与该版本一起使用。它仍然适用于以前版本的 Electron 4。

解决我的问题的是几个来源的组合。

首先,安装gRPC (thanks to murgatroid99 and Nicolas Noble):

npm rebuild grpc --runtime=electron --target=4.0.3

我假设这会安装 gRPC 二进制文件,因此我可以在 Electron 4.0.3 上使用它们(不是最新的,因为它似乎不适用于最新的)
虽然因为它只是安装 gRPC,我仍然需要单独安装 Electron,所以:

yarn add -D electron@4.0.3

如果你想保持一行:

npm rebuild grpc --runtime=electron --target=4.0.3 && yarn add -D electron@4.0.3

然后我收到 this 错误并 Google 解决了它,但没有找到明确的答案。
然后我意识到,感谢this article (translated to English), that the module node-record-lpcm16 was just being used as a bridge from my software to SoX
所以实际上,这个错误只是关于无法从命令行使用 sox 程序(无法生成进程),至少不是纯粹基于仅键入 sox(我可以CMD,但由于某些原因我的应用程序不能)。
因此我:

1) 将 recordProgram: 'rec' 更改为 recordProgram: 'sox' (renderer.ts)
2) 输入 node_modules\node-record-lpcm16\index.js
3) 改变

case 'sox':
  var cmd = 'sox';
  var cmdArgs = [
    '-q',                     // show no progress
    '-t', 'waveaudio',        // audio type
    '-d',                     // use default recording device
    '-r', options.sampleRate, // sample rate
    '-c', options.channels,   // channels
    '-e', 'signed-integer',   // sample encoding
    '-b', '16',               // precision (bits)
    '-',                      // pipe
    // end on silence
    'silence', '1', '0.1', options.thresholdStart || options.threshold + '%',
    '1', options.silence, options.thresholdEnd || options.threshold + '%'
  ];
  break

case 'sox':
  var cmd = 'C:\Program Files (x86)\sox-14-4-2\sox.exe';
  var cmdArgs = [ // ^ SPECIFYING FULL PATH
    '-q',                     // show no progress
    '-t', 'waveaudio',        // audio type
    '-d',                     // use default recording device
    '-r', options.sampleRate, // sample rate
    '-c', options.channels,   // channels
    '-e', 'signed-integer',   // sample encoding
    '-b', '16',               // precision (bits)
    '-',                      // pipe
    // end on silence
    'silence', '1', '0.1', options.thresholdStart || options.threshold + '%',
    '1', options.silence, options.thresholdEnd || options.threshold + '%'
  ];
  break

然后,事实证明,如果不添加上述文章中提到的额外位,录音麦克风将无法工作,所以:

case 'sox':
  var cmd = 'C:\Program Files (x86)\sox-14-4-2\sox.exe';
  var cmdArgs = [
    '-q',                     // show no progress
    '-t', 'waveaudio',        // audio type
    '-d',                     // use default recording device
    '-r', options.sampleRate, // sample rate
    '-c', options.channels,   // channels
    '-e', 'signed-integer',   // sample encoding
    '-t', 'raw', // Added
    '-b', '16',               // precision (bits)
    '-',                      // pipe
    // end on silence
    'silence', '1', '0.1', options.thresholdStart || options.threshold + '%',
    '1', options.silence, options.thresholdEnd || options.threshold + '%'
  ];
  break

如果您遇到 Google 云凭据身份验证问题,请参阅

这些解决了我无法录制的问题!

然后我遇到了一个问题,Google 将音频流限制为 65 秒,所以通过堆栈跟踪行,我追踪了导致问题的行并评论了这些行,直到我到达第二个renderer.ts 中的可能输出(输出 Reached transcription time limit, press Ctrl+C),所以我只是将变量和记录函数包装在一个函数中并递归调用该函数,如下所示:

function startRecording() {
    // Create a recognize stream
    const recognizeStream = client
        .streamingRecognize(request)
        .on('error', console.error)
        .on('data', data => {
            if (data.results[0] && data.results[0].alternatives[0]) {
                console.log(`Transcription: ${data.results[0].alternatives[0].transcript}\n`);
            } else {
                console.log(`\n\nReached transcription time limit, press Ctrl+C\n`);
                startRecording();
            }
        });

    // 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);
}

startRecording();

这个解决方案似乎可以解决那个问题!