index.js
function record(options) {
  return new Promise((res, rej) => {
    var micInstance = mic({
      rate: "16000",
      channels: "1",
      debug: true,
      exitOnSilence: 3,
    });

    var micInputStream = micInstance.getAudioStream();

    // Encode the file as Opus in an Ogg container, to send to server.
    var rate = 16000;
    var channels = 1;
    var opusEncodeStream = new opus.Encoder(rate, channels);
    var oggEncoder = new ogg.Encoder();

    micInputStream.pipe(opusEncodeStream).pipe(oggEncoder.stream());

    var bufs = [];

    oggEncoder.on("data", function(buffer) {
      bufs.push(buffer);
    });

    oggEncoder.on("end", function() {
      // Package up encoded recording into buffer to send
      // over the network to the API endpoint.
      var buffer = Buffer.concat(bufs);

      sendRecordingToServer(buffer, options)
        .then((results) => {
          res(results);
        })
        .catch((err) => {
          console.error("ERR", err);
          rej(err);
        });
    });

    micInputStream.on("silence", function() {
      // Stop recording at first silence after speaking.
      micInstance.stop();
    });

    micInputStream.on("error", function(err) {
      console.error("Error in Input Stream: " + err);
    });

    micInstance.start();
  });
}
function record(options) {
  return new Promise((res, rej) => {
    var micInstance = mic({
      rate: "16000",
      channels: "1",
      debug: true,
      exitOnSilence: 3,
    });

    var micInputStream = micInstance.getAudioStream();

    // Encode the file as Opus in an Ogg container, to send to server.
    var rate = 16000;
    var channels = 1;
    var opusEncodeStream = new opus.Encoder(rate, channels);
    var oggEncoder = new ogg.Encoder();

    micInputStream.pipe(opusEncodeStream).pipe(oggEncoder.stream());

    var bufs = [];

    oggEncoder.on("data", function(buffer) {
      bufs.push(buffer);
    });

    oggEncoder.on("end", function() {
      // Package up encoded recording into buffer to send
      // over the network to the API endpoint.
      var buffer = Buffer.concat(bufs);

      sendRecordingToServer(buffer, options)
        .then((results) => {
          res(results);
        })
        .catch((err) => {
          console.error("ERR", err);
          rej(err);
        });
    });

    micInputStream.on("silence", function() {
      // Stop recording at first silence after speaking.
      micInstance.stop();
    });

    micInputStream.on("error", function(err) {
      console.error("Error in Input Stream: " + err);
    });

    micInstance.start();
  });
}
function record ( options ) { return new Promise ( ( res , rej ) => { var micInstance = mic ( { rate : "16000" , channels : "1" , debug : true , exitOnSilence : 3 , } ) ; var micInputStream = micInstance . getAudioStream ( ) ; // Encode the file as Opus in an Ogg container, to send to server. var rate = 16000 ; var channels = 1 ; var opusEncodeStream = new opus . Encoder ( rate , channels ) ; var oggEncoder = new ogg . Encoder ( ) ; micInputStream . pipe ( opusEncodeStream ) . pipe ( oggEncoder . stream ( ) ) ; var bufs = [ ] ; oggEncoder . on ( "data" , function ( buffer ) { bufs . push ( buffer ) ; } ) ; oggEncoder . on ( "end" , function ( ) { // Package up encoded recording into buffer to send // over the network to the API endpoint. var buffer = Buffer . concat ( bufs ) ; sendRecordingToServer ( buffer , options ) . then ( ( results ) => { res ( results ) ; } ) . catch ( ( err ) => { console . error ( "ERR" , err ) ; rej ( err ) ; } ) ; } ) ; micInputStream . on ( "silence" , function ( ) { // Stop recording at first silence after speaking. micInstance . stop ( ) ; } ) ; micInputStream . on ( "error" , function ( err ) { console . error ( "Error in Input Stream: " + err ) ; } ) ; micInstance . start ( ) ; } ) ; }
Listen, record, send to server. Promise that returns array of results from server.
Listen record send to server . Promise that returns array of results from server .