将文件作为输入提供给Android上的Pocketsphinx

时间:2015-03-12 11:07:08

标签: pocketsphinx-android

我正在使用最新的pocketsphinx android演示程序(强大的计算机),它从麦克风输入。我想给一个wav文件作为输入。我尝试使用decoder.processrow()函数。但我不知道如何使用hmm,lm等配置解码器。

2 个答案:

答案 0 :(得分:3)

用于处理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());
    }
}

答案 1 :(得分:1)

添加来自Nikolay的答案,这就是如何在Android上完成,调整此处的SpeechRecognizer Android实施示例: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);
}