Opus & Wave Recorder
- Libopus: v1.3 compiled with emscripten 1.38.21
- speexDSP: 1.2RC3 compiled with emscripten 1.38.21
The required files are in the
dist folder. Unminified sources are in
Examples for recording, encoding, and decoding are in
Recorder object is available in the global namespace and supports CommonJS and AMD imports.
var rec = new Recorder([config]);
Creates a recorder instance.
- config - An optional configuration object.
General Config options
- encoderPath - (optional) Path to
waveWorker.min.jsworker script. Defaults to
- mediaTrackConstraints - (optional) Object to specify media track constraints. Defaults to
- monitorGain - (optional) Sets the gain of the monitoring output. Gain is an a-weighted value between
1. Defaults to
- numberOfChannels - (optional) The number of channels to record.
2= stereo. Defaults to
2channels are supported.
- recordingGain - (optional) Sets the gain of the recording input. Gain is an a-weighted value between
1. Defaults to
Config options for OGG OPUS encoder
- encoderApplication - (optional) Supported values are:
2049- Full Band Audio,
2051- Restricted Low Delay. Defaults to
- encoderBitRate - (optional) Target bitrate in bits/sec. The encoder selects an application-specific default when this is not specified.
- encoderComplexity - (optional) Value between 0 and 10 which determines latency and processing for encoding.
0is fastest with lowest complexity.
10is slowest with highest complexity. The encoder selects a default when this is not specified.
- encoderFrameSize - (optional) Specifies the frame size in ms used for encoding. Defaults to
- encoderSampleRate - (optional) Specifies the sample rate to encode at. Defaults to
48000. Supported values are
- maxFramesPerPage - (optional) Maximum number of frames to collect before generating an Ogg page. This can be used to lower the streaming latency. The lower the value the more overhead the ogg stream will incur. Defaults to
- originalSampleRateOverride - (optional) Override the ogg opus 'input sample rate' field. Google Speech API requires this field to be
- resampleQuality - (optional) Value between 0 and 10 which determines latency and processing for resampling.
0is fastest with lowest quality.
10is slowest with highest quality. Defaults to
- streamPages - (optional)
dataAvailableevent will fire after each encoded page. Defaults to
- reuseWorker - (optional) If true, the worker is not automatically destroyed when
stopis called. Instead, it is reused for subsequent
startcalls and must be explicitly destroyed after stopping by calling
destroyWorker. Defaults to
Config options for WAV recorder
- wavBitDepth - (optional) Desired bit depth of the WAV file. Defaults to
16. Supported values are
32bits per sample.
pause will keep the stream and monitoring alive, but will not be recording the buffers. If
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.
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
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
rec.start( [sourceNode] )
start Initalizes the worker, audio context, and an audio stream and begin capturing audio. Returns a promise which resolves when recording is started. Will callback
onstart when started. Optionally accepts a source node which can be used in place of initializing the microphone stream. For iOS support,
start needs to be initiated from a user action. If a sourceNode is provided, then the stream and audioContext will need to be managed by the implementation.
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.
destroyWorker will destroy the worker freeing up the browser resources. If the recorder is re-started, a new worker will be created. Note that
destroyWorker is automatically called when stopping unless
reuseWorker is true.
loadWorker triggers pre-loading of the worker. This can reduce the startup latency when calling
destroyWorker to clean the worker when the recorder is stopped/not started, or it will be automatically cleaned up after stopping unless
reuseWorker is true.
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.
Returns a truthy value indicating if the browser supports recording.
rec.ondataavailable( arrayBuffer )
A callback which returns an array buffer of audio data. If
true, this will be called with each page of encoded audio. If
false, this will be called when the recording is finished with the complete data.
A callback which occurs when media recording is paused.
A callback which occurs when media recording resumes after being paused.
A callback which occurs when media recording starts.
A callback which occurs when media recording ends.
- To be able to read the mic stream, the page must be served over https
- iOS Safari requires
rec.start()to be called from a user initiated event
- macOS Safari v11 native opus playback is not yet supported
- iOS Safari v11 native opus playback is not yet supported
- Microsoft Edge native opus playback is not yet supported
- Chrome v58
- Firefox v53
- Microsoft Edge v41
- Opera v44
- macOS Safari v11
- iOS Safari v11
- IE 11 and below
- iOS 11 Chrome
- iOS 11.2.2 and iOS 11.2.5 are not working due to a regression in WebAssembly: https://bugs.webkit.org/show_bug.cgi?id=181781
- Firefox does not support sample rates above 48000Hz: https://bugzilla.mozilla.org/show_bug.cgi?id=1124981
- macOS Safari v11 does not support sample rates above 44100Hz
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 npm dependencies:
checkout, compile and create the dist from sources:
npm run make
Running the unit tests:
Clean the dist folder and git submodules: