Ejemplo n.º 1
0
        /// <summary>
        /// 构造函数,初始化引擎,开Session
        /// </summary>
        /// <param name="c1">初始化引擎的参数</param>
        /// <param name="c2">开session的参数</param>
        public FlyIsr(string c1, string c2)
        {
            var ret = 0;

            //引擎初始化,只需初始化一次
            ret = AsrDll.MSPLogin(null, null, c1);
            if (ret != 0)
            {
                throw new Exception("QISP初始化失败,错误代码:" + ((ErrorCode)ret).ToString("G"));
            }
            //第二个参数为传递的参数,使用会话模式,使用speex编解码,使用16k16bit的音频数据
            //第三个参数为返回码
            var param = c2;

            _sessID = FlyTts.Ptr2Str(AsrDll.QISRSessionBegin(string.Empty, param, ref ret));
            if (ret != 0)
            {
                throw new Exception("QISRSessionBegin失败,错误代码:" + ((ErrorCode)ret).ToString("G"));
            }
        }
Ejemplo n.º 2
0
        private void wis_DataAvailable(object sender, WaveInEventArgs e)
        {
            Console.WriteLine(DateTime.Now + ":" + DateTime.Now.Millisecond);

            var ret        = 0;
            var bp         = Marshal.AllocHGlobal(e.BytesRecorded);
            var status     = AudioStatus.IsrAudioSampleContinue;
            var epStatus   = EpStatus.IsrEpNull;
            var recStatus  = RecogStatus.IsrRecNull;
            var rsltStatus = RecogStatus.IsrRecNull;

            //ep_status        端点检测(End-point detected)器所处的状态
            //rec_status       识别器所处的状态
            //rslt_status      识别器所处的状态
            Marshal.Copy(e.Buffer, 0, bp, e.BytesRecorded);
            //开始向服务器发送音频数据
            ret = AsrDll.QISRAudioWrite(_sessID, bp, (uint)e.BytesRecorded, status, ref epStatus, ref recStatus);
            if (ret != 0)
            {
                throw new Exception("QISRAudioWrite err,errCode=" + ((ErrorCode)ret).ToString("G"));
            }

            //服务器返回部分结果
            if (recStatus == RecogStatus.IsrRecStatusSuccess)
            {
                var p = AsrDll.QISRGetResult(_sessID, ref rsltStatus, 0, ref ret);
                if (p != IntPtr.Zero)
                {
                    var tmp = FlyTts.Ptr2Str(p);
                    DataArrived?.Invoke(this, new DataArrivedEventArgs(tmp)); //激发识别数据到达事件
                    Console.WriteLine(@"服务器返回部分结果!-->" + tmp);
                }
            }
            Thread.Sleep(500);
            Marshal.FreeHGlobal(bp);
            //System.Console.WriteLine(System.DateTime.Now + ":" + System.DateTime.Now.Millisecond);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 进行声音识别
        /// </summary>
        /// <param name="inFile">音频文件,pcm无文件头,采样率16k,数据16位,单声道</param>
        /// <param name="outFile">输出识别结果到文件</param>
        public void Audio2Txt(string inFile, string outFile = null)
        {
            var ret    = 0;
            var result = "";

            try
            {
                //模拟录音,输入音频
                if (!File.Exists(inFile))
                {
                    throw new Exception("文件" + inFile + "不存在!");
                }
                if (inFile.Substring(inFile.Length - 3, 3).ToUpper() != "WAV" && inFile.Substring(inFile.Length - 3, 3).ToUpper() != "PCM")
                {
                    throw new Exception("音频文件格式不对!");
                }
                var fp = new FileStream(inFile, FileMode.Open);
                if (inFile.Substring(inFile.Length - 3, 3).ToUpper() == "WAV")
                {
                    fp.Position = 44;
                }
                var buff = new byte[BufferNum];
                var bp   = Marshal.AllocHGlobal(BufferNum);
                int len;
                var status     = AudioStatus.IsrAudioSampleContinue;
                var epStatus   = EpStatus.IsrEpNull;
                var recStatus  = RecogStatus.IsrRecNull;
                var rsltStatus = RecogStatus.IsrRecNull;
                //ep_status        端点检测(End-point detected)器所处的状态
                //rec_status       识别器所处的状态
                //rslt_status      识别器所处的状态
                while (fp.Position != fp.Length)
                {
                    len = fp.Read(buff, 0, BufferNum);
                    Marshal.Copy(buff, 0, bp, buff.Length);
                    //开始向服务器发送音频数据
                    ret = AsrDll.QISRAudioWrite(_sessID, bp, (uint)len, status, ref epStatus, ref recStatus);
                    if (ret != 0)
                    {
                        fp.Close();
                        throw new Exception("QISRAudioWrite err,errCode=" + ((ErrorCode)ret).ToString("G"));
                    }
                    //服务器返回部分结果
                    if (recStatus == RecogStatus.IsrRecStatusSuccess)
                    {
                        var p = AsrDll.QISRGetResult(_sessID, ref rsltStatus, WaitTime, ref ret);
                        if (p != IntPtr.Zero)
                        {
                            var tmp = FlyTts.Ptr2Str(p);
                            DataArrived?.Invoke(this, new DataArrivedEventArgs(tmp));
                            result += tmp;
                            Console.WriteLine(@"返回部分结果!:" + tmp);
                        }
                    }
                    Thread.Sleep(500);
                }
                fp.Close();

                //最后一块数据
                status = AudioStatus.IsrAudioSampleLast;

                ret = AsrDll.QISRAudioWrite(_sessID, bp, 1, status, ref epStatus, ref recStatus);
                if (ret != 0)
                {
                    throw new Exception("QISRAudioWrite write last audio err,errCode=" + ((ErrorCode)ret).ToString("G"));
                }
                Marshal.FreeHGlobal(bp);
                var loopCount = 0;
                //最后一块数据发完之后,循环从服务器端获取结果
                //考虑到网络环境不好的情况下,需要对循环次数作限定
                do
                {
                    var p = AsrDll.QISRGetResult(_sessID, ref rsltStatus, WaitTime, ref ret);
                    if (p != IntPtr.Zero)
                    {
                        var tmp = FlyTts.Ptr2Str(p);
                        DataArrived?.Invoke(this, new DataArrivedEventArgs(tmp)); //激发识别数据到达事件
                        result += tmp;
                        Console.WriteLine(@"传完音频后返回结果!:" + tmp);
                    }
                    if (ret != 0)
                    {
                        throw new Exception("QISRGetResult err,errCode=" + ((ErrorCode)ret).ToString("G"));
                    }
                    Thread.Sleep(200);
                } while (rsltStatus != RecogStatus.IsrRecStatusSpeechComplete && loopCount++ < 30);
                if (outFile != null)
                {
                    var fout = new FileStream(outFile, FileMode.OpenOrCreate);
                    fout.Write(Encoding.Default.GetBytes(result), 0, Encoding.Default.GetByteCount(result));
                    fout.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                ret = AsrDll.QISRSessionEnd(_sessID, string.Empty);
                ret = AsrDll.MSPLogout();
                IsrEnd?.Invoke(this, new EventArgs()); //通知识别结束
            }
        }