├── README.md ├── LICENSE.txt ├── index.html ├── volume-meter.js └── main.js /README.md: -------------------------------------------------------------------------------- 1 | # Simple volume meter 2 | 3 | I whipped this app up to show a basic volume meter on live audio input. It does both clip detection and RMS volume. 4 | 5 | A "volume" meter can mean many things; if you want to do clip detection, you really need to access every sample. If you don't need clip detection, I might suggest using an Analyser and getByteTimeDomainData, since it will likely have lower CPU overhead. Note that it is CRITICALLY IMPORTANT to disassociate visual rendering (in the requestAnimationFrame loop) from the onaudioprocess function - you do NOT want to trigger a relayout from inside your audio handler, or it may glitch or cause other issues. 6 | 7 | It's also hosted at https://webaudiodemos.appspot.com/volume-meter/. 8 | 9 | Check it out, feel free to fork, submit pull requests, etc. MIT-Licensed - party on. 10 | 11 | -Chris 12 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Chris Wilson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Volume Meter Sample 5 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

This sample shows how to implement a clip-indicating volume meter in Web Audio, using a ScriptProcessor. It's necessary to use a ScriptProcessor in order to not miss any clipping samples - otherwise you could implement this using a RealtimeAnalyser to only grab samples when necessary.

14 | 15 | 16 |

Check out the source on Github.

17 |

The usage is quite easy: 18 |

var meter = createAudioMeter(audioContext,clipLevel,averaging,clipLag);
19 | 
20 | audioContext: the AudioContext you're using.
21 | clipLevel: the level (0 to 1) that you would consider "clipping".  Defaults to 0.98.
22 | averaging: how "smoothed" you would like the meter to be over time.  Should be between 0 and less than 1.  Defaults to 0.95.
23 | clipLag: how long you would like the "clipping" indicator to show after clipping has occured, in milliseconds.  Defaults to 750ms.
24 | 
25 | meter.checkClipping();
26 | 
27 | returns true if the node has clipped in the last clipLag milliseconds.
28 | 
29 | meter.shutdown();
30 | 
31 | used to destroy the node (it's important to disconnect and remove the event handler for any ScriptProcessor).
32 | 
33 | 34 | 35 | -------------------------------------------------------------------------------- /volume-meter.js: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2014 Chris Wilson 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | */ 24 | 25 | /* 26 | 27 | Usage: 28 | audioNode = createAudioMeter(audioContext,clipLevel,averaging,clipLag); 29 | 30 | audioContext: the AudioContext you're using. 31 | clipLevel: the level (0 to 1) that you would consider "clipping". 32 | Defaults to 0.98. 33 | averaging: how "smoothed" you would like the meter to be over time. 34 | Should be between 0 and less than 1. Defaults to 0.95. 35 | clipLag: how long you would like the "clipping" indicator to show 36 | after clipping has occured, in milliseconds. Defaults to 750ms. 37 | 38 | Access the clipping through node.checkClipping(); use node.shutdown to get rid of it. 39 | */ 40 | 41 | function createAudioMeter(audioContext,clipLevel,averaging,clipLag) { 42 | var processor = audioContext.createScriptProcessor(512); 43 | processor.onaudioprocess = volumeAudioProcess; 44 | processor.clipping = false; 45 | processor.lastClip = 0; 46 | processor.volume = 0; 47 | processor.clipLevel = clipLevel || 0.98; 48 | processor.averaging = averaging || 0.95; 49 | processor.clipLag = clipLag || 750; 50 | 51 | // this will have no effect, since we don't copy the input to the output, 52 | // but works around a current Chrome bug. 53 | processor.connect(audioContext.destination); 54 | 55 | processor.checkClipping = 56 | function(){ 57 | if (!this.clipping) 58 | return false; 59 | if ((this.lastClip + this.clipLag) < window.performance.now()) 60 | this.clipping = false; 61 | return this.clipping; 62 | }; 63 | 64 | processor.shutdown = 65 | function(){ 66 | this.disconnect(); 67 | this.onaudioprocess = null; 68 | }; 69 | 70 | return processor; 71 | } 72 | 73 | function volumeAudioProcess( event ) { 74 | var buf = event.inputBuffer.getChannelData(0); 75 | var bufLength = buf.length; 76 | var sum = 0; 77 | var x; 78 | 79 | // Do a root-mean-square on the samples: sum up the squares... 80 | for (var i=0; i=this.clipLevel) { 83 | this.clipping = true; 84 | this.lastClip = window.performance.now(); 85 | } 86 | sum += x * x; 87 | } 88 | 89 | // ... then take the square root of the sum. 90 | var rms = Math.sqrt(sum / bufLength); 91 | 92 | // Now smooth this out with the averaging factor applied 93 | // to the previous sample - take the max here because we 94 | // want "fast attack, slow release." 95 | this.volume = Math.max(rms, this.volume*this.averaging); 96 | } 97 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2014 Chris Wilson 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | */ 24 | var audioContext = null; 25 | var meter = null; 26 | var canvasContext = null; 27 | var WIDTH=500; 28 | var HEIGHT=50; 29 | var rafID = null; 30 | 31 | window.onload = function() { 32 | 33 | // grab our canvas 34 | canvasContext = document.getElementById( "meter" ).getContext("2d"); 35 | 36 | // monkeypatch Web Audio 37 | window.AudioContext = window.AudioContext || window.webkitAudioContext; 38 | 39 | // grab an audio context 40 | audioContext = new AudioContext(); 41 | 42 | // Attempt to get audio input 43 | try { 44 | // monkeypatch getUserMedia 45 | navigator.getUserMedia = 46 | navigator.getUserMedia || 47 | navigator.webkitGetUserMedia || 48 | navigator.mozGetUserMedia; 49 | 50 | // ask for an audio input 51 | navigator.getUserMedia( 52 | { 53 | "audio": { 54 | "mandatory": { 55 | "googEchoCancellation": "false", 56 | "googAutoGainControl": "false", 57 | "googNoiseSuppression": "false", 58 | "googHighpassFilter": "false" 59 | }, 60 | "optional": [] 61 | }, 62 | }, gotStream, didntGetStream); 63 | } catch (e) { 64 | alert('getUserMedia threw exception :' + e); 65 | } 66 | 67 | } 68 | 69 | 70 | function didntGetStream() { 71 | alert('Stream generation failed.'); 72 | } 73 | 74 | var mediaStreamSource = null; 75 | 76 | function gotStream(stream) { 77 | // Create an AudioNode from the stream. 78 | mediaStreamSource = audioContext.createMediaStreamSource(stream); 79 | 80 | // Create a new volume meter and connect it. 81 | meter = createAudioMeter(audioContext); 82 | mediaStreamSource.connect(meter); 83 | 84 | // kick off the visual updating 85 | drawLoop(); 86 | } 87 | 88 | function drawLoop( time ) { 89 | // clear the background 90 | canvasContext.clearRect(0,0,WIDTH,HEIGHT); 91 | 92 | // check if we're currently clipping 93 | if (meter.checkClipping()) 94 | canvasContext.fillStyle = "red"; 95 | else 96 | canvasContext.fillStyle = "green"; 97 | 98 | // draw a bar based on the current volume 99 | canvasContext.fillRect(0, 0, meter.volume*WIDTH*1.4, HEIGHT); 100 | 101 | // set up the next visual callback 102 | rafID = window.requestAnimationFrame( drawLoop ); 103 | } 104 | --------------------------------------------------------------------------------