【发布时间】:2013-06-27 15:55:20
【问题描述】:
我正在尝试在 Java 中使用 Sphinx4 并通过网络从 InputStream 输入。目前,我的程序设置为将从网络传入的任意数量的数据包读取到名为all_data 的数组中。由此我创建了一个ByteArrayInputStream。我希望能够将其传递给 Sphinx 以使其被识别。
我目前的代码是:
InputStream audioToPlay = new ByteArrayInputStream(all_data);
ConfigurationManager cm;
cm = new ConfigurationManager(BTsend.class.getResource("roila.config.xml"));
System.out.println("Loading Recognizer...");
Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
recognizer.allocate();
StreamDataSource streamDataSource = (StreamDataSource) cm.lookup("streamDataSource");
streamDataSource.setInputStream(audioToPlay, "Main Stream");
System.out.println("Start speaking.\n");
Result result = recognizer.recognize();
if (result != null)
{
String resultText = result.getBestResultNoFiller();
System.out.println("You said: " + resultText + "\n");
}
else
{
System.out.println("I can't hear what you said.\n");
//break;
}
如果我使用麦克风作为输入,同样的代码也可以工作。我所做的一切都基于以下示例:http://roila.org/wp-content/uploads/2010/04/roila_java.txt。该代码确实有效,而我所做的只是将麦克风更改为 StreamDataSource 并尝试让它工作。
不幸的是,无论我尝试什么,结果总是返回 null,但是当我使用麦克风输入时(请参阅上面 roila.org 上的示例),效果很好!
我在 roila.confg.xml 文件中添加了以下内容:
<component name="streamDataSource" type="edu.cmu.sphinx.frontend.util.StreamDataSource">
<property name="sampleRate" value="16000" />
<property name="bigendianData" value="false" />
</component>
在我添加它之前,我得到了一个空指针异常,但之后streamDataSource 被拾取。
感谢您提供任何帮助 - 我最终希望能够做的是识别为数据不断进入,以使处理变得更容易。
【问题讨论】:
-
当您遇到 NullPointerException 问题时,请学习从 NullPointerException 提供堆栈跟踪。有关您的问题的所有重要信息都包含在堆栈跟踪中,您只需阅读它。
标签: speech-recognition sphinx4