'use strict';

var spawn     = require('child_process').spawn,
    stream    = require('stream');


var recording, // Will hold our passthrough audio stream
    cp;        // Recording process

// returns a Readable stream
exports.start = function (options) {
  recording = new stream.PassThrough(); // Create the passthrough audio stream
  rec = null; // Empty out possibly dead recording process

  var defaults = {
    sampleRate : 16000,
    compress   : false,
    threshold  : 0.5,
    verbose    : false,
    recordProgram : 'rec'
  };

  options = Object.assign(defaults, options);

  // Capture audio stream
  var cmd, cmdArgs;
  switch (options.recordProgram) {
    // On some Windows machines, sox is installed using the "sox" binary
    // instead of "rec"
    case 'sox':
    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.threshold + '%',
            '1','1.0', options.threshold + '%'
        ];
      break;
    // On some systems (RasPi), arecord is the prefered recording binary
    case 'arecord':
        cmd = 'arecord';
        cmdArgs = [
            '-q',                     // show no progress
            '-r', options.sampleRate, // sample rate
            '-c', '1',                // channels
            '-t', 'wav',              // audio type
            '-f', 'S16_LE',           // Sample format
            '-',                      // pipe
        ];
      break;
  }

  if (options.verbose)
    console.log('Recording with sample rate', options.sampleRate + '...');

  // Spawn audio capture command
  cp = spawn(cmd, cmdArgs, { encoding: 'binary' })
  var rec = cp.stdout;

  if (options.verbose)
    console.time('End Recording');

  // Fill recording stream with stdout
  rec.on('data', function (data) {

    if (options.verbose)
      console.log('Recording %d bytes', data.length);

    recording.write(new Buffer(data, 'binary')); // convert to binary buffer
  });

  // Verbose ending
  rec.on('end', function () {

    if (options.verbose)
      console.timeEnd('End Recording');

    recording.end();
  });

  return recording;

};

exports.stop = function () {
  if (typeof cp === 'undefined')
  {
    console.log('Please start a recording first');
    return false;
  }

 cp.kill(); // Exit the spawned process, exit gracefully
 return recording;
};

@@ -101,75 +101,11 @@
 var 
-recording, // Will hold our passthrough audio stream%0A    cp;       
+cp;
  // 
@@ -193,87 +193,8 @@
 ) %7B%0A
-  recording = new stream.PassThrough(); // Create the passthrough audio stream%0A
   re
@@ -2159,86 +2159,8 @@
 th);
-%0A%0A    recording.write(new Buffer(data, 'binary')); // convert to binary buffer
 %0A  %7D
@@ -2282,30 +2282,8 @@
 g');
-%0A%0A    recording.end();
 %0A  %7D
@@ -2298,24 +2298,17 @@
 turn rec
-ording;%0A
+;
 %0A%7D;%0A%0Aexp
@@ -2509,22 +2509,15 @@
  return 
-recording
+cp
 ;%0A%7D;%0A

remove PassThrough and use cross-spawn stream directly. resolves #16