欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中调用SAPI实现语音识别的2种方法

程序员文章站 2023-11-06 21:32:52
通过微软的sapi,不仅仅可以实现语音合成tts,同样可以实现语音识别sr。下面我们就介绍并贴出相关代码。主要有两种方式: 1、使用com组件技术,不管是c++,c#,d...

通过微软的sapi,不仅仅可以实现语音合成tts,同样可以实现语音识别sr。下面我们就介绍并贴出相关代码。主要有两种方式:

1、使用com组件技术,不管是c++,c#,delphi都能玩的转,开发出来的东西在xp和win7都能跑。(注意要引入系统组件speechlib,xp要安装识别引擎)
2、使用win7的windows api,其实最终还是调用了sapi,所以开发出来的东西就只能在win7上面跑。

其实不管是哪一种,都是调用sapi,可能后一种代码比较简单。

使用第一种方式,需要注意在com选项卡里面的microsoft speech  object  library引用

public class sprecognition
  {
    private static sprecognition _instance = null;
    private speechlib.ispeechrecogrammar isrg;
    private speechlib.spsharedrecocontextclass ssrcontex = null;

    public delegate void stringevent(string str);
    public stringevent setmessage;

    private sprecognition()
    {
      ssrcontex = new spsharedrecocontextclass();
      isrg = ssrcontex.creategrammar(1);
      speechlib._ispeechrecocontextevents_recognitioneventhandler rechandle =
         new _ispeechrecocontextevents_recognitioneventhandler(contexrecognition);
      ssrcontex.recognition += rechandle;
    }
    public void beginrec()
    {
      isrg.dictationsetstate(speechrulestate.sgdsactive);
    }
    public static sprecognition instance()
    {
      if (_instance == null)
        _instance = new sprecognition();
      return _instance;
    }
    public void closerec()
    {
      isrg.dictationsetstate(speechrulestate.sgdsinactive);
    }
    private void contexrecognition(int iindex, object obj, speechlib.speechrecognitiontype type, speechlib.ispeechrecoresult result)
    {
      if (setmessage != null)
      {
        setmessage(result.phraseinfo.gettext(0, -1, true));
      }
    }
  }

第二种同样需要引入,不过引入的是win7中的.net3.5类库

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.speech;
using system.speech.recognition;
using system.globalization;
using system.windows.forms;

namespace studybeta
{
  public class srecognition
  {
    public speechrecognitionengine recognizer = null;//语音识别引擎
    public dictationgrammar dictationgrammar = null; //自然语法
    public system.windows.forms.control cdisplay; //显示控件

    public srecognition(string[] fg) //创建关键词语列表
    {
      cultureinfo myciintl = new cultureinfo("en-us");
      foreach (recognizerinfo config in speechrecognitionengine. installedrecognizers())//获取所有语音引擎
      {
    if (config.culture.equals(myciintl) && config.id == "ms-1033-80-desk" )
        {
          recognizer = new speechrecognitionengine(config);
          break;
        }//选择美国英语的识别引擎
      }
      if (recognizer != null)
      {
        initializespeechrecognitionengine(fg);//初始化语音识别引擎
        dictationgrammar = new dictationgrammar();
      }
      else
      {
        messagebox.show("创建语音识别失败");
      }
    }
    private void initializespeechrecognitionengine(string[] fg)
    {
      recognizer.setinputtodefaultaudiodevice();//选择默认的音频输入设备
      grammar customgrammar = createcustomgrammar(fg);
  //根据关键字数组建立语法
      recognizer.unloadallgrammars();
      recognizer.loadgrammar(customgrammar);
  //加载语法
recognizer.speechrecognized += new eventhandler <speechrecognizedeventargs>(recognizer_speechrecognized);
recognizer.speechhypothesized += new eventhandler <speechhypothesizedeventargs>(recognizer_speechhypothesized);
    }
    public void beginrec(control tbresult)//关联窗口控件
    {
      turnspeechrecognitionon();
      turndictationon();
      cdisplay = tbresult;
    }
    public void over()//停止语音识别引擎
    {
      turnspeechrecognitionoff();
    }
    public virtual grammar createcustomgrammar(string[] fg) //创造自定义语法
    {
      grammarbuilder grammarbuilder = new grammarbuilder();
      grammarbuilder.append(new choices(fg));
      return new grammar(grammarbuilder);
    }
    private void turnspeechrecognitionon()//启动语音识别函数
    {
      if (recognizer != null)
      {
        recognizer.recognizeasync(recognizemode.multiple); 
//识别模式为连续识别
      }
      else
      {
        messagebox.show("创建语音识别失败");
      }
    }
    private void turnspeechrecognitionoff()//关闭语音识别函数
    {
      if (recognizer != null)
      {
        recognizer.recognizeasyncstop();
        turndictationoff();
      }
      else
      {
        messagebox.show("创建语音识别失败");
      }
    }
private void recognizer_speechrecognized(object sender, speechrecognized eventargs e)
    {
  //识别出结果完成的动作,通常把识别结果传给某一个控件
      string text = e.result.text;
      cdisplay.text = text;
    }
    private void turndictationon()
    {
      if (recognizer != null)
      {
        recognizer.loadgrammar(dictationgrammar);
  //加载自然语法
      }
      else
      {
        messagebox.show("创建语音识别失败");
      }
    }
    private void turndictationoff()
    {
      if (dictationgrammar != null)
      {
        recognizer.unloadgrammar(dictationgrammar);
  //卸载自然语法
      }
      else
      {
        messagebox.show("创建语音识别失败");
      }
    }
  }
}