I wrote on my own a set of code to record what I say through a microphone. I would like to understand the function that google uses for stopping this recording when there is "silence". For example if I go on google and press the microphone symbol, I can say what I plan to look for, but what is the function that it uses to understand the moment when I do not say anything (the moment of "silence")? I thought about doing a cycle in which they are recording some dB or RMS of a few sound frames, and by comparison I can understand if there's "silence". until now the time is static.
public static void main(String[] args) {
final Main REGISTRAZIONE = new Main();
Thread TIME = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(RECORD_TIME);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
REGISTRAZIONE.finish();
}
});
TIME.start();
REGISTRAZIONE.start();
}
Your approach of checking for dB is good. Then, you can use another Thread to check for silence, and stop the main thread when it finds it. You have to use your own implementation of Thread so that it can take TIME as parameter and stop it when there is silence:
public class Recorder {
static Long RECORD_TIME = 100000L; //or whatever time you use
public static void main(String[] args) {
final Main REGISTRAZIONE = new Main();
Thread TIME = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(RECORD_TIME);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
REGISTRAZIONE.finish();
}
});
TIME.start();
myThread finisher = new myThread(TIME);
finisher.start();
REGISTRAZIONE.start();
}
}
class myThread extends Thread implements Runnable {
private Thread TIME;
public myThread(Thread TIME) {
this.TIME = TIME;
}
public void run() {
while (!silence()) {
// do nothing
}
TIME.interrupt();
}
private boolean silence() {
//record and calculate the dB volume and compare to some level
return true;
}
}