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

 更新时间:2015年06月04日 17:42:59   投稿:junjie  
这篇文章主要介绍了C#中调用SAPI实现语音识别的2种方法,本文直接给出实现代码,需要的朋友可以参考下

通过微软的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("创建语音识别失败");
      }
    }
  }
}


相关文章

  • Unity实现简单手势识别

    Unity实现简单手势识别

    这篇文章主要为大家详细介绍了Unity实现简单手势识别,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • 基于C#编写一个接受图片流的OCR识别接口

    基于C#编写一个接受图片流的OCR识别接口

    这篇文章主要为大家详细介绍了如何使用C#写一个接受图片流的OCR识别接口,以及测试用例调用接口,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-03-03
  • 详解C#中线程传参,返回值和多线程冲突问题的解决

    详解C#中线程传参,返回值和多线程冲突问题的解决

    这篇文章主要为大家详细介绍了C#中线程传参,返回值和多线程冲突问题的解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2022-11-11
  • C#利用栈实现加减乘除运算

    C#利用栈实现加减乘除运算

    这篇文章主要介绍了C#利用栈实现加减乘除运算的实现方法,需要的朋友可以参考下
    2019-11-11
  • C#中4种深拷贝方法介绍

    C#中4种深拷贝方法介绍

    这篇文章主要介绍了C#中4种深拷贝方法介绍,本文讲解了利用反射实现、利用xml序列化和反序列化实现、利用二进制序列化和反序列化实现、利用silverlight DataContractSerializer实现,用于在silverlight 客户端使用等4种方法,需要的朋友可以参考下
    2015-06-06
  • C#实现的Socket服务器端、客户端代码分享

    C#实现的Socket服务器端、客户端代码分享

    这篇文章主要介绍了C#实现的Socket服务器端、客户端代码分享,2个非常简单的入门例子,需要的朋友可以参考下
    2014-08-08
  • C#自定义事件之属性改变引发事件示例

    C#自定义事件之属性改变引发事件示例

    这篇文章主要为大家详细介绍了C#自定义事件之属性改变引发事件示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • C# 递归算法详解

    C# 递归算法详解

    什么是递归函数/方法?任何一个方法既可以调用其他方法也可以调用自己,而当这个方法调用自己时,我们就叫它递归函数或递归算法,接下来详细介绍需要了解的朋友可以参考下
    2021-11-11
  • 聊聊C#中的Mixin的具体用法

    聊聊C#中的Mixin的具体用法

    本文主要介绍了C#中的Mixin的具体用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • 详解C#App.config和Web.config加密

    详解C#App.config和Web.config加密

    本篇文章给大家分享了C#App.config和Web.config加密的相关知识点以及具体代码步骤,有兴趣的朋友参考学习下。
    2018-05-05

最新评论