my goal is to let users speak by clicking a button and then when they click the button for stopping, the console outputs the result. I tried to apply the code that the CMU sphinx official website writes, as the following code:
LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);
// Start recognition process pruning previously cached data.
recognizer.startRecognition(true);
SpeechResult result = recognizer.getResult();
// Pause recognition process. It can be resumed then with startRecognition(false).
recognizer.stopRecognition();
This is how I implemented with zk-maven project:
public class SphinxSpeechRecog extends SelectorComposer<Component> {
Configuration configuration = new Configuration();
private LiveSpeechRecognizer recognizer;
private SpeechResult result;
public SphinxSpeechRecog() {
// TODO Auto-generated constructor stub
}
@Listen("onClick=#speakbtn")
public void startSpeaking() throws IOException, InstantiationException {
//System.out.println("hi");
configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");
recognizer = new LiveSpeechRecognizer(configuration);
recognizer.startRecognition(true);
result = recognizer.getResult();
recognizer.stopRecognition();
}
@Listen("onClick=#stopspeakbtn")
public void stopSpeaking() {
System.out.print("result: "+result);
}
}
the frontend(.zul file):
<?page title="sphinx speech recognition" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="sphinx speech recognition" border="normal" apply="sphinx.SphinxSpeechRecog">
<button id="speakbtn" label="speak"/>
<button id="stopspeakbtn" label="stop speaking"/>
</window>
</zk>
The java(eclipse) console outputs some weird result like this (after so many minutes):
result: edu.cmu.sphinx.api.SpeechResult@3be41473
What can I do to optimize my code? And how to get the spoken words I really want to see?
for(WordResult word : result.getWords()) {
System.out.print(word.toString());
}
See more examples here.