opus-encode

2.0.2 • Public • Published

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

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.0.2
    2
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 2.0.2
    2
  • 2.0.1
    1
  • 2.0.0
    1
  • 1.0.0
    1

Package Sidebar

Install

npm i opus-encode

Weekly Downloads

4

Version

2.0.2

License

Unlicense

Last publish

Collaborators

  • psirenny