Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.FileOutputStream;
- import java.io.PrintStream;
- import edu.cmu.sphinx.api.Configuration;
- import edu.cmu.sphinx.api.SpeechResult;
- import edu.cmu.sphinx.api.StreamSpeechRecognizer;
- import edu.cmu.sphinx.decoder.adaptation.Stats;
- import edu.cmu.sphinx.decoder.adaptation.Transform;
- import edu.cmu.sphinx.result.Result;
- import edu.cmu.sphinx.decoder.search.Token;
- public class TranscribeFile {
- public static void showUsage() {
- System.out.println("Usage: java com.example.TranscribeFile <input file> [<output file>]");
- }
- //returns a string representing <time> in the format
- //"hours:minutes:seconds.milliseconds"
- //
- //argument: <time> is the time in milliseconds
- public static String millisecondsToHMS(long time) {
- long h,m,s,mill;
- mill = time % 1000;
- time = (time - mill)/1000;
- s = time % 60;
- time = (time - s)/60;
- m = time % 60;
- time = (time - m)/60;
- h = time;
- return String.format("%d:%02d:%02d.%04d", h, m, s, mill);
- }
- public static void main(String[] args) throws Exception {
- boolean verbose = false;
- String in_file = null;
- String out_file = null;
- PrintStream out = null;
- //Parse options
- try {
- if (args.length < 1)
- throw new Exception("Too few arguments.");
- if (args.length > 2)
- throw new Exception("Too many arguments.");
- in_file = args[0];
- if (args.length >= 2)
- out_file = args[1];
- } catch (Exception e) {
- System.out.format("Error: %s\n", e.getMessage());
- showUsage();
- System.exit(1);
- }
- if (out_file != null)
- out = new PrintStream(new FileOutputStream(out_file));
- else
- out = System.out;
- Configuration configuration = new Configuration();
- //configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
- configuration.setAcousticModelPath("file:/Users/johnny/Downloads/cmusphinx-en-us-ptm-5.2");
- configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
- //configuration.setDictionaryPath("file:/Users/johnny/Downloads/cmudict-0.7b.dict");
- //configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");
- configuration.setLanguageModelPath("file:/Users/johnny/Downloads/en-70k-0.2-pruned.lm");
- StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(configuration);
- InputStream stream;
- //SpeechResult result;
- stream = new FileInputStream(new File(in_file));
- recognizer.startRecognition(stream);
- SpeechResult result;
- Token token;
- long time;
- while ((result = recognizer.getResult()) != null) {
- token = result.getResult().getBestToken();
- if (token != null) {
- time = token.getCollectTime();
- out.format("%s: %s\n", millisecondsToHMS(time), token.getWordPathNoFiller()); //print result together with timecode
- } else {
- out.println("");
- }
- }
- recognizer.stopRecognition();
- stream.close();
- out.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment