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

60
LINES

< > BotCompany Repo | #1026900 // Auto-Correlation VAD Test 1 [works for main mic & normal background noise]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (4910L/25K).

1  
!7
2  
3  
// Note: This is dependent on input buffer size which is not optimal.
4  
// Tested with buffer size 232 ms (using CD quality)
5  
6  
cmodule VAD > DynPrintLogAndEnabled {
7  
  switchable double windowSize = 1; // milliseconds
8  
  switchable double threshold =
9  
    // 10.0; // the magic value for 44 KHz stereo
10  
    1; // for 8 KHz mono
11  
    
12  
  transient double confidence;
13  
  transient short[] fromData; // only useful when sendOutClonedData = true
14  
15  
  start {
16  
    printLogUpdateInterval = 100;
17  
    // technically we are treating stereo input as mono samples
18  
    // but it might not matter
19  
    dm_addAudioListener(voidfunc(short[] data) enter {
20  
      if (!enabled) ret;
21  
      
22  
      short[] buffer = cloneArray(data);
23  
      dm_q(r {
24  
        if (q.size() > 2) ret with print("overload");
25  
        short[] buf = buffer;
26  
        //printStruct(takeFirstOfShortArray(8, buf));
27  
        float sampleRate = dm_audioInputSampleRate();
28  
        if (sampleRate == 0) ret;
29  
        float originalSampleRate = sampleRate;
30  
        int channels = dm_audioInputChannels();
31  
        if (channels > 1) buf = mixStereoShortArrayToMono(buf);
32  
        buf = convertSampleRate_shortArray_simple(buf, sampleRate, 8000);
33  
        sampleRate = 8000f;
34  
        long time = sysNow();
35  
        int windowLength = msToSamples_int(windowSize, sampleRate);
36  
        //printWithMilliseconds("Window length (" + sampleRate + "): " + windowLength);
37  
        new L<Double> l;
38  
        L<IntRange> chunks = intRangeChunks(buf, windowLength);
39  
        for (IntRange r : chunks)
40  
          l.add(normalizedAutocorrelationOfAudioSamples(buf, r));
41  
        
42  
        // print one table for reference  
43  
        print(asList(normalizedAutocorrelationTableForAudioSamples(buf, first(chunks))));
44  
        
45  
        double result = doubleAverage(l);
46  
        confidence = result/threshold*50;
47  
        fromData = data;
48  
        int vol = shortAbsMax(buf);
49  
        print("VAD " + (confidence >= 50 ? "XXX" : "   ") + " value: " + iround(confidence) + " % [" + elapsedMS(time) + " ms, "
50  
          + nWindows(l(buf)/windowLength) + " of size " + windowLength + ", converted from \*originalSampleRate*/ Hz, " + nChannels(channels) + ", volume: " + iround(vol/32767.0*100) + "]");
51  
      });
52  
    });
53  
  }
54  
  
55  
  // API
56  
  
57  
  Bool hasVoiceActivity(short[] fromData) {
58  
    ret fromData != this.fromData ? null : confidence >= 50;
59  
  }
60  
}

Author comment

Began life as a copy of #1026891

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: #1026900
Snippet name: Auto-Correlation VAD Test 1 [works for main mic & normal background noise]
Eternal ID of this version: #1026900/28
Text MD5: 2a4baf824dc5ed452578fb6ec752b6a6
Transpilation MD5: 0e135ae5578179ecee8fd2331cdd54ee
Author: stefan
Category: javax / audio
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-02 20:44:07
Source code size: 2403 bytes / 60 lines
Pitched / IR pitched: No / No
Views / Downloads: 229 / 1719
Version history: 27 change(s)
Referenced in: [show references]