-
Notifications
You must be signed in to change notification settings - Fork 0
Duplex Hello World
The answer is continuous recognition. One can constantly write bytes of data and receive them simultaneously. The best use of a such a feature would be for continuous recognition like in Google Glass or similar projects. While this is not an officially supported Google Speech API, it still requires an API key. But for some reason or another, all the Google Speech API keys work across all Speech APIs. One can sign up for an API key but following this method. Additionally, one can try to hijack one of Google's own API keys. Officially, we do not condone this as abusing API keys is programmatically sinful. Nevertheless, if one needs limitless access, one might have to resort to these methods.
##Note: The Recognizer Chunked class behaves virtually identically to this class.
public static void main(String[] args){
GSpeechDuplex dup = new GSpeechDuplex(YOUR_API_KEY);//Instantiate the API
dup.addResponseListener(new GSpeechResponseListener(){// Adds the listener
public void onResponse(GoogleResponse gr){
System.out.println("Google thinks you said: " + gr.getResponse());
System.out.println("with " +
((gr.getConfidence()!=null)?(Double.parseDouble(gr.getConfidence())*100):null)
+ "% confidence.");
System.out.println("Google also thinks that you might have said:"
+ gr.getOtherPossibleResponses());
}
});
Microphone mic = new Microphone(FLACFileWriter.FLAC);//Instantiate microphone and have
// it record FLAC file.
File file = new File("CRAudioTest.flac");//The File to record the buffer to.
//You can also create your own buffer using the getTargetDataLine() method.
while(true){
try{
mic.captureAudioToFile(file);//Begins recording
Thread.sleep(10000);//Records for 10 seconds
mic.close();//Stops recording
//Sends 10 second voice recording to Google
Byte[] data = Files.read(mic.getAudioFile());//Saves data into memory.
dup.recognize(data, (int)mic.getAudioFormat().getSampleRate());
mic.getAudioFile().delete();//Deletes Buffer file
//REPEAT
}
catch(Exception ex){
ex.printStackTrace();//Prints an error if something goes wrong.
}
}
}