title: Streaming microphone through sockets using socket IO and Node JS
body: <p>I am making an application where I want the user to use their mic (on their phone) and be able to talk to each other in the game lobby. However, this has proven to be more than difficult.</p>
  
  <p>I am using Node JS socket io and socket io stream</p>
  
  <p>on my client I am using the audio api to take my microphones input ( I am not really worried about this all that much because I am going to make this a Native IOS app)</p>
  
  <pre><code>    navigator.getUserMedia = ( navigator.getUserMedia ||
                       navigator.webkitGetUserMedia ||
                       navigator.mozGetUserMedia ||
                       navigator.msGetUserMedia);
  
      if (navigator.getUserMedia) {
            navigator.getUserMedia (
              // constraints
            {
              video: false,
              audio: true
            },
  
  
      function(localMediaStream) {
          var video = document.querySelector('audio');
          video.src = window.URL.createObjectURL(localMediaStream);
         lcm = localMediaStream;
         var audioContext = window.AudioContext;
         var context = new audioContext();
         var audioInput = context.createMediaStreamSource(localMediaStream);
         var bufferSize = 2048;
        // create a javascript node
        var recorder = context.createScriptProcessor(bufferSize, 1, 1);
        // specify the processing function
        recorder.onaudioprocess = recorderProcess;
        // connect stream to our recorder
        audioInput.connect(recorder);
        // connect our recorder to the previous destination
        recorder.connect(context.destination);
        },
  
    // errorCallback
    function(err) {
       console.log("The following error occured: " + err);
       $("video").remove();
       alert("@#");
            }
          );
              } else {
                   console.log("getUserMedia not supported");
              }
  
          function recorderProcess(e) {
              var left = e.inputBuffer.getChannelData(0);
              window.stream.write(convertFloat32ToInt16(left));
             //var f = $("#aud").attr("src");
             var src = window.URL.createObjectURL(lcm);
              ss(socket).emit('file', src, {size: src.size});
             ss.createBlobReadStream(src).pipe(window.stream);
            //ss.createReadStream(f).pipe(widnow.stream);
  
              }
           function convertFloat32ToInt16(buffer)
         {
              l = buffer.length;
              buf = new Int16Array(l);
              while (l--) {
                 buf[l] = Math.min(1, buffer[l])*0x7FFF;
                }      
             return buf.buffer;
           }
  
  
  
               });
  
           ss(socket).on('back', function(stream, data) {
  
                  //console.log(stream);
  
                  var video = document.querySelector('audio');
                video.src = window.URL.createObjectURL(stream);
                console.log("getting mic data");
              });
  </code></pre>
  
  <p>i which I can successfully listen to my self speak on the microphone. I am using the stream socket to create a blob to upload to my server...</p>
  
  <pre><code>        index.ss(socket).on('file', function(stream, data) {
                  console.log("getting stream");
                 var filename = index.path.basename(data.name);
               //var myfs = index.fs.createWriteStream(filename);
  
              var fileWriter = new index.wav.FileWriter('demo.wav', {
                  channels: 1,
                  sampleRate: 48000,
                  bitDepth: 16
               });
            var streams = index.ss.createStream();
            streams.pipe(fileWriter);
  
          index.ss(socket).emit('back', fileWriter, {size: fileWriter.size});
  
  
          });
  </code></pre>
  
  <p>I cannot get the stream to write to a file or even a temporary buffer, and Then stream back to a client so I can then play or "stream" the audio real time. After a while the server crashes with saying that the pipe is not writable.</p>
  
  <p>Has anyone else encountered this?</p>