Created
April 20, 2014 22:14
-
-
Save EricZeiberg/11126602 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
String filename = "test.wav"; | |
String filename1 = "test1.wav"; | |
File file = new File("testfile.flac"); | |
if (!file.exists()){ | |
file.createNewFile(); | |
} | |
microphoneAnalyzer.open(); | |
microphoneAnalyzer.captureAudioToFile(filename); | |
int THRESHOLD = 7; | |
System.out.println(microphoneAnalyzer.getAudioVolume()); | |
boolean isSpeaking; | |
if (microphoneAnalyzer.getAudioVolume() >= THRESHOLD ){ | |
System.out.println("Recording..."); | |
isSpeaking = true; | |
while (isSpeaking){ | |
mic.captureAudioToFile(filename1); | |
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