Skip to content

Instantly share code, notes, and snippets.

@EricZeiberg
Created April 24, 2014 20:05
Show Gist options
  • Save EricZeiberg/11267714 to your computer and use it in GitHub Desktop.
Save EricZeiberg/11267714 to your computer and use it in GitHub Desktop.
package me.masterejay.gorobo;
import com.darkprograms.speech.microphone.Microphone;
import com.darkprograms.speech.microphone.MicrophoneAnalyzer;
import com.darkprograms.speech.recognizer.GoogleResponse;
import com.darkprograms.speech.recognizer.Recognizer;
import javaFlacEncoder.FLACFileWriter;
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{
Microphone mic = new Microphone(FLACFileWriter.FLAC);
mic.close();
microphoneAnalyzer.close();
File file = new File("testfile.flac");
if (!file.exists()){
file.createNewFile();
}
microphoneAnalyzer.open();
int THRESHOLD = 8;
System.out.println(microphoneAnalyzer.getAudioVolume());
boolean isSpeaking;
if (microphoneAnalyzer.getAudioVolume() >= THRESHOLD ){
System.out.println("Recording...");
isSpeaking = true;
while (isSpeaking){
mic.captureAudioToFile(file);
if (microphoneAnalyzer.getAudioVolume() <= THRESHOLD){
isSpeaking = false;
mic.close();
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.getRecognizedDataForFlac(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();
}
file.deleteOnExit();//Deletes the file as it is no longer necessary.
ambientListening();
}
}
}
else {
Thread.sleep(100);
ambientListening();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment