Exemplo n.º 1
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));
        }
Exemplo n.º 2
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();
        }