예제 #1
0
        public async Task Update(int[] epId, EpStatus status)
        {
            if (epId == null)
            {
                throw new ArgumentNullException(nameof(epId));
            }
            if (epId.Length <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(status));
            }
            if (!Enum.IsDefined(typeof(EpStatus), status))
            {
                throw new ArgumentOutOfRangeException(nameof(status));
            }

            var url = $"/ep/{epId.Last()}/status/{status.ToString().ToLowerInvariant()}";

            using (var client = new BangumiClient(_oauthProvider))
            {
                using (var postContent = new FormUrlEncodedContent(Enumerable.Empty <KeyValuePair <string, string> >()))
                {
                    await client.PostAsync(url, postContent);
                }
            }
        }
예제 #2
0
        public static string GetValue(this EpStatus status)
        {
            switch (status)
            {
            case EpStatus.Watched:
                return("watched");

            case EpStatus.Queue:
                return("queue");

            case EpStatus.Drop:
                return("drop");

            case EpStatus.Remove:
                return("remove");

            default:
                return(status.ToString());
            }
        }
예제 #3
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();
        }
예제 #4
0
 private static extern int QISRAudioWrite(string sessionID, byte[] waveData, uint waveLen, AudioStatus audioStatus, ref EpStatus epStatus, ref RecogStatus recogStatus);
예제 #5
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();
        }
예제 #6
0
 public static extern int QISRAudioWrite(string sessionID, byte[] waveData, uint waveLen, AudioStatus audioStatus, ref EpStatus epStatus, ref RsltStatus rsltStatus);
예제 #7
0
 public static extern int QISRAudioWrite(string sessionID, IntPtr waveData, uint waveLen, AudioStatus audioStatus, ref  EpStatus epStatus, ref RecogStatus recogStatus);
예제 #8
0
        private void ThreadStart()
        {
            ErrorCode   errorCode   = ErrorCode.MSP_SUCCESS;
            AudioStatus audioStatus = AudioStatus.MSP_AUDIO_SAMPLE_FIRST;
            EpStatus    epStatus    = EpStatus.MSP_EP_IN_SPEECH;
            RsltStatus  rsltStatus  = RsltStatus.MSP_REC_STATUS_SUCCESS;

            // 开始
            IntPtr ptr = Msc.QISRSessionBegin(null, @params, out errorCode);

            if (errorCode != ErrorCode.MSP_SUCCESS)
            {
                Console.WriteLine("开始识别出错:(错误码)" + (int)errorCode);
                Account.Logout();
                return;
            }

            int maxLength = 20480;
            int tims      = m_Datas.Length / maxLength;

            if ((m_Datas.Length % maxLength) != 0)
            {
                tims++;
            }


            for (int i = 0; i < tims; i++)
            {
                if (i == 0)
                {
                    audioStatus = AudioStatus.MSP_AUDIO_SAMPLE_FIRST;
                }
                else if (i == tims - 1)
                {
                    audioStatus = AudioStatus.MSP_AUDIO_SAMPLE_LAST;
                }
                else
                {
                    audioStatus = AudioStatus.MSP_AUDIO_SAMPLE_CONTINUE;
                }

                byte[] by = null;
                if ((m_Datas.Length - maxLength * i) >= maxLength)
                {
                    by = new byte[maxLength];
                }
                else
                {
                    by = new byte[(m_Datas.Length - maxLength * i)];
                }

                by = m_Datas.Skip(i * maxLength).Take(by.Length).ToArray();

                ErrorCode errorCode1 = Msc.QISRAudioWrite(ptr, by, (uint)by.Length, audioStatus, out epStatus, out rsltStatus);

                if (errorCode1 != ErrorCode.MSP_SUCCESS)
                {
                    Console.WriteLine("音频数据写入失败:(错误码)" + (int)errorCode1);
                    Account.Logout();
                    return;
                }
            }

            RsltStatus    status     = RsltStatus.MSP_REC_STATUS_INCOMPLETE;
            ErrorCode     errorCode2 = ErrorCode.MSP_SUCCESS;
            StringBuilder msg        = new StringBuilder();

            while (status != RsltStatus.MSP_REC_STATUS_COMPLETE)
            {
                IntPtr data = Msc.QISRGetResult(ptr, out status, 5000, out errorCode2);

                if (errorCode2 != ErrorCode.MSP_SUCCESS)
                {
                    Console.WriteLine("获取失败:(错误码)" + (int)errorCode2);
                    Account.Logout();
                    return;
                }

                if (data != null)
                {
                    msg.Append(Marshal.PtrToStringAnsi(data));
                }
                Thread.Sleep(200);
            }

            ErrorCode errorCode3 = Msc.QISRSessionEnd(ptr, "识别结束");

            if (errorCode3 != ErrorCode.MSP_SUCCESS)
            {
                Console.WriteLine("结束失败:(错误码)" + (int)errorCode3);
            }

            Data   = msg.ToString();
            isDown = true;
        }
예제 #9
0
 public Task <BangumiResult> UpdateEpStatusAsync(int epId, EpStatus status, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(_client.UpdateEpStatusAsync(epId, status, cancellationToken));
 }
예제 #10
0
파일: fmIFY.cs 프로젝트: mxq00812314/MXQ
        string filename = $"Call.wav"; //合成的语音文件

        #endregion

        #region 事件处理

        /// <summary>
        /// 本地语音识别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLocalVoice_Click(object sender, EventArgs e)
        {
            //业务流程:
            //1.调用 MSPLogin(...)接口登入,可以只登入一次,但是必须保证在调用其他接口前先登入;
            //2.调用 QISRSessionBegin(...)开始一次语音听写;
            //3.循环调用 QISRAudioWrite(...) 分块写入音频数据
            //4.循环调用 QISRGetResult(...) 接口返回听写结果
            //5.调用 QISRSessionEnd(...) 主动结束本次听写
            //6.不再使用服务的时候 调用MSPLogout()登出,避免不必要的麻烦。

            try
            {
                //识别结果
                string text = String.Empty;
                //登录参数,自己注册后获取的appid
                string login_configs = "appid=5983daf6";
                //语音路径
                var voicePath = AppDomain.CurrentDomain.BaseDirectory + filename;
                //检测文件是否存在
                if (!File.Exists(voicePath))
                {
                    MessageBox.Show("文件" + voicePath + "不存在!");
                }
                //读取音频文件
                using (FileStream fileStream = new FileStream(voicePath, FileMode.OpenOrCreate))
                {
                    IntPtr intPtr = Marshal.AllocHGlobal(BUFFER_NUM);
                    byte[] array  = new byte[BUFFER_NUM];


                    LoadingHelper.ShowLoading("加载中", this, o =>
                    {
                        AudioStatus audioStatus = AudioStatus.MSP_AUDIO_SAMPLE_CONTINUE;
                        EpStatus epStatus       = EpStatus.MSP_EP_LOOKING_FOR_READY;
                        RsltStatus recogStatus  = RsltStatus.MSP_REC_STATUS_FOR_READY;

                        //MSPLogin(...)接口登入
                        this.BeginInvoke(updateStatus, "登录接口");
                        ret = IFYDll.MSPLogin("*****@*****.**", "jkljlk123", login_configs);
                        if (ret != (int)ErrorCode.MSP_SUCCESS)
                        {
                            return;
                        }

                        //QISRSessionBegin 开始一次语音识别
                        this.BeginInvoke(updateStatus, "开始一次语音识别");
                        string _params = $"domain=iat,sub=iat,aue=speex-wb;7,sample_rate=16000,result_type=plain";
                        session_ID     = IFYDll.QISRSessionBegin(null, _params, ref ret);
                        if (ret != (int)ErrorCode.MSP_SUCCESS)
                        {
                            return;
                        }

                        //QISRAudioWrite 写入本次识别的音频。
                        this.BeginInvoke(updateStatus, "分块写入识别的音频");
                        while (fileStream.Position != fileStream.Length)
                        {
                            int waveLen = fileStream.Read(array, 0, BUFFER_NUM);
                            Marshal.Copy(array, 0, intPtr, array.Length);
                            ret = IFYDll.QISRAudioWrite(Ptr2Str(session_ID), intPtr, (uint)waveLen, audioStatus, ref epStatus, ref recogStatus);
                            if (ret != 0)
                            {
                                fileStream.Close();
                                throw new Exception("QISRAudioWrite err,errCode=" + ret);
                            }
                            Thread.Sleep(500);
                        }

                        this.BeginInvoke(updateStatus, "写入最后一块音频");
                        audioStatus = AudioStatus.MSP_AUDIO_SAMPLE_LAST;
                        ret         = IFYDll.QISRAudioWrite(Ptr2Str(session_ID), intPtr, 1u, audioStatus, ref epStatus, ref recogStatus);
                        if (ret != 0)
                        {
                            throw new Exception("QISRAudioWrite write last audio err,errCode=" + ret);
                        }

                        //QISRGetResult 读取识别结果
                        this.BeginInvoke(updateStatus, "读取识别结果");
                        while (true)
                        {
                            IntPtr intPtr2 = IFYDll.QISRGetResult(Ptr2Str(session_ID), ref recogStatus, waitTime, ref ret);
                            if (intPtr2 != IntPtr.Zero)
                            {
                                text += Ptr2Str(intPtr2);
                            }
                            if (ret != 0)
                            {
                                break;
                            }
                            Thread.Sleep(500);
                            if (recogStatus == RsltStatus.MSP_REC_STATUS_COMPLETE)
                            {
                                break;
                            }
                        }
                    });
                    txtLocalVoiceRecognitionResult.Text = text;
                }
            }
            catch (Exception ex)
            {
                txtLocalVoiceRecognitionResult.Text = ex.Message;
            }
            finally
            {
                //主动结束本次语音识别
                this.BeginInvoke(updateStatus, "结束本次识别结果");
                ret = IFYDll.QISRSessionEnd(Ptr2Str(session_ID), "");

                //退出登录
                this.BeginInvoke(updateStatus, "退出登录");
                ret = IFYDll.MSPLogout();
            }
            Thread.Sleep(3000);
            this.BeginInvoke(updateStatus, "就绪");
        }
예제 #11
0
파일: fmIFY.cs 프로젝트: mxq00812314/MXQ
        /// <summary>
        /// 进行语音识别
        /// </summary>
        /// <param name="inFile"></param>
        /// <returns></returns>
        public string AudioToString(string inFile)
        {
            int        ret        = 0;
            string     text       = String.Empty;
            FileStream fileStream = new FileStream(inFile, FileMode.OpenOrCreate);

            byte[]      array       = new byte[BUFFER_NUM];
            IntPtr      intPtr      = Marshal.AllocHGlobal(BUFFER_NUM);
            AudioStatus audioStatus = AudioStatus.MSP_AUDIO_SAMPLE_CONTINUE;
            EpStatus    epStatus    = EpStatus.MSP_EP_LOOKING_FOR_READY;
            RsltStatus  recogStatus = RsltStatus.MSP_REC_STATUS_FOR_READY;
            RsltStatus  rsltStatus  = RsltStatus.MSP_REC_STATUS_FOR_READY;

            while (fileStream.Position != fileStream.Length)
            {
                int waveLen = fileStream.Read(array, 0, BUFFER_NUM);
                Marshal.Copy(array, 0, intPtr, array.Length);
                ret = IFYDll.QISRAudioWrite(Ptr2Str(session_ID), intPtr, (uint)waveLen, audioStatus, ref epStatus, ref recogStatus);
                if (ret != 0)
                {
                    fileStream.Close();
                    throw new Exception("QISRAudioWrite err,errCode=" + ret);
                }
                if (recogStatus == 0)
                {
                    IntPtr intPtr2 = IFYDll.QISRGetResult(Ptr2Str(session_ID), ref rsltStatus, 0, ref ret);
                    if (intPtr2 != IntPtr.Zero)
                    {
                        text += Ptr2Str(intPtr2);
                    }
                }
                Thread.Sleep(500);
            }
            fileStream.Close();
            audioStatus = AudioStatus.MSP_AUDIO_SAMPLE_LAST;
            ret         = IFYDll.QISRAudioWrite(Ptr2Str(session_ID), intPtr, 1u, audioStatus, ref epStatus, ref recogStatus);
            if (ret != 0)
            {
                throw new Exception("QISRAudioWrite write last audio err,errCode=" + ret);
            }
            int timesCount = 0;

            while (true)
            {
                IntPtr intPtr2 = IFYDll.QISRGetResult(Ptr2Str(session_ID), ref rsltStatus, 0, ref ret);
                if (intPtr2 != IntPtr.Zero)
                {
                    text += Ptr2Str(intPtr2);
                }
                if (ret != 0)
                {
                    break;
                }
                Thread.Sleep(200);
                if (rsltStatus == RsltStatus.MSP_REC_STATUS_COMPLETE || timesCount++ >= 50)
                {
                    break;
                }
            }
            return(text);
        }
예제 #12
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
            }
        }
예제 #13
0
 public static extern int QISEAudioWrite(string sessionID, IntPtr waveData, ref uint waveLen, ref AudioStatus audioStatus, ref EpStatus epStatus, ref int Status);
예제 #14
0
 public static extern int QISRAudioWrite(string sessionID, IntPtr waveData, uint waveLen, AudioStatus audioStatus, ref EpStatus epStatus, ref RecogStatus recogStatus);
예제 #15
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));
        }
예제 #16
0
        /// <summary>
        /// 更新收视进度
        /// </summary>
        /// <param name="client"></param>
        /// <param name="epId">章节 ID</param>
        /// <param name="status">收视类型</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static Task <BangumiResult> UpdateEpStatusAsync(this IBangumiClient client, int epId, EpStatus status, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            var url = $"/ep/{epId}/status/{status.GetValue()}";

            return(client.GetAsync <BangumiResult>(url, cancellationToken));
        }
예제 #17
0
        public void RunIat(string audioFile, string param)
        {
            string     sessionID  = null;
            EpStatus   epStatus   = EpStatus.MSP_EP_LOOKING_FOR_SPEECH;
            RsltStatus rsltStatus = RsltStatus.MSP_REC_STATUS_SUCCESS;
            long       count      = 0; //计次?
            uint       totalLen   = 0;

            //获取音频文件
            using (FileStream fs = new FileStream(audioFile, FileMode.Open, FileAccess.Read))
            {
                fs.Seek(0, SeekOrigin.End);
                var size = fs.Length;
                fs.Seek(0, SeekOrigin.Begin);
                var data = new byte[size];
                if (data == null)
                {
                    if (onErrorEvent != null)
                    {
                        onErrorEvent.Invoke("内存不足.");
                    }
                    return;
                }
                var readSize = fs.Read(data, 1, (int)size);
                if (readSize != size)
                {
                    onErrorEvent.Invoke("读" + audioFile + "出错");
                    return;
                }

                int errcode = 0;
                //开始语音听写
                sessionID = Marshal.PtrToStringAuto(MSCDLL.QISRSessionBegin(null, param, ref errcode));
                if (errcode != (int)ErrorCode.MSP_SUCCESS)
                {
                    if (onErrorEvent != null)
                    {
                        onErrorEvent.Invoke("Iat开启出错" + errcode);
                    }
                    return;
                }
                while (true)
                {
                    uint len = 10 * 640;
                    int  ret = 0;
                    if (size < 2 * len)
                    {
                        len = (uint)size;
                    }
                    if (len <= 0)
                    {
                        break;
                    }
                    var audioStatus = AudioStatus.MSP_AUDIO_SAMPLE_CONTINUE;
                    if (count == 0)
                    {
                        audioStatus = AudioStatus.MSP_AUDIO_SAMPLE_FIRST;
                    }
                    ret = MSCDLL.QISRAudioWrite(sessionID, data, len, audioStatus, ref epStatus, ref rsltStatus);
                    if (ret != (int)ErrorCode.MSP_SUCCESS)
                    {
                        if (onErrorEvent != null)
                        {
                            onErrorEvent.Invoke("写入本次识别的音频失败." + ret);
                        }
                        return;
                    }
                    count += len;
                    size  -= len;
                    //已经有部分听写结果
                    if (rsltStatus == RsltStatus.MSP_REC_STATUS_SUCCESS)
                    {
                        var result = Marshal.PtrToStringAuto(MSCDLL.QISRGetResult(sessionID, ref rsltStatus, 0, ref ret));
                        if (ret != 0)
                        {
                            if (onErrorEvent != null)
                            {
                                onErrorEvent.Invoke("获取识别结果失败." + ret);
                            }
                            return;
                        }
                        if (result != null)
                        {
                            uint resultLen = (uint)result.Length;
                            totalLen += resultLen;
                            if (totalLen >= 4096)
                            {
                                if (onErrorEvent != null)
                                {
                                    onErrorEvent.Invoke("对于临时资源没有足够的缓存空间。");
                                }
                                return;
                            }
                        }
                    }
                    if (epStatus == EpStatus.MSP_EP_AFTER_SPEECH)
                    {
                        break;
                    }
                    Thread.Sleep(200);
                }
                errcode = MSCDLL.QISRAudioWrite(sessionID, null, 0, AudioStatus.MSP_AUDIO_SAMPLE_LAST, ref epStatus, ref rsltStatus);
                if (errcode != 0)
                {
                    if (onErrorEvent != null)
                    {
                        onErrorEvent.Invoke("音频写入失败。" + errcode);
                    }
                    return;
                }
            }
        }
예제 #18
0
 public static extern int QISRAudioWrite(IntPtr sessionID, byte[] waveData, uint waveLen, AudioStatus audioStatus, ref EpStatus epStatus, ref RecogStatus recogStatus);
예제 #19
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));
        }