程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#中挪用SAPI完成語音辨認的2種辦法

C#中挪用SAPI完成語音辨認的2種辦法

編輯:C#入門知識

C#中挪用SAPI完成語音辨認的2種辦法。本站提示廣大學習愛好者:(C#中挪用SAPI完成語音辨認的2種辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是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("創立語音辨認掉敗");
      }
    }
  }
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved