Skip to content

Hello World Recognizer and Synthesiser

Aaron Gokaslan edited this page Jan 20, 2016 · 1 revision

Speech Recognize

##We recommend using this example when using live human speech. ##Disclaimer: The API does change from time to time and these examples may become out of date. Please consult the JavaDoc for the latest information on using this API.

 import java.io.File;
 import javaFlacEncoder.FLACFileWriter;
 import com.darkprograms.speech.microphone.Microphone;
 import com.darkprograms.speech.recognizer.Recognizer;
 import com.darkprograms.speech.recognizer.GoogleResponse;

 /**
   * Jarvis Speech API Tutorial
   * @author Aaron Gokaslan (Skylion)
   *
   */
   public class helloWorld {

public static void main(String[] args) {
	Microphone mic = new Microphone(FLACFileWriter.FLAC);
	File file = new File("testfile2.flac");//Name your file whatever you want
	try {
		mic.captureAudioToFile(file);
	} catch (Exception ex) {//Microphone not available or some other error.
		System.out.println("ERROR: Microphone is not availible.");
		ex.printStackTrace();
		//TODO Add your error Handling Here
	}
	/* User records the voice here. Microphone starts a separate thread so do whatever you want
	 * in the mean time. Show a recording icon or whatever.
	 */
	try {
		System.out.println("Recording...");
		Thread.sleep(5000);//In our case, we'll just wait 5 seconds.
                    mic.close();
	} catch (InterruptedException ex) {
		// TODO Auto-generated catch block
		ex.printStackTrace();
	}
	
	mic.close();//Ends recording and frees the resources
	System.out.println("Recording stopped.");
	
	Recognizer recognizer = new Recognizer(Recognizer.Languages.ENGLISH_US, "INSERT_YOU_API_KEY"); //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, (int)mic.getAudioFormat().getSampleRate());
		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);
		}
	} 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.
	
}

}

Speech Synthesizer

public static void talk(String text){
	//String language = "auto";//Uses language autodetection
            //** While the API can detect language by itself, this is reliant on the Google Translate API which is prone to breaking. For maximum stability, please specify the language.**//
        String language = "en-us";//English (US) language code	 //If you want to specify a language use the ISO code for your country. Ex: en-us
	/*If you are unsure of this code, use the Translator class to automatically detect based off of
	* Either text from your language or your system settings.
	*/
	Synthesiser synth = new Synthesiser(language);
	try {
		InputStream is = synth.getMP3Data("Hello World!");
		//TODO Use any Java MP3 Implementation to play back the AudioFile from the InputStream.
	} catch (Exception e) {
		// TODO Auto-generated catch block
		System.out.println("Error");
		e.printStackTrace();
		return;
	}
   }