Resumé

.Net 3.0 System.Speech API

Cristian Merighi () 5.00

A tiny howto about giving your website speech faculty: how to dynamically output audio files using .Net Framework 3.0 System.Speech objects.
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.

As far as I started to work with internet applications and interfaces, I dreamt about developing interfaces more television-alike than book-alike. That meant I had to find the way to have content moving and ...talking!

Such a huge problem: how could I handle interactive spoken content?!
.Net 3.0 Framework, please help me! ...Done.

I'm still at the approach level in terms of handling the .Net 3.0 libraries, but its richness already had a magnum WOW effect on me! (I'm gonna expose some interesting things soon...) ...and digging through the namespaces I found System.Speech! Nothing exactly new, in fact it's based on Microsoft Speech APIs, but actually completely integrated in our managed code Framework.

Here I report some lines of code (C#) which allow to interactively and dynamically generate wave files ready to be played using Windows Media Player, Adobe® Flash or something else embedded in your web page.

public void DoSpeak()
{
    System.Speech.Synthesis.SpeechSynthesizer task = new System.Speech.Synthesis.SpeechSynthesizer();
    ReadOnlyCollection<InstalledVoice> coll = task.GetInstalledVoices();
    // screamage between installed voices in different cultures
    // Culture is an elsewhere set property
    if (Culture != CultureInfo.InvariantCulture)
    {
        coll = task.GetInstalledVoices(Culture);
    }            
    if (coll.Count == 0) return;
    VoiceInfo voice = coll[0].VoiceInfo;
    if (!string.IsNullOrEmpty(Voice))
    {
        foreach (InstalledVoice v in coll)
        {
            if (v.VoiceInfo.Name.Equals(Voice, StringComparison.CurrentCultureIgnoreCase))
            {
                voice = v.VoiceInfo;
                break;
            }
        }
    }
    task.SelectVoice(voice.Name);
    // WaveOutputPath: property elsewhere set.
    // Gets or sets the path of the output file
    if (!string.IsNullOrEmpty(WaveOutputPath))
        task.SetOutputToWaveFile(WaveOutputPath);
    task.Speak(Phrase);
    // always dispose resources!
    task.Dispose();
}

That is the core subroutine of a simple class developed and used the following way (VB.Net code, just to vary syntax from time to time)

Private Sub HandleSpeech()
    Page.AsyncTimeout = TimeSpan.FromSeconds(5)
    ' Define the asynchronuous task.
    Dim mytask As New Pacem.Speech.SpeechAsyncTask()
    mytask.Culture = New System.Globalization.CultureInfo("en-US")
    Dim phrase As String = "This chapter describes the special facilities in PDF for dealing with text— specifically, " & _
"for representing characters with glyphs from fonts. A glyph is a graphical " & _
"shape and is subject to all graphical manipulations, such as coordinate transformation. " & _
"Because of the importance of text in most page descriptions, PDF provides " & _
"higher-level facilities that permit an application to describe, select, and " & _
"render glyphs conveniently and efficiently."
    mytask.Phrase = phrase
    mytask.WaveOutputPath = Server.MapPath("~/speech.wav")
    Dim asynctask As New PageAsyncTask( _
        AddressOf mytask.OnBegin, AddressOf mytask.OnEnd, AddressOf mytask.OnTimeout, DBNull.Value)

    AddHandler mytask.Success, AddressOf Me.Speech_Success

    ' Register the asynchronous task.
    Page.RegisterAsyncTask(asynctask)

    ' Execute the register asynchronous task.
    Page.ExecuteRegisteredAsyncTasks()
    
End Sub

Sub Speech_Success(ByVal obj As Object, ByVal e As EventArgs)
    ' Do some stuff as the wave file got saved.
    Dim mySavedWav As String = CType(obj, Pacem.Speech.SpeechAsyncTask).WaveOutputPath
    ' etc...
End Sub

Try to use this API yourself ...or just listen to the generated output.

zip file « download code

Take care. Bye.

Feedbacks

  • Re: .Net 3.0 System.Speech API

    Peter Bridger Saturday, March 14, 2009 5.00

    Hi Cristian Thanks for sharing this code - it's articles like this which help the greater .net community! I'm currently wresling with TTS under Windows through a asp.net webpage. I want access to the .net 3 TTS engine, but all my attempts so far have resulted in different failures. I stumbled across your code and thought I'd found a fresh angle, however running your code through asp.net on Windows Server 2008 results in the follow error: No voice installed on the system or none available with the current security setting. At first I thought it was because the soundcard wasn't set-up under 2008, but after installing the correct drivers nothing changed. Googling for the error message hasn't resulted in any real clues either. So in summary, just how did you get TTS working through a asp.net webpage using .net 3 :) Best wishes Pete

  • Re: System.Speech API di .Net 3.0

    Michele Friday, June 11, 2010 0.00

    Ma la voce in italiano non esiste (almeno per Windows 7)!!!

feedback
 

Syndicate

Author

Cristian Merighi facebook twitter google+ youtube

Latest articles

Top rated

Archive

Where am I?

Author

Cristian Merighi facebook twitter google+ youtube

I'm now reading

Feeds