一个基于Windows Vista speech API5 3以及WPF技术的语音朗读代码

闲暇无事,利用window SDK 与vs2008,基于Windows Vista speech API5.3以及WPF技术开发了一套语音朗读的代码,

牛刀小试

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Collections.ObjectModel;
using System.Xml;

using System.Speech.Synthesis;

namespace speechSynth
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

public partial class Window1 : System.Windows.Window
    {
        private SpeechSynthesizer synth;

public Window1()
        {
            InitializeComponent();

synth = new SpeechSynthesizer();
            synth.StateChanged += new EventHandler<StateChangedEventArgs>(synth_StateChanged);
            synth.BookmarkReached += new EventHandler<BookmarkReachedEventArgs>(synth_BookmarkReached);
            synth.PhonemeReached += new EventHandler<PhonemeReachedEventArgs>(synth_PhonemeReached);
            synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synth_SpeakCompleted);
            synth.SpeakProgress += new EventHandler<SpeakProgressEventArgs>(synth_SpeakProgress);
            synth.SpeakStarted += new EventHandler<SpeakStartedEventArgs>(synth_SpeakStarted);
            synth.VisemeReached += new EventHandler<VisemeReachedEventArgs>(synth_VisemeReached);
            synth.VoiceChange += new EventHandler<VoiceChangeEventArgs>(synth_VoiceChange);

this.btnSpeakText.Click += new RoutedEventHandler(btnSpeakText_Click);
            this.btnWavFile.Click += new RoutedEventHandler(btnWavFile_Click);
            this.btnVoices.Click += new RoutedEventHandler(btnVoices_Click);
            this.btnSpeakSsml.Click += new RoutedEventHandler(btnSpeakSsml_Click);
            this.btnPause.Click += new RoutedEventHandler(btnPause_Click);
            this.btnResume.Click += new RoutedEventHandler(btnResume_Click);
            this.btnPromptBuilder.Click += new RoutedEventHandler(btnPromptBuilder_Click);
            this.btnToXml.Click += new RoutedEventHandler(btnToXml_Click);
            this.btnSsmlPitch.Click += new RoutedEventHandler(btnSsmlPitch_Click);
            this.btnCutIn.Click += new RoutedEventHandler(btnCutIn_Click);

this.sliderRate.ValueChanged += new RoutedPropertyChangedEventHandler<double>(sliderRate_ValueChanged);
            this.sliderVolume.ValueChanged += new RoutedPropertyChangedEventHandler<double>(sliderVolume_ValueChanged);
        }

void btnCutIn_Click(object sender, RoutedEventArgs e)
        {
            synth.SpeakAsyncCancelAll(); //stops current async call
            synth.SpeakAsync("this is some interrupting text"); //just appends, unless AsyncCancelAll() called first
            //synth.Speak("this is some interrupting text"); //does not interrupt async call
        }

void btnSsmlPitch_Click(object sender, RoutedEventArgs e)
        {
            XmlDocument xd = new XmlDocument();
            xd.Load("ssmlPitch.xml");           
            synth.SpeakSsmlAsync(xd.OuterXml);
        }

#region SYNTHESIZER_EVENTS
        void synth_VoiceChange(object sender, VoiceChangeEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("VoiceChange : " + e.Voice.Name);
        }

void synth_VisemeReached(object sender, VisemeReachedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("VisemeReached : " + e.Viseme.ToString());
        }

void synth_SpeakStarted(object sender, SpeakStartedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("SpeakStarted");
        }

void synth_SpeakProgress(object sender, SpeakProgressEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("SpeakProgress : " + e.AudioPosition.TotalSeconds.ToString());
        }

void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("SpeakCompleted");
        }

void synth_PhonemeReached(object sender, PhonemeReachedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("PhonemeReached : " + e.Phoneme.ToString());
        }

void synth_BookmarkReached(object sender, BookmarkReachedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("BookmarkReached : " + e.Bookmark);
        }

void synth_StateChanged(object sender, StateChangedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("StateChanged : " + e.State.ToString());
            lblState.Content = e.State.ToString();
        }
        #endregion

void btnToXml_Click(object sender, RoutedEventArgs e)
        {
            PromptBuilder myPrompt = GetBuiltPrompt();
            MessageBox.Show(myPrompt.ToXml());
        }

void btnPromptBuilder_Click(object sender, RoutedEventArgs e)
        {
            PromptBuilder pb = GetBuiltPrompt();

//Now let‘s get the synthesizer to render this message
            //SpeechSynthesizer synth = new SpeechSynthesizer();
            synth.SetOutputToDefaultAudioDevice();
            synth.SpeakAsync(pb);
        }

private PromptBuilder GetBuiltPrompt()
        {
            //from http://msdn.microsoft.com/msdnmag/issues/06/01/speechinWindowsVista/

//This prompt is quite complicated
            //So I‘m going to build it first, and then render it.
            PromptBuilder myPrompt = new PromptBuilder();

//Start the main speaking style
            PromptStyle mainStyle = new PromptStyle();
            mainStyle.Rate = PromptRate.Medium;
            mainStyle.Volume = PromptVolume.Loud;
            myPrompt.StartStyle(mainStyle);

//Alert the listener
            myPrompt.AppendAudio(new Uri("file://c://windows//media//notify.wav"), "Attention!");
            myPrompt.AppendText("Here are some important messages.");

//Here‘s the first important message
            myPrompt.AppendTextWithPronunciation("WinFX", "w?n?f?ks");
            myPrompt.AppendText("is a great platform.");

//And the second one
            myPrompt.AppendTextWithHint("ASP", SayAs.SpellOut);
            myPrompt.AppendText("is an acronym for Active Server Pages. Whereas an ASP is a snake.");

myPrompt.AppendBreak();

//Let‘s emphasise how important these messages are
            PromptStyle interimStyle = new PromptStyle();
            interimStyle.Emphasis = PromptEmphasis.Strong;
            myPrompt.StartStyle(interimStyle);
            myPrompt.AppendText("Please remember these two things.");
            myPrompt.EndStyle();

//Then we can revert to the main speaking style
            myPrompt.AppendBreak();
            myPrompt.AppendText("Thank you");

myPrompt.EndStyle();
            return myPrompt;
        }

void btnResume_Click(object sender, RoutedEventArgs e)
        {
            synth.Resume();
        }

void btnPause_Click(object sender, RoutedEventArgs e)
        {
            synth.Pause();
        }

void btnSpeakSsml_Click(object sender, RoutedEventArgs e)
        {
            //http://www.w3.org/TR/speech-synthesis/
            synth.SetOutputToDefaultAudioDevice();
           
            //XmlDocument xd = new XmlDocument();
            ////xd.Load("sampleSSML.xml"); //works
            //xd.Load("ssmlPitch.xml"); //works
            //synth.SpeakSsmlAsync(xd.OuterXml);

//XmlDocument xd = new XmlDocument();
            ////xd.Load("sampleSSML.xml"); //TODO doesn‘t work?
            //xd.Load("ssmlPitch.xml"); //TODO doesn‘t work?
            //PromptBuilder pb = new PromptBuilder();
            //pb.AppendSsmlMarkup(xd.DocumentElement.OuterXml);
            //synth.SpeakAsync(pb);

PromptBuilder pb = new PromptBuilder();
            pb.AppendText("blah");
            pb.AppendSsml("sampleSSML.xml"); //works
            //pb.AppendSsml("ssmlPitch.xml"); //TODO doesn‘t work
            synth.SpeakAsync(pb);
        }

void sliderVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            synth.Volume = (int)sliderVolume.Value;
        }

void sliderRate_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            synth.Rate = (int)sliderRate.Value;
        }

void btnVoices_Click(object sender, RoutedEventArgs e)
        {
            ReadOnlyCollection<InstalledVoice> voices = synth.GetInstalledVoices();
            string retVal = String.Empty;
            foreach (InstalledVoice iv in voices)
            {
                retVal += iv.VoiceInfo.Name + "/r/n";
            }
            MessageBox.Show(retVal);
        }

void btnWavFile_Click(object sender, RoutedEventArgs e)
        {
            synth.SetOutputToWaveFile("spoken.wav");
            synth.Speak(GetText());
            synth.SetOutputToNull();
            MessageBox.Show("done");
        }

void btnSpeakText_Click(object sender, RoutedEventArgs e)
        {
            synth.SetOutputToDefaultAudioDevice();
            synth.SpeakAsync(GetText());           
        }

private string GetText()
        {
            return txtToSpeak.Text.Trim();
        }

}
}

需要的留下Email,我给大家发

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

原文地址:https://www.cnblogs.com/wicnwicnwh/p/10308342.html

时间: 2024-10-02 20:38:18

一个基于Windows Vista speech API5 3以及WPF技术的语音朗读代码的相关文章

一个基于Windows Vista speech API5 3以及WPF技术的语音识别代码

本人小试牛刀,试验了一下用c#.net3.0 WPF技术开发了一个语音识别程序, windows.cs using System;using System.Collections.Generic;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input

基于Qt5.5.0的sql数据库、SDK_tts文本语音朗读的CET四六级单词背诵系统软件的编写V1.0

作者:小波 QQ:463431476 请关注我的博客园:http://www.cnblogs.com/xiaobo-Linux/  感觉我要火~~这比我是装定了!! 我的第二款软件:CET四六级单词背诵软件.基于QT5.5.0.sql数据库.以及TTS文本识别语音单词朗读的一款软件. 第一款软件的sql数据库软件的编写:http://www.cnblogs.com/xiaobo-Linux/p/4676950.html 现在来讲解我的第二款高大上的代码: 这个是.pro的代码,其中axconta

Wizard Framework:一个自己开发的基于Windows Forms的向导开发框架

最近因项目需要,我自己设计开发了一个基于Windows Forms的向导开发框架,目前我已经将其开源,并发布了一个NuGet安装包.比较囧的一件事是,当我发布了NuGet安装包以后,发现原来已经有一个.NET的向导开发框架了,它叫Microsoft Visual Studio 2013 Wizard Framework.我并没有对其进行深入研究,单从名称上看,该框架是否只能在Visual Studio 2013下使用?上网搜索过,也没发现微软有比较详细的官方资料介绍这个框架.不过无论如何,我还是

雨林木风 Windows Vista ULTIMATE With SP1 精简版 Y2.1

软件大小:2.66G   软件语言:简体中文   软件类别:国外软件/操作系统   运行环境:Win2003, WinXP, Win2000, NT, WinME,winvista,win7   授权方式:免费软件 软件评级: 雨林木风 Windows Vista ULTIMATE With SP1 精简版,基于 Windows Vista Ultimate SP1 X86 MSDN 官方简体中文版精简优化而成,无人值守安装,免激活,免序列号,安装方便快捷! 雨林木风 Vista 2.1 精简版

Windows Vista,生而伟大

本文于2018年12月23日首发于IT之家. 地址:点击这里 笔者看过一篇关于Windows Vista早期原型系统的文章,那篇文章向大家展示了Windows Longhorn的Aero UX演示视频. ▲视频截图:Windows Longhorn原型系统Aero UX演示,完整视频请点击这里 关于Longhorn这个系统,大家很多人已经知道,它是Windows XP的继任者,肩负着微软巨大的希望,虽然这款系统预期的功能最终没能完全实现,可它的成品Windows Vista却对此后的Window

基于Windows Server 2012 R2部署KMS服务器

基于Windows Server 2012 R2部署KMS服务器 关于Microsoft Windows / Microsoft Office "VLK" 和"MAK"两种激活的异同 "VLK"和"MAK"都是微软为"大客户"量身特定的激活方式.只要实施激活,就是永久激活."VLK"是Volume Licensing Key的缩写.微软对于"VLK"密钥施行"

在 Windows Vista、Windows 7 和 Windows Server 2008 上设置 SharePoint 2010 开发环境

适用范围: SharePoint Foundation 2010 | SharePoint Server 2010 本文内容 步骤 1:选择和预配置操作系统 步骤 2:安装 SharePoint 2010 的必备组件 步骤 3:安装 SharePoint 2010 步骤 4:安装 Visual Studio 2010 和开发人员文档 步骤 5:从已安装的系统创建 Hyper-V 映像 当您创建或自定义 SharePoint 解决方案时,通常最好在安装了 Microsoft SharePoint

基于Windows Sdk 与visual C++2008 在微软平台上构架自己的语音识别引擎(适用于windows 2

基于Windows Sdk 与visual C++2008 在微软平台上构架自己的语音识别引擎(适用于windows 2000/xp2003/vista   windows CE /mobile),本项目开源,源码请留下你们的Email,我给大家发 本人闲来无事,自行开发了一个小型的语音识别引擎,搭建起在微软平台上的语音识别框架服务体系, 鉴于本人个人力量有限,为了将语音识别引擎做的功能更加强悍,强大, 现在将该系统开源,需要源码的请在本人CSDN博客下留下EMail, 本系统属于系统框架,搭建

【Neko C Engine】一个基于 C 语言的游戏基础代码库

原本已经用 C++ 在写着一个游戏代码库了,个人作品,规模很小,只能满足基本的游戏开发需求,后来突发奇想,试着用 C 语言重写了一次自己的游戏代码库,姑且叫做 Neko C Engine 吧,缩写 nkCEngine,基于 Windows 系统,底层封装了 Win32 窗口.D3D.DirectSound 等等,使用的时候不需要操心 Win32 窗口,不需要操心绘图与音效播放的细节,直接一个 C 函数创建对象,然后用对应的 C 函数操作对象: 这个库的最终目标,是希望公开出来给所有对游戏开发感兴