Home

Awesome

Opus & Wave Recorder

A javascript library to encode the output of Web Audio API nodes in Ogg Opus or WAV format using WebAssembly.

Libraries Used

Required Files

The required files are in the dist folder. Unminified sources are in dist-unminified. Examples for recording, encoding, and decoding are in examples folder.


Usage

Constructor

The Recorder object is available in the global namespace and supports CommonJS and AMD imports.

var rec = new Recorder([config]);

Creates a recorder instance.


General Config options

Config options for OGG OPUS encoder

Config options for WAV recorder


Instance Methods

rec.close()

close will close the audioContext, destroy the workers, disconnect the audio nodes and close the mic stream. A new Recorder instance will be required for additional recordings. if a sourceNode was provided in the initial config, then the implementation will need to close the audioContext and close the mic stream.

rec.pause([flush])

pause will keep the stream and monitoring alive, but will not be recording the buffers. If flush is true and streamPages is set, any pending encoded frames of data will be flushed, and it will return a promise that only resolves after the frames have been flushed to ondataavailable. Will call the onpause callback when paused. Subsequent calls to resume will add to the current recording.

rec.resume()

resume will resume the recording if paused. Will call the onresume callback when recording is resumed.

rec.setRecordingGain( gain )

setRecordingGain will set the volume on what will be passed to the recorder. Gain is an a-weighted value between 0 and 1.

rec.setMonitorGain( gain )

setMonitorGain will set the volume on what will be passed to the monitor. Monitor level does not affect the recording volume. Gain is an a-weighted value between 0 and 1.

rec.start()

start Begins a new recording. Returns a promise which resolves when recording is started. Will callback onstart when started. start needs to be initiated from a user action (click or touch) so that the audioContext can be resumed and the stream can have audio data.

rec.stop()

stop will cease capturing audio and disable the monitoring and mic input stream. Will request the recorded data and then terminate the worker once the final data has been published. Will call the onstop callback when stopped.


Instance Fields

rec.encodedSamplePosition

Reads the currently encoded sample position (the number of samples up to and including the most recent data provided to ondataavailable). For Opus, the encoded sample rate is always 48kHz, so a time position can be determined by dividing by 48000.


Static Methods

Recorder.isRecordingSupported()

Returns a truthy value indicating if the browser supports recording.

Static Properties

Recorder.version

The version of the library.


Callback Handlers

rec.ondataavailable( arrayBuffer )

A callback which returns an array buffer of audio data. If streamPages is true, this will be called with each page of encoded audio. If streamPages is false, this will be called when the recording is finished with the complete data.

rec.onpause()

A callback which occurs when media recording is paused.

rec.onresume()

A callback which occurs when media recording resumes after being paused.

rec.onstart()

A callback which occurs when media recording starts.

rec.onstop()

A callback which occurs when media recording ends.


Getting started with webpack

Add to your webpack.config.js before all other loaders.

module.exports = {
  module: {
    rules: [
      {
        test: /encoderWorker\.min\.js$/,
        use: [{ loader: 'file-loader' }]
      }
    ]
  }
};

Then get the encoderPath using an import

import Recorder from 'opus-recorder';
import encoderPath from 'opus-recorder/dist/encoderWorker.min.js';

const rec = new Recorder({ encoderPath });

Gotchas


Browser Support

Supported:

Unsupported:


Known Issues


Building from sources

Prebuilt sources are included in the dist folder. However below are instructions if you want to build them yourself. Opus and speex are compiled without SIMD optimizations. Performace is significantly worse with SIMD optimizations enabled.

Mac: Install autotools using MacPorts

port install automake autoconf libtool pkgconfig

Windows: Install autotools using MSYS2

pacman -S make autoconf automake libtool pkgconfig

Install Node.js

Install EMScripten

Install npm dependencies:

npm install

checkout, compile and create the dist from sources:

npm run make

Running the unit tests:

npm test

Clean the dist folder and git submodules:

make clean