/* Jaivox version 0.5 August 2013 Copyright 2010-2013 by Bits and Pixels, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import com.jaivox.interpreter.Command; import com.jaivox.interpreter.Interact; import com.jaivox.synthesizer.PATsynthesizer.Synthesizer; import com.jaivox.util.Log; 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 java.util.Properties; public class PATliveOneSphinx extends Thread { static String config = "PATlive.xml"; static String basedir = "./"; Interact inter; Synthesizer speaker; public PATliveOneSphinx () { Log log = new Log (); log.setLevelByName ("PATlog_level"); initializeInterpreter (); processSpeech (); } void initializeInterpreter () { Properties kv = new Properties (); kv.setProperty ("data_file", "PATdata_file"); kv.setProperty ("common_words", "PATcommon_words"); kv.setProperty ("specs_file", "PATspecs_file"); kv.setProperty ("questions_file", "PATquestions_file"); kv.setProperty ("grammar_file", "PATgrammar_file"); kv.setProperty ("language", "PATttslang"); Command cmd = new Command (); inter = new Interact (basedir, kv, cmd); speaker = new Synthesizer (kv); } void processSpeech () { ConfigurationManager cm = new ConfigurationManager ( PATliveOneSphinx.class.getResource (config)); // allocate the recognizer Log.info ("Loading..."); Recognizer recognizer = (Recognizer) cm.lookup ("recognizer"); recognizer.allocate (); // start the microphone or exit if the programm if this is not possible Microphone microphone = (Microphone) cm.lookup ("microphone"); if (!microphone.startRecording ()) { Log.severe ("Cannot start microphone."); recognizer.deallocate (); System.exit (1); } System.out.println ("Sample questions are in lm_training_file"); try { // loop the recognition until the programm exits. while (true) { System.out.println ("Start speaking. Press Ctrl-C to quit.\n"); Result result = recognizer.recognize (); String recognized = null; String response = null; if (result != null) { recognized = result.getBestResultNoFiller (); System.out.println ("You said: " + recognized + '\n'); } else { System.out.println ("I can't hear what you said."); continue; } if (recognized != null) { response = inter.execute (recognized); System.out.println ("Reply: " + response); Thread.sleep (4000); speaker.speak (response); } } } catch (Exception e) { e.printStackTrace (); } } public static void main (String args []) { new PATliveOneSphinx (); } }