I'm trying to write a small program that react when the user is speaking. like have a circle get bigger or something like that.
im using this code to access the microphone, but how do I make it react only when the user is speaking? e.g. when the recorded volume is larger than some amount.
TargetDataLine line = null;
AudioFormat format = new AudioFormat(16000, 16, 1, true, true);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if(! AudioSystem.isLineSupported(info)){
System.out.println("Line is not supported");
}
try{
line = (TargetDataLine) AudioSystem.getLine(info);
line.open();
}catch(LineUnavailableException e){
System.out.println("Failed to get line");
System.exit(-1);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
int numBytesRead;
byte[] data = new byte[line.getBufferSize() / 5];
// Begin audio capture.
line.start();
int i = 0;
// Here, stopped is a global boolean set by another thread.
while (i<100) {
// Read the next chunk of data from the TargetDataLine.
numBytesRead = line.read(data, 0, data.length);
// Save this chunk of data.
out.write(data, 0, numBytesRead);
i++;
System.out.println(i);
}