• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#语音识别(wavtotext)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

最近在搞一个语音识别的项目,wav to 文字,一开始写的代码在使用多线程的时候会出现识别超时的现象,不过后来终于google到解决方法。下面给出代码,不过遗憾的是资源释放不完全,希望给位给点建议。

public class SpRecognizer
    {
        private SpInProcRecoContext m_wavRecoContext;
        private ISpeechRecoGrammar m_Grammar;
        private SpFileStream m_infile;
        public CtiLine ctiLine;
        private string result = string.Empty;
        SpInprocRecognizer recognizer;

        public SpRecognizer()
        {
            try
            {
                recognizer = new SpInprocRecognizer();
                m_wavRecoContext = (SpInProcRecoContext)recognizer.CreateRecoContext();
                m_wavRecoContext.RetainedAudio = SpeechRetainedAudioOptions.SRAORetainAudio;
                m_infile = new SpFileStreamClass();
                m_infile.Format.GetWaveFormatEx();
            }
            catch (Exception ex)
            {
                SearchLog.Log.AppendLog("RecognizeWav", ex.ToString());
            }
        }

        public void OpenWAV()
        {
            try
            {
                m_Grammar = m_wavRecoContext.CreateGrammar(0);
                m_Grammar.DictationLoad("", SpeechLoadOption.SLOStatic);
                m_wavRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition);
                m_wavRecoContext.EndStream += new _ISpeechRecoContextEvents_EndStreamEventHandler(RecoContext_EndRecognition);
                m_infile.Open(ctiLine.MStation.RecorderFilePath, SpeechStreamFileMode.SSFMOpenForRead, false);
                m_wavRecoContext.Recognizer.AudioInputStream = m_infile;
                m_Grammar.DictationSetState(SpeechRuleState.SGDSActive);
            }
            catch (Exception ex)
            {
                SearchLog.Log.AppendLog("OpenWAV", ex.ToString());
            }
        }

        void RecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
        {
            if (m_infile != null)
            {
                result += Result.PhraseInfo.GetText(0, -1, true);
            }
        }

        void RecoContext_EndRecognition(int StreamNumber, object StreamPosition, bool f)
        {
            ctiLine.MStation.MobileIsStation = IsMobileStation();
            SearchLog.Log.AppendLog("(" + ctiLine.MStation.MobileNo + ")" + "Recognize Result", result);       
            m_wavRecoContext.Recognizer.AudioInputStream = null;
            m_Grammar.DictationUnload();
            ctiLine.IsRecognized = true;        
            m_infile.Close();        
            m_infile = null;
            m_wavRecoContext = null;
            m_Grammar = null;
            recognizer = null;
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#using实现强制资源清理发布时间:2022-07-10
下一篇:
c#sleep例子发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap