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

77
LINES

< > BotCompany Repo | #1027700 // hVAD - voice activity detector in JavaScript

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

Transpiled version (2643L) is out of date.

sS hVAD(S onVoiceStart, S onVoiceStop, bool autoStart) {
  ret hjs_createAudioMeter() +
    [[
<script type="text/javascript" src="https://botcompany.de/serve/]] + psI(#1027698) + [["></script>
<script type="text/javascript">
  var vadStarted = false, haveVoice = false;
  var vadStream;
  var audioMeter;
  var audioMeterClipLag = 5000;

  // Define function called by getUserMedia 
  function startUserMedia(stream) {
    vadStream = stream;
    
    // Create AudioContext
    window.AudioContext = window.AudioContext || window.webkitAudioContext;
    var audioContext = new AudioContext();
  
    // Create MediaStreamAudioSourceNode
    var source = audioContext.createMediaStreamSource(stream);
    
    // create audioMeter
    audioMeter = createAudioMeter(audioContext, null, null, audioMeterClipLag);
    source.connect(audioMeter);

    // Setup options
    var options = {
     source: source,
     voice_stop: function() { haveVoice = false; ]] + onVoiceStop + [[ }, 
     voice_start: function() { haveVoice = true; ]] + onVoiceStart + [[ }
    }; 
    
    // Create VAD
    var vad = new VAD(options);
  }
  
  function stopVAD() {
    if (!vadStarted) return;
    vadStarted = false;
    console.log("Stopping VAD");
    if (vadStream != undefined) {
      if (vadStream.active)
        vadStream.getTracks().forEach(function (track) { track.stop(); });
      vadStream = null;
    }
    if (audioMeter != undefined) {
      audioMeter.shutdown();
      audioMeter = null;
    }
  }

  function startVAD() {
    if (vadStarted) return;
    vadStarted = true;
    console.log("Starting VAD");
    // Ask for audio device
    navigator.getUserMedia = ( navigator.getUserMedia
                             || navigator.webkitGetUserMedia 
                             || navigator.mozGetUserMedia 
                             || navigator.msGetUserMedia);
    navigator.getUserMedia({audio: true}, startUserMedia, function(e) {
      console.log("No live audio input in this browser: " + e);
    });
  }
  
  function vadHasVoice() {
    return vadStarted && haveVoice;
  }
  
  function vadHasClipping() {
    return audioMeter && audioMeter.checkClipping();
  }
    
  ]] + (autoStart ? "startVAD();" : "") + [[
</script>
  ]];
}

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: #1027700
Snippet name: hVAD - voice activity detector in JavaScript
Eternal ID of this version: #1027700/20
Text MD5: fdab2e518400c4dcd109d381bd0f5745
Author: stefan
Category: javax / javascript audio magic
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-05 15:08:49
Source code size: 2323 bytes / 77 lines
Pitched / IR pitched: No / No
Views / Downloads: 290 / 445
Version history: 19 change(s)
Referenced in: [show references]