在 C# 中,语音识别,语音到文本,文本,语音和语音合成

分享于 

11分钟阅读

多媒体

  繁體

Contents的表

免责声明

根据MSDN,SpeechRecognitionEngine类在.NET 4.5、4、3.5、3.0和.NET中可用。 NET 4客户端配置文件,以及受支持的Windows版本是:

  • Windows 8
  • Windows Server 2012
  • Windows 7
  • Windows Vista SP2
  • Windows Server 2008 (不支持服务器核心角色)
  • Windows Server 2008 R2 (服务器核心角色支持SP1或更高版本;不支持Itanium )
  • Windows Vista SP1或更高版本
  • Windows Server 2008 (不支持服务器核心)
  • Windows Server 2008 R2 (SP1或更高版本支持的服务器核心)
  • Windows Server 2003 SP2
  • Windows XP SP2
  • Windows Server 2008 R2
  • Windows Server 2008
  • Windows Server 2003
  • Windows 98,Windows Server 2000 SP4
  • Windows CE
  • Windows Millennium Edition
  • 用于Pocket PC的Windows Mobile
  • 适用于智能手机的Windows Mobile
  • Windows XP Media Center版
  • Windows XP Professional x64版
  • Windows XP SP2
  • Windows XP入门版

在本文中,我将介绍如何使用C#的System.Speech库编程语音识别,语音到文本,文本到语音和语音合成。

C#中的语音识别

语音识别

要在C#中创建有语音识别的程序,你需要添加System.Speech库,然后,将此using namespace语句添加到代码文件的顶部:

using System.Speech.Recognition;using System.Speech.Synthesis;using System.Threading;

然后,创建SpeechRecognitionEngine的一个实例:

SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();

然后,我们需要将语法加载到SpeechRecognitionEngine,如果你不这样做的话,语音识别器不会识别短语,例如,添加带有短语"test"的语法,我们给该语法命名为"testGrammar":

_recognizer.LoadGrammar(new Grammar(new GrammarBuilder("test")) { Name = "testGrammar" }); // load a grammar"test"

或者:

Grammar gr = new Grammar(new GrammarBuilder("test"));
gr.Name = "testGrammar";
_recognizer.LoadGrammar(gr);

如果不想给语法命名,请执行以下操作:

_recognizer.LoadGrammar(new Grammar(new GrammarBuilder("test"))); // load a"test" grammar

只有在希望卸载程序中的语法时,才能添加名称,要异步加载文法,请使用方法LoadGrammarAsync,如果要在识别器运行时加载语法,请调用requestRecognizerUpdate方法[>]事件处理程序。

然后,添加该事件处理程序:

_recognizer.SpeechRecognized += _recognizer_SpeechRecognized;

如果语音识别,将调用方法_recognizer_SpeechRecognized,我们需要创建方法,当程序识别出短语"test"时,输出"The test was successful"。为此,请使用以下命令:

void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
 if (e.Result.Text == "test") // e.Result.Text contains the recognized text {
 Console.WriteLine("The test was successful!");
 } 
} 

正如你在注释行中看到的,e.Result.Text包含识别的文本,如果你有更多的语法,这很有用,但是,语音识别器没有启动。为此,请在_recognizer.SpeechRecognized += _recognizer_SpeechRecognized

_recognizer.SetInputToDefaultAudioDevice(); // set the input of the speech recognizer to the default audio device_recognizer.RecognizeAsync(RecognizeMode.Multiple); // recognize speech asynchronous

现在,如果我们合并所有方法,我们得到:

staticvoid Main(string[] args)
{
 SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
 _recognizer.LoadGrammar(new Grammar(new GrammarBuilder("test")) Name = { "testGrammar" }); // load a grammar _recognizer.SpeechRecognized += _recognizer_SpeechRecognized; 
 _recognizer.SetInputToDefaultAudioDevice(); // set the input of the speech recognizer to the default audio device _recognizer.RecognizeAsync(RecognizeMode.Multiple); // recognize speech asynchronous} void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
 if (e.Result.Text == "test") // e.Result.Text contains the recognized text {
 Console.WriteLine("The test was successful!");
 } 
} 

由于存在两个线程:主线程和语音识别线程,我们可以暂停主线程,直到语音识别线程未完成为止,语音识别完成后,我们将配置语音识别引擎(最坏的情况可能需要3秒钟的时间,最多50毫秒):

static ManualResetEvent _completed = null;staticvoid Main(string[] args)
{
 _completed = new ManualResetEvent(false);
 SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
 _recognizer.LoadGrammar(new Grammar(new GrammarBuilder("test")) Name = { "testGrammar" }); // load a grammar _recognizer.LoadGrammar(new Grammar(new GrammarBuilder("exit")) Name = { "exitGrammar" }); // load a"exit" grammar _recognizer.SpeechRecognized += _recognizer_SpeechRecognized; 
 _recognizer.SetInputToDefaultAudioDevice(); // set the input of the speech recognizer to the default audio device _recognizer.RecognizeAsync(RecognizeMode.Multiple); // recognize speech asynchronous _completed.WaitOne(); // wait until speech recognition is completed _recognizer.Dispose(); // dispose the speech recognition engine} void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
 if (e.Result.Text == "test") // e.Result.Text contains the recognized text {
 Console.WriteLine("The test was successful!");
 } 
 elseif (e.Result.Text == "exit")
 {
 _completed.Set();
 }
}

如果正在编写Windows应用程序,则不需要创建ManualResetEvent,因为仅当用户关闭窗体时,UI线程才会结束。

要卸载文法,请使用语音识别引擎中的UnloadGrammar方法,并使用UnloadAllGrammars方法卸载所有文法,不要忘记调用RequestRecognizerUpdate方法,如果识别器正在运行,则在RecognizerUpdateReached事件处理程序中加载语法,例如,卸载"test"语法:

foreach (Grammar gr in _recognizer.Grammars)
{
 if (gr.Name == "testGrammar")
 {
 _recognizer.UnloadGrammar(gr);
 break;
 }
} 
  • 创建文法,并加载如下所示的语法:
Grammar testGrammar = new Grammar(new GrammarBuilder("test"));
_recognizer.LoadGrammar(testGrammar); 
  • 然后,你可以像这样卸载语法:
  • _recognizer.UnloadGrammar(testGrammar);

如果使用第二种方法卸载语法,就必须确保所有访问修饰符都正确,第一种方法是最简单的方法,因为如果使用第一种方法,访问修饰符并不重要。

语音被拒绝

如果将SpeechRecognitionRejected事件处理程序添加到SpeechRecognitionEngine,就可以显示语音识别引擎找到的候选词组,首先,添加一个SpeechRecognitionRejected事件处理程序:

_recognizer.SpeechRecognitonRejected += _recognizer_SpeechRecognitionRejected; 

然后,创建_recognizer_SpeechRecognitionRejected 函数:

staticvoid _recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
{
 if (e.Result.Alternates.Count == 0)
 {
 Console.WriteLine("Speech rejected. No candidate phrases found.");
 return;
 }
 Console.WriteLine("Speech rejected. Did you mean:");
 foreach (RecognizedPhrase r in e.Result.Alternates)
 {
 Console.WriteLine("" + r.Text);
 }
}

如果语音识别被拒绝,此函数显示语音识别引擎找到的所有候选词组。

确保计算机与您讲话(文本到语音)

在同一个库中,有一个名称空间System.Speech.Synthesis,在该命名空间中,你将找到一个类SpeechSythesizer,在该类中有一个Speak方法,添加命名空间添加代码文件的顶部,然后尝试以下操作:

SpeechSynthesizer _synthesizer = new SpeechSynthesizer();
_synthesizer.Speak("Now the computer is speaking to you.");

如果您运行该代码,则计算机将显示: " now the computer is talking to you.",可以使用语音识别代码,但可以使用以下语法代替测试语法:

_recognizer.LoadGrammar(new Grammar(new GrammarBuilder("hello computer"))); // load a grammar

_recognizer_SpeechRecognizer方法中,添加以下内容:

void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
 if (e.Result.Text == "hello computer") // e.Result.Text contains the recognized text {
 SpeechSynthesizer synthesizer = new SpeechSynthesizer();
 synthesizer.Speak("hello user");
 synthesizer.Dispose(); // dispose the SpeechSynthesizer }
 _completed.Set();
} 

使用SpeechSynthesizer.Dispose释放SpeechSynthesizer。现在,如果你说"hello computer ",计算机响应"hello user"。。


文本  TEX  syn  Speech  Synth  Synthesis  
相关文章