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

!7

// Note: This is dependent on input buffer size which is not optimal.
// Tested with buffer size 232 ms (using CD quality)

cmodule VAD > DynPrintLogAndEnabled {
  switchable double windowSize = 1; // milliseconds
  switchable double threshold =
    // 10.0; // the magic value for 44 KHz stereo
    1; // for 8 KHz mono
    
  transient double confidence;
  transient short[] fromData; // only useful when sendOutClonedData = true

  start {
    printLogUpdateInterval = 100;
    // technically we are treating stereo input as mono samples
    // but it might not matter
    dm_addAudioListener(voidfunc(short[] data) enter {
      if (!enabled) ret;
      
      short[] buffer = cloneArray(data);
      dm_q(r {
        if (q.size() > 2) ret with print("overload");
        short[] buf = buffer;
        //printStruct(takeFirstOfShortArray(8, buf));
        float sampleRate = dm_audioInputSampleRate();
        if (sampleRate == 0) ret;
        float originalSampleRate = sampleRate;
        int channels = dm_audioInputChannels();
        if (channels > 1) buf = mixStereoShortArrayToMono(buf);
        buf = convertSampleRate_shortArray_simple(buf, sampleRate, 8000);
        sampleRate = 8000f;
        long time = sysNow();
        int windowLength = msToSamples_int(windowSize, sampleRate);
        //printWithMilliseconds("Window length (" + sampleRate + "): " + windowLength);
        new L<Double> l;
        L<IntRange> chunks = intRangeChunks(buf, windowLength);
        for (IntRange r : chunks)
          l.add(normalizedAutocorrelationOfAudioSamples(buf, r));
        
        // print one table for reference  
        print(asList(normalizedAutocorrelationTableForAudioSamples(buf, first(chunks))));
        
        double result = doubleAverage(l);
        confidence = result/threshold*50;
        fromData = data;
        int vol = shortAbsMax(buf);
        print("VAD " + (confidence >= 50 ? "XXX" : "   ") + " value: " + iround(confidence) + " % [" + elapsedMS(time) + " ms, "
          + nWindows(l(buf)/windowLength) + " of size " + windowLength + ", converted from \*originalSampleRate*/ Hz, " + nChannels(channels) + ", volume: " + iround(vol/32767.0*100) + "]");
      });
    });
  }
  
  // API
  
  Bool hasVoiceActivity(short[] fromData) {
    ret fromData != this.fromData ? null : confidence >= 50;
  }
}

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: 300 / 1810
Version history: 27 change(s)
Referenced in: [show references]