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;
        }

 

你可能感兴趣的:(text)