Socket
Socket
Sign inDemoInstall

audio-recorder-polyfill

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    audio-recorder-polyfill

MediaRecorder polyfill to record audio in Edge and Safari


Version published
Maintainers
1
Install size
17.7 kB
Created

Changelog

Source

0.1.1

  • Use less memory during WAV encoding.

0.1

  • Initial release.

Readme

Source

Audio Recorder Polyfill

MediaRecorder polyfill to record audio in Edge and Safari 11. It uses Web Audio API and WAV encoder in Web Worker. Try it in online demo.

  • Spec compatible. In the future when other browsers will support MediaRecorder, you will be able to remove polyfill.
  • Small. Less than 1 KB (minified and gzipped). No dependencies. It uses Size Limit to control size.
  • One file. In contrast to other recorders, this polyfill uses “inline worker” and don’t need a separated file for Web Worker.
navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
  recorder = new MediaRecorder(stream)
  recorder.addEventListener('dataavailable', e => {
    audio.src = URL.createObjectURL(e.data)
  })
  recorder.start()
})
Sponsored by Evil Martians

Install

Install package:

npm install --save audio-recorder-polyfill

We recommend creating separated webpack bundle with polyfill. In this case, polyfill will be downloaded only by Edge and Safari. Good browsers will download less.

  entry: {
    app: './src/app.js',
+   polyfill: './src/polyfill.js'
  }

Install polyfill as MediaRecorder in this new bundle src/polyfill.js:

window.MediaRecorder = require('audio-recorder-polyfill')

Add this code to your HTML to load this new bundle only for browsers without MediaRecorder support:

+   <script>
+     if (!window.MediaRecorder) {
+       document.write(unescape('%3Cscript src="/polyfill.js">%3C/script>'))
+     }
+   </script>
    <script src="/app.js" defer></script>

Usage

In the begging, we need to show a warning in browsers without Web Audio API:

if (MediaRecorder.notSupported) {
  noSupport.style.display = 'block'
  dictaphone.style.display = 'none'
}

Then you can use standard MediaRecorder API:

let recorder

recordButton.addEventListener('click', () => {
  // Request permissions to record audio
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
    recorder = new MediaRecorder(stream)

    // Set record to <audio> when recording will be finished
    recorder.addEventListener('dataavailable', e => {
      audio.src = URL.createObjectURL(e.data)
    })

    // Start recording
    recorder.start()
  })
})

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

If you need to upload record to the server, we recommend using timeslice. MediaRecorder will send recorded data every specified millisecond. So you will start uploading before recording would finish.

// Will be executed every second with next part of audio file
recorder.addEventListener('dataavailable', e => {
  sendNextPiece(e.data)
})
// Dump audio data every second
recorder.start(1000)

Audio Formats

Chrome records natively only to .webm files. Firefox to .ogg.

This polyfill saves records to .wav files. Compression is not very good, but encoding is fast and simple.

You can get used file format in e.data.type:

recorder.addEventListener('dataavailable', e => {
  e.data.type //=> 'audio/wav' with polyfill
              //   'audio/webm' in Chrome
              //   'audio/ogg' in Firefox
})

Limitations

This polyfill tries to be MediaRecorder API compatible. But it still has small differences.

  • WAV format contains duration in the file header. As result, with timeslice or requestData() call, dataavailable will receive a separated file with header on every call. In contrast, MediaRecorder sends header only to first dataavailable. Other events receive addition bytes to the same file.
  • Constructor options are not supported.
  • Polyfill ignores method call in the wrong state (like stop() during inactive state) instead of throwing an error.
  • BlobEvent.timecode is not supported.

Custom Encoder

If you need audio format with better compression, you can change polyfill’s encoder:

  window.MediaRecorder = require('audio-recorder-polyfill')
+ MediaRecorder.encoder = require('./ogg-opus-encoder')
+ MediaRecorder.mimeType = 'audio/ogg'

The encoder should be a function with Web Worker in the body. Polyfill converts function to the string to make Web Worker.

module.exports = function () {
  function encode (input) {
    …
  }

  function dump (sampleRate) {
    …
    postMessage(output)
  }

  onmessage = function (e) {
    if (e.data[0] === 'encode') {
      encode(e.data[1])
    } else {
      dump(e.data[1])
    }
  }
}

Keywords

FAQs

Last updated on 01 Dec 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc