今天更新unity讯飞的语音唤醒功能,由于之前使用的是语音识别,识别出唤醒词来做了一个假的语音唤醒。
缺点:语音识别一直处于识别中。结果可想而知,一天的识别量达到了1W次,这次多么恐怖的一件事啊。
要想知道1000元购买的20W次。20天就能用完,那是多么可怕的事,所以我决定加上语音唤醒功能。
文章最后附上Demo
那么我们进入正题
- 首先从官网下载SDK,并把SDK中的msc.dlll库导入到项目中注意dll分32位和64位
唤醒文件:wakeupresource.jet放到StreamingAssets文件夹下。
需要注意的事:dll库和appid是绑定的,appid不一致会导致SDK无法正常使用。 - 创建SDK接口 MSCDLL
using System;
using System.Runtime.InteropServices;
namespace msc
{
public class MSCDLL
{
#region 登录登出
[DllImport("msc_x64", CallingConvention = CallingConvention.StdCall)]
public static extern int MSPLogin(string usr, string pwd, string parameters);
[DllImport("msc_x64", CallingConvention = CallingConvention.StdCall)]
public static extern int MSPLogout();
#endregion
#region 语音唤醒
//定义回调函数
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int ivw_ntf_handler(IntPtr sessionID, int msg, int param1, int param2, IntPtr info, IntPtr userData);
//调用 QIVWSessionBegin(...)开始一次语音唤醒
[DllImport("msc_x64", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr QIVWSessionBegin(string grammarList, string _params, ref int errorCode);
//调用 QIVWAudioWrite(...) 分块写入音频数据
[DllImport("msc_x64", CallingConvention = CallingConvention.StdCall)]
public static extern int QIVWAudioWrite(IntPtr sessionID, byte[] waveData, uint waveLen, AudioStatus audioStatus);
[DllImport("msc_x64", CallingConvention = CallingConvention.StdCall)]
public static extern int QIVWGetResInfo(string resPath, string resInfo, uint infoLen, string _params);
//调用 QIVWRegisterNotify(...) 注册回调函数到msc。
//如果唤醒成功,msc 调用回调函数通知唤醒成功息同时给出相应唤醒数据。如果出错,msc 调用回调函数给出错误信息
[DllImport("msc_x64", CallingConvention = CallingConvention.StdCall)]
public static extern int QIVWRegisterNotify(IntPtr sessionID, [MarshalAs(UnmanagedType.FunctionPtr)]ivw_ntf_handler msgProcCb, IntPtr userData);
//调用 QIVWSessionEnd(...) 主动结束本次唤醒
[DllImport("msc_x64", CallingConvention = CallingConvention.StdCall)]
public static extern int QIVWSessionEnd(IntPtr sessionID, string hints);
#endregion
}
public enum Errors
{
MSP_SUCCESS = 0,
MSP_ERROR_FAIL = -1,
MSP_ERROR_EXCEPTION = -2,
/* General errors 10100(0x2774) */
MSP_ERROR_GENERAL = 10100, /* 0x2774 */
MSP_ERROR_OUT_OF_MEMORY = 10101, /* 0x2775 */
MSP_ERROR_FILE_NOT_FOUND = 10102, /* 0x2776 */
MSP_ERROR_NOT_SUPPORT = 10103, /* 0x2777 */
MSP_ERROR_NOT_IMPLEMENT = 10104, /* 0x2778 */
MSP_ERROR_ACCESS = 10105, /* 0x2779 */
MSP_ERROR_INVALID_PARA = 10106, /* 0x277A */ /* 缺少参数 */
MSP_ERROR_INVALID_PARA_VALUE = 10107, /* 0x277B */ /* 无效参数值 */
MSP_ERROR_INVALID_HANDLE = 10108, /* 0x277C */
MSP_ERROR_INVALID_DATA = 10109, /* 0x277D */
MSP_ERROR_NO_LICENSE = 10110, /* 0x277E */ /* 引擎授权不足 */
MSP_ERROR_NOT_INIT = 10111, /* 0x277F */ /* 引擎未初始化,可能是引擎崩溃 */
MSP_ERROR_NULL_HANDLE = 10112, /* 0x2780 */
MSP_ERROR_OVERFLOW = 10113, /* 0x2781 */ /* 单用户下模型数超上限(10个), */
/* 只出现在测试时对一个用户进行并发注册 */
MSP_ERROR_TIME_OUT = 10114, /* 0x2782 */ /* 超时 */
MSP_ERROR_OPEN_FILE = 10115, /* 0x2783 */
MSP_ERROR_NOT_FOUND = 10116, /* 0x2784 */ /* 数据库中模型不存在 */
MSP_ERROR_NO_ENOUGH_BUFFER = 10117, /* 0x2785 */
MSP_ERROR_NO_DATA = 10118, /* 0x2786 */ /* 从客户端读音频或从引擎段获取结果时无数据 */
MSP_ERROR_NO_MORE_DATA = 10119, /* 0x2787 */
MSP_ERROR_NO_RESPONSE_DATA = 10120, /* 0x2788 */
MSP_ERROR_ALREADY_EXIST = 10121, /* 0x2789 */ /* 数据库中模型已存在 */
MSP_ERROR_LOAD_MODULE = 10122, /* 0x278A */
MSP_ERROR_BUSY = 10123, /* 0x278B */
MSP_ERROR_INVALID_CONFIG = 10124, /* 0x278C */
MSP_ERROR_VERSION_CHECK = 10125, /* 0x278D */
MSP_ERROR_CANCELED = 10126, /* 0x278E */
MSP_ERROR_INVALID_MEDIA_TYPE = 10127, /* 0x278F */
MSP_ERROR_CONFIG_INITIALIZE = 10128, /* 0x2790 */
MSP_ERROR_CREATE_HANDLE = 10129, /* 0x2791 */
MSP_ERROR_CODING_LIB_NOT_LOAD = 10130, /* 0x2792 */
MSP_ERROR_USER_CANCELLED = 10131, /* 0x2793 */
MSP_ERROR_INVALID_OPERATION = 10132, /* 0x2794 */
MSP_ERROR_MESSAGE_NOT_COMPLETE = 10133, /* 0x2795 */ /* flash */
MSP_ERROR_NO_EID = 10134, /* 0x2795 */
MSP_ERROE_OVER_REQ = 10135, /* 0x2797 */ /* client Redundancy request */
MSP_ERROR_USER_ACTIVE_ABORT = 10136, /* 0x2798 */ /* user abort */
MSP_ERROR_BUSY_GRMBUILDING = 10137, /* 0x2799 */
MSP_ERROR_BUSY_LEXUPDATING = 10138, /* 0x279A */
MSP_ERROR_SESSION_RESET = 10139, /* 0x279B */ /* msc主动终止会话,准备重传 */
MSP_ERROR_BOS_TIMEOUT = 10140, /* 0x279C */ /* VAD前端点超时 */
MSP_ERROR_STREAM_FILTER = 10141, /* 0X279D */ /* AIUI当前Stream被过滤 */
MSP_ERROR_STREAM_CLEAR = 10142, /* 0X279E */ /* AIUI当前Stream被清理 */
/* Error codes of network 10200(0x27D8)*/
MSP_ERROR_NET_GENERAL = 10200, /* 0x27D8 */
MSP_ERROR_NET_OPENSOCK = 10201, /* 0x27D9 */ /* Open socket */
MSP_ERROR_NET_CONNECTSOCK = 10202, /* 0x27DA */ /* Connect socket */
MSP_ERROR_NET_ACCEPTSOCK = 10203, /* 0x27DB */ /* Accept socket */
MSP_ERROR_NET_SENDSOCK = 10204, /* 0x27DC */ /* Send socket data */
MSP_ERROR_NET_RECVSOCK = 10205, /* 0x27DD */ /* Recv socket data */
MSP_ERROR_NET_INVALIDSOCK = 10206, /* 0x27DE */ /* Invalid socket handle */
MSP_ERROR_NET_BADADDRESS = 10207, /* 0x27EF */ /* Bad network address */
MSP_ERROR_NET_BINDSEQUENCE = 10208, /* 0x27E0 */ /* Bind after listen/connect */
MSP_ERROR_NET_NOTOPENSOCK = 10209, /* 0x27E1 */ /* Socket is not opened */
MSP_ERROR_NET_NOTBIND = 10210, /* 0x27E2 */ /* Socket is not bind to an address */
MSP_ERROR_NET_NOTLISTEN = 10211, /* 0x27E3 */ /* Socket is not listening */
MSP_ERROR_NET_CONNECTCLOSE = 10212, /* 0x27E4 */ /* The other side of connection is closed */
MSP_ERROR_NET_NOTDGRAMSOCK = 10213, /* 0x27E5 */ /* The socket is not datagram type */
MSP_ERROR_NET_DNS = 10214, /* 0x27E6 */ /* domain name is invalid or dns server does not function well */
MSP_ERROR_NET_INIT = 10215, /* 0x27E7 */ /* ssl ctx create failed */
/*nfl error*/
MSP_ERROR_NFL_INNER_ERROR = 10216, /* NFL inner error */
MSP_ERROR_MSS_TIME_OUT = 10217, /* MSS TIMEOUT */
MSP_ERROT_CLIENT_TIME_OUT = 10218, /* CLIENT TIMEOUT */
MSP_ERROR_CLIENT_CLOSE = 10219, /* CLIENT CLOSED CONNECTION */
MSP_ERROR_CLIENT_AREA_CHANGE = 10220,
MSP_ERROR_NET_SSL_HANDSHAKE = 10221,
MSP_ERROR_NET_INVALID_ROOT_CERT = 10222,
MSP_ERROR_NET_INVALID_CLIENT_CERT = 10223,
MSP_ERROR_NET_INVALID_SERVER_CERT = 10224,
MSP_ERROR_NET_INVALID_KEY = 10225,
MSP_ERROR_NET_CERT_VERIFY_FAILED = 10226,
MSP_ERROR_NET_WOULDBLOCK = 10227,
MSP_ERROR_NET_NOTBLOCK = 10228,
/* Error codes of mssp message 10300(0x283C) */
MSP_ERROR_MSG_GENERAL = 10300, /* 0x283C */
MSP_ERROR_MSG_PARSE_ERROR = 10301, /* 0x283D */
MSP_ERROR_MSG_BUILD_ERROR = 10302, /* 0x283E */
MSP_ERROR_MSG_PARAM_ERROR = 10303, /* 0x283F */
MSP_ERROR_MSG_CONTENT_EMPTY = 10304, /* 0x2840 */
MSP_ERROR_MSG_INVALID_CONTENT_TYPE = 10305, /* 0x2841 */
MSP_ERROR_MSG_INVALID_CONTENT_LENGTH = 10306, /* 0x2842 */
MSP_ERROR_MSG_INVALID_CONTENT_ENCODE = 10307, /* 0x2843 */
MSP_ERROR_MSG_INVALID_KEY = 10308, /* 0x2844 */
MSP_ERROR_MSG_KEY_EMPTY