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).

1  
sS hVAD(S onVoiceStart, S onVoiceStop, bool autoStart) {
2  
  ret [[
3  
<script type="text/javascript" src="https://botcompany.de/1014036/raw/]] + psI(#1027698) + [["></script>
4  
<script type="text/javascript">
5  
  var vadStarted = false, haveVoice = false;
6  
  var vadStream;
7  
8  
  // Define function called by getUserMedia 
9  
  function startUserMedia(stream) {
10  
    vadStream = stream;
11  
    
12  
    // Create AudioContext
13  
    window.AudioContext = window.AudioContext || window.webkitAudioContext;
14  
    var audioContext = new AudioContext();
15  
  
16  
    // Create MediaStreamAudioSourceNode
17  
    var source = audioContext.createMediaStreamSource(stream);
18  
19  
    // Setup options
20  
    var options = {
21  
     source: source,
22  
     voice_stop: function() { haveVoice = false; ]] + onVoiceStop + [[ }, 
23  
     voice_start: function() { haveVoice = true; ]] + onVoiceStart + [[ }
24  
    }; 
25  
    
26  
    // Create VAD
27  
    var vad = new VAD(options);
28  
  }
29  
  
30  
  function stopVAD() {
31  
    if (!vadStarted) return;
32  
    vadStarted = false;
33  
    console.log("Stopping VAD");
34  
    if (vadStream != undefined) {
35  
      if (vadStream.active)
36  
        vadStream.getTracks().forEach(function (track) { track.stop(); });
37  
      vadStream = null;
38  
    }
39  
  }
40  
41  
  function startVAD() {
42  
    if (vadStarted) return;
43  
    vadStarted = true;
44  
    console.log("Starting VAD");
45  
    // Ask for audio device
46  
    navigator.getUserMedia = ( navigator.getUserMedia
47  
                             || navigator.webkitGetUserMedia 
48  
                             || navigator.mozGetUserMedia 
49  
                             || navigator.msGetUserMedia);
50  
    navigator.getUserMedia({audio: true}, startUserMedia, function(e) {
51  
      console.log("No live audio input in this browser: " + e);
52  
    });
53  
  }
54  
  
55  
  function vadHasVoice() {
56  
    return vadStarted && haveVoice;
57  
  }
58  
  ]] + (autoStart ? "startVAD();" : "") + [[
59  
</script>
60  
  ]];
61  
}

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: 186 / 272
Referenced in: [show references]