I am using the latest pocketsphinx android demo (mighty computer),which takes input from microphone. I want to give a wav file as input to the same. I tried using decoder.processrow() function. But I don't know how to configure the decoder using hmm, lm etc.
            Asked
            
        
        
            Active
            
        
            Viewed 2,640 times
        
    2 Answers
3
            
            
        Code to process files in pocketsphinx-java
    Config c = Decoder.defaultConfig();
    c.setString("-hmm", "../../model/en-us/en-us");
    c.setString("-lm", "../../model/en-us/en-us.lm.dmp");
    c.setString("-dict", "../../model/en-us/cmudict-en-us.dict");
    Decoder d = new Decoder(c);
    URL testwav = new URL("file:../../test/data/goforward.wav");
    FileInputStream stream = new FileInputStream(new File(testwav)));
    d.startUtt();
    byte[] b = new byte[4096];
    try {
        int nbytes;
        while ((nbytes = stream.read(b)) >= 0) {
            ByteBuffer bb = ByteBuffer.wrap(b, 0, nbytes);
            // Not needed on desktop but required on android
            bb.order(ByteOrder.LITTLE_ENDIAN); 
            short[] s = new short[nbytes/2];
            bb.asShortBuffer().get(s);
            d.processRaw(s, nbytes/2, false, false);
        }
    } catch (IOException e) {
        fail("Error when reading goforward.wav" + e.getMessage());
    }
    d.endUtt();
    System.out.println(d.hyp().getHypstr());
    for (Segment seg : d.seg()) {
        System.out.println(seg.getWord());
    }
}
        Nikolay Shmyrev
        
- 24,897
 - 5
 - 43
 - 87
 
- 
                    Do pocketsphinx only takes audio as input or can it take text from text-to-speech as input too? – June Wang Oct 04 '19 at 14:07
 - 
                    @JuneWang pocketsphinx is speech recognition, so it takes audio – Nikolay Shmyrev Oct 04 '19 at 23:14
 
1
            
            
        Adding to the answer from Nikolay, this is how it can be done on Android, adapting the SpeechRecognizer Android implementation example found here: http://cmusphinx.sourceforge.net/wiki/tutorialandroid
//statically load our library
static {
    System.loadLibrary("pocketsphinx_jni");
}
//convert an inputstream to text
private void convertToSpeech(final InputStream stream){
    new AsyncTask<Void, Void, Exception>() {
        @Override
        protected Exception doInBackground(Void... params) {
            try {
                Assets assets = new Assets(WearService.this);
                File assetsDir = assets.syncAssets();
                Config c = Decoder.defaultConfig();
                c.setString("-hmm", new File(assetsDir, "en-us-ptm").getPath());
                c.setString("-dict", new File(assetsDir, "cmudict-en-us.dict").getPath());
                c.setBoolean("-allphone_ci", true);
                c.setString("-lm", new File(assetsDir, "en-phone.dmp").getPath());
                Decoder d = new Decoder(c);
                d.startUtt();
                byte[] b = new byte[4096];
                try {
                    int nbytes;
                    while ((nbytes = stream.read(b)) >= 0) {
                        ByteBuffer bb = ByteBuffer.wrap(b, 0, nbytes);
                        // Not needed on desktop but required on android
                        bb.order(ByteOrder.LITTLE_ENDIAN);
                        short[] s = new short[nbytes/2];
                        bb.asShortBuffer().get(s);
                        d.processRaw(s, nbytes/2, false, false);
                    }
                } catch (IOException e) {
                    fail("Error when reading inputstream" + e.getMessage());
                }
                d.endUtt();
                System.out.println(d.hyp().getHypstr());
                for (Segment seg : d.seg()) {
                    //do something with the result here
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
        wblaschko
        
- 3,252
 - 1
 - 18
 - 24