javainputstreamjava-6cmusphinxhelios

InputStream and URL incompatibility


I am working in Eclipse Helios Java 1.6. I have downloaded CMU Sphinx-4 and added into Java project. The code below is to convert audio file into text file.

import java.io.File;

import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.StreamSpeechRecognizer;

public class AudioToText {
    public static void main(String [] args){
        Configuration configuration = new Configuration();

        // Set path to acoustic model.
        configuration.setAcousticModelPath("C:/Program Files/eclipse/sphinx4-5prealpha/models/acoustic");
        // Set path to dictionary.
        configuration.setDictionaryPath("C:/Program Files/eclipse/sphinx4-5prealpha/models/acoustic/wsj/dict/cmudict.0.6d");
        // Set language model.
        configuration.setLanguageModelPath("C:/Program Files/eclipse/sphinx4-5prealpha/models/language/en-us.lm.dmp");

        StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(configuration);
        recognizer.startRecognition(new File("D:/audio.mp3").toURI().toURL());
        SpeechResult result = recognizer.getResult();
        recognizer.stopRecognition();
    }
}

Error is in line:

recognizer.startRecognition(new File("D:/audio.mp3").toURI().toURL());

Error:

- The method startRecognition(InputStream) in the type StreamSpeechRecognizer is not applicable for the 
 arguments (URL)
- The method startRecognition(InputStream) in the type StreamSpeechRecognizer is not applicable for the 
 arguments (File)
- Line breakpoint:AudioToText [line: 19] - main(String[])

When I looked into startRecognition() definition,

public void startRecognition(InputStream stream) {
    recognizer.allocate();
    context.setSpeechSource(stream);
}

How to fix this compatibility issue? Code is taken from here

Shifting from Helios to upgraded version or Java 1.6 to higher might be expensive because a lot of previous code is written on Helios and Java 1.6.


Solution

  • The method requires an InputStream as argument, you cannot pass in a File or an URL instead.

    Simply pass in an InputStream:

    recognizer.startRecognition(new FileInputStream("D:/audio.mp3"));