码迷,mamicode.com
首页 > 其他好文 > 详细

一个基于c 3 0的开发基于2000/XP/2003下语音识别的通用类

时间:2019-01-23 13:55:29      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:with   XML   tps   http   stat   hand   基础   人工   fromfile   

废话少说看代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;
using System.Windows.Forms;
using System.Collections;


namespace yincheng01@163.com
{
    public class speechxp
    {


        private SpeechLib.SpSharedRecoContext objRecoContext;
        private SpeechLib.ISpeechRecoGrammar grammar;
        private SpeechLib.ISpeechGrammarRule rule = null;
        private SpeechLib.ISpeechGrammarRuleState state;
        private SpeechLib.ISpeechPhraseElement PhraseElem;
        public System.Windows.Forms.Control cDisplay;

        public speechxp(string FileName, Control cDisplay)
        {


       
        }

        private void initSAPI()
        {
            try
            {
                objRecoContext = new SpeechLib.SpSharedRecoContext();
                objRecoContext.AudioLevel += new _ISpeechRecoContextEvents_AudioLevelEventHandler(RecoContext_VUMeter);//设置声音事件的响应函数
                objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition);//设置成功识别事件的响应函数
                objRecoContext.EventInterests = SpeechLib.SpeechRecoEvents.SRERecognition | SpeechLib.SpeechRecoEvents.SREAudioLevel;//设置响应事件

                //create grammar interface with ID = 0
                grammar = objRecoContext.CreateGrammar(1);//语法的实例
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception /n" + ex.ToString(), "Error - initSAPI");
            }
        }
        private void SAPIGrammarFromFile(string FileName)
        {
            try
            {
              
                grammar.CmdLoadFromFile(FileName, SpeechLib.SpeechLoadOption.SLODynamic);
                grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSActive);

            }
            catch
            {
                MessageBox.Show("载入语法文件失败 " + FileName + "/n", "Error - SAPIGrammarFromFile");
            }
        }
        public void RecoContext_Recognition(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult e)
        {
     

            int i;
            ArrayList reco = new ArrayList();
            string phrase = e.PhraseInfo.GetText(0, -1, true);

            cDisplay.Text = e.PhraseInfo.GetText(0, -1, true);

            foreach (SpeechLib.ISpeechPhraseElement PhraseElem in e.PhraseInfo.Elements)
            {
               /// listBox1.Items.Add(PhraseElem.Pronunciation);添加语音指令

            }

            i = e.PhraseInfo.Elements.Count;
            for (int h = 0; h < i; h++)
            {
            }

 

        }

        public void RecoContext_VUMeter(int StreamNumber, object StreamPosition, int e)
        {
            int hg = e;
        }
    }
}

类的调用

 

 hhh = new SpRecognition(file);  定义好的语法XML文件
 hhh.BeginRec(textBox10);  关联到显示识别结果的控件
              

 


 

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

一个基于c 3 0的开发基于2000/XP/2003下语音识别的通用类

标签:with   XML   tps   http   stat   hand   基础   人工   fromfile   

原文地址:https://www.cnblogs.com/wicnwicnwh/p/10308385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!