volume-meter.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014 Chris Wilson
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. /*
  21. Usage:
  22. audioNode = createAudioMeter(audioContext,clipLevel,averaging,clipLag);
  23. audioContext: the AudioContext you're using.
  24. clipLevel: the level (0 to 1) that you would consider "clipping".
  25. Defaults to 0.98.
  26. averaging: how "smoothed" you would like the meter to be over time.
  27. Should be between 0 and less than 1. Defaults to 0.95.
  28. clipLag: how long you would like the "clipping" indicator to show
  29. after clipping has occured, in milliseconds. Defaults to 750ms.
  30. Access the clipping through node.checkClipping(); use node.shutdown to get rid of it.
  31. */
  32. function createAudioMeter(audioContext,clipLevel,averaging,clipLag) {
  33. var processor = audioContext.createScriptProcessor(512);
  34. processor.onaudioprocess = volumeAudioProcess;
  35. processor.clipping = false;
  36. processor.lastClip = 0;
  37. processor.volume = 0;
  38. processor.clipLevel = clipLevel || 0.98;
  39. processor.averaging = averaging || 0.95;
  40. processor.clipLag = clipLag || 750;
  41. // this will have no effect, since we don't copy the input to the output,
  42. // but works around a current Chrome bug.
  43. processor.connect(audioContext.destination);
  44. processor.checkClipping =
  45. function(){
  46. if (!this.clipping)
  47. return false;
  48. if ((this.lastClip + this.clipLag) < window.performance.now())
  49. this.clipping = false;
  50. return this.clipping;
  51. };
  52. processor.shutdown =
  53. function(){
  54. this.disconnect();
  55. this.onaudioprocess = null;
  56. };
  57. return processor;
  58. }
  59. function volumeAudioProcess( event ) {
  60. var buf = event.inputBuffer.getChannelData(0);
  61. var bufLength = buf.length;
  62. var sum = 0;
  63. var x;
  64. // Do a root-mean-square on the samples: sum up the squares...
  65. for (var i=0; i<bufLength; i++) {
  66. x = buf[i];
  67. if (Math.abs(x)>=this.clipLevel) {
  68. this.clipping = true;
  69. this.lastClip = window.performance.now();
  70. }
  71. sum += x * x;
  72. }
  73. // ... then take the square root of the sum.
  74. var rms = Math.sqrt(sum / bufLength);
  75. // Now smooth this out with the averaging factor applied
  76. // to the previous sample - take the max here because we
  77. // want "fast attack, slow release."
  78. this.volume = Math.max(rms, this.volume*this.averaging);
  79. }