WPF语音合成与语音识别

 windows vista 和windows 7语音识别功能相关简介:

    相信用过windows vista 和windows 7的人都知道或者了解过里面的语音识别功能。它可以使用声音命令来控制电脑,实现更方便的人机互动,还可以通过声音控制窗口、启动程序、在窗口之间切换,使用菜单和单击按钮等功能。利用声音让计算机听写文本,只要大声的朗读字词,就可以创建文本文档,也可在文档中进行修改或更正错误。但此项技术并不是很成熟,存在文本识别率不高,许多非微软的程序不支持Windows的语音命令等缺陷。

    那么在我们的WPF程序中,该如何利用此功能呢?(目前使用改技术意义不大,但是在将来,我相信会有很大的发展,在此,而且是新手,我只是以玩的心态做这些,希望大家不要笑 :)

    在语音合成和语音识别上,微软提供 Speech SDK 开发包,那么在我们的WPF程序中,我们怎么使用呢?

其实很简单,我们主要用到了

.NET Framework 类库中

在System.Speech.Synthesis命名空间下

程序集:  System.Speech(在 System.Speech.dll 中)

SpeechSynthesizer 类
  
此类中的成员包括如下:
 构造函数
  名称 说明
ms596247.pubmethod(zh-cn,VS.90).gif SpeechSynthesizer Creates a new instance of SpeechSynthesizer.
页首
  名称 说明
ms596247.pubmethod(zh-cn,VS.90).gif AddLexicon
ms596247.pubmethod(zh-cn,VS.90).gif Dispose
ms596247.pubmethod(zh-cn,VS.90).gif Equals 确定指定的 Object 是否等于当前的 Object(继承自 Object。)
ms596247.protmethod(zh-cn,VS.90).gif Finalize 允许 Object 在“垃圾回收”回收 Object 之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
ms596247.pubmethod(zh-cn,VS.90).gif GetCurrentlySpokenPrompt
ms596247.pubmethod(zh-cn,VS.90).gif GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
ms596247.pubmethod(zh-cn,VS.90).gif GetInstalledVoices 已重载。 Returns the collection of installed TTS voices.
ms596247.pubmethod(zh-cn,VS.90).gif GetType 获取当前实例的 Type(继承自 Object。)
ms596247.protmethod(zh-cn,VS.90).gif MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
ms596247.pubmethod(zh-cn,VS.90).gif Pause Pauses the synthesizer.
ms596247.pubmethod(zh-cn,VS.90).gif RemoveLexicon
ms596247.pubmethod(zh-cn,VS.90).gif Resume
ms596247.pubmethod(zh-cn,VS.90).gif SelectVoice Selects a specific voice.
ms596247.pubmethod(zh-cn,VS.90).gif SelectVoiceByHints 已重载。 Selects a voice with specific voice characteristics.
ms596247.pubmethod(zh-cn,VS.90).gif SetOutputToAudioStream
ms596247.pubmethod(zh-cn,VS.90).gif SetOutputToDefaultAudioDevice
ms596247.pubmethod(zh-cn,VS.90).gif SetOutputToNull
ms596247.pubmethod(zh-cn,VS.90).gif SetOutputToWaveFile 已重载。
ms596247.pubmethod(zh-cn,VS.90).gif SetOutputToWaveStream
ms596247.pubmethod(zh-cn,VS.90).gif Speak 已重载。 Speaks a prompt.
ms596247.pubmethod(zh-cn,VS.90).gif SpeakAsync 已重载。 Speaks asynchronously.
ms596247.pubmethod(zh-cn,VS.90).gif SpeakAsyncCancel Cancels asynchronous speaking of the specified prompt.
ms596247.pubmethod(zh-cn,VS.90).gif SpeakAsyncCancelAll Cancels asynchronous speaking of all queued prompts.
ms596247.pubmethod(zh-cn,VS.90).gif SpeakSsml Speaks the specified SSML string.
ms596247.pubmethod(zh-cn,VS.90).gif SpeakSsmlAsync Speaks the specified text string asynchronously.
ms596247.pubmethod(zh-cn,VS.90).gif ToString 返回表示当前 ObjectString(继承自 Object。)
页首
  名称 说明
ms596247.pubproperty(zh-cn,VS.90).gif Rate Gets the speaking rate of the SpeechSynthesizer.
ms596247.pubproperty(zh-cn,VS.90).gif State Gets the speaking state of the SpeechSynthesizer.
ms596247.pubproperty(zh-cn,VS.90).gif Voice Gets the voice of the SpeechSynthesizer.
ms596247.pubproperty(zh-cn,VS.90).gif Volume Gets the speaking volume of the SpeechSynthesizer.
页首
  名称 说明
ms596247.pubevent(zh-cn,VS.90).gif BookmarkReached Raised when a bookmark is reached.
ms596247.pubevent(zh-cn,VS.90).gif PhonemeReached Raised when a phoneme is reached.
ms596247.pubevent(zh-cn,VS.90).gif SpeakCompleted Raised when the SpeechSynthesizer completes the speaking of a prompt.
ms596247.pubevent(zh-cn,VS.90).gif SpeakProgress
ms596247.pubevent(zh-cn,VS.90).gif SpeakStarted Raised when the SpeechSynthesizer begins the speaking of a prompt.
ms596247.pubevent(zh-cn,VS.90).gif StateChanged Raised when the state of the SpeechSynthesizer changes.
ms596247.pubevent(zh-cn,VS.90).gif VisemeReached Raised when a viseme is reached.
ms596247.pubevent(zh-cn,VS.90).gif VoiceChange Raised when the voice of the SpeechSynthesizer changes.
 
 
    了解了以上相关知识,我们就开始来做今天的小程序啦,实现一个能根据你提问:“现在几点了?”,然后
计算机将获取当前时间,在界面上显示时间的同时,以语音报时。。。。实在是有点简单,嘿嘿。。
    首先新建一个WPF Application ;
    然后在通过设计视窗在界面上拖拽两个Lable控件和一个Button控件,结果就变成下面这个样子了:
怎么Button不见了?嘿嘿,被我给隐藏了,把Opcity属性设置了0,我们使用这个Button主要是为了让它获得焦点,
并且触发它的Click事件,但是,我们现在是懒得动手去按了,话说我们今天要说说话去命令它。然后触发这个事件,得到当前时间,并做出相关反应。
整个过程就这样简单。所以,为了产生神秘感,就把Button给隐藏了 :)
说了这么多,接下来看代码吧:
 
MainWindow.xaml
 1 <Window x:Class="MySpeach.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="Speech" Height="350" Width="525">
 5     <Grid>
 6         <Label Content="现在时间:" Height="32" HorizontalAlignment="Left" Margin="85,119,0,0" Name="label1" VerticalAlignment="Top" Width="81" />
 7         <Label Height="30" HorizontalAlignment="Left" Margin="198,119,0,0" Name="label2" VerticalAlignment="Top" Width="185" IsEnabled="False" Visibility="Visible" />
 8         <Button Content="现在现在几点了" Height="25" HorizontalAlignment="Left" Margin="198,65,0,0" Name="button1" VerticalAlignment="Top" Width="60" Opacity="0" Click="button1_Click"/>
 9     </Grid>
10 </Window>
 
MainWindow.xaml.cs
 1 namespace MySpeach
 2 {
 3     /// <summary>
 4     /// Interaction logic for MainWindow.xaml
 5     /// </summary>
 6     public partial class MainWindow : Window
 7     {
 8         public MainWindow()
 9         {
10             InitializeComponent();
11            
12             button1.Focus();              //按钮获取输入焦点
13             
14             
15 
16         }
17         public PromptBuilder BuildPB()                //建立并构建PromptBuilder 对象并返回此对象
18         {
19             PromptBuilder pb=new PromptBuilder();     
20             pb.StartVoice("大哥");                      //构建pb对象内容
21             pb.AppendText("主人现在是北京时间");
22             pb.AppendTextWithHint(DateTime.Now.ToString("HH:MM"),SayAs.Time24);
23             pb.AppendBreak(new TimeSpan(0,0,4));
24             pb.EndVoice();
25 
26 
27             return pb;
28         }
29         private void button1_Click(object sender, RoutedEventArgs e)
30         {
31             label2.Content ="现在是北京时间"+DateTime.Now.ToString("HH:MM")+"" ;
32             SpeechSynthesizer syn = new SpeechSynthesizer();
33             syn.SpeakAsync(BuildPB());                 //通过调用SpeechSynthesizer对象的SpeakAsync()方法,输出语音
34             button1.Focus();
35             
36         }
37     }

其实上面的程序也没什么可讲的,无非就是几个方法的调用,没什么技术含量,重要部分都已经注释。最后,要注意的是,别忘记了

对System.Speech.Synthesis;命名空间和相关程序集的引用。好了,就娱乐到这里,次程序在windows 7平台下使用vs2010编译通过,并能正常运行(只要你讲的普通话接近标准,嘿嘿:) 最后,在运行时别忘记了打开windows自带的语音识别程序,来进行聆听,不然,你喊死了计算机都不来鸟你 - -!

原文地址:https://www.cnblogs.com/vimsk/p/1853226.html