网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

语音验证java代码 java语音识别代码

在java怎么调用julius做语音识别啊?求接口代码

package com点吸烟 blogs.htynkn;

创新互联长期为千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为邗江企业提供专业的网站建设、成都网站建设,邗江网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

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);

/**

* 初始化MSC的ISR部分

*

* @param configs

* 初始化时传入的字符串,以指定合成用到的一些配置参数,各个参数以“参数名=参数值”的形式出现,大小写不敏感,不同的参数之间以“

* ,”或“\n”隔开,不设置任何值时可以传入NULL或空串:

* @return 如果函数调用成功返回MSP_SUCCESS,否则返回错误代码,错误代码参见msp_errors

*/

public int QISRInit(String configs);

/**

* 开始一个ISR会话

*

* @param grammarList

* uri-list格式的语法,可以是一个语法文件的URL或者一个引擎内置语法列表。可以同时指定多个语法,不同的语法之间以“,”

* 隔开。进行语音听写时不需要语法,此参数设定为NULL或空串即可;进行语音识别时则需要语法,语法可以在此参数中指定,

* 也可以随后调用QISRGrammarActivate指定识别所用的语法。

* @param params

* 本路ISR会话使用的参数,可设置的参数及其取值范围请参考《可设置参数列表_MSP20.xls》,各个参数以“参数名=参数值”

* 的形式出现,不同的参数之间以“,”或者“\n”隔开。

* @param errorCode

* 如果函数调用成功则其值为MSP_SUCCESS,否则返回错误代码,错误代码参见msp_errors。几个主要的返回值:

* MSP_ERROR_NOT_INIT 未初始化 MSP_ERROR_INVALID_PARA 无效的参数

* MSP_ERROR_NO_LICENSE 开始一路会话失败

* @return MSC为本路会话建立的ID,用来唯一的标识本路会话,供以后调用其他函数时使用。函数调用失败则会返回NULL。

*/

public String QISRSessionBegin(String grammarList, String params,

IntByReference errorCode);

/**

* 传入语法

*

* @param sessionID

* 由QISRSessionBegin返回过来的会话ID。

* @param grammar

* 语法字符串

* @param type

* 语法类型,可以是uri-list、abnf、xml等

* @param weight

* 本次传入语法的权重,本参数在MSP 2.0中会被忽略。

* @return 如果函数调用成功返回MSP_SUCCESS,否则返回错误代码,错误代码参见msp_errors

*/

public int QISRGrammarActivate(String sessionID, String grammar,

String type, int weight);

/**

* 写入用来识别的语音

*

* @param sessionID

* 由QISRSessionBegin返回过来的会话ID。

* @param waveData

* 音频数据缓冲区起始地址

* @param waveLen

* 音频数据长度,其大小不能超过设定的max_audio_size

* @param audioStatus

* 用来指明用户本次识别的音频是否发送完毕,可能值如下:

* MSP_AUDIO_SAMPLE_FIRST = 1 第一块音频

* MSP_AUDIO_SAMPLE_CONTINUE = 2 还有后继音频

* MSP_AUDIO_SAMPLE_LAST = 4 最后一块音频

* @param epStatus

* 端点检测(End-point detected)器所处的状态,可能的值如下:

* MSP _EP_LOOKING_FOR_SPEECH = 0 还没有检测到音频的前端点。

* MSP _EP_IN_SPEECH = 1 已经检测到了音频前端点,正在进行正常的音频处理。

* MSP _EP_AFTER_SPEECH = 3 检测到音频的后端点,后继的音频会被MSC忽略。

* MSP _EP_TIMEOUT = 4 超时。

* MSP _EP_ERROR= 5 出现错误。

* MSP _EP_MAX_SPEECH = 6 音频过大。

* @param recogStatus

* 识别器所处的状态

* @return

*/

public int QISRAudioWrite(String sessionID, Pointer waveData, int waveLen,

int audioStatus, IntByReference epStatus, IntByReference recogStatus);

/**

* 获取识别结果

*

* @param sessionID 由QISRSessionBegin返回过来的会话ID。

* @param rsltStatus 识别结果的状态,其取值范围和含义请参考QISRAudioWrite的参数recogStatus

* @param waitTime 与服务器交互的间隔时间,可以控制和服务器的交互频度。单位为ms,建议取值为5000。

* @param errorCode 如果函数调用成功返回MSP_SUCCESS,否则返回错误代码,错误代码参见msp_errors

* @return 函数执行成功并且获取到识别结果时返回识别结果,函数执行成功没有获取到识别结果时返回NULL

*/

public String QISRGetResult(String sessionID, IntByReference rsltStatus,

int waitTime, IntByReference errorCode);

/**

* 结束一路会话

*

* @param sessionID 由QISRSessionBegin返回过来的会话ID。

* @param hints 结束本次会话的原因描述,用于记录日志,便于用户查阅或者跟踪某些问题。

* @return

*/

public int QISRSessionEnd(String sessionID, String hints);

/**

* 获取与识别交互相关的参数

*

* @param sessionID 由QISRSessionBegin返回过来的会话ID。

* @param paramName 要获取的参数名称;支持同时查询多个参数,查询多个参数时,参数名称按“,” 或“\n”分隔开来。

* @param paramValue 获取的参数值,以字符串形式返回;查询多个参数时,参数值之间以“;”分开,不支持的参数将返回空的值。

* @param valueLen 参数值的长度。

* @return

*/

public int QISRGetParam(String sessionID, String paramName,

String paramValue, IntByReference valueLen);

/**

* 逆初始化MSC的ISR部分

*

* @return

*/

public int QISRFini();

}

*Msp_errors

?

package com点吸烟 blogs.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; /* 0x2774 */

public static final int ERROR_OUT_OF_MEMORY= 10101; /* 0x2775 */

public static final int ERROR_FILE_NOT_FOUND= 10102; /* 0x2776 */

public static final int ERROR_NOT_SUPPORT= 10103; /* 0x2777 */

public static final int ERROR_NOT_IMPLEMENT= 10104; /* 0x2778 */

public static final int ERROR_ACCESS= 10105; /* 0x2779 */

public static final int ERROR_INVALID_PARA= 10106; /* 0x277A */

public static final int ERROR_INVALID_PARA_VALUE= 10107; /* 0x277B */

public static final int ERROR_INVALID_HANDLE= 10108; /* 0x277C */

public static final int ERROR_INVALID_DATA= 10109; /* 0x277D */

public static final int ERROR_NO_LICENSE= 10110; /* 0x277E */

public static final int ERROR_NOT_INIT= 10111; /* 0x277F */

public static final int ERROR_NULL_HANDLE= 10112; /* 0x2780 */

public static final int ERROR_OVERFLOW= 10113; /* 0x2781 */

public static final int ERROR_TIME_OUT= 10114; /* 0x2782 */

public static final int ERROR_OPEN_FILE= 10115; /* 0x2783 */

public static final int ERROR_NOT_FOUND= 10116; /* 0x2784 */

public static final int ERROR_NO_ENOUGH_BUFFER= 10117; /* 0x2785 */

public static final int ERROR_NO_DATA= 10118; /* 0x2786 */

public static final int ERROR_NO_MORE_DATA= 10119; /* 0x2787 */

public static final int ERROR_NO_RESPONSE_DATA= 10120; /* 0x2788 */

public static final int ERROR_ALREADY_EXIST= 10121; /* 0x2789 */

public static final int ERROR_LOAD_MODULE= 10122; /* 0x278A */

public static final int ERROR_BUSY = 10123; /* 0x278B */

public static final int ERROR_INVALID_CONFIG= 10124; /* 0x278C */

public static final int ERROR_VERSION_CHECK= 10125; /* 0x278D */

public static final int ERROR_CANCELED= 10126; /* 0x278E */

public static final int ERROR_INVALID_MEDIA_TYPE= 10127; /* 0x278F */

public static final int ERROR_CONFIG_INITIALIZE= 10128; /* 0x2790 */

public static final int ERROR_CREATE_HANDLE= 10129; /* 0x2791 */

public static final int ERROR_CODING_LIB_NOT_LOAD= 10130; /* 0x2792 */

/* Error codes of network 10200(0x27D8)*/

public static final int ERROR_NET_GENERAL= 10200; /* 0x27D8 */

public static final int ERROR_NET_OPENSOCK= 10201; /* 0x27D9 */ /* Open socket */

public static final int ERROR_NET_CONNECTSOCK= 10202; /* 0x27DA */ /* Connect socket */

public static final int ERROR_NET_ACCEPTSOCK = 10203; /* 0x27DB */ /* Accept socket */

public static final int ERROR_NET_SENDSOCK= 10204; /* 0x27DC */ /* Send socket data */

public static final int ERROR_NET_RECVSOCK= 10205; /* 0x27DD */ /* Recv socket data */

public static final int ERROR_NET_INVALIDSOCK= 10206; /* 0x27DE */ /* Invalid socket handle */

public static final int ERROR_NET_BADADDRESS = 10207; /* 0x27EF */ /* Bad network address */

public static final int ERROR_NET_BINDSEQUENCE= 10208; /* 0x27E0 */ /* Bind after listen/connect */

public static final int ERROR_NET_NOTOPENSOCK= 10209; /* 0x27E1 */ /* Socket is not opened */

public static final int ERROR_NET_NOTBIND= 10210; /* 0x27E2 */ /* Socket is not bind to an address */

public static final int ERROR_NET_NOTLISTEN = 10211; /* 0x27E3 */ /* Socket is not listening */

public static final int ERROR_NET_CONNECTCLOSE= 10212; /* 0x27E4 */ /* The other side of connection is closed */

public static final int ERROR_NET_NOTDGRAMSOCK= 10213; /* 0x27E5 */ /* The socket is not datagram type */

public static final int ERROR_NET_DNS= 10214; /* 0x27E6 */ /* domain name is invalid or dns server does not function well */

/* Error codes of mssp message 10300(0x283C) */

public static final int ERROR_MSG_GENERAL= 10300; /* 0x283C */

public static final int ERROR_MSG_PARSE_ERROR= 10301; /* 0x283D */

public static final int ERROR_MSG_BUILD_ERROR= 10302; /* 0x283E */

public static final int ERROR_MSG_PARAM_ERROR= 10303; /* 0x283F */

public static final int ERROR_MSG_CONTENT_EMPTY= 10304; /* 0x2840 */

public static final int ERROR_MSG_INVALID_CONTENT_TYPE = 10305; /* 0x2841 */

public static final int ERROR_MSG_INVALID_CONTENT_LENGTH = 10306; /* 0x2842 */

public static final int ERROR_MSG_INVALID_CONTENT_ENCODE = 10307; /* 0x2843 */

public static final int ERROR_MSG_INVALID_KEY= 10308; /* 0x2844 */

public static final int ERROR_MSG_KEY_EMPTY= 10309; /* 0x2845 */

public static final int ERROR_MSG_SESSION_ID_EMPTY= 10310; /* 0x2846 */

public static final int ERROR_MSG_LOGIN_ID_EMPTY= 10311; /* 0x2847 */

public static final int ERROR_MSG_SYNC_ID_EMPTY= 10312; /* 0x2848 */

public static final int ERROR_MSG_APP_ID_EMPTY= 10313; /* 0x2849 */

public static final int ERROR_MSG_EXTERN_ID_EMPTY= 10314; /* 0x284A */

public static final int ERROR_MSG_INVALID_CMD= 10315; /* 0x284B */

public static final int ERROR_MSG_INVALID_SUBJECT= 10316; /* 0x284C */

public static final int ERROR_MSG_INVALID_VERSION= 10317; /* 0x284D */

public static final int ERROR_MSG_NO_CMD= 10318; /* 0x284E */

public static final int ERROR_MSG_NO_SUBJECT= 10319; /* 0x284F */

public static final int ERROR_MSG_NO_VERSION= 10320; /* 0x2850 */

public static final int ERROR_MSG_MSSP_EMPTY= 10321; /* 0x2851 */

public static final int ERROR_MSG_NEW_RESPONSE= 10322; /* 0x2852 */

public static final int ERROR_MSG_NEW_CONTENT= 10323; /* 0x2853 */

public static final int ERROR_MSG_INVALID_SESSION_ID = 10324; /* 0x2854 */

/* Error codes of DataBase 10400(0x28A0)*/

public static final int ERROR_DB_GENERAL= 10400; /* 0x28A0 */

public static final int ERROR_DB_EXCEPTION= 10401; /* 0x28A1 */

public static final int ERROR_DB_NO_RESULT= 10402; /* 0x28A2 */

public static final int ERROR_DB_INVALID_USER= 10403; /* 0x28A3 */

public static final int ERROR_DB_INVALID_PWD= 10404; /* 0x28A4 */

public static final int ERROR_DB_CONNECT= 10405; /* 0x28A5 */

public static final int ERROR_DB_INVALID_SQL= 10406; /* 0x28A6 */

public static final int ERROR_DB_INVALID_APPID= 10407; /* 0x28A7 */

/* Error codes of Resource 10500(0x2904)*/

public static final int ERROR_RES_GENERAL= 10500; /* 0x2904 */

public static final int ERROR_RES_LOAD = 10501; /* 0x2905 */ /* Load resource */

public static final int ERROR_RES_FREE = 10502; /* 0x2906 */ /* Free resource */

public static final int ERROR_RES_MISSING = 10503; /* 0x2907 */ /* Resource File Missing */

public static final int ERROR_RES_INVALID_NAME = 10504; /* 0x2908 */ /* Invalid resource file name */

public static final int ERROR_RES_INVALID_ID = 10505; /* 0x2909 */ /* Invalid resource ID */

public static final int ERROR_RES_INVALID_IMG = 10506; /* 0x290A */ /* Invalid resource image pointer */

public static final int ERROR_RES_WRITE= 10507; /* 0x290B */ /* Write read-only resource */

public static final int ERROR_RES_LEAK = 10508; /* 0x290C */ /* Resource leak out */

public static final int ERROR_RES_HEAD = 10509; /* 0x290D */ /* Resource head currupt */

public static final int ERROR_RES_DATA = 10510; /* 0x290E */ /* Resource data currupt */

public static final int ERROR_RES_SKIP = 10511; /* 0x290F */ /* Resource file skipped */

/* Error codes of TTS 10600(0x2968)*/

public static final int ERROR_TTS_GENERAL= 10600; /* 0x2968 */

public static final int ERROR_TTS_TEXTEND = 10601; /* 0x2969 */ /* Meet text end */

public static final int ERROR_TTS_TEXT_EMPTY= 10602; /* 0x296A */ /* no synth text */

/* Error codes of Recognizer 10700(0x29CC) */

public static final int ERROR_REC_GENERAL= 10700; /* 0x29CC */

public static final int ERROR_REC_INACTIVE= 10701; /* 0x29CD */

public static final int ERROR_REC_GRAMMAR_ERROR= 10702; /* 0x29CE */

public static final int ERROR_REC_NO_ACTIVE_GRAMMARS = 10703; /* 0x29CF */

public static final int ERROR_REC_DUPLICATE_GRAMMAR= 10704; /* 0x29D0 */

public static final int ERROR_REC_INVALID_MEDIA_TYPE = 10705; /* 0x29D1 */

public static final int ERROR_REC_INVALID_LANGUAGE= 10706; /* 0x29D2 */

public static final int ERROR_REC_URI_NOT_FOUND= 10707; /* 0x29D3 */

public static final int ERROR_REC_URI_TIMEOUT= 10708; /* 0x29D4 */

public static final int ERROR_REC_URI_FETCH_ERROR= 10709; /* 0x29D5 */

/* Error codes of Speech Detector 10800(0x2A30) */

public static final int ERROR_EP_GENERAL= 10800; /* 0x2A30 */

public static final int ERROR_EP_NO_SESSION_NAME= 10801; /* 0x2A31 */

public static final int ERROR_EP_INACTIVE = 10802; /* 0x2A32 */

public static final int ERROR_EP_INITIALIZED = 10803; /* 0x2A33 */

/* Error codes of TUV */

public static final int ERROR_TUV_GENERAL= 10900; /* 0x2A94 */

public static final int ERROR_TUV_GETHIDPARAM = 10901; /* 0x2A95 */ /* Get Busin Param huanid*/

public static final int ERROR_TUV_TOKEN= 10902; /* 0x2A96 */ /* Get Token */

public static final int ERROR_TUV_CFGFILE= 10903; /* 0x2A97 */ /* Open cfg file */

public static final int ERROR_TUV_RECV_CONTENT = 10904; /* 0x2A98 */ /* received content is error */

public static final int ERROR_TUV_VERFAIL = 10905; /* 0x2A99 */ /* Verify failure */

/* Error codes of IMTV */

public static final int ERROR_LOGIN_SUCCESS= 11000; /* 0x2AF8 */ /* 成功 */

public static final int ERROR_LOGIN_NO_LICENSE = 11001; /* 0x2AF9 */ /* 试用次数结束,用户需要付费 */

public static final int ERROR_LOGIN_SESSIONID_INVALID = 11002; /* 0x2AFA */ /* SessionId失效,需要重新登录通行证 */

public static final int ERROR_LOGIN_SESSIONID_ERROR= 11003; /* 0x2AFB */ /* SessionId为空,或者非法 */

public static final int ERROR_LOGIN_UNLOGIN = 11004; /* 0x2AFC */ /* 未登录通行证 */

public static final int ERROR_LOGIN_INVALID_USER = 11005; /* 0x2AFD */ /* 用户ID无效 */

public static final int ERROR_LOGIN_INVALID_PWD = 11006; /* 0x2AFE */ /* 用户密码无效 */

public static final int ERROR_LOGIN_SYSTEM_ERROR= 11099; /* 0x2B5B */ /* 系统错误 */

/* Error codes of HCR */

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;

/* Error codes of http 12000(0x2EE0) */

public static final int ERROR_HTTP_BASE= 12000; /* 0x2EE0 */

/*Error codes of ISV */

public static final int ERROR_ISV_NO_USER = 13000; /* 32C8 */ /* the user doesn't exist */

/* Error codes of Lua scripts */

public static final int ERROR_LUA_BASE= 14000; /* 0x36B0 */

public static final int ERROR_LUA_YIELD= 14001; /* 0x36B1 */

public static final int ERROR_LUA_ERRRUN= 14002; /* 0x36B2 */

public static final int ERROR_LUA_ERRSYNTAX= 14003; /* 0x36B3 */

public static final int ERROR_LUA_ERRMEM= 14004; /* 0x36B4 */

public static final int ERROR_LUA_ERRERR= 14005; /* 0x36B5 */

在java程序中加入语音功能,例如给段字符串“A101”,就能发出声音,请大家帮帮忙,谢谢

为应用程序加上语音能力有什么好处呢?粗略地讲,是为了趣味,它适合所有注重趣味的应用,比如游戏。当然,从更严肃的角度来讲,它还涉及到应用的可用性问题。注意,这里我考虑的不仅是可视化界面固有的不足,而且还有这样一些情形:一些时候,让双眼离开当前的工作很不方便,甚至是不合法的。比如,假设有一个带语音功能的浏览器,你就可以在外出散步或开车上班的同时,用听的方式浏览自己喜爱的网站。从目前来看,邮件阅读器或许是语音技术更实际的应用,在JavaMail API的帮助下,这一切已经可能。邮件阅读器可以定期地检查收件箱,然后用语音“You have new mail, would you like me to read it to you?”引起你的注意。按照类似的思路,我们还可以考虑一个带语音功能的提醒器,把它连接到一个日历应用:它会及时地提醒你“Don't forget your meeting with the boss in 10 minutes!”。 也许你已经被这些主意吸引,或者有了自己更好的主意,现在让我们继续。首先我将介绍如何启用本文提供的语音引擎,这样,如果你认为语音引擎的实现细节过于复杂,就可以直接使用它而忽略其实现细节。

一、试用语音引擎 要使用这个语音引擎,你必须在CLASSPATH中加入本文提供的javatalk.jar文件,然后从命令行运行(或者从Java程序调用)com.lotontech.speech.Talker类。如果从命令行运行,则命令为: java com.lotontech.speech.Talker "h|e|l|oo" 如果从Java程序调用,则代码为: com.lotontech.speech.Talker talker=new com.lotontech.speech.Talker(); talker.sayPhoneWord("h|e|l|oo"); 现在,对于在命令行上(或者调用sayPhoneWord()方法时)提供的“h|e|l|oo”字符串,你或许有所不解。下面我就来解释一下。 语音引擎的工作原理是把细小的声音样本连接起来,每一个样本都是人的语言发音(英语)的一个最小单位。这些声音样本称为音素(allophone)。每一个因素对应一个、二个或者三个字母。从前面“hello”的语音表示可以看出,一些字母组合的发音显而易见,还有一些却不是很明显: h -- 读音显而易见 e -- 读音显而易见 l -- 读音显而易见,但注意两个“l”被简缩成了一个“l”。 OO -- 应该读作“hello”中的读音,不应读作“bot”、“too”中的读音。 下面是一个有效音素的清单: a : 如cat b : 如cab c : 如cat d : 如dot e : 如bet f : 如frog g : 如frog h : 如hog i : 如pig j : 如jig k : 如keg l : 如leg m : 如met n : 如begin o : 如not p : 如pot r : 如rot s : 如sat t : 如sat u : 如put v : 如have w : 如wet y : 如yet z : 如zoo aa : 如fake ay : 如hay ee : 如bee ii : 如high oo : 如go bb : b的变化形式,重音不同 dd : d的变化形式,重音不同 ggg : g的变化形式,重音不同 hh : h的变化形式,重音不同 ll : l的变化形式,重音不同 nn : n的变化形式,重音不同 rr : r的变化形式,重音不同 tt : t的变化形式,重音不同 yy : y的变化形式,重音不同 ar : 如car aer : 如care ch : 如which ck : 如check ear : 如beer er : 如later err : 如later (长音) ng : 如feeding or : 如law ou : 如zoo ouu : 如zoo (长音) ow : 如cow oy : 如boy sh : 如shut th : 如thing dth : 如this uh : u 的变化形式 wh : 如where zh : 如Asian 人说话的时候,语音在整个句子之内起落变化。语调变化使得语音更自然、更富有感染力,使得问句和陈述句能够相互区别。请考虑下面两个句子: It is fake -- f|aa|k Is it fake? -- f|AA|k 也许你已经猜想到,提高语调的方法是使用大写字母。 以上就是使用该软件时你需要了解的东西。如果你对其后台实现细节感兴趣,请继续阅读。

二、实现语音引擎 语音引擎的实现只包括一个类,四个方法。它利用了J2SE 1.3包含的Java Sound API。在这里,我不准备全面地介绍这个API,但你可以通过实例学习它的用法。Java Sound API并不是一个特别复杂的API,代码中的注释将告诉你必须了解的知识。 下面是Talker类的基本定义: package com.lotontech.speech; import javax.sound.sampled.*; import java.io.*; import java.util.*; import java点虐 .*; public class Talker { private SourceDataLine line=null; } 如果从命令行执行Talker,下面的main()方法将作为入口点运行。main()方法获取第一个命令行参数,然后把它传递给sayPhoneWord()方法: /* * 读出在命令行中指定的表示读音的字符串 */ public static void main(String args[]) { Talker player=new Talker(); if (args.length0) player.sayPhoneWord(args[0]); System.exit(0); }

sayPhoneWord()方法既可以通过上面的main()方法调用,也可以在Java程序中直接调用。从表面上看,sayPhoneWord()方法比较复杂,其实并非如此。实际上,它简单地遍历所有单词的语音元素(在输入字符串中语音元素以“|”分隔),通过一个声音输出通道一个元素一个元素地播放出来。为了让声音更自然一些,我把每一个声音样本的结尾和下一个声音样本的开头合并了起来: /* * 读出指定的语音字符串 */ public void sayPhoneWord(String word) { // 为上一个声音构造的模拟byte数组 byte[] previousSound=null; // 把输入字符串分割成单独的音素 StringTokenizer st=new StringTokenizer(word,"|",false); while (st.hasMoreTokens()) { // 为音素构造相应的文件名字 String thisPhoneFile=st.nextToken(); thisPhoneFile="/allophones/"+thisPhoneFile+".au"; // 从声音文件读取数据 byte[] thisSound=getSound(thisPhoneFile); if (previousSound!=null) { // 如果可能的话,把前一个音素和当前音素合并 int mergeCount=0; if (previousSound.length=500 thisSound.length=500) mergeCount=500; for (int i=0; i { previousSound[previousSound.length-mergeCount+i] =(byte)((previousSound[previousSound.length -mergeCount+i]+thisSound[i])/2); } // 播放前一个音素 playSound(previousSound); // 把经过截短的当前音素作为前一个音素 byte[] newSound=new byte[thisSound.length-mergeCount]; for (int ii=0; ii newSound[ii]=thisSound[ii+mergeCount]; previousSound=newSound; } else previousSound=thisSound; } // 播放最后一个音素,清理声音通道 playSound(previousSound); drain(); } 在sayPhoneWord()的后面,你可以看到它调用playSound()输出单个声音样本(即一个音素),然后调用drain()清理声音通道。下面是playSound()的代码: /* * 该方法播放一个声音样本 */ private void playSound(byte[] data) { if (data.length0) line.write(data, 0, data.length); } 下面是drain()的代码: /* * 该方法清理声音通道 */ private void drain() { if (line!=null) line.drain(); try {Thread.sleep(100);} catch (Exception e) {} }

现在回过头来看sayPhoneWord(),这里还有一个方法我们没有分析,即getSound()方法。 getSound()方法从一个au文件以字节数据的形式读入预先录制的声音样本。要了解读取数据、转换音频格式、初始化声音输出行(SouceDataLine)以及构造字节数据的详细过程,请参考下面代码中的注释: /* * 该方法从文件读取一个音素, * 并把它转换成byte数组 */ private byte[] getSound(String fileName) { try { URL url=Talker.class.getResource(fileName); AudioInputStream stream = AudioSystem.getAudioInputStream(url); AudioFormat format = stream.getFormat(); // 把一个ALAW/ULAW声音转换成PCM以便回放 if ((format.getEncoding() == AudioFormat.Encoding.ULAW) || (format.getEncoding() == AudioFormat.Encoding.ALAW)) { AudioFormat tmpFormat = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), format.getSampleSizeInBits() * 2, format.getChannels(), format.getFrameSize() * 2, format.getFrameRate(), true); stream = AudioSystem.getAudioInputStream(tmpFormat, stream); format = tmpFormat; } DataLine.Info info = new DataLine.Info( Clip.class, format, ((int) stream.getFrameLength() * format.getFrameSize())); if (line==null) { // 输出线还没有实例化 // 是否能够找到合适的输出线类型? DataLine.Info outInfo = new DataLine.Info(SourceDataLine.class, format); if (!AudioSystem.isLineSupported(outInfo)) { System.out.println("不支持匹配" + outInfo + "的输出线"); throw new Exception("不支持匹配" + outInfo + "的输出线"); } // 打开输出线 line = (SourceDataLine) AudioSystem.getLine(outInfo); line.open(format, 50000); line.start(); } int frameSizeInBytes = format.getFrameSize(); int bufferLengthInFrames = line.getBufferSize() / 8; int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes; byte[] data=new byte[bufferLengthInBytes]; // 读取字节数据,并计数 int numBytesRead = 0; if ((numBytesRead = stream.read(data)) != -1) { int numBytesRemaining = numBytesRead; } // 把字节数据切割成合适的大小 byte[] newData=new byte[numBytesRead]; for (int i=0; i newData[i]=data[i]; return newData; } catch (Exception e) { return new byte[0]; } } 这就是全部的代码,包括注释在内,一个大约150行代码的语音合成器。

三、文本-语音转换 以语音元素的格式指定待朗读的单词似乎过于复杂,如果要构造一个能够朗读文本(比如Web页面或Email)的应用,我们希望能够直接指定原始的文本。 深入分析这个问题之后,我在本文后面的ZIP文件中提供了一个试验性的文本-语音转换类。运行这个类,它将显示出分析结果。文本-语音转换类可以从命令行执行,如下所示: java com.lotontech.speech.Converter "hello there" 输出结果类如: hello - h|e|l|oo there - dth|aer 如果运行下面这个命令: java com.lotontech.speech.Converter "I like to read JavaWorld" 则输出结果为: i - ii like - l|ii|k to - t|ouu read - r|ee|a|d java - j|a|v|a world - w|err|l|d 这个转换类是如何工作的呢?实际上,我的方法相当简单,转换过程就是以一定的次序应用一组文本替换规则。例如对于单词“ant”、“want”、“wanted”、“unwanted”和“unique”,则我们想要应用的替换规则可能依次为: 用“|y|ou|n|ee|k|”替换“*unique*” 用“|w|o|n|t|”替换“*want*” 用“|a|”替换“*a*” 用“|e|”替换“*e*” 用“|d|”替换“*d*” 用“|n|”替换“*n*” 用“|u|”替换“*u*” 用“|t|”替换“*t*” 对于“unwanted”,输出序列为: unwanted un[|w|o|n|t|]ed (规则2) [|u|][|n|][|w|o|n|t|][|e|][|d|] (规则4、5、6、7) u|n|w|o|n|t|e|d (删除多余的符之后) 你将看到包含字母“wont”的单词和包含字母“ant”的单词以不同的方式发音,还将看到在特例规则的作用下,“unique”作为一个完整单词优先于其他规则,从而“unique”这个单词读作“y|ou...”而不是“u|n...”。

java可以实现语音识别吗

这个是可以实现的。

注:test.pcm是语音文件,可以用audacity软件打开,选择 文件-导入-裸数据。 设置采样率为8000Hz。点击播放就能听见声音了。

这个时候程序跑起来还有问题,需要将apiKey 以及secretKey填写上。这两个值是你申请应用对应的分配好的。

cuid填本机mac地址就可以了,这个值我试过好像无所谓没啥要求。

程序能跑起来,并且按照正常返回识别的语音结果。但是返回结果的编码为GBK,所以汉字显示为乱码,需要对其进行一次转码。转码的代码是我自己加上去的

网站通过语音接收用户的信息,这种编码是如何实现的?如果是用java怎么做?求指点?

语言接收用户信息是可以有的,但不推荐有,语言这种东西,并不是每个地方的普通话都是一样的,语速上也有着差异性,语音接收肯定就要有语音识别的标准,现在百度和手机自带的,微软上的语言识别也不是都百分百正确的,你说快了准确性会更低,还需要台式机配备语音输入设备,= = ,说了这么多有点跑题,你可以去网上找一下语言封装过的模块,自己写你需要学习很多东西,找一个模块,down一个帮助文档,调用接口写就是的

语音验证码实现就跟图片是一样的,一个是把验证文字转成图片,一个是转成语言


网站标题:语音验证java代码 java语音识别代码
当前路径:http://bjjierui.cn/article/ddjpcep.html

其他资讯