/*1The MIT License (MIT)23Copyright (c) 2014 Chris Wilson45Permission is hereby granted, free of charge, to any person obtaining a copy6of this software and associated documentation files (the "Software"), to deal7in the Software without restriction, including without limitation the rights8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9copies of the Software, and to permit persons to whom the Software is10furnished to do so, subject to the following conditions:1112The above copyright notice and this permission notice shall be included in all13copies or substantial portions of the Software.1415THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21SOFTWARE.22*/2324/*2526Usage:27audioNode = createAudioMeter(audioContext,clipLevel,averaging,clipLag);2829audioContext: the AudioContext you're using.30clipLevel: the level (0 to 1) that you would consider "clipping".31Defaults to 0.98.32averaging: how "smoothed" you would like the meter to be over time.33Should be between 0 and less than 1. Defaults to 0.95.34clipLag: how long you would like the "clipping" indicator to show35after clipping has occured, in milliseconds. Defaults to 750ms.3637Access the clipping through node.checkClipping(); use node.shutdown to get rid of it.38*/3940function createAudioMeter(audioContext,clipLevel,averaging,clipLag) {41var processor = audioContext.createScriptProcessor(512);42processor.onaudioprocess = volumeAudioProcess;43processor.clipping = false;44processor.lastClip = 0;45processor.volume = 0;46processor.clipLevel = clipLevel || 0.98;47processor.averaging = averaging || 0.95;48processor.clipLag = clipLag || 750;4950// this will have no effect, since we don't copy the input to the output,51// but works around a current Chrome bug.52processor.connect(audioContext.destination);5354processor.checkClipping =55function(){56if (!this.clipping)57return false;58if ((this.lastClip + this.clipLag) < window.performance.now())59this.clipping = false;60return this.clipping;61};6263processor.shutdown =64function(){65this.disconnect();66this.onaudioprocess = null;67};6869return processor;70}7172function volumeAudioProcess( event ) {73var buf = event.inputBuffer.getChannelData(0);74var bufLength = buf.length;75var sum = 0;76var x;7778// Do a root-mean-square on the samples: sum up the squares...79for (var i=0; i<bufLength; i++) {80x = buf[i];81if (Math.abs(x)>=this.clipLevel) {82this.clipping = true;83this.lastClip = window.performance.now();84}85sum += x * x;86}8788// ... then take the square root of the sum.89var rms = Math.sqrt(sum / bufLength);9091// Now smooth this out with the averaging factor applied92// to the previous sample - take the max here because we93// want "fast attack, slow release."94this.volume = Math.max(rms, this.volume*this.averaging);95}9697