Example #1
0
 /// <summary>
 /// 单次识别
 /// </summary>
 /// <returns></returns>
 public RecogStatus SingleVoiceDistinguish()
 {
     try
     {
         Debug.Log(string.Format("登陆成功,语音识别正在加载..."));
         //语音转文字
         string session_params = "engine_type=cloud,sub = iat, domain = iat, language = zh_cn, accent = mandarin, sample_rate = 16000, result_type = plain, result_encoding = UTF-8 ,vad_eos = 5000";//可停止说话5秒保持语音识别状态
         string ssid           = Utils.Ptr2Str(MSC.QISRSessionBegin(string.Empty, session_params, ref ret));
         Debug.Log(string.Format("-->开启一次语音识别[{0}]", ssid));
         if (ret != (int)ErrorCode.MSP_SUCCESS)
         {
             Debug.Log("加载失败!"); return(RecogStatus.ISR_REC_NULL);
         }
         recoStatus = SingleSpeechRecognition(ssid);
         MSC.QISRSessionEnd(ssid, string.Empty);
     }
     catch (Exception e)
     {
         Debug.Log(e.Message);
     }
     return(recoStatus);
 }
Example #2
0
 public static extern IntPtr QISRGetResult(IntPtr sessionID, ref RecogStatus rsltStatus, int waitTime, ref int errorCode);
Example #3
0
 public static extern int QISRAudioWrite(IntPtr sessionID, byte[] waveData, uint waveLen, AudioStatus audioStatus, ref EpStatus epStatus, ref RecogStatus recogStatus);
Example #4
0
    /// <summary>
    /// 语音识别方法
    /// </summary>
    /// <param name="sid"></param>
    public static void SpeechRecognition(List <VoiceData> VoiceBuffer)
    {
        audio_stat = audioStatus.MSP_AUDIO_SAMPLE_CONTINUE;
        ep_status  = epStatus.MSP_EP_LOOKING_FOR_SPEECH;
        recoStatus = RecogStatus.ISR_REC_STATUS_SUCCESS;
        sid        = Utils.Ptr2Str(MSC.QISRSessionBegin(string.Empty, speech_param, ref ret));
        Debug.Log(string.Format("-->开启一次语音识别[{0}]", sid));
        if (ret != (int)ErrorCode.MSP_SUCCESS)
        {
            Debug.Log("加载失败!"); return;
        }

        for (int i = 0; i < VoiceBuffer.Count(); i++)
        {
            audio_stat = audioStatus.MSP_AUDIO_SAMPLE_CONTINUE;
            if (i == 0)
            {
                audio_stat = audioStatus.MSP_AUDIO_SAMPLE_FIRST;
            }
            ret = MSC.QISRAudioWrite(sid, VoiceBuffer[i].data, (uint)VoiceBuffer[i].data.Length, audio_stat, ref ep_status, ref recoStatus);
            if ((int)ErrorCode.MSP_SUCCESS != ret)
            {
                MSC.QISRSessionEnd(sid, null);
            }
        }

        ret = MSC.QISRAudioWrite(sid, null, 0, audioStatus.MSP_AUDIO_SAMPLE_LAST, ref ep_status, ref recoStatus);
        if ((int)ErrorCode.MSP_SUCCESS != ret)
        {
            Debug.Log("\nQISRAudioWrite failed! error code:" + ret);
            return;
        }

        while (RecogStatus.ISR_REC_STATUS_SPEECH_COMPLETE != recoStatus)
        {
            IntPtr rslt = MSC.QISRGetResult(sid, ref recoStatus, 0, ref ret);
            if ((int)ErrorCode.MSP_SUCCESS != ret)
            {
                Debug.Log("\nQISRGetResult failed, error code: " + ret);
                break;
            }
            if (IntPtr.Zero != rslt)
            {
                string tempRes = Utils.Ptr2Str(rslt);

                ask_rec_result = ask_rec_result + tempRes;
                if (ask_rec_result.Length >= BUFFER_SIZE)
                {
                    Debug.Log("\nno enough buffer for rec_result !\n");
                    break;
                }
            }
        }
        int errorcode = MSC.QISRSessionEnd(sid, "正常结束");

        //语音识别结果
        if (ask_rec_result.Length != 0)
        {
            FlowManage.P2MMode(nar);
            Debug.Log("识别结果是:" + ask_rec_result);
            ask_rec_result = "";
        }
    }
Example #5
0
        /// <summary>
        /// 将录取的录音持续转换成文本直到Stop_Translate方法被执行
        /// </summary>
        /// <param name="_login_configs"> 登录参数,需求appid </param>
        /// <param name="_param1"> QISRSessionBegin转换相关参数,详见使用手册 </param>
        public void Begin_Translate1(string _login_configs, string _param1)
        {
            Console.WriteLine("Start");
            // MscNet mn = new MscNet(null, null, null, null);
            SoundRecorder sr = new SoundRecorder();//录音相关类

            //参数
            AudioStatus audStat   = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE;
            EpStatus    epStatus  = EpStatus.ISR_EP_NULL;
            RecogStatus recStatus = RecogStatus.ISR_REC_NULL;



            string login_configs = _login_configs;
            string param1        = _param1;
            string sessionID     = null;
            int    errCode       = 0;


            isstop = false;//开始停止属性



            sr.SetFileName("output.wav");        //设置录音存储文件
            sr.RecStart();                       //开始录音
            MSPLogin(null, null, login_configs); //登陆


            sessionID = PtrToStr(QISRSessionBegin(null, param1, ref errCode));//开始一路会话
            while (!isstop)
            {
                System.Threading.Thread.Sleep(100);

                byte[] data = new byte[sr.CurrentData.Count];
                data = sr.CurrentData.ToArray <byte>();
                lock (sr.CurrentData)
                {
                    sr.ClearDate();
                }


                int len = data.Count();
                audStat = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE;

                QISRAudioWrite(sessionID, data, (uint)len, audStat, ref epStatus, ref recStatus);

                if (recStatus == RecogStatus.ISR_REC_STATUS_SUCCESS)
                {
                    string rslt = PtrToStr(QISRGetResult(sessionID, ref recStatus, 0, ref errCode));//服务端已经有识别结果,可以获取
                    if (null != rslt)
                    {
                        if (DataReceive != null)
                        {
                            DataReceive(rslt);
                        }
                    }
                    System.Threading.Thread.Sleep(10);
                }
                if (epStatus == EpStatus.ISR_EP_AFTER_SPEECH)
                {
                    while (recStatus != RecogStatus.ISR_REC_STATUS_SPEECH_COMPLETE && 0 == errCode)
                    {
                        string rslt = PtrToStr(QISRGetResult(sessionID, ref recStatus, 0, ref errCode));
                        if (null != rslt)
                        {
                            if (DataReceive != null)
                            {
                                DataReceive(rslt);
                            }
                        }
                        ;
                        System.Threading.Thread.Sleep(10);
                    }
                    QISRSessionEnd(sessionID, "Wraing");
                    System.Threading.Thread.Sleep(30);
                    sessionID = PtrToStr(QISRSessionBegin(null, param1, ref errCode));//开始一路会话

                    if (sessionID == null)
                    {
                        if (ShowInfomation != null)
                        {
                            ShowInfomation("错误,可能由于appid与msc.dll不匹配,或appid不存在");
                        }
                        return;
                    }
                }
            }
            QISRAudioWrite(sessionID, new byte[1], 0, AudioStatus.ISR_AUDIO_SAMPLE_LAST, ref epStatus, ref recStatus);
            while (recStatus != RecogStatus.ISR_REC_STATUS_SPEECH_COMPLETE && 0 == errCode)
            {
                string rslt = PtrToStr(QISRGetResult(sessionID, ref recStatus, 0, ref errCode));
                if (null != rslt)
                {
                    if (DataReceive != null)
                    {
                        DataReceive(rslt);
                    }
                }
                System.Threading.Thread.Sleep(30);
            }
            QISRSessionEnd(sessionID, "normal");
            MSPLogout();
            sr.RecStop();

            if (ShowInfomation != null)
            {
                ShowInfomation("录音结束");
            }
            if (VoiceToTextStopEven != null)
            {
                VoiceToTextStopEven(this, new EventArgs());
            }
            byte[] mb = new byte[10];
            mb.Contains((byte)(7 * 16 + 11));
        }
Example #6
0
 private static extern IntPtr QISRGetResult(string sessionID, ref RecogStatus rsltStatus, int waitTime, ref int errorCode);
Example #7
0
        /// <summary>
        ///  将声音文件转换成文字或语义识别
        /// </summary>
        /// <param name="_login_configs">登录参数</param>
        /// <param name="_param1">合成文字参数</param>
        /// <param name="filename">声音源文件</param>
        /// <param name="userwordType">字典类型,
        /// 0:听写用户词典,需要登录参数中指定词典名,且为听写模式。
        /// 1:识别模式时,需要为语义识别模式。
        /// </param>
        /// <param name="userword">用户词典/关键词 文件名</param>
        /// <param name="testId">语法ID(关键词在服务器的ID),上传后可获得</param>
        /// <param name="gramname">语法文件名</param>
        public void TranslateVoiceFile(string _login_configs, string _param1, string filename,
                                       int userwordType = 0, string userword = null, string testId = null, string gramname = null)
        {
            FileStream   fs = new FileStream(filename, FileMode.OpenOrCreate);
            BinaryReader br = new BinaryReader(fs);

            fs.Seek(44, 0);
            byte[] data = new byte[fs.Length];
            br.Read(data, 0, (int)fs.Length);


            AudioStatus audStat   = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE;
            EpStatus    epStatus  = EpStatus.ISR_EP_NULL;
            RecogStatus recStatus = RecogStatus.ISR_REC_NULL;



            string login_configs = _login_configs;
            string param1        = _param1;
            string sessionID     = null;
            int    errCode       = 0;
            long   len           = 6400;
            long   write_len     = 0;
            long   totalLen      = data.Count();

            //登陆
            MSPLogin(null, null, login_configs);

            //用户词典或关键字上传
            if (userword != null)
            {
                upload_user_vocabulary(userword, userwordType, ref testId);
            }

            //开始一路会话
            sessionID = PtrToStr(QISRSessionBegin(testId, param1, ref errCode));
            if (sessionID == null)
            {
                if (ShowInfomation != null)
                {
                    ShowInfomation("APPID不正确或与msc.dll不匹配");
                }
            }

            //激活语法
            if (gramname != null)
            {
                FileStream   gramfs = new FileStream(gramname, FileMode.OpenOrCreate);
                StreamReader grmsr  = new StreamReader(gramfs, Encoding.Default);

                string gram = grmsr.ReadToEnd();

                int result = QISRGrammarActivate(sessionID, gram, null, 0);

                grmsr.Close();
                gramfs.Close();
                gramfs.Dispose();
                grmsr.Dispose();
            }

            //开始正式转换
            while (audStat != AudioStatus.ISR_AUDIO_SAMPLE_LAST)
            {
                audStat = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE;
                if (epStatus == EpStatus.ISR_EP_NULL)
                {
                    audStat = AudioStatus.ISR_AUDIO_SAMPLE_FIRST;
                }

                if ((totalLen - write_len) <= len)
                {
                    len     = (totalLen - write_len);
                    audStat = AudioStatus.ISR_AUDIO_SAMPLE_LAST;
                }

                byte[] dataTemp = new byte[len];

                Array.Copy(data, write_len, dataTemp, 0, len);

                QISRAudioWrite(sessionID, dataTemp, (uint)len, audStat, ref epStatus, ref recStatus);

                if (recStatus == RecogStatus.ISR_REC_STATUS_SUCCESS)
                {
                    string rslt = PtrToStr(QISRGetResult(sessionID, ref recStatus, 0, ref errCode));//服务端已经有识别结果,可以获取
                    if (null != rslt)
                    {
                        if (DataReceive != null)
                        {
                            DataReceive(rslt);
                        }
                    }
                    System.Threading.Thread.Sleep(10);
                }
                write_len += len;
                if (epStatus == EpStatus.ISR_EP_AFTER_SPEECH)
                {
                    break;
                }

                System.Threading.Thread.Sleep(100);
            }
            QISRAudioWrite(sessionID, new byte[1], 0, AudioStatus.ISR_AUDIO_SAMPLE_LAST, ref epStatus, ref recStatus);
            while (recStatus != RecogStatus.ISR_REC_STATUS_SPEECH_COMPLETE && 0 == errCode)
            {
                string rslt = PtrToStr(QISRGetResult(sessionID, ref recStatus, 0, ref errCode));
                if (null != rslt)
                {
                    if (DataReceive != null)
                    {
                        DataReceive(rslt);
                    }
                }
                System.Threading.Thread.Sleep(30);
            }
            QISRSessionEnd(sessionID, "normal");
            MSPLogout();
            if (ShowInfomation != null)
            {
                ShowInfomation(string.Format("Error Code:{0}\n", errCode));
            }

            if (ShowInfomation != null)
            {
                ShowInfomation("转换结束");
            }
            if (VoiceToTextStopEven != null)
            {
                VoiceToTextStopEven(this, new EventArgs());
            }

            br.Close();
            fs.Close();
            fs.Dispose();
        }
Example #8
0
        /// <summary>
        /// 将录取的录音持续转换成文本,自动识别结尾或Stop_Translate方法被执行时停止
        /// </summary>
        /// <param name="_login_configs">登录参数,需求appid</param>
        /// <param name="_param1">QISRSessionBegin转换相关参数,详见使用手册</param>
        public void Begin_Translate2(string _login_configs, string _param1)
        {
            string login_configs = _login_configs;//登录参数
            string param1        = _param1;


            // MscNet mn = new MscNet(null, null, null, null);
            SoundRecorder sr        = new SoundRecorder();
            AudioStatus   audStat   = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE;
            EpStatus      epStatus  = EpStatus.ISR_EP_NULL;
            RecogStatus   recStatus = RecogStatus.ISR_REC_NULL;

            string sessionID = null;
            int    errCode   = 0;

            quitState = "normal";
            isstop    = false;


            sr.SetFileName("output.wav");
            sr.RecStart();
            MSPLogin(null, null, login_configs);

            sessionID = PtrToStr(QISRSessionBegin(null, param1, ref errCode));//开始一路会话
            if (sessionID == null)
            {
                if (ShowInfomation != null)
                {
                    ShowInfomation("错误,可能由于appid与msc.dll不匹配,或appid不存在");
                }
                return;
            }

            while (audStat != AudioStatus.ISR_AUDIO_SAMPLE_LAST)
            {
                if (epStatus != EpStatus.ISR_EP_NULL)
                {
                    audStat = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE;
                }

                if (isstop)//在传输数据期间点击按钮
                {
                    audStat = AudioStatus.ISR_AUDIO_SAMPLE_LAST;
                    break;
                }

                System.Threading.Thread.Sleep(100);
                if (isstop)////在延时获取数据期间点击按钮
                {
                    audStat = AudioStatus.ISR_AUDIO_SAMPLE_LAST;
                }

                byte[] data = new byte[sr.CurrentData.Count];
                lock (sr.currentdata)
                {
                    data = sr.CurrentData.ToArray <byte>();
                    sr.ClearDate();
                }

                int len = data.Count();
                QISRAudioWrite(sessionID, data, (uint)len, audStat, ref epStatus, ref recStatus);//向讯飞服务端发送音频
                if (recStatus == RecogStatus.ISR_REC_STATUS_SUCCESS)
                {
                    string rslt = PtrToStr(QISRGetResult(sessionID, ref recStatus, 0, ref errCode));//服务端已经有识别结果,可以获取
                    if (null != rslt)
                    {
                        if (DataReceive != null)
                        {
                            DataReceive(rslt);                     //触发数据接收事件
                        }
                    }
                }
                if (epStatus == EpStatus.ISR_EP_AFTER_SPEECH)//服务端判断音频结束
                {
                    break;
                }
            }

            QISRAudioWrite(sessionID, new byte[1], 0, AudioStatus.ISR_AUDIO_SAMPLE_LAST, ref epStatus, ref recStatus);
            sr.RecStop();
            if (ShowInfomation != null)
            {
                ShowInfomation("录音结束,请等待转换结果");
            }

            while (recStatus != RecogStatus.ISR_REC_STATUS_SPEECH_COMPLETE && 0 == errCode)
            {
                string rslt = PtrToStr(QISRGetResult(sessionID, ref recStatus, 0, ref errCode));
                if (null != rslt)
                {
                    if (DataReceive != null)
                    {
                        DataReceive(rslt);                      //触发数据接收事件
                    }
                }
                System.Threading.Thread.Sleep(30);
            }
            //QISRSessionEnd(sessionID, quitState);
            MSPLogout();
            if (ShowInfomation != null)
            {
                ShowInfomation("完成");
            }
            if (VoiceToTextStopEven != null)
            {
                VoiceToTextStopEven(this, new EventArgs());
            }
            sr.ClearDate();
        }
Example #9
0
 private static extern int QISRAudioWrite(string sessionID, byte[] waveData, uint waveLen, AudioStatus audioStatus, ref EpStatus epStatus, ref RecogStatus recogStatus);
Example #10
0
 public static extern IntPtr QISRGetResult(string sessionID, ref RecogStatus rsltStatus, int waitTime, ref int errorCode);
Example #11
0
 public static extern int QISRAudioWrite(string sessionID, IntPtr waveData, uint waveLen, AudioStatus audioStatus, ref  EpStatus epStatus, ref RecogStatus recogStatus);
Example #12
0
        public static Task RunIAT(List <VoiceData> VoiceReady, string session_begin_params, ref SendDataPipe SendDataPipe)
        {
            IntPtr      session_id = IntPtr.Zero;
            string      rec_result = string.Empty;
            string      hints      = "正常结束";
            AudioStatus aud_stat   = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE;
            EpStatus    ep_stat    = EpStatus.ISR_EP_LOOKING_FOR_SPEECH;
            RecogStatus rec_stat   = RecogStatus.ISR_REC_STATUS_SUCCESS;
            int         errcode    = (int)ErrorCode.MSP_SUCCESS;

            session_id = MSCDLL.QISRSessionBegin(null, session_begin_params, ref errcode);
            if ((int)ErrorCode.MSP_SUCCESS != errcode)
            {
                Debug.WriteLine("\nQISRSessionBegin failed! error code:{0}\n", errcode);
                //return;
            }

            for (int i = 0; i < VoiceReady.Count(); i++)
            {
                aud_stat = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE;
                if (i == 0)
                {
                    aud_stat = AudioStatus.ISR_AUDIO_SAMPLE_FIRST;
                }
                errcode = MSCDLL.QISRAudioWrite(PtrToStr(session_id), VoiceReady[i].data, (uint)VoiceReady[i].data.Length, aud_stat, ref ep_stat, ref rec_stat);
                if ((int)ErrorCode.MSP_SUCCESS != errcode)
                {
                    MSCDLL.QISRSessionEnd(PtrToStr(session_id), null);
                }
            }

            errcode = MSCDLL.QISRAudioWrite(PtrToStr(session_id), null, 0, AudioStatus.ISR_AUDIO_SAMPLE_LAST, ref ep_stat, ref rec_stat);
            if ((int)ErrorCode.MSP_SUCCESS != errcode)
            {
                Debug.WriteLine("\nQISRAudioWrite failed! error code:{0} \n", errcode);
                //return;
            }

            while (RecogStatus.ISR_REC_STATUS_SPEECH_COMPLETE != rec_stat)
            {
                IntPtr rslt = MSCDLL.QISRGetResult(PtrToStr(session_id), ref rec_stat, 0, ref errcode);

                if ((int)ErrorCode.MSP_SUCCESS != errcode)
                {
                    Debug.WriteLine("\nQISRGetResult failed, error code: {0}\n", errcode);
                    break;
                }
                if (IntPtr.Zero != rslt)
                {
                    string tempRes = PtrToStr(rslt);

                    rec_result = rec_result + tempRes;
                    if (rec_result.Length >= BUFFER_SIZE)
                    {
                        Debug.WriteLine("\nno enough buffer for rec_result !\n");
                        break;
                    }
                }
            }

            //结果
            Debug.WriteLine(rec_result);

            SendDataPipe.Start(rec_result);

            int errorcode = MSCDLL.QISRSessionEnd(PtrToStr(session_id), hints);

            if ((int)ErrorCode.MSP_SUCCESS == errorcode)
            {
                Debug.WriteLine("\nQISRGetResult successfull, {0}\n", hints);
            }

            return(Task.FromResult(rec_result));
        }
Example #13
0
        //语音识别
        private void RunIAT(List <VoiceData> VoiceBuffer, string session_begin_params)
        {
            IntPtr      session_id = IntPtr.Zero;
            string      rec_result = string.Empty;
            string      hints      = "正常结束";
            AudioStatus aud_stat   = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE;
            EpStatus    ep_stat    = EpStatus.ISR_EP_LOOKING_FOR_SPEECH;
            RecogStatus rec_stat   = RecogStatus.ISR_REC_STATUS_SUCCESS;
            int         errcode    = (int)ErrorCode.MSP_SUCCESS;

            session_id = MSCDLL.QISRSessionBegin(null, session_begin_params, ref errcode);
            if ((int)ErrorCode.MSP_SUCCESS != errcode)
            {
                SetText("\nQISRSessionBegin failed! error code:" + errcode);
                return;
            }

            for (int i = 0; i < VoiceBuffer.Count(); i++)
            {
                aud_stat = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE;
                if (i == 0)
                {
                    aud_stat = AudioStatus.ISR_AUDIO_SAMPLE_FIRST;
                }
                errcode = MSCDLL.QISRAudioWrite(PtrToStr(session_id), VoiceBuffer[i].data, (uint)VoiceBuffer[i].data.Length, aud_stat, ref ep_stat, ref rec_stat);
                if ((int)ErrorCode.MSP_SUCCESS != errcode)
                {
                    MSCDLL.QISRSessionEnd(PtrToStr(session_id), null);
                }
            }

            errcode = MSCDLL.QISRAudioWrite(PtrToStr(session_id), null, 0, AudioStatus.ISR_AUDIO_SAMPLE_LAST, ref ep_stat, ref rec_stat);
            if ((int)ErrorCode.MSP_SUCCESS != errcode)
            {
                SetText("\nQISRAudioWrite failed! error code:" + errcode);
                return;
            }

            while (RecogStatus.ISR_REC_STATUS_SPEECH_COMPLETE != rec_stat)
            {
                IntPtr rslt = MSCDLL.QISRGetResult(PtrToStr(session_id), ref rec_stat, 0, ref errcode);
                if ((int)ErrorCode.MSP_SUCCESS != errcode)
                {
                    SetText("\nQISRGetResult failed, error code: " + errcode);
                    break;
                }
                if (IntPtr.Zero != rslt)
                {
                    string tempRes = PtrToStr(rslt);

                    rec_result = rec_result + tempRes;
                    if (rec_result.Length >= BUFFER_SIZE)
                    {
                        SetText("\nno enough buffer for rec_result !\n");
                        break;
                    }
                }
            }
            int errorcode = MSCDLL.QISRSessionEnd(PtrToStr(session_id), hints);

            //语音识别结果
            if (rec_result.Length != 0)
            {
                SetText(rec_result);


                //返回错误代码10111时,可调用SpeechRecognition()函数执行MSPLogin
            }
        }
Example #14
0
 public static extern int QISRAudioWrite(string sessionID, IntPtr waveData, uint waveLen, AudioStatus audioStatus, ref EpStatus epStatus, ref RecogStatus recogStatus);