示例#1
0
 /// <summary>
 /// 处理错误
 /// </summary>
 /// <param name="errorMsg">错误的函数</param>
 /// <param name="ret">错误的返回值</param>
 /// <param name="sessionID">sessionID</param>
 /// <param name="fs">打开的文件</param>
 private static void HandleErrorMsg(string errorMsg, int ret, string sessionID, FileStream fs)
 {
     Utils.CustomPrint(errorMsg + " err,errCode=" + ((ErrorCode)ret).ToString("G"));
     DllImports.QISESessionEnd(sessionID, errorMsg);
     if (fs != null)
     {
         fs.Close();
         fs = null;
     }
 }
示例#2
0
        /// <summary>
        /// 运行语音评测功能
        /// </summary>
        /// <returns></returns>
        public IEnumerator RunISE()
        {
            int    len;
            int    audStatus = (int)AudioStatus.MSP_AUDIO_SAMPLE_CONTINUE; ///音频状态
            int    epStatus  = (int)EpStatus.MSP_EP_NULL;                  ///端点检测
            int    recStatus = (int)RecogStatus.MSP_REC_NULL;              ///识别状态
            bool   first     = true;
            uint   rlstLen   = 0;
            IntPtr rsltPrt   = IntPtr.Zero;

            int       ret        = 0;
            const int BUFFER_NUM = 640 * 10;/// 每次写入200ms音频(16k,16bit):1帧音频20ms,10帧=200ms。16k采样率的16位音频,一帧的大小为640Byte

            FileStream textFS = null;
            FileStream waveFS = null;

            string parameters, textPath, wavePath;

            GetValuesByCategory(category, out parameters, out textPath, out wavePath);

#if UNITY_ANDROID
            textPath = Application.persistentDataPath + "/" + textPath;
            wavePath = Application.persistentDataPath + "/" + wavePath;
#elif UNITY_STANDALONE_WIN
            textPath = Application.streamingAssetsPath + "/" + textPath;
            wavePath = Application.streamingAssetsPath + "/" + wavePath;
#endif

            IntPtr ptrSessionID = DllImports.QISESessionBegin(parameters, "", out ret);
            string sessionID    = Marshal.PtrToStringAnsi(ptrSessionID);
            if (ErrorCode.MSP_SUCCESS != (ErrorCode)ret)
            {
                Utils.CustomPrint("QISESessionBegin err,errCode=" + ((ErrorCode)ret).ToString("G"));
                yield break;
            }

            if (!File.Exists(textPath) || !File.Exists(wavePath))
            {
                Utils.CustomPrint("文件" + textPath + " 或者 " + wavePath + "不存在!");
                DllImports.QISESessionEnd(sessionID, "File not exist");
                yield break;
            }
            textFS = new FileStream(textPath, FileMode.Open);
            int    textLength = (int)textFS.Length;
            byte[] buff       = new byte[textLength];
            textFS.Read(buff, 0, textLength);
            textFS.Close();
            textFS = null;

            ret = DllImports.QISETextPut(sessionID, Encoding.Default.GetString(buff), (uint)textLength, "");
            Utils.CustomPrint("Encoding:" + Encoding.Default.EncodingName.ToLower());
            Utils.CustomPrint(Regex.Unescape(Encoding.Default.GetString(buff)) + "|length:" + textLength);
            if (ErrorCode.MSP_SUCCESS != (ErrorCode)ret)
            {
                HandleErrorMsg("QISETextPut", ret, sessionID, null);
                yield break;
            }

            waveFS = new FileStream(wavePath, FileMode.Open);
            buff   = new byte[BUFFER_NUM];
            IntPtr bp = Marshal.AllocHGlobal(BUFFER_NUM);

            while (waveFS.Position != waveFS.Length)
            {
                len = waveFS.Read(buff, 0, BUFFER_NUM);
                Marshal.Copy(buff, 0, bp, buff.Length);

                audStatus = (int)AudioStatus.MSP_AUDIO_SAMPLE_CONTINUE;
                if (first)
                {
                    audStatus = (int)AudioStatus.MSP_AUDIO_SAMPLE_FIRST;
                    first     = false;
                }

                Utils.CustomPrint(">" + len);
                ///开始向服务器发送音频数据
                ret = DllImports.QISEAudioWrite(sessionID, bp, (uint)len, audStatus, ref epStatus, ref recStatus);
                if (ErrorCode.MSP_SUCCESS != (ErrorCode)ret)
                {
                    HandleErrorMsg("QISEAudioWrite", ret, sessionID, waveFS);
                    yield break;
                }

                if (RecogStatus.MSP_REC_STATUS_SUCCESS == (RecogStatus)recStatus)
                {
                    rsltPrt = DllImports.QISEGetResult(sessionID, ref rlstLen, ref recStatus, ref ret);
                    if (ErrorCode.MSP_SUCCESS != (ErrorCode)ret)
                    {
                        HandleErrorMsg("QISEGetResult", ret, sessionID, waveFS);
                        yield break;
                    }
                    if (IntPtr.Zero != rsltPrt)
                    {
                        Utils.CustomPrint("rlstLen:" + rlstLen);
                        byte[] data = new byte[rlstLen];
                        Marshal.Copy(rsltPrt, data, 0, (int)rlstLen);
                        Utils.CustomPrint("+++传完音频后返回结果!:" + GBToUnicode(data, data.Length));
                    }
                }

                if (EpStatus.MSP_EP_AFTER_SPEECH == (EpStatus)epStatus)
                {
                    break;
                }
                ///sleep一下很有必要,防止MSC端无缓冲的识别结果时浪费CPU资源
                yield return(new WaitForSeconds(0.2f));
            }
            buff = null;
            waveFS.Close();
            waveFS = null;
            Marshal.FreeHGlobal(bp);

            ret = DllImports.QISEAudioWrite(sessionID, IntPtr.Zero, 0, (int)AudioStatus.MSP_AUDIO_SAMPLE_LAST, ref epStatus, ref recStatus);
            if (ErrorCode.MSP_SUCCESS != (ErrorCode)ret)
            {
                HandleErrorMsg("QISEAudioWrite", ret, sessionID, null);
                yield break;
            }

            while (RecogStatus.MSP_REC_STATUS_COMPLETE != (RecogStatus)recStatus)
            {
                rsltPrt = DllImports.QISEGetResult(sessionID, ref rlstLen, ref recStatus, ref ret);
                if (ErrorCode.MSP_SUCCESS != (ErrorCode)ret)
                {
                    HandleErrorMsg("QISEGetResult", ret, sessionID, null);
                    yield break;
                }
                if (IntPtr.Zero != rsltPrt)
                {
                    Utils.CustomPrint("rlstLen:" + rlstLen);
                    byte[] data = new byte[rlstLen];
                    Marshal.Copy(rsltPrt, data, 0, (int)rlstLen);
                    Utils.CustomPrint("---传完音频后返回结果!:" + GBToUnicode(data, data.Length));
                }
                yield return(new WaitForSeconds(0.2f));
            }
            rsltPrt = IntPtr.Zero;
            ret     = DllImports.QISESessionEnd(sessionID, "Normal");
            if (ErrorCode.MSP_SUCCESS != (ErrorCode)ret)
            {
                Utils.CustomPrint("QISESessionEnd failed, errCode=" + ((ErrorCode)ret).ToString("G"));
            }
            ptrSessionID = IntPtr.Zero;
            Utils.CustomPrint("评测完成\r\n");
            yield break;
        }