Skip to content

Instantly share code, notes, and snippets.

@EricZeiberg
Created April 25, 2014 00:57
Show Gist options
  • Save EricZeiberg/11274633 to your computer and use it in GitHub Desktop.
Save EricZeiberg/11274633 to your computer and use it in GitHub Desktop.
package me.masterejay.gorobo;
import com.darkprograms.speech.microphone.MicrophoneAnalyzer;
import com.darkprograms.speech.recognizer.GoogleResponse;
import com.darkprograms.speech.recognizer.Recognizer;
import javax.sound.sampled.AudioFileFormat;
import java.io.File;
/**
* @author MasterEjay
*/
public class AmbientListener{
private static final MicrophoneAnalyzer microphoneAnalyzer = new MicrophoneAnalyzer(AudioFileFormat.Type.WAVE);
public static void ambientListening() throws Exception{
microphoneAnalyzer.close();
File file = new File("testfile.wav");
microphoneAnalyzer.open();
int THRESHOLD = 8;
System.out.println(microphoneAnalyzer.getAudioVolume());
boolean isSpeaking;
if (microphoneAnalyzer.getAudioVolume() >= THRESHOLD){
System.out.println("Recording...");
isSpeaking = true;
while (isSpeaking){
microphoneAnalyzer.captureAudioToFile(file);
if (microphoneAnalyzer.getAudioVolume() <= THRESHOLD){
isSpeaking = false;
System.out.println("Recording stopped.");
Recognizer recognizer = new Recognizer(Recognizer.Languages.ENGLISH_US);//Specify your language here.
//Although auto-detect is avalible, it is recommended you select your region for added accuracy.
try {
int maxNumOfResponses = 4;
GoogleResponse response = recognizer.getRecognizedDataForWave(file, maxNumOfResponses);
System.out.println("Google Response: " + response.getResponse());
System.out.println("Google is " + Double.parseDouble(response.getConfidence())*100 + "% confident in"
+ " the reply");
System.out.println("Other Possible responses are: ");
for(String s: response.getOtherPossibleResponses()){
System.out.println("\t" + s);
}
if (response.getResponse() != null){
ModuleHandler.moduleExecute(response.getResponse());
}
} catch (Exception ex) {
// TODO Handle how to respond if Google cannot be contacted
System.out.println("ERROR: Google cannot be contacted");
ex.printStackTrace();
}
ambientListening();
}
}
}
else {
ambientListening();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment