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).gifSpeechSynthesizerCreates a new instance of SpeechSynthesizer.
页首
  名称说明
ms596247.pubmethod(zh-cn,VS.90).gifAddLexicon
ms596247.pubmethod(zh-cn,VS.90).gifDispose
ms596247.pubmethod(zh-cn,VS.90).gifEquals确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)
ms596247.protmethod(zh-cn,VS.90).gifFinalize允许 Object 在“垃圾回收”回收 Object 之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
ms596247.pubmethod(zh-cn,VS.90).gifGetCurrentlySpokenPrompt
ms596247.pubmethod(zh-cn,VS.90).gifGetHashCode用作特定类型的哈希函数。 (继承自 Object。)
ms596247.pubmethod(zh-cn,VS.90).gifGetInstalledVoices已重载。 Returns the collection of installed TTS voices.
ms596247.pubmethod(zh-cn,VS.90).gifGetType获取当前实例的 Type。 (继承自 Object。)
ms596247.protmethod(zh-cn,VS.90).gifMemberwiseClone创建当前 Object 的浅表副本。 (继承自 Object。)
ms596247.pubmethod(zh-cn,VS.90).gifPausePauses the synthesizer.
ms596247.pubmethod(zh-cn,VS.90).gifRemoveLexicon
ms596247.pubmethod(zh-cn,VS.90).gifResume
ms596247.pubmethod(zh-cn,VS.90).gifSelectVoiceSelects a specific voice.
ms596247.pubmethod(zh-cn,VS.90).gifSelectVoiceByHints已重载。 Selects a voice with specific voice characteristics.
ms596247.pubmethod(zh-cn,VS.90).gifSetOutputToAudioStream
ms596247.pubmethod(zh-cn,VS.90).gifSetOutputToDefaultAudioDevice
ms596247.pubmethod(zh-cn,VS.90).gifSetOutputToNull
ms596247.pubmethod(zh-cn,VS.90).gifSetOutputToWaveFile已重载。
ms596247.pubmethod(zh-cn,VS.90).gifSetOutputToWaveStream
ms596247.pubmethod(zh-cn,VS.90).gifSpeak已重载。 Speaks a prompt.
ms596247.pubmethod(zh-cn,VS.90).gifSpeakAsync已重载。 Speaks asynchronously.
ms596247.pubmethod(zh-cn,VS.90).gifSpeakAsyncCancelCancels asynchronous speaking of the specified prompt.
ms596247.pubmethod(zh-cn,VS.90).gifSpeakAsyncCancelAllCancels asynchronous speaking of all queued prompts.
ms596247.pubmethod(zh-cn,VS.90).gifSpeakSsmlSpeaks the specified SSML string.
ms596247.pubmethod(zh-cn,VS.90).gifSpeakSsmlAsyncSpeaks the specified text string asynchronously.
ms596247.pubmethod(zh-cn,VS.90).gifToString返回表示当前 ObjectString。 (继承自 Object。)
页首
  名称说明
ms596247.pubproperty(zh-cn,VS.90).gifRateGets the speaking rate of the SpeechSynthesizer.
ms596247.pubproperty(zh-cn,VS.90).gifStateGets the speaking state of the SpeechSynthesizer.
ms596247.pubproperty(zh-cn,VS.90).gifVoiceGets the voice of the SpeechSynthesizer.
ms596247.pubproperty(zh-cn,VS.90).gifVolumeGets the speaking volume of the SpeechSynthesizer.
页首
  名称说明
ms596247.pubevent(zh-cn,VS.90).gifBookmarkReachedRaised when a bookmark is reached.
ms596247.pubevent(zh-cn,VS.90).gifPhonemeReachedRaised when a phoneme is reached.
ms596247.pubevent(zh-cn,VS.90).gifSpeakCompletedRaised when the SpeechSynthesizer completes the speaking of a prompt.
ms596247.pubevent(zh-cn,VS.90).gifSpeakProgress
ms596247.pubevent(zh-cn,VS.90).gifSpeakStartedRaised when the SpeechSynthesizer begins the speaking of a prompt.
ms596247.pubevent(zh-cn,VS.90).gifStateChangedRaised when the state of the SpeechSynthesizer changes.
ms596247.pubevent(zh-cn,VS.90).gifVisemeReachedRaised when a viseme is reached.
ms596247.pubevent(zh-cn,VS.90).gifVoiceChangeRaised 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://my.oschina.net/u/1446855/blog/643504

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值