Java LiveSpeechRecognitor打印随机单词

Java LiveSpeechRecognitor打印随机单词,java,speech-recognition,cmusphinx,Java,Speech Recognition,Cmusphinx,我的程序基本上是从语音到文本的。我正在使用LiveSpeechRecognizer CMU Sphinx。我的程序正在运行,没有任何错误,但它没有打印文字。我的意思是它从我的语法文件中随机打印单词 我用我的三星手机耳机作为麦克风 代码: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools |

我的程序基本上是从语音到文本的。我正在使用
LiveSpeechRecognizer CMU Sphinx
。我的程序正在运行,没有任何错误,但它没有打印文字。我的意思是它从我的语法文件中随机打印单词

我用我的三星手机耳机作为麦克风

代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package SpeechRec;
import java.io.IOException;

import javax.sound.sampled.*;

import edu.cmu.sphinx.api.*;
/**
 *
 * @author divyanshu kunwar
 */
public class SpeechRec{

    // Variables
    private String result;

    // Threads
    Thread  speechThread;
    Thread  resourcesThread;

    // LiveRecognizer
    private LiveSpeechRecognizer recognizer;

    /**
     * Constructor
     */
    public SpeechRec() {

        // Loading Message

        // Configuration
        Configuration configuration = new Configuration();

        // Load model from the jar
        configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
        configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");

        // if you want to use LanguageModelPath disable the 3 lines after which
        // are setting a custom grammar->



        // Grammar
        configuration.setGrammarPath("resource:/grammars");
        configuration.setGrammarName("grammar");
        configuration.setUseGrammar(true);



        try {
            recognizer = new LiveSpeechRecognizer(configuration);
        } catch (IOException ex) {
        }

        // Start recognition process pruning previously cached data.
        recognizer.startRecognition(true);

        // Start the Thread
        startSpeechThread();
        startResourcesThread();
    }

    /**
     * Starting the main Thread of speech recognition
     */
    protected void startSpeechThread() {

        // alive?
        if (speechThread != null && speechThread.isAlive())
            return;

        // initialise
        speechThread = new Thread(() -> {
            try {
                while (true) {
                    /*
                     * This method will return when the end of speech is
                     * reached. Note that the end pointer will determine the end
                     * of speech.
                     */
                    SpeechResult speechResult = recognizer.getResult();
                    if (speechResult != null) {

                        result = speechResult.getHypothesis();
                        System.out.println("You said: [" + result + "]\n");
                        // logger.log(Level.INFO, "You said: " + result + "\n")

                    } else{}

                }
            } catch (Exception ex) {
            }

            });

        // Start
        speechThread.start();

    }

    /**
     * Starting a Thread that checks if the resources needed to the
     * SpeechRecognition library are available
     */
    protected void startResourcesThread() {

        // alive?
        if (resourcesThread != null && resourcesThread.isAlive())
            return;

        resourcesThread = new Thread(() -> {
            try {

                // Detect if the microphone is available
                while (true) {
                    if (AudioSystem.isLineSupported(Port.Info.MICROPHONE)) {
                        // logger.log(Level.INFO, "Microphone is available.\n")
                    } else {
                        // logger.log(Level.INFO, "Microphone is not
                        // available.\n")

                    }

                    // Sleep some period
                    Thread.sleep(3500);
                }

            } catch (InterruptedException ex) {
                resourcesThread.interrupt();
            }
        });

        // Start
        resourcesThread.start();
    }

    /**
     * Takes a decision based on the given result
     */
    public void makeDesicion(String result) {
        //implemented in the part 2
    }

    /**
     * Java Main Application Method
     * 
     * @param args
     */
    public static void main(String[] args) {

        // // Be sure that the user can't start this application by not giving
        // the
        // // correct entry string
        // if (args.length == 1 && "SPEECH".equalsIgnoreCase(args[0]))
        new SpeechRec();
        // else
        // Logger.getLogger(Main.class.getName()).log(Level.WARNING, "Give me
        // the correct entry string..");

    }

}

Grammar File:
#JSGF V1.0;
grammar grammar;

public <numbers> = (one | two | three| four| five | six | seven | eight | nine | ten);
public <words>=(how | where);


Output:
You said: [one]

21:01:38.251 INFO liveCMN              35.23 -8.72 -11.38 11.38 2.68 -13.93 3.08 1.74 -8.43 -1.72 -0.49 -8.30 -3.83 
21:01:38.519 INFO speedTracker            This  Time Audio: 0.76s  Proc: 0.73s  Speed: 0.96 X real time
21:01:38.519 INFO speedTracker            Total Time Audio: 2.93s  Proc: 0.99s 0.34 X real time

You said: [one]

21:01:38.519 INFO memoryTracker           Mem  Total: 434.00 Mb  Free: 303.12 Mb
21:01:38.519 INFO memoryTracker           Used: This: 130.88 Mb  Avg: 124.48 Mb  Max: 130.88 Mb
21:01:40.233 INFO speedTracker            This  Time Audio: 0.30s  Proc: 0.19s  Speed: 0.64 X real time
You said: [two]

21:01:40.233 INFO speedTracker            Total Time Audio: 3.23s  Proc: 1.18s 0.37 X real time
21:01:40.233 INFO memoryTracker           Mem  Total: 434.00 Mb  Free: 295.40 Mb
21:01:40.233 INFO memoryTracker           Used: This: 138.60 Mb  Avg: 129.18 Mb  Max: 138.60 Mb
21:01:41.777 INFO liveCMN              35.55 -8.33 -10.84 13.40 3.05 -13.95 4.24 1.77 -9.87 -2.61 -1.27 -8.00 -3.56 
21:01:42.853 INFO liveCMN              35.35 -7.67 -10.27 11.74 2.97 -13.25 3.25 1.60 -9.12 -2.91 -1.70 -7.04 -3.10 
21:01:42.990 INFO speedTracker            This  Time Audio: 1.29s  Proc: 1.25s  Speed: 0.97 X real time
21:01:42.990 INFO speedTracker            Total Time Audio: 4.52s  Proc: 2.43s 0.54 X real time

You said: [one]

21:01:42.990 INFO memoryTracker           Mem  Total: 434.00 Mb  Free: 279.92 Mb
21:01:42.991 INFO memoryTracker           Used: This: 154.08 Mb  Avg: 135.41 Mb  Max: 154.08 Mb
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
一揽子演讲;
导入java.io.IOException;
导入javax.sound.sampled.*;
导入edu.cmu.sphinx.api.*;
/**
*
*@作者divyanshu kunwar
*/
公开课讲演{
//变数
私有字符串结果;
//线程
线程说线程;
线程资源线程;
//肝脏识别器
专用LiveSpeechRecognitor识别器;
/**
*建造师
*/
公众演讲{
//加载消息
//配置
配置=新配置();
//从jar加载模型
setAcousticModelPath(“资源:/edu/cmu/sphinx/models/en-us/en-us”);
setDictionaryPath(“resource:/edu/cmu/sphinx/models/en-us/cmudict en-us.dict”);
//如果要使用LanguageModelPath,请禁用后面的3行
//正在设置自定义语法->
//文法
setGrammarPath(“资源:/grammars”);
setGrammarName(“语法”);
setUseGrammar(true);
试一试{
识别器=新的LiveSpeechRecognizer(配置);
}捕获(IOEX异常){
}
//开始识别过程修剪以前缓存的数据。
识别器。startRecognition(真);
//开线
startSpeechThread();
startResourcesThread();
}
/**
*启动语音识别的主线
*/
受保护的void startSpeechThread(){
//活着?
if(speechThread!=null&&speechThread.isAlive())
返回;
//初始化
speechThread=新线程(()->{
试一试{
while(true){
/*
*此方法将在结束讲话时返回
*已到达。请注意,结束指针将确定结束
*说话的方式。
*/
SpeechResult SpeechResult=recognizer.getResult();
if(speechResult!=null){
结果=speechResult.gethyporation();
System.out.println(“您说过:[“+result+”]\n”);
//logger.log(Level.INFO,“您说:“+result+”\n”)
}else{}
}
}捕获(例外情况除外){
}
});
//开始
speechThread.start();
}
/**
*启动一个线程,该线程检查
*语音识别库可用
*/
受保护的void startResourcesThread(){
//活着?
if(resourcesThread!=null&&resourcesThread.isAlive()
返回;
resourcesThread=新线程(()->{
试一试{
//检测麦克风是否可用
while(true){
if(AudioSystem.isLineSupported(端口信息麦克风)){
//logger.log(Level.INFO,“麦克风可用。\n”)
}否则{
//logger.log(Level.INFO),“麦克风不可用
//可用。\n“
}
//睡一段时间
睡眠(3500);
}
}捕获(中断异常例外){
resourcesThread.interrupt();
}
});
//开始
resourcesThread.start();
}
/**
*根据给定的结果做出决定
*/
公共void makedecision(字符串结果){
//在第2部分中实现
}
/**
*Java主应用方法
* 
*@param args
*/
公共静态void main(字符串[]args){
////确保用户不能通过不提供
//
////正确的输入字符串
//if(args.length==1&&“SPEECH.equalsIgnoreCase(args[0]))
新SpeechRec();
//否则
//Logger.getLogger(Main.class.getName()).log(Level.WARNING,“告诉我
//正确的输入字符串…);
}
}
语法文件:
#jsgfv1.0;
语法;
公共=(一|二|三|四|五|六|七|八|九|十);
public=(how | where);
输出:
你说:[一]
21:01:38.251信息直播CMN 35.23-8.72-11.38 11.38 2.68-13.93 3.08 1.74-8.43-1.72-0.49-8.30-3.83
21:01:38.519信息速度跟踪器这次音频:0.76s进程:0.73s速度:0.96 X实时
21:01:38.519信息速度跟踪器总时间音频:2.93s进程:0.99s 0.34 X实时
你说:[一]
21:01:38.519信息存储器Tracker Mem总计:434.00 Mb免费:303.12 Mb
21:01:38.519使用的信息内存跟踪程序:此:130.88 Mb平均:124.48 Mb最大:130.88 Mb
21:01:40.233信息速度跟踪器这次音频:0.30s进程:0.19s速度:0.64 X实时
你说:[两个]
21:01:40.233信息速度跟踪器总时间音频:3.23s过程:1.18s 0.37 X实时
21:01:40.233信息存储器Tracker Mem总计:434.00 Mb免费:295.40 Mb
21:01:40.233使用的信息内存跟踪程序:此:138.60 Mb平均:129.18 Mb最大:138.60 Mb
21:01:41.777信息直播CMN 35.55-8.33-10.84 13.40 3.05-13.95 4.24 1.77-9.87-2.61-1.27-8.00-3.56
21:01:42.853信息直播CMN 35.35-7.67-10.27 11.74 2.97-13.25 3.25 1.60-9.12-2.91-1.70-7.04-3.10
21:01:42.990信息速度跟踪器