LiveSpeechRecognizer打印随机单词

时间:2017-02-12 15:33:47

标签: 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 | 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

我没说这些话。请帮忙。

1 个答案:

答案 0 :(得分:0)

获取没有噪音的正确麦克风。