C#程序员干货系列之语音识别

说实话,这些年来从博客园收获了不少东西。自从当年注册以来就想平时分享点简单的小程序啥的。因为平时比较懒,突然发现近2年没更新了。准备陆续分享些小程序,这些也算是本猿手头上的一些自制小工具吧。

以后会陆续分享些WPF的自制按钮控件。

 

语音识别小程序,调用了windows的识别组件。精简了一些代码,算是比较简单易懂的一个语音识别类。

开发测试环境win7,VS2008。如果有其它环境中的,欢迎补充。

 

SRecognition.cs

  1 using System;
  2 using System.Speech.Recognition;
  3 using System.Globalization;
  4 using System.Windows.Forms;
  5 
  6 namespace NingTao
  7 {
  8   public class SRecognition
  9   {
 10     public SpeechRecognitionEngine recognizer = null;//语音识别引擎  
 11     public DictationGrammar dictationGrammar = null; //自然语法  
 12     public System.Windows.Forms.Control cDisplay; //显示控件  
 13 
 14     public SRecognition(string[] fg) //创建关键词语列表  
 15     {
 16       CultureInfo myCIintl = new CultureInfo("zh-CN");
 17       foreach (RecognizerInfo config in SpeechRecognitionEngine.InstalledRecognizers())//获取所有语音引擎  
 18       {
 19         if (config.Culture.Equals(myCIintl) && config.Id == "MS-2052-80-DESK")
 20         {
 21           recognizer = new SpeechRecognitionEngine(config);
 22           break;
 23         }//选择识别引擎
 24       }
 25       if (recognizer != null)
 26       {
 27         InitializeSpeechRecognitionEngine(fg);//初始化语音识别引擎  
 28         dictationGrammar = new DictationGrammar();
 29       }
 30       else
 31       {
 32         MessageBox.Show("创建语音识别失败");
 33       }
 34     }
 35     private void InitializeSpeechRecognitionEngine(string[] fg)
 36     {
 37       recognizer.SetInputToDefaultAudioDevice();//选择默认的音频输入设备  
 38       Grammar customGrammar = CreateCustomGrammar(fg);
 39       //根据关键字数组建立语法  
 40       recognizer.UnloadAllGrammars();
 41       recognizer.LoadGrammar(customGrammar);
 42       //加载语法  
 43       recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
 44       //recognizer.SpeechHypothesized += new EventHandler <SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);  
 45     }
 46     public void BeginRec(Control tbResult)//关联窗口控件  
 47     {
 48       TurnSpeechRecognitionOn();
 49       TurnDictationOn();
 50       cDisplay = tbResult;
 51     }
 52     public void over()//停止语音识别引擎  
 53     {
 54       TurnSpeechRecognitionOff();
 55     }
 56     public virtual Grammar CreateCustomGrammar(string[] fg) //创造自定义语法  
 57     {
 58       GrammarBuilder grammarBuilder = new GrammarBuilder();
 59       grammarBuilder.Append(new Choices(fg));
 60       return new Grammar(grammarBuilder);
 61     }
 62     private void TurnSpeechRecognitionOn()//启动语音识别函数  
 63     {
 64       if (recognizer != null)
 65       {
 66         recognizer.RecognizeAsync(RecognizeMode.Multiple);
 67         //识别模式为连续识别  
 68       }
 69       else
 70       {
 71         MessageBox.Show("创建语音识别失败");
 72       }
 73     }
 74     private void TurnSpeechRecognitionOff()//关闭语音识别函数  
 75     {
 76       if (recognizer != null)
 77       {
 78         recognizer.RecognizeAsyncStop();
 79         TurnDictationOff();
 80       }
 81       else
 82       {
 83         MessageBox.Show("创建语音识别失败");
 84       }
 85     }
 86     private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
 87     {
 88       //识别出结果完成的动作,通常把识别结果传给某一个控件  
 89       string text = e.Result.Text;
 90       cDisplay.Text += text;
 91     }
 92     private void TurnDictationOn()
 93     {
 94       if (recognizer != null)
 95       {
 96         recognizer.LoadGrammar(dictationGrammar);
 97         //加载自然语法  
 98       }
 99       else
100       {
101         MessageBox.Show("创建语音识别失败");
102       }
103     }
104     private void TurnDictationOff()
105     {
106       if (dictationGrammar != null)
107       {
108         recognizer.UnloadGrammar(dictationGrammar);
109         //卸载自然语法  
110       }
111       else
112       {
113         MessageBox.Show("创建语音识别失败");
114       }
115     }
116   }
117 }

 

form调用,其中2个按钮(开始,停止),1个文本框(识别结果)

using System;
using System.Windows.Forms;

namespace NingTao
{
  public partial class Form1 : Form
  {
    private SRecognition sr;

    public Form1()
    {
      InitializeComponent();

      string[] fg = { "东方", "西方", "南方", "北方" };
      sr = new SRecognition(fg);
      button2.Enabled = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      sr.BeginRec(textBox1);
      button1.Enabled = false;
      button2.Enabled = true;
    }

    private void button2_Click(object sender, EventArgs e)
    {
      sr.over();
      button1.Enabled = true;
      button2.Enabled = false;
    }
  }
}

 

然后就可以测试语音识别了。

 

下载地址

posted @ 2013-05-07 17:49  geeksnail  阅读(16815)  评论(13编辑  收藏  举报