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.

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

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: 291 / 446
Version history: 19 change(s)
Referenced in: [show references]