!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 { double windowSize = 1; // milliseconds transient short[] buffer; switchable double threshold = // 10.0; // the magic value for 44 KHz stereo 1; // for 8 KHz mono 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; buffer = cloneArray(data); dm_q(r { if (q.size() > 2) ret with print("overload"); short[] buf = buffer; float sampleRate = dm_audioInputSampleRate(); if (sampleRate == 0) ret; float originalSampleRate = sampleRate; if (dm_audioInputChannels() > 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 l; for (IntRange r : intRangeChunks(buf, windowLength)) l.add(normalizedAutocorrelationOfAudioSamples(buf, r)); double result = doubleAverage(l); double confidence = result/threshold*50; print("VAD " + (confidence >= 50 ? "XXX" : " ") + " value: " + iround(confidence) + " % [" + elapsedMS(time) + " ms, " + nWindows(l(buf)/windowLength) + " of size " + windowLength + ", converted from \*originalSampleRate*/ Hz]"); }); }); } }