2011-03-25 96 views
2

是否有可能从java中识别wav文件是否可以使用cloudgarden java语音API他们是来自cloudgarden示例的示例代码,任何改变都需要我们自己的wav文件 示例代码是低于从wav文件发言到文本Java

import javax.speech.*; 
import javax.speech.recognition.*; 
import java.io.*; 
import com.cloudgarden.audio.*; 
import com.cloudgarden.speech.*; 
import com.cloudgarden.speech.userinterface.SpeechEngineChooser; 

/** 
* Tests running a dictation grammar against input from a wave file. 
*/ 

public class DictationFromFile { 
    static Recognizer rec = null; 

    static DictationGrammar dictation; 

    public static void main(String[] args) { 
     try { 

      RecognizerModeDesc desc = new RecognizerModeDesc(null, Boolean.TRUE); 
      SpeechEngineChooser chooser = null; 

      try { 
       chooser = SpeechEngineChooser.getRecognizerDialog(desc); 
       chooser.show(); 
      } 
      catch (NoClassDefFoundError e) { 
       System.out.println("Swing classes not found - continuing anyway"); 
      } 
      if (chooser != null) 
       desc = chooser.getRecognizerModeDesc(); 

      rec = Central.createRecognizer(desc); 
      rec.addEngineListener(new TestEngineListener()); 

      System.out.println("STARTING TEST"); 

      RecognizerAudioAdapter raud = new TestAudioListener(); 
      CGAudioManager audioMan = (CGAudioManager) rec.getAudioManager(); 
      audioMan.addAudioListener(raud); 

      audioMan.addTransferListener(new TransferListener() { 
       public void bytesTransferred(TransferEvent evt) { 
        System.out.println("transferred " + evt.getLength()); 
       } 
      }); 

      rec.allocate(); 
      rec.waitEngineState(Recognizer.ALLOCATED); 

      dictation = rec.getDictationGrammar("dictation"); 
      dictation.setEnabled(true); 
      // Set the TestResultListener to play back the audio and deallocate after one 
      // recognition. 
      dictation.addResultListener(new TestResultListener(rec, 1, true)); 
      RecognizerProperties props = rec.getRecognizerProperties(); 
      // Retain audio so it can be played back later (see TestResultListener) 
      props.setResultAudioProvided(true); 
      props.setNumResultAlternatives(4); 

      System.out.println("Using engine " + rec.getEngineModeDesc()); 
      SpeakerManager speakerManager = rec.getSpeakerManager(); 
      if (chooser != null) { 
       SpeakerProfile prof = chooser.getSpeakerProfile(); 
       speakerManager.setCurrentSpeaker(prof); 
      } 
      else { 
       SpeakerProfile[] profs = speakerManager.listKnownSpeakers(); 
       speakerManager.setCurrentSpeaker(profs[0]); 
      } 

      System.out.println("Current Profile is " + speakerManager.getCurrentSpeaker()); 

      AudioFileSource source = new AudioFileSource(new File("resources\\hello_world.wav")); 

      System.out.println("file fmt=" + source.getAudioFormat()); 
      System.out.println("rec fmt=" + audioMan.getAudioFormat()); 

      // convert to the recognizer audio format 
      new AudioFormatConverter(source, audioMan, true); 

      // need to use an AudioConverter as above - the following line used 
      // in place of the above line will throw an Exception if the AudioManager 
      // and source have different AudioFormats 
      // audioMan.setSource(source); 

      rec.commitChanges(); 
      rec.requestFocus(); 
      rec.waitEngineState(rec.LISTENING); 

      source.startSending(); 
      System.out.println("sending"); 

      source.drain(); 
      System.out.println("drained"); 

      // deallocate after 10 seconds - in case nothing was recognized 
      Thread killThread = new Thread() { 
       public void run() { 
        try { 
         sleep(10000); 
         System.out.println("Given up waiting for an Accepted Result"); 
         System.out.println("disabling dictation after audio data finished"); 
         dictation.setEnabled(false); 
         if (!rec.testEngineState(rec.DEALLOCATED) 
          && !rec.testEngineState(rec.DEALLOCATING_RESOURCES)) { 
          rec.commitChanges(); 
          rec.waitEngineState(rec.LISTENING); 
          sleep(5000); 
         } 
         System.out.println("Forcing finalize\n"); 
         // forceFinalize causes a RESULT_ACCEPTED event to be sent, and 
         // the TestResultListener will deallocate the recognizer 
         rec.forceFinalize(true); 
         System.out.println("Forced finalize\n"); 
         rec.deallocate(); 
         System.out.println("deallocating\n"); 
        } 
        catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      }; 
      killThread.start(); 

      rec.waitEngineState(Recognizer.DEALLOCATED); 
      // one recognition and the TestResultListener deallocates 

      System.out.println("All done"); 

     } 
     catch (Exception e) { 
      e.printStackTrace(System.out); 
     } 
     catch (Error e1) { 
      e1.printStackTrace(System.out); 
     } 
     finally { 
      try { 
       rec.deallocate(); 
      } 
      catch (Exception e2) { 
       e2.printStackTrace(System.out); 
      } 
      System.exit(0); 
     } 
    } 
} 
+2

Err ...问题是什么?你介意格式化代码并添加标点符号吗? – Thomas 2011-03-25 07:27:53

+1

欢迎来到堆栈溢出!重新问同样的问题在这里是不可接受的行为。如果你想引起人们对你的问题的关注,那么两天后你将被允许[放置赏金](http://stackoverflow.com/faq#bounty)。您还可以[编辑您的问题](http://stackoverflow.com/posts/5219937/edit)添加其他信息,这可能会使您的问题更易于理解和回答。 – Will 2011-03-25 13:33:32

回答

1

是&没有。

是的。 Java Speech API为插件提供了钩子,可以将文本转换为语音,也可以将语音转换为文本(语音识别)。

号不幸的是所有环节,为语音识别插件页面上列出的API,要么破裂或导致该不提供插件(免费商业)的地方。对于在Java Sound API中进行语音识别的插件,您似乎需要自己编写它。