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<bufLength; i++) {
|
81 | x = buf[i]; |
82 | if (Math.abs(x)>=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 | } |
Began life as a copy of #1014014
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1014015 |
| Snippet name: | Volume Meter volume-meter.js |
| Eternal ID of this version: | #1014015/1 |
| Text MD5: | 6ae0ae57be97b8d6d71c593ab357d84c |
| Author: | stefan |
| Category: | javax / html / speech |
| Type: | Document |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2018-03-22 10:54:03 |
| Source code size: | 3345 bytes / 96 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 740 / 230 |
| Referenced in: | [show references] |