LoginSignup
1
1

More than 5 years have passed since last update.

Google Cloud Speech API on Windows 10 Nodeを使うときに困ったこと

Posted at

最近Google Cloud Speech APIをNodeで触り始めました。
サンプルを動かしながら覚えていこうとしていたのですが、
すんなりと動いてくれませんでした。。。
今回はWindows環境でサンプルを動かすまでの設定を紹介します。

直面したエラー

Listening, press Ctrl+C to stop.
events.js:183
      throw er;  Unhandled 'error' event
Error: spawn rec ENOENT
    at _errnoException (util.js:1022:11)
    at Process.ChildProcess._handle.onexit 

今回の環境

OS:windows 10
Node:8.9.4
SoX:14-4-2
node-record-lpcm16:0.3.0
google-cloud/speech:1.0.1
サンプルコードページ:音声ストリームでのストリーミング音声認識

対策

1.node-record-lpcm16を使うときはrecordProgramに'sox'を指定する

recordProgramはサンプルコードでは'rec'になっていますが、
Windows環境で'sox'を設定してください。

2. node-record-lpcm16のindex.jsを書き換える

node-record-lpcm16のindex.jsの29行付近を下記のように書き換えてください。

index.js
// Capture audio stream
  var cmd, cmdArgs, cmdOptions
  switch (options.recordProgram) {
    // On some Windows machines, sox is installed using the "sox" binary
    // instead of "rec"
    case 'sox':
      // ---- 追記ここから ----
      cmd = 'sox'
      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
      ]
      break
      // ---- 追記ここまで----
    case 'rec':
    default:
      cmd = options.recordProgram
      cmdArgs = [
        '-q',                     // show no progress
        '-r', options.sampleRate, // sample rate
        '-c', '1',                // channels
        '-e', 'signed-integer',   // sample encoding
        '-b', '16',               // precision (bits)
        '-t', 'wav',              // audio type
        '-',                      // pipe
            // end on silence
        'silence', '1', '0.1', options.thresholdStart || options.threshold + '%',
        '1', options.silence, options.thresholdEnd || options.threshold + '%'
      ]
      break
    // On some systems (RasPi), arecord is the prefered recording binary

デフォルトのcmdArgsに2つオプションが足りないようです。
'-t', 'waveaudio', // input-type
'-d', // use default recording device

この対策を行った結果サンプルコードが動かせるようになりました。

参考

https://github.com/gillesdemey/node-record-lpcm16/issues/8
https://github.com/gillesdemey/node-record-lpcm16#options

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1