Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

最近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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした