Pocketsphinx的JNI绑定中的访问冲突

时间:2016-05-04 12:50:05

标签: java cmusphinx pocketsphinx

我尝试用Pocketsphinx编写一个小的VoiceRecognition。

首先,我建立了sphinxbase和pocketsphinx。我能够从cmd开始pocketphinx_continuous。我想只在inmic-mode中使用它。

现在我尝试重写c-source,所以我只有inmic-mode。我从这段代码中构建了一个.dll,并尝试在Eclipse中运行它。

这是我的Java Source

public class Test {

    private native Test Decoder_defaultConfig();
    private native void recognize_from_mic();

    public static void main(final String[] args) {

        final Test test = new Test().Decoder_defaultConfig();
        test.recognize_from_mic();

    }
    static {
        System.loadLibrary("pocketsphinx_Test");
    }
}

生成的Header-File:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Test */

#ifndef _Included_Test
#define _Included_Test
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Test
 * Method:    Decoder_defaultConfig
 * Signature: ()LTest;
 */
JNIEXPORT jobject JNICALL Java_Test_Decoder_1defaultConfig
  (JNIEnv *, jobject);

/*
 * Class:     Test
 * Method:    recognize_from_mic
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_Test_recognize_1from_1mic
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

我将我的c-source改为:

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <jni.h>
#include <sphinxbase\cmd_ln.h>

#include <windows.h>

#include <sphinxbase/err.h>
#include <sphinxbase/ad.h>

#include "pocketsphinx.h"

#define MODELDIR "C:/Users/310086414/Desktop/pocketsphinx/pocketsphinx/bin/Release/x64/model"

const char * recognize_from_microphone();

static ps_decoder_t *ps;
static cmd_ln_t *config;
static FILE *rawfd;
ad_rec_t *ad;
int16 adbuf[2048];
uint8 utt_started, in_speech;
int32 k;
char const *hyp;
char const *decoded_speech;


JNIEXPORT void JNICALL Java_Test_Decoder_1defaultConfig(JNIEnv *env, jobject obj)
{
    config = cmd_ln_init(NULL, ps_args(), TRUE,                   // Load the configuration structure - ps_args() passes the default values
        "-hmm", MODELDIR "/en-us/en-us",  // path to the standard english language model
        "-lm", MODELDIR "/en-us/en-us.lm.bin",                                         // custom language model (file must be present)
        "-dict", MODELDIR "/en-us/cmudict_en_us.dict",                                    // custom dictionary (file must be present)
        NULL);

    if (config == NULL) {
        cmd_ln_free_r(config);
        E_INFO("configuration failed");
    }

    ps = ps_init(config);                                                        // initialize the pocketsphinx decoder

    if (ps == NULL) {
        cmd_ln_free_r(config);
        E_INFO("decoder failed");
    }


    ad = ad_open_dev("sysdefault", (int)cmd_ln_float32_r(config, "-samprate")); // open default microphone at default samplerate

    while (1) {
        decoded_speech = recognize_from_microphone();                 // call the function to capture and decode speech           
        printf("You Said: %s\n", decoded_speech);                               // send decoded speech to screen

    }

    ad_close(ad);                                                    // close the microphone
}

const char * recognize_from_microphone() {

    ad_start_rec(ad);                                // start recording
    ps_start_utt(ps);                                // mark the start of the utterance
    utt_started = FALSE;                             // clear the utt_started flag

    while (1) {
        k = ad_read(ad, adbuf, 4096);                // capture the number of frames in the audio buffer
        ps_process_raw(ps, adbuf, k, FALSE, FALSE);  // send the audio buffer to the pocketsphinx decoder

        in_speech = ps_get_in_speech(ps);            // test to see if speech is being detected

        if (in_speech && !utt_started) {             // if speech has started and utt_started flag is false                           
            utt_started = TRUE;                      // then set the flag
        }

        if (!in_speech && utt_started) {             // if speech has ended and the utt_started flag is true
            ps_end_utt(ps);                          // then mark the end of the utterance
            ad_stop_rec(ad);                         // stop recording
            hyp = ps_get_hyp(ps, NULL);             // query pocketsphinx for "hypothesis" of decoded statement
            return hyp;                              // the function returns the hypothesis
            break;                                   // exit the while loop and return to main
        }
    }

    }

如果我现在使用Eclipse运行Java-source,我会收到以下消息:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000023bfbfc, pid=9780, tid=0x0000000000002a38
#
# JRE version: OpenJDK Runtime Environment (8.0_74-b02) (build 1.8.0_74-b02)
# Java VM: OpenJDK 64-Bit Server VM (25.74-b02 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [pocketsphinx.dll+0x1fbfc]  ps_start_utt+0x1c
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\sde\workspaceAW_4.5\pocketsphinx\hs_err_pid9780.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

它说ps_start_utt有任何问题,但我没有改变任何东西。使用sphinaxbase / pocketsphinx时又出现了错误吗?

1 个答案:

答案 0 :(得分:0)

这部分

   config = cmd_ln_init(NULL, ps_args(), "-inmic", "yes", TRUE, NULL);

    if (config == NULL || cmd_ln_boolean_r(config, "-inmic") == FALSE) {
        E_INFO("Specify '-inmic yes' to recognize from microphone.\n");
        cmd_ln_free_r(config);

    }

不太正确。 &#34; -inmic&#34; pocketphinx_continuous是一个选项,不是sphinxbase的选项。它应该只是

    config = cmd_ln_init(NULL, ps_args(), TRUE, NULL);

此外,您需要在cmd_ln_free_r(config)之后退出程序,而不是继续执行。当您释放配置并稍后尝试使用它时,会导致内存违规。