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

61
LINES

< > BotCompany Repo | #1028684 // hVAD - voice activity detector in JavaScript [backup without volume meter]

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

Libraryless. Click here for Pure Java version (2485L/16K).

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

  // 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);

    // 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;
    }
  }

  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;
  }
  ]] + (autoStart ? "startVAD();" : "") + [[
</script>
  ]];
}

Author comment

Began life as a copy of #1027700

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: #1028684
Snippet name: hVAD - voice activity detector in JavaScript [backup without volume meter]
Eternal ID of this version: #1028684/1
Text MD5: 1d35043cfce1c8f6ec14d3120fbbcb8d
Transpilation MD5: 4f2f9c13bb50abb8eb6e2143686bb270
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 14:32:43
Source code size: 1902 bytes / 61 lines
Pitched / IR pitched: No / No
Views / Downloads: 184 / 269
Referenced in: [show references]