Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

71
LINES

< > BotCompany Repo | #1028685 // hjs_createAudioMeter - see #1014015

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (2610L) is out of date.

1  
sS hjs_createAudioMeter() {
2  
  ret hjs([[
3  
var audioMeterDebug = false;
4  
  
5  
function createAudioMeter(audioContext,clipLevel,averaging,clipLag) {
6  
  var processor = audioContext.createScriptProcessor(512);
7  
  processor.onaudioprocess = volumeAudioProcess;
8  
  processor.clipping = false;
9  
  processor.lastClip = 0;
10  
  processor.volume = 0;
11  
  processor.clipLevel = clipLevel || 0.98;
12  
  processor.averaging = averaging || 0.95;
13  
  processor.clipLag = clipLag || 750;
14  
  processor.absLevel = 0;
15  
16  
  // this will have no effect, since we don't copy the input to the output,
17  
  // but works around a current Chrome bug.
18  
  processor.connect(audioContext.destination);
19  
20  
  processor.checkClipping =
21  
    function(){
22  
      if (!this.clipping)
23  
        return false;
24  
      if ((this.lastClip + this.clipLag) < window.performance.now())
25  
        this.clipping = false;
26  
      return this.clipping;
27  
    };
28  
29  
  processor.shutdown =
30  
    function(){
31  
      this.disconnect();
32  
      this.onaudioprocess = null;
33  
    };
34  
35  
  return processor;
36  
}
37  
38  
function volumeAudioProcess( event ) {
39  
  var buf = event.inputBuffer.getChannelData(0);
40  
  var bufLength = buf.length;
41  
  var sum = 0;
42  
    var x;
43  
44  
  // Do a root-mean-square on the samples: sum up the squares...
45  
  var absLevel = 0;
46  
  for (var i=0; i<bufLength; i++) {
47  
    x = buf[i];
48  
    var abs = Math.abs(x);
49  
    absLevel = Math.max(absLevel, abs);
50  
    if (!this.clipping && abs >= this.clipLevel) {
51  
      console.log("Got clipping: " + x);
52  
      this.clipping = true;
53  
      this.lastClip = window.performance.now();
54  
    }
55  
    sum += x * x;
56  
  }
57  
58  
  // ... then take the square root of the sum.
59  
  var rms =  Math.sqrt(sum / bufLength);
60  
61  
  this.absLevel = absLevel;
62  
  if (audioMeterDebug)
63  
    console.log("absLevel: " + absLevel);
64  
65  
  // Now smooth this out with the averaging factor applied
66  
  // to the previous sample - take the max here because we
67  
  // want "fast attack, slow release."
68  
  this.volume = Math.max(rms, this.volume*this.averaging);
69  
}
70  
  ]]);
71  
}

Author comment

Began life as a copy of #1014015

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028685
Snippet name: hjs_createAudioMeter - see #1014015
Eternal ID of this version: #1028685/9
Text MD5: 9c707958fc356ec371281403a259566e
Author: stefan
Category: javax / html / speech
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-05 15:02:03
Source code size: 2032 bytes / 71 lines
Pitched / IR pitched: No / No
Views / Downloads: 215 / 309
Version history: 8 change(s)
Referenced in: [show references]