所以我正在构建一个sphinx-4程序,它只会在你按住shift键的时候监听。这是为了防止出现错误,并且只在我按住shift键的时候才让它听我说。当我松开shift按钮时,我希望程序等到我再次按住它。然后,当按下ctrl-c组合键时,程序将完全退出。我使用一个keylistener来做这件事。
我遇到的问题是,程序在我按下shift键后开始监听,但当我松开它时,它不会停止监听。我不确定我的代码出了什么问题。下面是我创建的MKeyListener类中的内容:
public class MKeyListener implements KeyListener {
static ConfigurationManager cm;
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("Please hold down the shift button to have Sphinx listen.");
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_SHIFT)
{
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()) {
System.out.println("Cannot start microphone.");
recognizer.deallocate();
System.exit(1);
}
System.out.println("Say: (Good morning | Hello) ( Bhiksha | Evandro | Paul | Philip | Rita | Will )");
// loop the recognition until the programm exits.
Boolean var = e.isShiftDown();
while (var == true) {
System.out.println("Start speaking. Press Ctrl-C to quit.\n");
Result result = recognizer.recognize();
if (result != null) {
String resultText = result.getBestFinalResultNoFiller();
System.out.println("You said: " + resultText + '\n');
} else {
System.out.println("I can't hear what you said.\n");
}
var = e.isShiftDown();
System.out.println(var);
}
recognizer.deallocate();
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_SHIFT)
{
Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
recognizer.deallocate();
}下面是我正在运行的主类:
public class HelloWorld extends MKeyListener implements KeyListener{
public static void main(String[] args) throws Exception {
JTextField textField = new JTextField();
textField.addKeyListener(new MKeyListener());
JFrame jframe = new JFrame();
jframe.add(textField);
jframe.setSize(400, 350);
jframe.setVisible(true);
if (args.length > 0) {
cm = new ConfigurationManager(args[0]);
} else {
cm = new ConfigurationManager(HelloWorld.class.getResource("helloworld.config.xml"));
}
}我在这里做错了什么?
发布于 2013-07-22 04:20:49
你的方法看起来很昂贵。虽然我不是Sphinx4方面的专家,但您正在尝试做一些看起来既不必要又可能有问题的事情
我写了一个应用程序,它做了完全相同的事情(除了它监听空格而不是shift)。我的方法完全不同,而且我认为是相当轻量级的。
config.xml:
<component name="keyPressedSpeechClassifier"
type="package.KeyPressedSpeechClassifier">
</component>
<component name="epFrontEnd" type="edu.cmu.sphinx.frontend.FrontEnd">
<propertylist name="pipeline">
<item>microphone</item>
<item>speechClassifier</item>
<item>keyPressedSpeechClassifier</item>
<item>speechMarker</item>
<item>nonSpeechDataFilter</item>
<item>premphasizer</item>
<item>windower</item>
<item>fft</item>
<item>melFilterBank</item>
<item>dct</item>
<item>liveCMN</item>
<item>featureExtraction</item>
</propertylist>
</component>KeyPressedSpeechClassifier:
public class KeyPressedSpeechClassifier extends BaseDataProcessor {
static KeyPressedSpeechClassifier _instance;
public static KeyPressedSpeechClassifier getInstance() {return _instance;}
LinkedList<Data> queue;
volatile boolean isRecognitionEnabled = false;
public KeyPressedSpeechClassifier() {
queue = new LinkedList<Data>();
_instance = this;
System.out.println("KeyPressedSpeechClassifier created");
}
@Override
public Data getData() throws DataProcessingException {
Data data = getPredecessor().getData();
if (data instanceof DoubleData) {
DoubleData dd = (DoubleData) data;
queue.push(new SpeechClassifiedData(dd, isRecognitionEnabled));
} else if (data instanceof SpeechClassifiedData) {
((SpeechClassifiedData) data).setSpeech(isRecognitionEnabled);
queue.push(data);
} else {
queue.push(data);
}
if (queue.isEmpty()) {
return null;
} else {
return queue.pop();
}
}
public void setRecognitionEnabled(boolean enabled) {
this.isRecognitionEnabled = enabled;
}
}https://stackoverflow.com/questions/17703397
复制相似问题