自我介绍
大家好,我是VAIN,这是我在优快云的第一篇文章,之前一直在微博博客上写文章,今后会用优快云给大家更新一些技术帖,还希望大家多多关照!
项目介绍
因为公司项目要求,今天给大家分享一个unity制作AI助手的帖子,由于网上相关的文章还是比较少的,要么就是不是特别的全面。所以分享一下,希望可以帮助到小伙伴们。
思路
1.需要将我们说的话转成文字(语音识别)
2.AI助手理解我们说的话(人机交互)
3.将AI助手的返回信息转成语音(语音合成)
制作准备
我做的是PC端的,用的是讯飞的SDK和百度的SDK。至于为什么用两个SDK,我也不想啊,C++我也不会啊。这里吐槽一下,讯飞的技术文档真的写给自己看的。
讯飞:语音识别、语音合成(Windows MSC)
百度:人机交互(UNIT)
开始
1.首先我们去讯飞下载SDK,至于怎么下,一些平台操作,这里不做过多的讲解,网上有很多。
2.导入到Unity项目中 msc是语音识别和语音合成会用到的,aiui(人机交互)可以不用导入,用不到。我这边调用aiui的接口不知道为什么一直差找不到接口。所以才用了百度的UNIT。
这里需要注意的是自己下载SDK只能用对应自己的appid,如果你用了别的SDK,那你就只能用别人的appid。
3.讯飞的SDK是C++写的,我们想要使用,还需要使用非托管DLL的方式
这里我就直接贴出来了
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 语音识别
[DllImport("msc_x64", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr QISRSessionBegin(string grammarList, string _params, ref int errorCode);
[DllImport("msc_x64", CallingConvention = CallingConvention.StdCall)]
public static extern int QISRAudioWrite(IntPtr sessionID, byte[] waveData, uint waveLen, AudioStatus audioStatus, ref EpStatus epStatus, ref RecogStatus recogStatus);
[DllImport("msc_x64", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr QISRGetResult(IntPtr sessionID, ref RecogStatus rsltStatus, int waitTime, ref int errorCode);
[DllImport("msc_x64", CallingConvention = CallingConvention.StdCall)]
public static extern int QISRSessionEnd(IntPtr sessionID, string hints);
[DllImport("msc_x64", CallingConvention = CallingConvention.StdCall)]
public static extern int QISRBuildGrammar(IntPtr grammarType, string grammarContent, uint grammarLength, string _params, GrammarCallBack callback, IntPtr userData);
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)]
public delegate int GrammarCallBack(int errorCode, string info, object udata);
[DllImport("msc.dll", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr QISRUploadData(string sessionID, string dataName, byte[] userData, uint lenght, string paramValue, ref int errorCode);
#endregion
#region 语音唤醒
//定义回调函数
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int ivw_ntf_handler(string sessionID, int msg, int param1, int param2, IntPtr info, IntPtr userData);
//调用 QIVWSessionBegin(...)开始一次语音唤醒
[DllImport("msc_x64", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr QIVWSess