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.

sS hjs_createAudioMeter() {
  ret hjs([[
var audioMeterDebug = false;
  
function createAudioMeter(audioContext,clipLevel,averaging,clipLag) {
  var processor = audioContext.createScriptProcessor(512);
  processor.onaudioprocess = volumeAudioProcess;
  processor.clipping = false;
  processor.lastClip = 0;
  processor.volume = 0;
  processor.clipLevel = clipLevel || 0.98;
  processor.averaging = averaging || 0.95;
  processor.clipLag = clipLag || 750;
  processor.absLevel = 0;

  // this will have no effect, since we don't copy the input to the output,
  // but works around a current Chrome bug.
  processor.connect(audioContext.destination);

  processor.checkClipping =
    function(){
      if (!this.clipping)
        return false;
      if ((this.lastClip + this.clipLag) < window.performance.now())
        this.clipping = false;
      return this.clipping;
    };

  processor.shutdown =
    function(){
      this.disconnect();
      this.onaudioprocess = null;
    };

  return processor;
}

function volumeAudioProcess( event ) {
  var buf = event.inputBuffer.getChannelData(0);
  var bufLength = buf.length;
  var sum = 0;
    var x;

  // Do a root-mean-square on the samples: sum up the squares...
  var absLevel = 0;
  for (var i=0; i<bufLength; i++) {
    x = buf[i];
    var abs = Math.abs(x);
    absLevel = Math.max(absLevel, abs);
    if (!this.clipping && abs >= this.clipLevel) {
      console.log("Got clipping: " + x);
      this.clipping = true;
      this.lastClip = window.performance.now();
    }
    sum += x * x;
  }

  // ... then take the square root of the sum.
  var rms =  Math.sqrt(sum / bufLength);

  this.absLevel = absLevel;
  if (audioMeterDebug)
    console.log("absLevel: " + absLevel);

  // Now smooth this out with the averaging factor applied
  // to the previous sample - take the max here because we
  // want "fast attack, slow release."
  this.volume = Math.max(rms, this.volume*this.averaging);
}
  ]]);
}

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: 214 / 307
Version history: 8 change(s)
Referenced in: [show references]