Menu

Passing stream to pockstsphinx. Getting BufferUnderFlow

Help
TheEditor
2016-08-08
2016-08-10
  • TheEditor

    TheEditor - 2016-08-08

    I've been trying to piece this together from various sources. I'm close but getting an error.

    I'm getting a java.nio.BufferUnderflowExectpion. This is the only java example I've found to base this off of. https://github.com/cmusphinx/pocketsphinx/blob/master/swig/java/test/DecoderTest.java

    Thanks

    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.TargetDataLine;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.ByteOrder;
    
    import edu.cmu.pocketsphinx.Decoder;
    import edu.cmu.pocketsphinx.Config;
    
    public class Controller {
    static {
        System.loadLibrary("pocketsphinx_jni");
    }
    
    public static void main(String args[]) {
    
        AudioFormat format = new AudioFormat(44100, 16, 1, true, true);
    
        DataLine.Info targetInfo = new DataLine.Info(TargetDataLine.class, format);
    
        try {
           TargetDataLine targetLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
            targetLine.open(format, 4096);
    
           Config c = Decoder.defaultConfig();
            c.setString("-hmm", "/usr/local/share/pocketsphinx/model/en-us/en-us/");
            c.setString("-dict", "/usr/local/share/pocketsphinx/model/en-us/cmudict-en-us.dict");
            c.setString("-keyphrase", "abomination");
            c.setFloat("-kws_threshold", 1e-20);
            Decoder d = new Decoder(c);
    
            targetLine.start();
            d.startUtt();
    
            int nbytes;
            byte[] data = new byte[targetLine.getBufferSize() / 4];
    
            while ((nbytes = targetLine.read(data, 0, data.length)) >= 0) {
                ByteBuffer bb = ByteBuffer.wrap(data, 0, nbytes);
                bb.order(ByteOrder.LITTLE_ENDIAN);
                short[] s = new short[nbytes];
                bb.asShortBuffer().get(s);
                d.processRaw(s, nbytes, false, false);
            }
            d.endUtt();
    
            if (d.hyp() !=  null) {
                System.out.println(d.hyp().getHypstr());
            }
        }
        catch (Exception e) {
            System.err.println(e);
        }
        }
        }
    
     
  • TheEditor

    TheEditor - 2016-08-08

    I got rid of the buffer error. Now just getting null hypothesis.

    I stopped for a moment to test my mic. Didn't work. Had to install libasound2-dev and recompile sphinxbase. Once done works fine from command line via:

    pocketsphinx_continuous -inmic yes
    

    The following code runs with no error. It only seems to return null hypothesis. I ge this console note every few seconds.

    INFO: cmn_prior.c(99): cmn_prior_update: from < 40.00  3.00 -1.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 >
    
    
    
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.TargetDataLine;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.ByteOrder;
    import java.util.Arrays;
    
    import edu.cmu.pocketsphinx.Decoder;
    import edu.cmu.pocketsphinx.Config;
    import edu.cmu.pocketsphinx.Hypothesis;
    
    public class Controller {
    static {
        System.loadLibrary("pocketsphinx_jni");
    }
    
    private static ByteArrayOutputStream out;
    
    public static void main(String args[]) {
    
        AudioFormat format = new AudioFormat(44100, 16, 1, true, true);
        TargetDataLine targetLine = null;
        DataLine.Info targetInfo = new DataLine.Info(TargetDataLine.class, format);
        boolean running = true;
    
    
        try {
    
            targetLine = AudioSystem.getTargetDataLine(format);
            targetLine.open();
            out = new ByteArrayOutputStream();
            int numBytesRead;
            byte[] data = new byte[targetLine.getBufferSize() / 5];
    
    
            Config c = Decoder.defaultConfig();
            c.setString("-hmm", "/usr/local/share/pocketsphinx/model/en-us/en-us/");
            c.setString("-dict", "/usr/local/share/pocketsphinx/model/en-us/cmudict-en-us.dict");
            c.setString("-keyphrase", "abomination");
            c.setFloat("-kws_threshold", 1e-20);
    
            Decoder d = new Decoder(c);
            d.setRawdataSize(300000);
    
            targetLine.start();
            System.out.println("Recorder started");
    
            byte[] b = new byte[4096];
    
            d.startUtt();
    
            System.out.println("Decoder started");
    
            while ((running)) {
                int nbytes;
                short[] s = null;
                nbytes = targetLine.read(b,0,b.length);
    
                ByteBuffer bb = ByteBuffer.wrap(b, 0, nbytes);
                s = new short[nbytes/2];
    
                bb.asShortBuffer().get(s);
    
                d.processRaw(s, nbytes/2, false, false);
    
                if (nbytes > 0) {
    
                    Hypothesis hypothesis = d.hyp();
                    if (hypothesis != null) {
                        System.out.println("------------------------------------------------------");
                        System.out.println(hypothesis.getHypstr());
                        System.out.println("------------------------------------------------------");
    
                        d.endUtt();
                        d.startUtt();
                    }
                }
            }
    
        }
        catch (Exception e) {
            System.err.println(e);
        }
    }
    }
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.