You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.2 KiB
39 lines
1.2 KiB
package com.jotuntech.sketcher.client.voice;
|
|
|
|
import java.util.Arrays;
|
|
|
|
/** Class for detecting voice activity. Calculates a moving root-mean-square
|
|
* and stores measurements in a delay line. Returns the absolute difference
|
|
* between the current and oldest RMS measurements. The length of the
|
|
* delay line and the moving average is determined by the 'windows' variable,
|
|
* specified in seconds (fraction of the sample rate).
|
|
*/
|
|
|
|
public class VoiceDetector {
|
|
private float delayLine[];
|
|
private float alpha;
|
|
private float mean;
|
|
|
|
public VoiceDetector(float sampleRate, float window) {
|
|
delayLine = new float[(int) (sampleRate * window)];
|
|
Arrays.fill(delayLine, 0f);
|
|
alpha = 1f / delayLine.length;
|
|
mean = 0f;
|
|
}
|
|
|
|
public float process(float x) {
|
|
/** Shift delay line backwards by one sample */
|
|
for(int i = 1; i < delayLine.length; i++) {
|
|
delayLine[i - 1] = delayLine[i];
|
|
}
|
|
|
|
/** Calculate moving Root-Mean */
|
|
mean += (Math.sqrt(Math.abs(x)) - mean) * alpha;
|
|
|
|
/** Place at head of delay line */
|
|
delayLine[delayLine.length - 1] = mean;
|
|
|
|
/** Return difference of Root-Means */
|
|
return Math.abs(mean - delayLine[0]);
|
|
}
|
|
}
|
|
|