4

I seek some insight in creating an application that converts text to speech in ASP.NET. From my initial research, it appears that:

  1. MS SAPI requires the client to download an ActiveX component and can support large amounts of text to be converted. Our clients are not willing to install any components on their systems, so this approach may or may not fly.

  2. I do understand with .NET 3.0, we have the System.Speech.Synthesis namespace. Does the conversion take place on the server? If so, how would I serve it to the client?

Our requirements are ability to convert large amount of text, should be scalable and reliable. Which technology is "production ready" capable of serving a large number of requests in a short time interval.

Any thoughts are appreciated.

Nick
  • 7,475
  • 18
  • 77
  • 128

5 Answers5

6

By default, ASP.Net applications don't run with sufficient permissions to access Speech Synthesis, and attempting to run Larsenal's code will fail with a security error.

I was able to get around this in an app by having a separate WCF service running on the server, as a regular Windows Service. The ASP.Net application then communicated with that service. That service just wrapped Larsenal's code, returning an array of bytes, given a string of text.

Also, one megabyte of text? That's a good-sized novel.

Edit, 11-12-09, answering some comments:

System.Speech can either return an array of bytes, or save to a wav file, which you can then feed to a media player embedded on the user's page. When I built my talking web page, it worked like this:

1) Page.aspx includes an 'embed' tag that puts a Windows Media Player on the page. The source is "PlayText.aspx?Textid=whatever".
2) PlayText.aspx loads the appropriate text, and communicates (via WCF) to the speechreader service, handing it the text to read.
3) The Speechreader service creates a MemoryStream and calls SpeechSynthesiser.SetOutputToWaveStream, and then returns the stream as a single array of bytes. This array is Response.Write()-ed to the client.

Here's the meat of the SpeechReader service:

    byte[] ITextReader.SpeakText(string text)
    {
        using (SpeechSynthesizer s = new SpeechSynthesizer())
        {
            using (MemoryStream ms = new MemoryStream())
            {
                s.SetOutputToWaveStream(ms);
                s.Speak(text);
                return ms.GetBuffer();
            }
        }
    }

I'm pretty sure that on the back end, this returns an enormous XML array-of-bytes, and is horribly inefficient. I just did it as a proof of concept, and so didn't research that. If you intend to use this in production, make sure that it's not internally returning something like this:

<byte>23</byte>
<byte>42</byte>
<byte>117</byte>
...
Aric TenEyck
  • 8,002
  • 1
  • 34
  • 48
  • I was working on this problem myself for a long time and now I have an answer. Thanks. – Phil Nov 11 '09 at 17:32
  • @Aric - With your approach, I don't have to save the wav file on the server? The "array of bytes" that the service returns - how do i play this on the client's machine? – Nick Nov 11 '09 at 17:35
  • Good point about the security. My sample was provided so that OP could test System.Speech with what he describes as a LOT of text. – Larsenal Nov 11 '09 at 17:38
  • @Aric - Yes, the text sizes are challenging, which is why I want to evaluate the different options before presenting it to the decision makers – Nick Nov 11 '09 at 17:38
  • Check my answer for results of a quick test with 44k words. – Larsenal Nov 11 '09 at 17:48
  • @Lrsenal - Thanks! Were you testing on your dev enviornment which had both the client and the server? Any idea about Aric's approach - does it eliminate saving the wav file on the server? How do I play the array of bytes on the client side? – Nick Nov 11 '09 at 19:09
  • The test wasn't a WCF service. It just gives you *some* idea of how fast the library can convert a large amount of text. You REALLY don't want to try to transfer 600MB back from a WCF service. I'd look into compressing the WAV into MP3 using some of your sample data sets to see whether returning a byte array over WCF makes sense. – Larsenal Nov 11 '09 at 19:22
0

With the SpeechSynthesizer, you can output to a WAV file. You could then have a secondary process compress or convert to another format if needed. All this could be done on the server and then sent up through the browser.

This CodeProject article is a good introduction to .NET Speech Synthesis.

If you want to see how it performs with a LOT of text.... Add a reference to System.Speech and then use the following as a starting point:

using System;
using System.Speech.Synthesis;

namespace SpeakToMe
{
    class Program
    {
        static void Main(string[] args)
        {
            SpeechSynthesizer synth = new SpeechSynthesizer();
            synth.SetOutputToWaveFile("c:\\test.wav");
            synth.Speak("Hello, world.");
            synth.SetOutputToDefaultAudioDevice();

            Console.ReadLine();
        }
    }
}

A quick test on a file of 44,700 words (238KB) on my relatively fast machine...

  • Completed in 55 seconds
  • Generated a 626 MB WAV file
Larsenal
  • 49,878
  • 43
  • 152
  • 220
  • Does SpeechSynthesizer have the ability of converting large amounts of text (> 1mb)? Is this technology ready for production apps? Also, it's a good idea to create it on the server and send it up through the browser. It appears that I have to save the wav file on the server which might end up taking a lot of space? If I want to purge the file after the user has used it, what trigger is reliable (close the browser window?) Thanks – Nick Nov 11 '09 at 16:54
  • To save space, you could have a secondary process that converts to MP3. If the file is meant to be heard only one time by the user, you'd probably want to purge files on a recurring basis (every night, for example). If the user needed to come back to the same conversion, you'd want some way of first checking whether you still had the MP3 and if not, rebuilding it. As far as it's ability to convert large amounts of text... try it. The CP article is a great starting point. – Larsenal Nov 11 '09 at 17:04
0

I achieved this by using codeBehind to run a javascript function that runs the text-to-speech command:

codeBehind:

Page.ClientScript.RegisterStartupScript(
GetType(), 
"anythingHere", 
"nameOfFunction();", 
true);

javascript:

<script>
  function nameOfFunction()
    {//start
       var msg = new SpeechSynthesisUtterance('READ ME!');
       window.speechSynthesis.speak(msg);
    }//end
</script>
dizad87
  • 448
  • 4
  • 15
0

I searched for "Convert Text Into Speech In Asp.Net" in Google and found a very nice and usefull link:

http://codeprojectdownload.com/asp-net-2/convert-text-into-speech-in-asp-net/#.T0ScXIfXDZE

It may also be useful to you.

liwp
  • 6,746
  • 1
  • 27
  • 39
-1

I wrote an article on this on my blog: http://weblogs.asp.net/ricardoperes/archive/2014/04/08/speech-synthesis-with-asp-net-and-html5.aspx. I used AJAX and Data URIs to send voice data back and forth between the client and the server.

Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74