Java中怎么通过调用jna实现语音识别功能,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
JNA
java调用.dll获取.so一般通过JNI,但是JNI的使用比较复杂,需要用C另写一个共享库进行适配。而JNA是一个自动适配工具,通过它调用.dll只需要一个借口即可。
官网:https://github.com/twall/jna/。下载jna.jar即可。
编写接口
科大讯飞语音云主要提供语音合成和语音识别两个方面的东西,我主要使用语音识别这块的功能。
建立接口QTSR,继承Library。
将msc.dll等文件复制到项目根目录。
加载msc.dll
QTSR INSTANCE = (QTSR) Native.loadLibrary("msc", QTSR.class);
然后来看一下msc.dll公开了哪些方法。首先是QISRInit,这是一个全局初始化函数。
它的返回值为int,参数是const char*。int还是java的int,但是char*就对应的是java的String了。
所以在QTSR中添加方法:
public int QISRInit(String configs);
返回值在msp_errors.h中定义,等一下我们还是要弄在java里面去。
继续看QISRInit函数,在官方文档中有调用示例:
const char* configs=“server_url=dev.voicecloud.cn, timeout=10000, vad_enable=true”; int ret = QISRInit( configs ); if(MSP_SUCCESS != ret ) { printf( “QISRInit failed, error code is: %d”, ret ); }
对应的在java中的调用代码如下:
String config = "server_url=dev.voicecloud.cn, timeout=10000, vad_enable=true"; int code = QTSR.INSTANCE.QISRInit(config); if (code != 0) { System.out.println("QISRInit failed, error code is:" + code); }
我们在看一个函数:QISRSessionBegin,这个开始一路ISR会话。
还是刚才的思路,char*对应java的String,但是注意一下int *errorCode。这个函数其实传入两个参数,传出两个参数。即本身返回的sessionId,还有errorCode。
这里的int*对应的是jna的IntByReference。所以添加方法:
public String QISRSessionBegin(String grammarList, String params,IntByReference errorCode);
同样看看官方示例:
const char* params= “ssm=1,sub=iat,aue=speex-wb;7,auf=audio/L16;rate=16000,ent=sms16k,rst=plain,vad_timeout=1000,vad_speech_tail=1000”; int ret = MSP_SUCCESS; const char* session_id = QISRSessionBegin( NULL, params, &ret ); if(MSP_SUCCESS != ret ) { printf( “QISRSessionBegin failed, error code is: %d”, ret ); }
在java这样写:
String params = "ssm=1,sub=iat,aue=speex-wb;7,auf=audio/L16;rate=16000,ent=sms16k,rst=plain,vad_timeout=1000,vad_speech_tail=1000"; IntByReference errorCode = new IntByReference(); String sessionId = QTSR.INSTANCE.QISRSessionBegin(null, params,errorCode);
运行效果:
其他的函数处理方式大致相同,这里贴上一个c和java在jna中的类型对应表:
其中Unsigned类型和signed在java中对应是一样的。
.h文件和常量处理
在SDK的include目录有4个.h文件,定义了一些常量,比如上面一节中的0其实是msp_errors.h中MSP_SUCCESS。
我以msp_errors.h为例,建立一个接口Msp_errors,继承StdCallLibrary。
照着msp_errors.h中的定义在Msp_errors中进行定义。
public static final int MSP_SUCCESS = 0; public static final int ERROR_FAIL = -1; public static final int ERROR_EXCEPTION= -2; public static final int ERROR_GENERAL= 10100; public static final int ERROR_OUT_OF_MEMORY= 10101; public static final int ERROR_FILE_NOT_FOUND= 10102; public static final int ERROR_NOT_SUPPORT= 10103;
使用很简单的,比如MSP_SUCCESS 就是Msp_errors.MSP_SUCCESS。
完整代码和文件
这个只是语音识别部分的,语音合成的话我记得有人做过jni接口的。
*QTSR.java
package com.cnblogs.htynkn; import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Pointer; import com.sun.jna.ptr.IntByReference; public interface QTSR extends Library { QTSR INSTANCE = (QTSR) Native.loadLibrary("msc", QTSR.class); public int QISRInit(String configs); public String QISRSessionBegin(String grammarList, String params, IntByReference errorCode); public int QISRGrammarActivate(String sessionID, String grammar, String type, int weight); public int QISRAudioWrite(String sessionID, Pointer waveData, int waveLen, int audioStatus, IntByReference epStatus, IntByReference recogStatus); public String QISRGetResult(String sessionID, IntByReference rsltStatus, int waitTime, IntByReference errorCode); public int QISRSessionEnd(String sessionID, String hints); public int QISRGetParam(String sessionID, String paramName, String paramValue, IntByReference valueLen); public int QISRFini(); } *Msp_errors ? package com.cnblogs.htynkn; import com.sun.jna.win32.StdCallLibrary; public interface Msp_errors extends StdCallLibrary { public static final int MSP_SUCCESS = 0; public static final int ERROR_FAIL = -1; public static final int ERROR_EXCEPTION= -2; public static final int ERROR_GENERAL= 10100; public static final int ERROR_OUT_OF_MEMORY= 10101; public static final int ERROR_FILE_NOT_FOUND= 10102; public static final int ERROR_NOT_SUPPORT= 10103; public static final int ERROR_NOT_IMPLEMENT= 10104; public static final int ERROR_ACCESS= 10105; public static final int ERROR_INVALID_PARA= 10106; public static final int ERROR_INVALID_PARA_VALUE= 10107; public static final int ERROR_INVALID_HANDLE= 10108; public static final int ERROR_INVALID_DATA= 10109; public static final int ERROR_NO_LICENSE= 10110; public static final int ERROR_NOT_INIT= 10111; public static final int ERROR_NULL_HANDLE= 10112; public static final int ERROR_OVERFLOW= 10113; public static final int ERROR_TIME_OUT= 10114; public static final int ERROR_OPEN_FILE= 10115; public static final int ERROR_NOT_FOUND= 10116; public static final int ERROR_NO_ENOUGH_BUFFER= 10117; public static final int ERROR_NO_DATA= 10118; public static final int ERROR_NO_MORE_DATA= 10119; public static final int ERROR_NO_RESPONSE_DATA= 10120; public static final int ERROR_ALREADY_EXIST= 10121; public static final int ERROR_LOAD_MODULE= 10122; public static final int ERROR_BUSY = 10123; public static final int ERROR_INVALID_CONFIG= 10124; public static final int ERROR_VERSION_CHECK= 10125; public static final int ERROR_CANCELED= 10126; public static final int ERROR_INVALID_MEDIA_TYPE= 10127; public static final int ERROR_CONFIG_INITIALIZE= 10128; public static final int ERROR_CREATE_HANDLE= 10129; public static final int ERROR_CODING_LIB_NOT_LOAD= 10130; public static final int ERROR_NET_GENERAL= 10200; public static final int ERROR_NET_OPENSOCK= 10201; public static final int ERROR_NET_CONNECTSOCK= 10202; public static final int ERROR_NET_ACCEPTSOCK = 10203; public static final int ERROR_NET_SENDSOCK= 10204; public static final int ERROR_NET_RECVSOCK= 10205; public static final int ERROR_NET_INVALIDSOCK= 10206; public static final int ERROR_NET_BADADDRESS = 10207; public static final int ERROR_NET_BINDSEQUENCE= 10208; public static final int ERROR_NET_NOTOPENSOCK= 10209; public static final int ERROR_NET_NOTBIND= 10210; public static final int ERROR_NET_NOTLISTEN = 10211; public static final int ERROR_NET_CONNECTCLOSE= 10212; public static final int ERROR_NET_NOTDGRAMSOCK= 10213; public static final int ERROR_NET_DNS= 10214; public static final int ERROR_MSG_GENERAL= 10300; public static final int ERROR_MSG_PARSE_ERROR= 10301; public static final int ERROR_MSG_BUILD_ERROR= 10302; public static final int ERROR_MSG_PARAM_ERROR= 10303; public static final int ERROR_MSG_CONTENT_EMPTY= 10304; public static final int ERROR_MSG_INVALID_CONTENT_TYPE = 10305; public static final int ERROR_MSG_INVALID_CONTENT_LENGTH = 10306; public static final int ERROR_MSG_INVALID_CONTENT_ENCODE = 10307; public static final int ERROR_MSG_INVALID_KEY= 10308; public static final int ERROR_MSG_KEY_EMPTY= 10309; public static final int ERROR_MSG_SESSION_ID_EMPTY= 10310; public static final int ERROR_MSG_LOGIN_ID_EMPTY= 10311; public static final int ERROR_MSG_SYNC_ID_EMPTY= 10312; public static final int ERROR_MSG_APP_ID_EMPTY= 10313; public static final int ERROR_MSG_EXTERN_ID_EMPTY= 10314; public static final int ERROR_MSG_INVALID_CMD= 10315; public static final int ERROR_MSG_INVALID_SUBJECT= 10316; public static final int ERROR_MSG_INVALID_VERSION= 10317; public static final int ERROR_MSG_NO_CMD= 10318; public static final int ERROR_MSG_NO_SUBJECT= 10319; public static final int ERROR_MSG_NO_VERSION= 10320; public static final int ERROR_MSG_MSSP_EMPTY= 10321; public static final int ERROR_MSG_NEW_RESPONSE= 10322; public static final int ERROR_MSG_NEW_CONTENT= 10323; public static final int ERROR_MSG_INVALID_SESSION_ID = 10324; public static final int ERROR_DB_GENERAL= 10400; public static final int ERROR_DB_EXCEPTION= 10401; public static final int ERROR_DB_NO_RESULT= 10402; public static final int ERROR_DB_INVALID_USER= 10403; public static final int ERROR_DB_INVALID_PWD= 10404; public static final int ERROR_DB_CONNECT= 10405; public static final int ERROR_DB_INVALID_SQL= 10406; public static final int ERROR_DB_INVALID_APPID= 10407; public static final int ERROR_RES_GENERAL= 10500; public static final int ERROR_RES_LOAD = 10501; public static final int ERROR_RES_FREE = 10502; public static final int ERROR_RES_MISSING = 10503; public static final int ERROR_RES_INVALID_NAME = 10504; public static final int ERROR_RES_INVALID_ID = 10505; public static final int ERROR_RES_INVALID_IMG = 10506; public static final int ERROR_RES_WRITE= 10507; public static final int ERROR_RES_LEAK = 10508; public static final int ERROR_RES_HEAD = 10509; public static final int ERROR_RES_DATA = 10510; public static final int ERROR_RES_SKIP = 10511; public static final int ERROR_TTS_GENERAL= 10600; public static final int ERROR_TTS_TEXTEND = 10601; public static final int ERROR_TTS_TEXT_EMPTY= 10602; public static final int ERROR_REC_GENERAL= 10700; public static final int ERROR_REC_INACTIVE= 10701; public static final int ERROR_REC_GRAMMAR_ERROR= 10702; public static final int ERROR_REC_NO_ACTIVE_GRAMMARS = 10703; public static final int ERROR_REC_DUPLICATE_GRAMMAR= 10704; public static final int ERROR_REC_INVALID_MEDIA_TYPE = 10705; public static final int ERROR_REC_INVALID_LANGUAGE= 10706; public static final int ERROR_REC_URI_NOT_FOUND= 10707; public static final int ERROR_REC_URI_TIMEOUT= 10708; public static final int ERROR_REC_URI_FETCH_ERROR= 10709; public static final int ERROR_EP_GENERAL= 10800; public static final int ERROR_EP_NO_SESSION_NAME= 10801; public static final int ERROR_EP_INACTIVE = 10802; public static final int ERROR_EP_INITIALIZED = 10803; public static final int ERROR_TUV_GENERAL= 10900; public static final int ERROR_TUV_GETHIDPARAM = 10901; public static final int ERROR_TUV_TOKEN= 10902; public static final int ERROR_TUV_CFGFILE= 10903; public static final int ERROR_TUV_RECV_CONTENT = 10904; public static final int ERROR_TUV_VERFAIL = 10905; public static final int ERROR_LOGIN_SUCCESS= 11000; public static final int ERROR_LOGIN_NO_LICENSE = 11001; public static final int ERROR_LOGIN_SESSIONID_INVALID = 11002; public static final int ERROR_LOGIN_SESSIONID_ERROR= 11003; public static final int ERROR_LOGIN_UNLOGIN = 11004; public static final int ERROR_LOGIN_INVALID_USER = 11005; public static final int ERROR_LOGIN_INVALID_PWD = 11006; public static final int ERROR_LOGIN_SYSTEM_ERROR= 11099; public static final int ERROR_HCR_GENERAL= 11100; public static final int ERROR_HCR_RESOURCE_NOT_EXIST = 11101; public static final int ERROR_HCR_CREATE= 11102; public static final int ERROR_HCR_DESTROY= 11103; public static final int ERROR_HCR_START= 11104; public static final int ERROR_HCR_APPEND_STROKES= 11105; public static final int ERROR_HCR_GET_RESULT= 11106; public static final int ERROR_HCR_SET_PREDICT_DATA= 11107; public static final int ERROR_HCR_GET_PREDICT_RESULT = 11108; public static final int ERROR_HTTP_BASE= 12000; public static final int ERROR_ISV_NO_USER = 13000; public static final int ERROR_LUA_BASE= 14000; public static final int ERROR_LUA_YIELD= 14001; public static final int ERROR_LUA_ERRRUN= 14002; public static final int ERROR_LUA_ERRSYNTAX= 14003; public static final int ERROR_LUA_ERRMEM= 14004; public static final int ERROR_LUA_ERRERR= 14005; }
看完上述内容,你们掌握Java中怎么通过调用jna实现语音识别功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程网行业资讯频道,感谢各位的阅读!