Click here to Skip to main content
15,867,568 members
Articles / Desktop Programming / XAML

Text-to-Speech Application using Silverlight 4 COM API

Rate me:
Please Sign up or sign in to vote.
4.95/5 (18 votes)
2 Jul 2010CPOL4 min read 54K   963   21   20
In this article, I will demonstrate in a step-by-step manner how we can create a Text-to-Speech application using the COM APIs in Silverlight 4. Read it. Learn and Vote for this Article. Feedback and/or Suggestions are most welcome.

Introduction

Have you ever heard of Text to Speech engine? “Yes” I think. Yes, it is not a new thing in the Computer World. It was available since Windows 98 (as much as I can recall) but it is completely new in Silverlight. You can now use the Silverlight application to give an API call to the SAPI Engine which will convert the text to voice. Sounds good? This is achievable using the COM APIs only.

Here I will describe how to use COM API to give a TTS call to the SAPI Engine. After reading this topic here, you will be able to write your own code to develop your own application having the Text to Speech functionality in Silverlight 4.

Prerequisite

To develop this application, you need the following tools:

  1. Microsoft Visual Studio 2010
  2. Silverlight Tools for Visual Studio 2010

Once you set up your development environment, create a Silverlight project. If you are new to the Silverlight application development and want to know about Silverlight and want to learn how to create a new Silverlight application, read the Silverlight Tutorial.

Setting up the Project for OOB

Before doing anything, you need to set up some configuration. Yes, because the COM API only works in Silverlight Out-of-Browser application, you need to change the project settings for that.

  • Right click on the Silverlight project and open the Properties.
  • Click the CheckBox “Enable running application out of the browser” as shown below and then click on the “Out-of-Browser Settings” button.

    image

  • Once the Settings page has been opened, set the desired options from the screen. Be sure that you checked “Show install menu” and “Require elevated trust when running outside the browser”.

    image

Once you done with these settings, you will be able to install this web version as an Out-of-Browser application.

Setting up the Project for dynamic Keyword

Now you have to add a DLL assembly reference to your project. As we will use dynamic keyword in our example, we need to add the “Microsoft.CSharp” assembly reference.

  • To do this, right click on the Silverlight project and chose “Add Reference” from the menu.

    image

  • Now from the “Add Reference” dialog, find “Microsoft.CSharp”, select it and click Ok. This will add the assembly reference to the project.

    image

Designing the UI

Once all the settings are done, it is time to design our UI. Add a TextBox and a Button inside the MainPage.xaml file and set name to those. In our example, I am using “txtMessage” as the name of the TextBox and “btnSpeak” as the name of the Button.

Here is my XAML code for you:

XML
<UserControl x:Class="Silverlight_4_Text_To_Speech_Demo.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <StackPanel x:Name="LayoutRoot" Orientation="Vertical" Width="320">
        <TextBlock FontWeight="Bold" FontSize="14" 
		Text="Silverlight 4 Text-To-Speech Demo" 
                    Margin="10" TextAlignment="Center" />
        <TextBlock Text="Enter some text and click on the 'Speak' 
			button to read out the entered text." 
                    Margin="10" TextWrapping="Wrap" />
        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="txtMessage" Width="200" Height="100" Margin="5" 
                     AcceptsReturn="True" TextWrapping="Wrap" />
            <Button x:Name="btnSpeak" Content="Speak" Width="100" Height="100" 
                    Margin="5" Click="btnSpeak_Click" />
        </StackPanel>
    </StackPanel>
</UserControl>

If you run this, it will look like the below snapshot:

image

Here, we will use the TextBox to enter some text inside it and once we click on the “Speak” button, it will start reading out the entered text. To do this, register the button click event and go for implementing the Text-to-Read functionality in our code behind file.

Playing with the Code

Let's start playing with the code now. Open MainPage.xaml.cs and inside the button click event, first create an object of Sapi.SpVoice from the AutomationFactory and assign it to a dynamic variable.

C#
dynamic textToSpeech = AutomationFactory.CreateObject("Sapi.SpVoice");

Set the Volume for the readout text by setting the Volume property of the Sapi object instance to 100. This will produce a full volume. Then call the Speak() method by passing the inserted text as a parameter to it.

Here is the full code snippet:

C#
private void btnSpeak_Click(object sender, RoutedEventArgs e)
{
    if (App.Current.IsRunningOutOfBrowser && App.Current.HasElevatedPermissions)
    {
        dynamic textToSpeech = AutomationFactory.CreateObject("Sapi.SpVoice");
        textToSpeech.Volume = 100;
        textToSpeech.Speak(txtMessage.Text);
    }
    else
    {
        MessageBox.Show("Please install it as Trusted Out-of-Browser application.");
    }
}

Party smile Hoho, our application is ready to run now. Build your solution for any errors. If succeeded, run the application by pressing CTRL + F5. Install the application as Silverlight Out-of-Browser application first. To do this, right click on the Silverlight UI and click “Install” from the context menu. Now follow the steps mentioned in the setup UI.

Once successfully installed as Out of Browser, it will open the installed one from your local drive. Enter some text inside the TextBox and click on the “Speak” button. You will hear the text readout by someone. Yes, that’s it. Your application now supports text to speech.

image

If you like this feature, download the sample application and run in your local machine to see the live demo. The source code for this article is also available.

Don’t forget to vote this article up and leave your feedback. Enjoy working with the new features of Silverlight 4.

History

  • 2nd July, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
GeneralMy vote of 5 Pin
Helen.Yu2-Nov-10 14:47
Helen.Yu2-Nov-10 14:47 
GeneralGood small project Pin
Sumit P20-Jul-10 2:01
Sumit P20-Jul-10 2:01 
GeneralRe: Good small project Pin
Kunal Chowdhury «IN»20-Jul-10 2:08
professionalKunal Chowdhury «IN»20-Jul-10 2:08 
QuestionWhat about XP? Pin
ffw_me15-Jul-10 9:22
ffw_me15-Jul-10 9:22 
AnswerRe: What about XP? Pin
Kunal Chowdhury «IN»20-Jul-10 2:04
professionalKunal Chowdhury «IN»20-Jul-10 2:04 
GeneralRe: What about XP? Pin
ffw_me20-Jul-10 3:09
ffw_me20-Jul-10 3:09 
GeneralMy vote of 5 Pin
Anurag Gandhi8-Jul-10 0:09
professionalAnurag Gandhi8-Jul-10 0:09 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»8-Jul-10 0:57
professionalKunal Chowdhury «IN»8-Jul-10 0:57 
GeneralMy vote of 5 Pin
nasser_yacout5-Jul-10 22:05
nasser_yacout5-Jul-10 22:05 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»8-Jul-10 0:56
professionalKunal Chowdhury «IN»8-Jul-10 0:56 
GeneralMy vote: 5 Pin
ramuknavap5-Jul-10 13:41
ramuknavap5-Jul-10 13:41 
GeneralRe: My vote: 5 Pin
Kunal Chowdhury «IN»5-Jul-10 19:39
professionalKunal Chowdhury «IN»5-Jul-10 19:39 
GeneralMy vote of 5 Pin
Abhijit Jana2-Jul-10 19:39
professionalAbhijit Jana2-Jul-10 19:39 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»2-Jul-10 23:25
professionalKunal Chowdhury «IN»2-Jul-10 23:25 
GeneralMy vote of 5 Pin
defwebserver2-Jul-10 14:24
defwebserver2-Jul-10 14:24 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»2-Jul-10 19:39
professionalKunal Chowdhury «IN»2-Jul-10 19:39 
GeneralMy vote of 5 Pin
Abhishek Sur2-Jul-10 9:16
professionalAbhishek Sur2-Jul-10 9:16 
GeneralRe: My vote of 5 Pin
Kunal Chowdhury «IN»2-Jul-10 9:31
professionalKunal Chowdhury «IN»2-Jul-10 9:31 
GeneralRe: My vote of 5 Pin
linanur15-Jan-12 0:01
linanur15-Jan-12 0:01 
AnswerRe: My vote of 5 Pin
Kunal Chowdhury «IN»15-Jan-12 2:23
professionalKunal Chowdhury «IN»15-Jan-12 2:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.