libopus.js
This is a port of libopus to pure JavaScript using Emscripten for use in a browser environment.
If you do not plan to use your module in a browser-like environment, you should probably use node-opus instead which will almost certainly provide better performance at the cost of portability.
Usage
JavaScript wrappers are provided for the most common usage scenarios.
Be aware that opus is a stateful codec, therefore one and only one Encoder/Decoder should be used for one and only one stream of audio data. Packet loss also has to be signalled to the decoder.
Encoding raw PCM samples:
var Encoder = Encoder;var enc = rate: 48000 channels: 1 ;// Accepts either 16 bit signed integers (more space efficient)var result = enc;// or 32 bit floating point numbers (used by the Web Audio API)var result = enc;// result is a nodejs BuffersomeStream;
The encoder can also be used with node.js streams:
var Encoder = Encoder;var enc = ;// A stream can only process input in either the Int16 format or the Float32 formatvar encStream = enc;// The stream can be used directlyencStream;var result = encStream;// or just like any other node streamsomeRawInput;
Decoding compressed opus packets:
var Decoder = Decoder;var dec = rate: 48000 channels: 1 ;// Decode to Int16Array (more space efficient)var result = dec;// or to Float32Array (used by the Web Audio API)var result = dec;// Signaling a lost packetvar result = dec;
The decoder can also be used with node.js streams:
var Decoder = Decoder;var dec = ;// A stream can only produce output in either the Int16 format or the Float32 formatvar decStream = dec;// The stream can be used directlydecStream;var result = encStream;// or just like any other node streamsomeEncodedInput;// Signaling a lost packet in a stream is done by sending an empty BufferencStream;
The raw libopus module as generated by emscripten can also be accessed (this is dangerous if you do not know what you are doing):
var libopus = libopus;var version = libopus;var mem = libopus;libopus;
The Repacketizer, the Opus Multistream API, Opus Custom and some other parts have not been exported.
Building from source
Prebuilt libopus binaries are available in build/
.
Building these yourself is rather simple (assuming you have common build tools already installed):
- Install Emscripten
- Run
make clean
- Run
make
License
The full license texts are available in LICENSE.md
.
libopus.js itself uses the MIT license.
The Makefile has been extracted from Chris Rudmin's fork of Recorderjs which also uses the MIT license.
The native opus library is licensed under a three-clause BSD license. See the Opus Licensing page for more details. Therefore the generated libopus.js file is as well.