title: Using Google NodeJS speech-to-text with audio from client microphone
body: <p>I am trying to use google's speech-to-text nodeJS library (<a href="https://github.com/googleapis/nodejs-speech" rel="nofollow noreferrer">https://github.com/googleapis/nodejs-speech</a>) and stream audio from the client's microphone input with navigator.mediaDevices.getUserMedia.</p>
  
  <p>I was able to <em>stream microphone with sox to nodejs-speech</em> streamingRecognize and it was working. 
  I was also able to <em>stream audio from the client side and make and pipe it to the speaker on the server side</em>. <strong>However, when I try to pipe it to streamingRecognize it doesn't recognize any word.</strong></p>
  
  <p><strong>Server-side</strong></p>
  
  <pre><code>var io = require("socket.io")(server);
  const speech = require('@google-cloud/speech');
  
  const request = {
      config: {
          encoding: encoding,
          sampleRateHertz: sampleRateHertz,
  
          languageCode: languageCode,
      },
      interimResults: true,
      //singleUtterance: false
  };
  let recognizeStream = speechClient
      .streamingRecognize(request)
      .on('data', data =&gt; {
          console.log(data);
          process.stdout.write(
              data.results[0] &amp;&amp; data.results[0].alternatives[0] ?
              `Transcription: ${data.results[0].alternatives[0].transcript}\n` :
              `\n\nReached transcription time limit, press Ctrl+C\n`
          )
      });
  io.on("connection", function (client) {
      client.on("userSpeaking", function (data) {
          if (recognizeStream !== null) {
              recognizeStream.write(new Uint8Array(data));
          }
      });
  });
  </code></pre>
  
  <p><strong>Client-side</strong></p>
  
  <pre><code>function convertFloat32ToInt16(buffer) {
      let l = buffer.length;
      let buf = new Int16Array(l);
      while (l--) {
          buf[l] = Math.min(1, buffer[l]) * 0x7FFF;
      }
      return buf.buffer;
  }
  AudioContext = window.AudioContext || window.webkitAudioContext;
  context = new AudioContext();
  processor = context.createScriptProcessor(bufferSize, 1, 1);
  processor.connect(context.destination);
  context.resume();
  
  function microphoneProcess(e) {
    var left = e.inputBuffer.getChannelData(0);
    var left16 = convertFloat32ToInt16(left);
    socket.emit('userSpeaking', left16);
  }
  navigator.mediaDevices
    .getUserMedia({
      video: false,
      audio: true
    }, {
      type: 'audio',
      sampleRate: 16000,
      desiredSampleRate: 16000,
      audioBitsPerSecond: 16000,
      mimeType: 'audio/webm;codecs=opus'
    })
    .then((stream) =&gt; {
      globalStream = stream;
      input = context.createMediaStreamSource(stream);
      input.connect(processor);
  
      processor.onaudioprocess = function (e) {
        microphoneProcess(e);
      };
    })
    .catch(console.error);
  </code></pre>