• c#语音识别(wav to text)


    最近在搞一个语音识别的项目,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;
            }
  • 相关阅读:
    CF1109F Sasha and Algorithm of Silence's Sounds LCT、线段树
    Solution -「CF 757F」Team Rocket Rises Again
    Solution -「ZJOI2012」「洛谷 P2597」灾难
    Solution -「CF 156D」Clues
    「矩阵树定理」学习笔记
    Solution -「JSOI2008」「洛谷 P4208」最小生成树计数
    Solution -「SHOI2016」「洛谷 P4336」黑暗前的幻想乡
    Solution -「Code+#2」「洛谷 P4033」白金元首与独舞
    Solution -「HDU 5498」Tree
    呐~「多项式」全家桶
  • 原文地址:https://www.cnblogs.com/bfyx/p/2747970.html
Copyright © 2020-2023  润新知