Skip to content
Aaron Gokaslan edited this page Mar 1, 2016 · 11 revisions

The Duplex Speech API

Why use the Duplex endpoint?

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 RecognizerChunked class behaves virtually identically to this class.

The code below will work with RecognizerChunked as well.

Example Code

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.readAllBytes(mic.getAudioFile().toPath());//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.
		}
	}
}

##Streaming Audio directly to the Duplex API This function is experimental and unique to the Duplex endpoint. Allows for mic data to be directly streamed to the API. This would be the main reason to use the Duplex API over the V2 chunked output endpoint.

	Microphone mic = new Microphone(FLACFileWriter.FLAC);
	MicDuplex duplex = new MicDuplex(YOUR_API_KEY);
	duplex.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());
		}
	});
	//mic.open();//Optional Duplex Will Open the Mic if not already open
	try{
		duplex.recognize(mic.getTargetDataLine(), mic.getAudioFormat());//Recognition done here.
	}
	catch(Exception ex){
		ex.printStackTrace();
	}

#FAQ ##Why am I getting a 403 error? If you are getting a 403 error, your API key is likely incorrect. Please ensure that it is correct, and valid. Check to make sure that on the your API dashboard, that the number of requests has increased. If it is still 0, you are likely using the incorrect type of API or using the API key incorrectly.