node package manager

opus-encode

opus-encode

Encode audio buffer streams to ogg opus. AFAIK, this only works in the browser.

Installation

npm install --save opus-encode

Usage

var concat = require('concat-stream');
var encode = require('opus-encode');
var stream = require('stream');

// request an audio file
var req = new XMLHttpRequest();
req.open('GET', '/path/to/file.wav', true);
req.responseType = 'arraybuffer';

req.onload = function () {
  var res = req.response;
  var ctx = new AudioContext();

  // decode the file to audio buffers
  ctx.decodeAudioData(res, function (buf) {
    var audio = new stream.Readable({objectMode: true});

    // convert the audio buffers to an object stream
    audio._read = function () {
      this.push(buf);
      this.push(null);
    };

    // create a stream that concats all encoded data
    var doSomething = concat(function (buf) {
      // this blob contains the ogg opus encoded audio
      var blob = new Blob([buf.toArrayBuffer()]);
    });

    audio.pipe(encode()).pipe(doSomething);
  });
};

req.send();

Disclaimer

I don't pretend to understand how the encoder actually works. I modified the encoder written by chris-rudmin in his fork of RecordJS.

Tests

In order to run tests in the browser make sure you open your browser with the following flag:

--allow-file-access-from-files