Pocketsphinx 的 JNI 绑定中的访问冲突
Access violation in JNI bindings for Pocketsphinx
我尝试用 Pocketsphinx 编写一些 VoiceRecognition 程序。
首先我构建了 sphinxbase 和 pocketsphinx。我可以从 cmd 启动 pocketsphinx_continuous。我只想在 inmic 模式下使用它。
现在我试图重写c-source,所以我只有inmic-mode。我用这段代码构建了一个 .dll 并尝试在 Eclipse 中 运行 它。
这是我的Java来源
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");
}
}
以及生成的头文件:
/* 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 源更改为:
#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是不是又出错了?
这部分
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);
}
不太正确。 “-inmic”是 pocketsphinx_continuous 的一个选项,不是 sphinxbase 的一个选项。应该是简单的
config = cmd_ln_init(NULL, ps_args(), TRUE, NULL);
另外,cmd_ln_free_r(config)
后需要退出程序,不能继续执行。当您释放配置并稍后尝试使用它时,您会导致内存冲突。
我尝试用 Pocketsphinx 编写一些 VoiceRecognition 程序。
首先我构建了 sphinxbase 和 pocketsphinx。我可以从 cmd 启动 pocketsphinx_continuous。我只想在 inmic 模式下使用它。
现在我试图重写c-source,所以我只有inmic-mode。我用这段代码构建了一个 .dll 并尝试在 Eclipse 中 运行 它。
这是我的Java来源
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");
}
}
以及生成的头文件:
/* 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 源更改为:
#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是不是又出错了?
这部分
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);
}
不太正确。 “-inmic”是 pocketsphinx_continuous 的一个选项,不是 sphinxbase 的一个选项。应该是简单的
config = cmd_ln_init(NULL, ps_args(), TRUE, NULL);
另外,cmd_ln_free_r(config)
后需要退出程序,不能继续执行。当您释放配置并稍后尝试使用它时,您会导致内存冲突。