【问题标题】:Electron - Cannot use Google's Speech to Text engineElectron - 无法使用 Google 的 Speech to Text 引擎
【发布时间】:2019-03-10 17:43:49
【问题描述】:

我想将Google Speech to Text engine 与我的麦克风关联。
我找到了this 页面,将代码复制到我的renderer.ts 文件中(取消注释带有const 的行),但是在运行时- 由于第7 行(const client = new speech.SpeechClient();)而出现以下错误:
@987654323 @

是的,我确实尝试同时运行 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.');

感谢您的帮助!

【问题讨论】:

  • 你能分享你的代码吗?
  • 您是否尝试过自己安装 gRPC 二进制模块(如错误所述)?
  • @Ahm。我从字面上复制了我链接的第二页 - 第二个代码块,它没有工作。
  • @Grynets 我也试过了,但没有运气。
  • 你确定它是相关的吗?该存储库是关于 Electron、Go 和 gRPC 之间的集成。我不是在 Go 中编程

标签: node.js typescript speech-recognition


【解决方案1】:

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

首先,安装gRPC(感谢murgatroid99Nicolas 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 错误并用谷歌搜索,但没有找到明确的答案。
然后我意识到,感谢this article(翻译成英文),模块node-record-lpcm16 只是被用作我的软件与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 Cloud 凭据身份验证问题,请参阅 this answer

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

然后我遇到了一个问题,即 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();

而这个解决方案似乎解决了这个问题!

【讨论】:

  • 此解决方案仅在当时具有相关性。这些天来,您需要找到一个使用最新版本的 Electron(评论时为 11.1.1)的不同解决方案,并且上述node-record-lpcm16index.js 具有现代 JS 代码,因此我的解决方案不再适用于今天。
【解决方案2】:

为了在 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。

【讨论】:

  • 这是我在上一段中警告过的 Electron 4.0.4 中的重大变化。正如我所说,该库不适用于该特定版本。
  • 那么我该怎么做才能解决这个问题?
  • 不使用 ^ 或 ~ 作为标记回滚到 package.json 中的电子 4.0.3。
  • 在反复尝试安装grpc,然后尝试在版本4.0.3 中安装electron 之后,我成功收到了与Google Cloud 身份验证相关的另一个错误。在resolving this 之后,我唯一的错误是this
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
  • 2018-03-24
  • 2017-04-10
  • 1970-01-01
  • 2019-04-16
相关资源
最近更新 更多