All Projects β†’ kbumsik β†’ Opus Media Recorder

kbumsik / Opus Media Recorder

Licence: other
MediaRecorder polyfill for Opus recording using WebAssembly

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Opus Media Recorder

Wasmer
πŸš€ The leading WebAssembly Runtime supporting WASI and Emscripten
Stars: ✭ 11,047 (+6847.8%)
Mutual labels:  emscripten, webassembly
Cobol Js Emscripten
Stars: ✭ 101 (-36.48%)
Mutual labels:  emscripten, webassembly
Emscripten Docker
Docker image with Emscripten to compile ASM.js and WebAssembly
Stars: ✭ 92 (-42.14%)
Mutual labels:  emscripten, webassembly
Magnum Plugins
Plugins for the Magnum C++11/C++14 graphics engine
Stars: ✭ 66 (-58.49%)
Mutual labels:  emscripten, webassembly
Crosswindow
πŸ’»πŸ“± A cross platform system abstraction library written in C++ for managing windows and performing OS tasks.
Stars: ✭ 155 (-2.52%)
Mutual labels:  emscripten, webassembly
Magnum Bootstrap
Bootstrap projects for Magnum C++11/C++14 graphics engine
Stars: ✭ 69 (-56.6%)
Mutual labels:  emscripten, webassembly
Dcmjs
dcmjs is a javascript cross-compile of dcmtk (dcmtk.org).
Stars: ✭ 92 (-42.14%)
Mutual labels:  emscripten, webassembly
Cppwasm Book
πŸ“š WebAssembly friendly programming with C/C++ -- Emscripten practice
Stars: ✭ 956 (+501.26%)
Mutual labels:  emscripten, webassembly
Opengl cmake skeleton
❀️ A ready to use cmake skeleton using GLFW, Glew and glm. πŸ‘
Stars: ✭ 118 (-25.79%)
Mutual labels:  emscripten, webassembly
Miniaudio
Single file audio playback and capture library written in C.
Stars: ✭ 1,889 (+1088.05%)
Mutual labels:  recording, emscripten
Wasmjit
Small Embeddable WebAssembly Runtime
Stars: ✭ 1,063 (+568.55%)
Mutual labels:  emscripten, webassembly
Modern Wasm Starter
πŸ›Έ Run C++ code on web and create blazingly fast websites! A starter template to easily create WebAssembly packages using type-safe C++ bindings with automatic TypeScript declarations.
Stars: ✭ 140 (-11.95%)
Mutual labels:  emscripten, webassembly
Waflash
A WebAssembly ActionScript 3.0 Flash player built with Emscripten
Stars: ✭ 46 (-71.07%)
Mutual labels:  emscripten, webassembly
Opus Stream Decoder
Instantly decode Ogg Opus audio streams in chunks with JavaScript & WebAssembly (Wasm)
Stars: ✭ 80 (-49.69%)
Mutual labels:  emscripten, webassembly
Cargo Web
A Cargo subcommand for the client-side Web
Stars: ✭ 1,026 (+545.28%)
Mutual labels:  emscripten, webassembly
Assortedwidgets
OpenGL GUI library
Stars: ✭ 92 (-42.14%)
Mutual labels:  emscripten, webassembly
Glchaos.p
3D GPUs Strange Attractors and Hypercomplex Fractals explorer - up to 256 Million particles in RealTime
Stars: ✭ 590 (+271.07%)
Mutual labels:  emscripten, webassembly
Securityworker
The best javascript code protection solution ever.
Stars: ✭ 626 (+293.71%)
Mutual labels:  emscripten, webassembly
Webassembly Raytracer
a performance comparison of a simple raytracer in JavaScript, asm.js, WebAssembly, and GLSL
Stars: ✭ 102 (-35.85%)
Mutual labels:  emscripten, webassembly
Rust Wasm Webpack Tutorial
Finished example project for my guide on setting up a Webpack project with Rust and WebAssembly
Stars: ✭ 122 (-23.27%)
Mutual labels:  emscripten, webassembly

opus-media-recorder

opus-media-recorder is a MediaRecorder API polyfill written in ES6 and WebAssembly. It aims for cross-browser Opus codec support with various audio formats such as Ogg and WebM. opus-media-recorder can be used as a polyfill, or it can replace the built-in MediaRecorder since opus-media-recorder supports more MIME types.

opus-media-recorder uses WebAssembly compiled from popular libraries (e.g libopus, libogg, libwebm, and speexdsp) to ensure good performance and standards-compliance.

Why opus-media-recorder?

opus-media-recorder Chrome Firefox iOS Edge
audio/ogg O X O X X
audio/webm O O X X X
audio/wav O X X X X

* Both audio/ogg and audio/webm refer containers for Opus audio codec.

Currently the MediaRecorder API suffers from the two problems:

  1. Not all browsers support MediaRecorder.
  2. Even the browsers that provides MediaRecorder don't support the same format.

opus-media-recorder tackles these problems by supporting all major modern browsers (Chrome, Firefox, iOS, and Edge) and by providing various formats.

By taking advantages of WebAssembly and Web Workers, opus-media-recorder tries to have minimum performace penalties of running encoders in a browser.

How to use

opus-media-recorder is compatible with the Mediastream Recording API standard.

Thing to know

  • The page must be served over HTTPS in order to record.
  • Being able to record does not always mean you can play it in a browser:
    • macOS/iOS Safari cannot play Opus natively yet.
    • Old Edge requires an extension to play Opus natively.
    • You can get an Opus decorder to play it. There are Opus decoders available, such as Chris Rudmin's Opus decoder.
    • Otherwise, users can download as a recording file and play it using apps like VLC.
  • When mimeType is not specified a default encoder is loaded depending on OS:
    • Chrome: audio/webm
    • Firefox: audio/ogg
    • Edge: audio/webm
    • iOS/macOS Safari: audio/wave - because they cannot play Opus at all.

JavaScript

For standard usages of MediaRecorder, see the MDN reference and other online resources. Our testing website and example section may be useful as well.

Examples

Installation

npm install --save-dev opus-media-recorder

Working with a bundler

Because opus-media-recorder needs to load a dedicated web worker and .wasm binaries, special configurations are necessary. In general:

import OpusMediaRecorder from 'opus-media-recorder';
// Choose desired format like audio/webm. Default is audio/ogg
const options = { mimeType: 'audio/ogg' }
// Web worker and .wasm configuration. Note: This is NOT a part of W3C standard.
const workerOptions = {
  encoderWorkerFactory: function () {
    // UMD should be used if you don't use a web worker bundler for this.
    return new Worker('.../path/to/opus-media-recorder/encoderWorker.umd.js')
  },
  OggOpusEncoderWasmPath: '.../path/to/opus-media-recorder/OggOpusEncoder.wasm',
  WebMOpusEncoderWasmPath: '.../path/to/opus-media-recorder/WebMOpusEncoder.wasm'
};

window.MediaRecorder = OpusMediaRecorder;
recorder = new MediaRecorder(stream, options, workerOptions);

Simple JavaScript example (webpack)

import MediaRecorder from 'opus-media-recorder';
// Use worker-loader
import EncoderWorker from 'worker-loader!opus-media-recorder/encoderWorker.js';
// You should use file-loader in webpack.config.js.
// See webpack example link in the above section for more detail.
import OggOpusWasm from 'opus-media-recorder/OggOpusEncoder.wasm';
import WebMOpusWasm from 'opus-media-recorder/WebMOpusEncoder.wasm';

// Non-standard options
const workerOptions = {
  encoderWorkerFactory: _ => new EncoderWorker(),
  OggOpusEncoderWasmPath: OggOpusWasm,
  WebMOpusEncoderWasmPath: WebMOpusWasm
};

let recorder;

function startRecording () {
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
    let options = { mimeType: 'audio/ogg' };
    // Start recording
    recorder = new MediaRecorder(stream, options, workerOptions);
    recorder.start();
    // Set record to <audio> when recording will be finished
    recorder.addEventListener('dataavailable', (e) => {
      audioElement.src = URL.createObjectURL(e.data);
    });
  });
}

// Recording should be started in user-initiated event like buttons
recordButton.addEventListener('click', startRecording);

// Stop recording
stopButton.addEventListener('click', () => {
  recorder.stop();
  // Remove β€œrecording” icon from browser tab
  recorder.stream.getTracks().forEach(i => i.stop());
})

HTML <script> tag

The OpusMediaRecorder object is available in the global namespace using UMD.

<!-- load OpusMediaRecorder.umd.js. OpusMediaRecorder will be loaded. -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/OpusMediaRecorder.umd.js"></script>
<!-- load encoderWorker.umd.js. This should be after OpusMediaRecorder. -->
<!-- This script tag will create OpusMediaRecorder.encoderWorker. -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/encoderWorker.umd.js"></script>

<script>
...
// If you already load encoderWorker.js using <script> tag,
// you don't need to define encoderWorkerFactory.
const workerOptions = {
  OggOpusEncoderWasmPath: 'https://cdn.jsdelivr.net/npm/[email protected]/OggOpusEncoder.wasm',
  WebMOpusEncoderWasmPath: 'https://cdn.jsdelivr.net/npm/[email protected]/WebMOpusEncoder.wasm'
};

// Replace MediaRecorder
window.MediaRecorder = OpusMediaRecorder;
let recorder = new MediaRecorder(stream, {}, workerOptions);
...
</script>

Use opus-media-recorder only when a browser doesn't support it

// Check if MediaRecorder available.
if (!window.MediaRecorder) {
  window.MediaRecorder = OpusMediaRecorder;
}
// Check if a target format (e.g. audio/ogg) is supported.
else if (!window.MediaRecorder.isTypeSupported('audio/ogg;codecs=opus')) {
  window.MediaRecorder = OpusMediaRecorder;
}

Browser support

Supported:

  • Chrome >= 58
  • Firefox >= 53
  • Microsoft Edge >= 41
  • Safari (macOS and iOS) >= 11

Browsers with issues:

MIME Type support

  • audio/webm
  • audio/webm; codecs=opus
  • audio/ogg
  • audio/ogg; codecs=opus
  • audio/wav or audio/wave

Limitations

  • Does not support Video recording.
  • opus-media-recorder throws generic Error objects instead of native DOMException.
  • Because audio/wav is not designed for streaming, when mimeType is audio/wav, each dataavailabe events produces a complete and separated .wav file that cannot be concatenated together unlike Ogg and WebM. Therefore, it is recommended not to use timeslice option when calling start(), unless you know what the implication is.
  • There is no SecurityError case implemented. (WIP)

How to build

  1. To build from the source, you need Emscripten, yarn, Python 2.7 or higher, and basic C program build systems such as GNU Make.

  2. yarn install to install JavaScript dependencies.

  3. yarn run build to build. yarn run build:production to build files for distribution.

  4. yarn run serve to run a test web server locally. Default URL is https://localhost:9000 (It has to be HTTPS). You might have to change DEV_SERVER_URL and DEV_SERVER_PORT to change the address of the local test server.

  5. yarn run clean to clean up build files.

Changelog

See CHANGELOG.md.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].