Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package demo.sphinx.helloworld;
- import edu.cmu.sphinx.frontend.util.Microphone;
- import edu.cmu.sphinx.recognizer.Recognizer;
- import edu.cmu.sphinx.result.Result;
- import edu.cmu.sphinx.util.props.ConfigurationManager;
- import edu.cmu.sphinx.util.props.PropertyException;
- import java.io.File;
- import java.io.IOException;
- import java.net.URL;
- import java.awt.AWTException;
- //For keyboard inputs
- import java.awt.Robot;
- import java.awt.event.InputEvent;
- import java.awt.event.KeyEvent;
- //For network related
- import java.net.HttpURLConnection;
- ////For text to speech
- import com.sun.speech.freetts.Voice;
- import com.sun.speech.freetts.VoiceManager;
- /**
- * A simple HelloWorld demo showing a simple speech application
- * built using Sphinx-4. This application uses the Sphinx-4 endpointer,
- * which automatically segments incoming audio into utterances and silences.
- */
- public class HelloWorld {
- /**
- * Main method for running the HelloWorld demo.
- */
- static int i=1;
- static String resultText;
- public void main(String[] args) {
- Voice v;
- VoiceManager vm = VoiceManager.getInstance();
- v=vm.getVoice("kevin16");
- v.allocate();
- try {
- URL url;
- if (args.length > 0) {
- url = new File(args[0]).toURI().toURL();
- }
- else {
- url = HelloWorld.class.getResource("helloworld.config.xml");
- }
- System.out.println("Loading...");
- v.speak("Hello world, I am a machine. This is the machine talking. I am a speech recognition system.");
- ConfigurationManager cm = new ConfigurationManager(url);
- Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
- Microphone microphone = (Microphone) cm.lookup("microphone");
- /* allocate the resource necessary for the recognizer */
- recognizer.allocate();
- /* the microphone will keep recording until the program exits */
- if (microphone.startRecording()) {
- System.out.println("Say: (Command | Program| Browser | Bluetooth | Device Manager |Power Options |Cal | Control | Player |task manager | Windows Security Center)");
- while (true) {
- System.out.println("Start speaking. Press Ctrl-C to quit.\n");
- /*
- * This method will return when the end of speech
- * is reached. Note that the endpointer will determine
- * the end of speech.
- */
- Result result = recognizer.recognize();
- // start(v, result);
- } // while end
- }
- else {
- System.out.println("Cannot start microphone.");
- v.speak("Cannot start the microphone.");
- recognizer.deallocate();
- System.exit(1);
- }
- }
- catch (IOException e) {
- System.err.println("Problem when loading HelloWorld: " + e);
- e.printStackTrace();
- }
- catch (PropertyException e) {
- System.err.println("Problem configuring HelloWorld: " + e);
- e.printStackTrace();
- }
- catch (InstantiationException e) {
- System.err.println("Problem creating HelloWorld: " + e);
- e.printStackTrace();
- }
- }
- private static void start(Voice v, Result result) {
- if (result != null)
- {
- System.out.println("Enter your choise"+ "\n");
- resultText = result.getBestFinalResultNoFiller();
- System.out.println("You said: " + resultText + "\n");
- // Applications*********************************************
- if(resultText.equalsIgnoreCase("Command Prompt"))
- {
- v.speak("Opening command prompt");
- try{
- Runtime.getRuntime().exec("cmd /c start cmd");
- }
- catch(Exception er){
- }
- }
- if (resultText.equalsIgnoreCase("close command prompt"))
- {
- v.speak("Closing command prompt");
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im cmd.exe /f");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("Program list"))
- {
- v.speak("Listing out the programs.");
- try{
- Runtime.getRuntime().exec("cmd /c start appwiz.cpl");
- }catch(Exception ae){}
- }
- if (resultText.equalsIgnoreCase("open calculator"))
- {
- v.speak("Opening calculator.");
- try{
- Runtime.getRuntime().exec("cmd /c start calc");
- }catch(Exception ae){}
- }
- else if(resultText.equalsIgnoreCase("close calculator"))
- {
- v.speak("Closing calculator.");
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im calc.exe /f");
- }catch(Exception ae){}
- }
- else if(resultText.equalsIgnoreCase("open windows paint"))
- {
- v.speak("Opening windows paint.");
- try{
- Runtime.getRuntime().exec("cmd /c start mspaint");
- }catch(Exception ae){}
- }
- else if(resultText.equalsIgnoreCase("close windows paint"))
- {
- v.speak("Closing windows paint.");
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im mspaint.exe /f");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("open word pad"))
- {
- v.speak("Opening word pad.");
- try{
- Runtime.getRuntime().exec("cmd /c write");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("close word pad"))
- {
- v.speak("Closing word pad.");
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im wordpad.exe /f");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("open note pad"))
- {
- v.speak("Opening note pad.");
- try{
- Runtime.getRuntime().exec("cmd /c start notepad");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("close note pad"))
- {
- v.speak("Closing note pad");
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im notepad.exe /f");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("open Player"))
- {
- v.speak("Opening player.");
- try{
- Runtime.getRuntime().exec("cmd /c start wmplayer");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("open microsoft word"))
- {
- v.speak("Opening microsoft word");
- try{
- Runtime.getRuntime().exec("cmd /c start winword");
- // System.out.println("inside");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("close microsoft word"))
- {
- v.speak("Closing microsoft wword.");
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im winword.exe /f");
- // System.out.println("inside");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("open microsoft Excel"))
- {
- v.speak("Opening microsoft excel.");
- try{
- Runtime.getRuntime().exec("cmd /c start excel");
- // System.out.println("inside");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("close mircosoft Excel"))
- {
- v.speak("Closing microsoft excel");
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im excel.exe /f");
- // System.out.println("inside");
- }catch(Exception ae){}
- }
- // Utility Action Command
- if (resultText.equalsIgnoreCase("show Power Options")) {
- v.speak("Displaying power options.");
- try{
- Runtime.getRuntime().exec("cmd /c powercfg.cpl");
- }catch(Exception ae){}
- }
- if (resultText.equalsIgnoreCase("show Blue tooth")) {
- v.speak("Displaying blue tooth options.");
- try{
- Runtime.getRuntime().exec("cmd /c fsquirt");
- }catch(Exception ae){}
- }
- if (resultText.equalsIgnoreCase("show Windows Security Center")) {
- v.speak("Displaying windows security center.");
- try{
- Runtime.getRuntime().exec("cmd /c wscui.cpl");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("show Control Panel"))
- {
- v.speak("Displaying control panel.");
- try{
- Runtime.getRuntime().exec("cmd /c control");
- }catch(Exception ae){}
- }
- else if(resultText.equalsIgnoreCase("show task manager"))
- {
- v.speak("Displaying task manager.");
- try{
- Runtime.getRuntime().exec("cmd /c start taskmgr.exe");
- // System.out.println("inside");
- }catch(Exception ae){}
- }
- else if(resultText.equalsIgnoreCase("hide task manager"))
- {
- v.speak("Closing task manager.");
- try{
- Runtime.getRuntime().exec("cmd /c start taskkill /im taskmgr.exe /f");
- // System.out.println("inside");
- }catch(Exception ae){}
- }
- else if (resultText.equalsIgnoreCase("show Device Manager"))
- {
- v.speak("Displaying device manager.");
- try{
- Runtime.getRuntime().exec("cmd /c start devmgmt.msc");
- // System.out.println("inside");
- }
- catch(Exception ae){}
- }
- // NETWORk related action commands
- if(resultText.equalsIgnoreCase("site face book"))
- {
- v.speak("Going to face book.");
- try{
- Runtime.getRuntime().exec("cmd /c start chrome www.facebook.com");
- // System.out.println("inside");
- }
- catch(Exception ae){}
- }
- if(resultText.equalsIgnoreCase("site mail"))
- {
- v.speak("Going to mail.");
- try{
- Runtime.getRuntime().exec("cmd /c start chrome www.gmail.com");
- // System.out.println("inside");
- }
- catch(Exception ae){}
- }
- if(resultText.equalsIgnoreCase("site go girl"))
- {
- v.speak("Goin to website google.");
- try{
- Runtime.getRuntime().exec("cmd /c start chrome www.google.com");
- // System.out.println("inside");
- }
- catch(Exception ae){}
- }
- if(resultText.equalsIgnoreCase("site news"))
- {
- v.speak("Displaying the news website.");
- try{
- Runtime.getRuntime().exec("cmd /c start chrome www.news.yahoo.com/");
- // System.out.println("inside");
- }
- catch(Exception ae){}
- }
- // Simulate action commands by importing the robot class above
- if(resultText.equalsIgnoreCase("scroll up"))
- {
- v.speak("Scrolling up.");
- try {
- Robot r = new Robot();
- r.keyPress(KeyEvent.VK_UP);
- r.delay(500);
- r.keyPress(KeyEvent.VK_UP);
- r.delay(500);
- r.keyPress(KeyEvent.VK_UP);
- } catch (AWTException e) {
- e.printStackTrace();
- }
- }
- if(resultText.equalsIgnoreCase("scroll down"))
- {
- v.speak("Scrolling down.");
- try {
- Robot r = new Robot();
- r.keyPress(KeyEvent.VK_DOWN);
- r.delay(500);
- r.keyPress(KeyEvent.VK_DOWN);
- r.delay(500);
- r.keyPress(KeyEvent.VK_DOWN);
- } catch (AWTException e) {
- e.printStackTrace();
- }
- }
- if(resultText.equalsIgnoreCase("change tab"))
- {
- v.speak("Changing to different windows. ");
- try {
- Robot r = new Robot();
- r.keyPress(KeyEvent.VK_UP);
- // r.delay(500);
- r.keyPress(KeyEvent.VK_UP);
- // r.delay(500);
- r.keyPress(KeyEvent.VK_UP);
- } catch (AWTException e) {
- e.printStackTrace();
- }
- }
- // System Command
- if(resultText.equalsIgnoreCase("go to sleep"))
- {
- v.speak("Going to sleep mode.");
- try{
- Runtime.getRuntime().exec("cmd /c start /wait scrnsave.scr /s");
- }
- catch(Exception ae)
- {}
- }
- if(resultText.equalsIgnoreCase("wake up"))
- {
- v.speak("Nice to be back.");
- try {
- Robot r = new Robot();
- r.mouseMove(100, 100);
- r.mousePress(InputEvent.BUTTON1_MASK);
- r.mouseRelease(InputEvent.BUTTON1_MASK);
- } catch (AWTException e) {
- e.printStackTrace();
- }
- }
- if(resultText.equalsIgnoreCase("check internet connection"))
- {
- try {
- try {
- URL url1 = new URL("http://www.google.com");
- System.out.println(url1.getHost());
- HttpURLConnection con = (HttpURLConnection) url1
- .openConnection();
- con.connect();
- if (con.getResponseCode() == 200){
- System.out.println("Connection established!!");
- v.speak("Connection is established.");
- try{
- Runtime.getRuntime().exec("cmd /c start notepad");
- // System.out.println("inside");
- } catch (Exception ae){
- }
- try {
- Robot r = new Robot();
- r.keyPress(KeyEvent.VK_C);
- r.keyPress(KeyEvent.VK_O);
- r.keyPress(KeyEvent.VK_N);
- r.keyPress(KeyEvent.VK_N);
- r.keyPress(KeyEvent.VK_E);
- r.keyPress(KeyEvent.VK_C);
- r.keyPress(KeyEvent.VK_T);
- r.keyPress(KeyEvent.VK_E);
- r.keyPress(KeyEvent.VK_D);
- r.delay(1000);
- } catch (AWTException e) {
- e.printStackTrace();
- }
- }
- } catch (Exception exception) {
- System.out.println("No Connection");
- v.speak("Could not connect to the internet.");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- // Program Action Command ABOUT
- if(resultText.equalsIgnoreCase("type your name"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c start notepad");
- // System.out.println("inside");
- } catch (Exception ae){
- }
- try {
- Robot r = new Robot();
- r.delay(100);
- r.keyPress(KeyEvent.VK_S);
- r.delay(100);
- r.keyPress(KeyEvent.VK_P);
- r.delay(100);
- r.keyPress(KeyEvent.VK_E);
- r.delay(100);
- r.keyPress(KeyEvent.VK_E);
- r.delay(100);
- r.keyPress(KeyEvent.VK_C);
- r.delay(100);
- r.keyPress(KeyEvent.VK_H);
- r.delay(100);
- r.keyPress(KeyEvent.VK_SPACE);
- r.delay(100);
- r.keyPress(KeyEvent.VK_R);
- r.delay(100);
- r.keyPress(KeyEvent.VK_E);
- r.delay(100);
- r.keyPress(KeyEvent.VK_C);
- r.delay(100);
- r.keyPress(KeyEvent.VK_O);
- r.delay(100);
- r.keyPress(KeyEvent.VK_G);
- r.delay(100);
- r.keyPress(KeyEvent.VK_N);
- r.delay(100);
- r.keyPress(KeyEvent.VK_I);
- r.delay(100);
- r.keyPress(KeyEvent.VK_T);
- r.delay(100);
- r.keyPress(KeyEvent.VK_I);
- r.delay(100);
- r.keyPress(KeyEvent.VK_O);
- r.delay(100);
- r.keyPress(KeyEvent.VK_N);
- r.delay(100);
- r.keyPress(KeyEvent.VK_SPACE);
- r.delay(100);
- r.keyPress(KeyEvent.VK_S);
- r.delay(100);
- r.keyPress(KeyEvent.VK_Y);
- r.delay(100);
- r.keyPress(KeyEvent.VK_S);
- r.delay(100);
- r.keyPress(KeyEvent.VK_T);
- r.delay(100);
- r.keyPress(KeyEvent.VK_E);
- r.delay(100);
- r.keyPress(KeyEvent.VK_M);
- } catch (AWTException e) {
- e.printStackTrace();
- }
- }
- if(resultText.equalsIgnoreCase("show commands"))
- {
- try{
- Runtime.getRuntime().exec("cmd /c commands.txt");
- }
- catch(Exception ae)
- {}
- }
- if(resultText.equalsIgnoreCase("who are you"))
- {
- try{
- v.speak("I am a program for speech recognition system.");
- }
- catch(Exception ae)
- {}
- }
- else if(resultText.equalsIgnoreCase("recognition stop"))
- {
- v.speak("Stopping the recognition process..");
- try{
- //recognizer.wait();
- System.out.println("See you later!");
- v.speak("It was nice working with you. See you next time.");
- System.exit(0);}
- catch(Exception estop ){}
- }
- else {
- System.out.println("I can't hear what you said.\n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement