https://github.com/jgthms/web-audio-peak-meter
Customizable peak meters, using the web audio API.
https://github.com/jgthms/web-audio-peak-meter
Last synced: 3 months ago
JSON representation
Customizable peak meters, using the web audio API.
- Host: GitHub
- URL: https://github.com/jgthms/web-audio-peak-meter
- Owner: jgthms
- License: mit
- Fork: true (esonderegger/web-audio-peak-meter)
- Created: 2021-03-29T13:47:11.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-01-18T01:54:19.000Z (over 4 years ago)
- Last Synced: 2024-10-01T01:23:08.223Z (7 months ago)
- Homepage: https://esonderegger.github.io/web-audio-peak-meter/
- Size: 6.12 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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[](https://travis-ci.org/esonderegger/web-audio-peak-meter)
## Demo
See [https://esonderegger.github.io/web-audio-peak-meter](https://esonderegger.github.io/web-audio-peak-meter) for a demo.
## Usage (basic)
To use these meters, first create a `
` with a width and height and an `` element:
```html
```
Then, at the bottom of your `` tag, add the script tag for these meters. Next, create an `AudioContext` if you don't have one already and create an [AudioNode](https://developer.mozilla.org/en-US/docs/Web/API/AudioNode) from the `` 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:
Note: for this to work in Google Chrome, we have to resume the audio context after a user gesture ([more info](https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#webaudio)). Adding a listener to the audio element's `play` event is one way to do this.
```htmlvar myMeterElement = document.getElementById('my-peak-meter');
var myAudio = document.getElementById('my-audio');
var audioCtx = new window.AudioContext();
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();
});```
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:
```bash
npm install --save-dev web-audio-peak-meter
```Next, import the webAudioPeakMeter module into your javascript:
```js
var webAudioPeakMeter = require('web-audio-peak-meter');
```Finally, use as you would in the above example:
```js
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)
## 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/index.html](http://localhost:6080/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](https://github.com/esonderegger/web-audio-peak-meter).
## Copyright and license
Code and documentation copyright 2016 [Evan Sonderegger](https://rpy.xyz) and released under the [MIT License](https://github.com/esonderegger/web-audio-peak-meter/blob/master/LICENSE).