Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Ads by Lake Quincy Media
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » WPF » Programming Speech in WPF - Speech Synthesis

Programming Speech in WPF - Speech Synthesis

Speech Synthesis also known as text-to-speech of SAPI, is a process of converting text to speech. This tutorial shows how to build speech enabled applications in WPF and .NET 3.5.

Author Rank:
Total page views :  15812
Total downloads :  569
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
SpeechSample2.zip
 
Become a Sponsor


New Microsoft Speech API (SAPI) version 5.3, which is an integral part of Windows Vista, is a managed API that allows developers to write speech-enable applications in .NET Framework 3.0.  This speech functionality is defined in the System.Speech and its five sub namespaces. Physically, the speech API resides in System.Speech.Dll assembly.

Here is a list of five namespaces that defines Speech related functionality.

  • System.Speech.Audioformat
  • System.Speech.Recognition
  • System.Speech.Recognition.SrgsGrammar
  • System.Speech.Synthesis
  • System.Speech.Synthesis.TtsEngine

To access Speech API in WPF, you must add System.Speech.Dll assembly reference to a project. Right click on the project name in Solution Explorer, select Add Reference and select System.Speech on the .NET Tab and select OK button as shown in Figure 1.

Figure 1.

This action will add System.Speech assembly reference and copy System.Speech.dll to the bin folder of your project.  Now you can import System.Speech related namespaces in your application.

Speech Synthesis

Speech Synthesis, known as text-to-speech in previous versions of SAPI, is a process of converting text to speech.

Windows Vista comes with a default voice called Microsoft Anna. Let's take a look at it. Go to Control Panel and click on Text to Speech. You will see Speech Properties dialog with two tabs - Text to Speech and Speech Recognition as you can see in Figure 2 and Figure 3.

Figure 2.

 

On Text to Speech dialog box, you will see Voice Selection dropdown showing Microsoft Anna. On this dialog, you may also test the voice and audio output. If you have more voices installed, you will see them in the dropdown list as well. You can install more voices when you install Microsoft Speech SDK. 5.1.

Figure 3.

 

Table 1 describes the classes available in System.Speech.Synthesis namespace.

Class

Description

FilePrompt

Represents a prompt spoken from a file.

InstalledVoice

Represents an installed Voice object.

Prompt

Plays a prompt from text or from a PromptBuilder.

PromptBuilder

Creates an empty Prompt object and provides methods for adding content.

PromptStyle

Defines a style of prompting that consists of settings for emphasis, rate, and volume.

SpeechSynthesizer

Supports the production of speech and DTMF output.

VoiceInfo

Represents a text-to-speech (TTS) voice.


In this article, our focus is on SpeechSynthesizer class and its methods and properties.

SpeechSynthesizer     

The SpeechSynthesizer generates text to speech.

The Speak method speaks the text synchronously. The following code creates a SpeechSynthesizer object and calls Speak method that says "Hello WPF.". By default, the SpeechSynthesizer uses Microsoft Mary voice.

SpeechSynthesizer ss = new SpeechSynthesizer();

ss.Speak("Hello WPF.");

SpeechSynthesizer Properties

The SpeechSynthesizer has four properties - Rate, State, Voice, and Volume that are used to get and set rate, state, voice, and volume of the speech. The value of rate is between -10 to 10 and value of Volume is between 0 and 100. The Voice is the VoiceInfo object and State is SynthesizerState object. I will discuss these properties in more details in my forthcoming articles.

Asynchronous Speech

The SpeakAsync method speaks asynchronously and takes a Prompt, PromptBuilder or string as input text.

SpeechSynthesizer ss = new SpeechSynthesizer();

ss.SpeakAsync("Hello WPF");

 

The Application

Based on above class, properties, and methods, I build an application that allows you to browse a text file, opens it in a RichTextBox control, set the volume and rate of the speech and speaks it for you.

The application UI looks like Figure 4.

 

Figure 4.

The XAML code for controls looks like following:

<Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,8"

        Name="TalkButton" VerticalAlignment="Bottom" Width="101" Click="TalkButton_Click">

    Speak

</Button>

<RichTextBox Margin="0,45,0,67" Name="richTextBox1" Background="#FF302F2F"

             Foreground="White"  />

<Button Height="23" HorizontalAlignment="Left" Margin="318,10,0,0"

        Name="OpenTextFileButton" VerticalAlignment="Top" Width="110" Click="OpenTextFileButton_Click">

    Open a Text File

</Button>

<Button Height="23" HorizontalAlignment="Right" Margin="0,10,12,0" Name="OpenWavFileButton"

        VerticalAlignment="Top" Width="110" Click="OpenWavFileButton_Click">

    Open a Wav File

</Button>

<TextBox Height="23" Margin="10,10,0,0" Name="FileNameTextBox" VerticalAlignment="Top"

         HorizontalAlignment="Left" Width="299" />

<ComboBox Height="23" HorizontalAlignment="Left" Margin="90,0,0,30"

          Name="VolumeList" VerticalAlignment="Bottom" Width="120" SelectedIndex="4" >

    <ComboBoxItem>10</ComboBoxItem>

    <ComboBoxItem>20</ComboBoxItem>

    <ComboBoxItem>30</ComboBoxItem>

    <ComboBoxItem>40</ComboBoxItem>

    <ComboBoxItem>50</ComboBoxItem>

    <ComboBoxItem>60</ComboBoxItem>

    <ComboBoxItem>70</ComboBoxItem>

    <ComboBoxItem>80</ComboBoxItem>

    <ComboBoxItem>90</ComboBoxItem>

    <ComboBoxItem>100</ComboBoxItem>

</ComboBox>

<Label Height="28" HorizontalAlignment="Left" Margin="0,0,0,25"

       Name="label1" VerticalAlignment="Bottom" Width="83">Volume:</Label>

<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,0,130,30"

          Name="RateList" VerticalAlignment="Bottom" Width="120"

          SelectedIndex="2">

    <ComboBoxItem>-10</ComboBoxItem>

    <ComboBoxItem>-5</ComboBoxItem>

    <ComboBoxItem>0</ComboBoxItem>

    <ComboBoxItem>5</ComboBoxItem>

    <ComboBoxItem>10</ComboBoxItem>

  

</ComboBox>

<Label Height="28" Margin="226,0,249,25" Name="label2" VerticalAlignment="Bottom">

    Rate:</Label>

 

In this application, The Open a Text File button opens a file browser and you can open browse a text file. After that it reads the text file and opens it in the RichTextBox control. The code listing is shown below on the Open a Text File button click event handler.

private void OpenTextFileButton_Click(object sender, RoutedEventArgs e)

{

    OpenFileDialog dlg = new OpenFileDialog();

    dlg.InitialDirectory = "c:\\";

    dlg.Filter = "Text files (*.txt)|*.txt|All Files (*.*)|*.*";

    dlg.RestoreDirectory = true;

    if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)

    {

        LoadTextDocument(dlg.FileName);

        FileNameTextBox.Text = dlg.FileName;

    }

}

     

 

private void LoadTextDocument(string fileName)

{

    TextRange range;

    System.IO.FileStream fStream;

    if (System.IO.File.Exists(fileName))            {

        range = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);

        fStream = new System.IO.FileStream(fileName, System.IO.FileMode.OpenOrCreate);

        range.Load(fStream, System.Windows.DataFormats.Text);

        fStream.Close();

    }

}

 

The Speak button click event handler sets the volume and rate of the speech after getting these values from the Volume and Rate ComboBoxes and sets the Rate and Volume property of SpeechSynthesizer and after that calls the Speak method.

The ConvertRichTextBoxContentsToString method below reads the contents of the RichTextBox and converts to a string.

Here is the Speak button click event handler.

private void TalkButton_Click(object sender, RoutedEventArgs e)

{

    ComboBoxItem volumeItem = (ComboBoxItem)VolumeList.Items[VolumeList.SelectedIndex];

    Int32 vol = Convert.ToInt32(volumeItem.Content.ToString());

    ComboBoxItem rateItem = (ComboBoxItem)RateList.Items[RateList.SelectedIndex];

    Int32 rate = Convert.ToInt32(rateItem.Content.ToString());

    talker.Volume = vol;

    talker.Rate = rate;

    talker.Speak(ConvertRichTextBoxContentsToString());

}

 

string ConvertRichTextBoxContentsToString()

{

    TextRange textRange = new TextRange(richTextBox1.Document.ContentStart,

        richTextBox1.Document.ContentEnd);

    return textRange.Text;

}

Summary

Speech API (SAPI) 5.3 is a managed API comes with Windows Vista. In this article, I discussed how we can use SAPI in a WPF application to build speech-enabled applications. This article covered the text-to-speech (TTS) or Speech Synthesis where we built an application that convert text to speech.  

In my next articles in this series, I will add more features to this application including word highligting and spell check. In the next part of this series, I will cover speech recognition in SAPI and WPF. 

I hope you enjoyed this article. All feedback and critics are most welcome. Feel free to post them at the bottom of this article.

 


Login to add your contents and source code to this article
 About the author
 
Mahesh Chand
Mahesh is a software developer with over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle. If you are looking for a Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
SpeechSample2.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
Empty SpeechSample1.zip by Wouter On October 1, 2008
There are no files at the sample file :-(
Reply | Email | Delete | Modify | 
Re: Empty SpeechSample1.zip by Mahesh On October 1, 2008

My bad. Just uploaded again.

Thanks for letting me know.

Cheers!

Reply | Email | Delete | Modify | 
Synthesized text-to-speech by Clive On December 9, 2008
I notice that the above regarding Synthesized text-to-speech is based around Windows Vista is there away of synthesizing speech in both Vista and XP? I ask because I am trying to develop a program using XP but should it develop into a reasonable program I would naturally like to seeing it being used on both formats. I look forward to recieivng any replies Regards Clive 1950
Reply | Email | Delete | Modify | 
Re: Synthesized text-to-speech by Ram On February 9, 2009
I have checked. This works with xp!
Reply | Email | Delete | Modify | 
Re: Re: Synthesized text-to-speech by Clive On February 9, 2009
Hello,

Just a note to say thank you very much for you reply regarding Speech Synthesis on both Vista & XP that I  have just received.

I can now attemp to create my program.

Since I am in the process of trying to sell  my house it may be sometime befotr I can start on it, but will be pleased to let you know my progress in the future.

Cheers

Clive
Reply | Email | Delete | Modify | 
question by mary On March 16, 2009
HI Can SAPI work with win xp ? thank you
Reply | Email | Delete | Modify | 
Re: question by Mahesh On March 16, 2009
Yes. See the previous comment.
Reply | Email | Delete | Modify | 
ABOUT sapi by mary On March 17, 2009
IS SAPI INSTALL WITH VISUAL STUDIO 2008 OR NOT? THANK YOU
Reply | Email | Delete | Modify | 
Re: ABOUT sapi by Mahesh On March 17, 2009
No. You need to download it from MSDN site. Search Google for SAPI 3.5 download.
Reply | Email | Delete | Modify | 
hello mr mahesh by mary On April 6, 2009
I want to know how to play an audio file automatic when I press a button. Such as: When I press F1, it will play "F1.mp3" Please help me. Thank you.
Reply | Email | Delete | Modify | 
Re: hello mr mahesh by Mahesh On April 7, 2009
Mary,
I do not know the answer. You may want to look at key stroke commands. You may also want to post the question on forums.
Reply | Email | Delete | Modify | 
speech to text by fei On April 7, 2009
excellent tutorial on Text to Speech, do you have anything to share with Speech to Text?
Reply | Email | Delete | Modify | 
Re: speech to text by Mahesh On April 7, 2009
Thanks. I have not finished the second part of this. Hopefully will have time soon and finish it.
Reply | Email | Delete | Modify | 
Using BackgroundWorker to run the speaking by Eddie On March 12, 2010
Can you show an example of using a BackgroundWorker with WorkerSupportsCancellation set to true to run the speak method?

I'd like to be able to cancel the speaking.

Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.