1

I am trying to put my speech recognizer into a calculator app so blind people can use it. Here is the code:

public class HelloWorld {

    public static Recognizer recognizer;

    public static void init(String[] args) {
        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...");

            ConfigurationManager cm = new ConfigurationManager(url);

            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: (Good morning | Hello) " +
                    "( Bhiksha | Evandro | Paul | Philip | Rita | Will )");


                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.
                 */ 

            }
        } 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();
        }
    }

    public static int convertToInt(String num){
        int tmp = 0;
        switch(num){

            case "one": tmp = 1; break;
            case "two": tmp = 2; break;
            case "three": tmp = 3; break;
            case "four": tmp = 4; break;
            case "five": tmp = 5; break;
            case "six": tmp = 6; break;
            case "seven": tmp = 7; break;
            case "eight": tmp = 8; break;
            case "nine": tmp = 9; break;
            default: tmp = -1;
        }
        System.out.print("The number is: " + tmp);
        return tmp;
    }

    public static int check(){
        Result result = recognizer.recognize();
        String resultText = "";
        if (result != null) {
            resultText = result.getBestFinalResultNoFiller();

            System.out.println("-"+resultText+"-");
            System.out.println("You said: " + resultText + "\n");
        } else {
            System.out.println("I can't hear what you said.\n");
        }
        return convertToInt(resultText);
    }
}

And here is the main loop for the app:

public static void main(String[] args) {
    HelloWorld.init(args);
    SwingUtilities.invokeLater( new Runnable() {
        public void run() {
            new Main();
            HelloWorld.check();
        }
    });   
}

I can't seem to make them run at the same time, can anyone help me figure this out?

5
  • Where is your Main Class definition? Commented Apr 29, 2016 at 7:11
  • Which class does that main belong to? Where is the HelloWorld.main and what does it do? What is Main? Commented Apr 29, 2016 at 7:16
  • @RealSkeptic All fixed Commented Apr 29, 2016 at 7:26
  • Is new Main(); supposed to run the calculator? What do you mean by 'make them run at the same time'? What does the calculators code look like? More importantly: how is the recognizer supposed to interact with the calculator? Commented Apr 29, 2016 at 7:41
  • @tamasrev yes, it is in the calculators code. I seperated the two classes. And it only reacts with it by giving it info, however, they aren't running side by side, one is always waiting on the other. Commented Apr 29, 2016 at 7:47

1 Answer 1

1

You need two different threads. Main running in the Swing thread and HelloWorld running in another thread, e.g. main thread.

It's yet unclear me how they would communicate. Anyway, this is how you could start them in two different threads:

public static void main(String[] args) {
    HelloWorld.init(args);
    SwingUtilities.invokeLater( new Runnable() {
        public void run() {
            new Main();
        }
    });   
    HelloWorld.check();
}

Note, however, that this code won't solve your problems. You can read about Swing threading here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.