• Stars
    star
    123
  • Rank 289,098 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Customizable peak meters, using the web audio API.

Web Audio Peak Meters

Customizable peak meters, using the web audio API. It can measure peak or true peak based on ITU-R BS.1770

Examples

Usage (basic)

To use these meters, first create a <div> with a width and height and an <audio> element:

<div id="my-peak-meter" style="width: 5em; height: 20em; margin: 1em 0;"></div>
<audio id="my-audio" preload="metadata" controls="controls">
  <source src="audio/marines_hymn.mp3" type="audio/mpeg" />
</audio>

Then, at the bottom of your <body> tag, add the script tag for these meters. I recommend copying the latest web-audio-peak-meter-<version>.min.js file from the docs directory and self-hosting it, or installing via npm and bundling it with your application. Next, create an AudioContext if you don't have one already and create an AudioNode from the <audio> element, connecting it to the destination node. Finally, create a meter node and call the createMeter function, passing in the Element object, the meter node, and an optional object for configuration options, like so:

<script>
  const myMeterElement = document.getElementById('my-peak-meter');
  const myAudio = document.getElementById('my-audio');
  const audioCtx = new window.AudioContext();
  const sourceNode = audioCtx.createMediaElementSource(myAudio);
  sourceNode.connect(audioCtx.destination);
  const myMeter = new webAudioPeakMeter.WebAudioPeakMeter(sourceNode, myMeterElement);
  myAudio.addEventListener('play', function () {
    audioCtx.resume();
  });
</script>

In this example we used an HTML5 audio element, but these meters can work with any web audio API source node. This example was just meant to show the simplest possible use case. If you are already familiar with the web audio API adapting this example to your needs should be fairly self-explanatory, but please reach out if anything isn't working or doesn't make sense.

Usage (advanced)

If you are compiling your javascript with a tool like browserify, webpack, or rollup, you can integrate these meters into your site using the CommonJS require() syntax.

First, add web-audio-peak-meter as a dev dependency to your project:

npm install --save-dev web-audio-peak-meter

Next, import the webAudioPeakMeter module into your javascript:

const webAudioPeakMeter = require('web-audio-peak-meter');

Finally, use as you would in the above example:

var myMeterElement = document.getElementById('my-peak-meter');
var myAudio = document.getElementById('my-audio');
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var sourceNode = audioCtx.createMediaElementSource(myAudio);
sourceNode.connect(audioCtx.destination);
var meterNode = webAudioPeakMeter.createMeterNode(sourceNode, audioCtx);
webAudioPeakMeter.createMeter(myMeterElement, meterNode, {});
myAudio.addEventListener('play', function () {
  audioCtx.resume();
});

(Note: the markup remains the same as in the basic example)

Options

The following options options are supported (the third parameter of the constructor)

  • vertical (boolean): if set to true, displays a vertical meter (default: false)
  • borderSize (number): the number of pixels to use as a border (default: 2)
  • fontSize (number): the font size in pixels used by the labels (default: 9)
  • backgroundColor (string): the background of the meter - can take any css format, for example #123456, rgba(0,0,0, 0.5), or slategray (default: black),
  • tickColor (string): the color of the ticks - can take any css format (default: lightgray),
  • labelColor (string): the color of the held peak labels - can take any css format (default: lightgray),
  • gradient (string[]): an array of space delimited color/percentage pairs to be used by the meter bars (default: ['red 1%', '#ff0 16%', 'lime 45%', '#080 100%']),
  • dbRangeMin (number): the decibel level of the floor of the metter (default: -48)
  • dbRangeMax (number): the decibel level of the ceiling of the metter (default: 0)
  • dbTickSize (number): the number of decibels to have between ticks (default: 6)
  • maskTransition (string): value used for the transition property of the meter bars. Use a longer value for a smoother animation and a shorter value for faster updates (default: 0.1s)
  • audioMeterStandard (string): Can be either peak-sample, or true-peak (default: peak-sample)
  • peakHoldDuration (number): the number, in milliseconds, to hold the peak value before resetting (default: 0, meaning never reset)

Frequently encountered problems

The AudioContext was not allowed to start

In an effort to prevent unwanted auto-playing audio, some browsers do not allow the web audio API's AudioContext to start when it is first created. It must be started by calling resume() on the context after the user interacts with the page. Different browsers implement this requirement differently, however:

  • Chrome: AudioContext is initially paused. Can be resumed by either a callback attached to a click event or by adding a listener to an audio/video element's play event. (more information)
  • Firefox: AudioContext is initially running
  • Webkit/Safari: AudioContext is initially paused. Can be resumed only by a callback attached to a click event - listening for play events on HTML media elements does not work.

MediaElementAudioSource outputs zeroes due to CORS access restrictions

If using <audio> or <video> elements and the source media is not on the same domain as the web site, the server serving the media must add an access-control-allow-origin header with the domain of the web site to the response. (more information)

Local Development

The minified javascript is built using rollup. There's no difference (for now) between the development version and the production version. To start a local server for debugging, run:

npm ci
npm run start

And open a browser to http://localhost:6080/web-audio-peak-meter/index.html to see a local version of the docs page.

Contributing

Contributions are welcome! I'd love to hear any ideas for how these meters could be more user-friendly as well as about any bugs or unclear documentation. If you are at all interested in this project, please create an issue or pull request on this project's github page.

Copyright and license

Code and documentation copyright 2016 Evan Sonderegger and released under the MIT License.

More Repositories

1

dotmil-domains

An incomplete listing of `.mil` domains and the code for the scraper used to build the list
Python
48
star
2

fecfile

a python parser for the .fec file format
Python
44
star
3

bmdStreamingScripts

Scripts for streaming from a Blackmagic DeckLink card to YouTube Live
Shell
7
star
4

PelcoBlackmagic

Arduino sketch for controlling Blackmagic Design cameras from a Pelco-D controller
Arduino
6
star
5

kuka_youbot

Description, controller, MoveIt configuration, and demo files for the Kuka YouBot.
Python
5
star
6

web-audio-peakmeter-react

React component for displaying a peak meter for a web audio source
JavaScript
4
star
7

Algae-CMS

A Little Google App Engine CMS, written in Python
Python
3
star
8

jagss

Just another generator for static sites
CSS
3
star
9

web-audio-goniometer-react

React component for displaying the stereo correlation of a web audio source
JavaScript
3
star
10

decklink-to-webrtc

Tiny demo server using pion
Go
3
star
11

yagss

Yet another generator for static sites
JavaScript
2
star
12

html5-audio-controls

Super-simple controls for your html5 audio elements
JavaScript
2
star
13

blackmagic-electron

Electron App for controlling the Blackmagic Micro Studio Camera via the Arduino shield
JavaScript
2
star
14

crustcrawler_ax12

Description, controller, MoveIt configuration, and demo files for the Crustcrawler AX-12 robotic arm.
Python
1
star
15

shopplymockup

Mockup for Shopply.com
Python
1
star
16

bmmsc-control

Arduino based controller for the Blackmagic Micro Studio Camera
C++
1
star
17

crotale

Crotale is an EBU R128 loudness correction web application, inspired by Xylo
Python
1
star
18

plaster

A Desktop App for Making Great Podcasts
JavaScript
1
star
19

django-podcast-host

A simple Django project for hosting podcasts using Django Rest Framework
Python
1
star
20

youbot_moveit_config

MoveIt Configuration files for the Kuka Youbot
1
star
21

esonderegger.github.io

JavaScript
1
star
22

fec2xlsx

A python library for making Excel files from FEC filings
Python
1
star