Creating a ASP.NET application converting text to speech - c#

I seek some insight in creating an application that converts text to speech in ASP.NET. From my initial research, it appears that:
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.
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.

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>
...

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

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.

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>

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.

Related

How to directly read value from Windows Service with VB6?

How can I directly read a value from Windows Service with VB6?
I wrote a Windows Service with C# which generate a string and I want to read this value with VB6.
For example, Windows Service Application generate a string variable like this for each 10 second:
string id = "1422144";
after 10 second
string id = "2462778";
I want to read this values with VB6 each 10 second. I can read it from text file when I create and write this value to text with Windows Service but I don't want to create a text file. Thanks in advance.
To use MSMQ (Message Queue) approach (note, I've not checked this exhaustively so it's just to get started):
https://msdn.microsoft.com/en-us/library/ms973860.aspx
http://www.informit.com/articles/article.aspx?p=131272&seqNum=6
To create self-hosted OWIN Web Api 2.0 service, I'd recommend using TopShelf (I've done this myself, but too much overhead for just passing a simple string along):
https://codeopinion.com/self-host-asp-net-web-api-as-a-windows-service/
Other options:
Communicate via TCP/IP directly
Use a database (server updates, VB6 reads)
use compicated system of arduino-activated relays to cause mice to take particular paths in a maze, and detect their movements (note: not entirely serious)
Lots of things I've not thought of

c# Streaming downgraded-quality video over HTTP

I have very large high quality videos that I need to stream over HTTP (for mobile devices). It is not possible to use ffmpeg to create a "streaming" version of the video.
I must also still support HTTP's seek/begin feature so that the user may skip ahead in the video.
I am using ServiceStack (not IIS).
Are there any options available out there?
I have access to the server, so any third party services are acceptable.
We've recently added Partial Content support both in directly serving Static files as well as via a Web Service. This will be available in v3.9.44 release of ServiceStack on NuGet which is planned for this weekend (you can build ServiceStack from the repo if you need it before then).
Partial Content support will be automatically enabled where it will be available to seek/stream any static file (served through ServiceStack) as well as any Service that:
returns a file:
return new HttpResult(new FileInfo(filePath), request.MimeType);
returns bytes:
return new HttpResult(byteArray, "audio/mpeg");
returns a stream:
return new HttpResult(memoryStream, "audio/mpeg");
returns a raw string:
return new HttpResult(customText, "text/plain");
To disable Partial Content support set:
Config.AllowPartialResponses = false;

C# upload file to server - both client and server side

I'm a C# game programmer with little web development experience.
I need to upload a small file (25-100 or so bytes, depending on it's content) to a server. This is on the Windows Phone 7 using XNA. The target server is fairly limited and only supports PHP and classic ASP.
Since the CF on the WP7 only has access to a limited subset of networking commands, it's looking like an HttpWebRequest GET aimed at a script that saves the file will be the best option. The data I'm sending is small in size, and should be able to be passed as a parameter in the url.
I've been searching but have yet to find a complete example of this, which handles both the client and server side script (mainly the latter). This is close to what I'm looking for, except it has no mention of the server side script: Upload files with HTTPWebrequest (multipart/form-data)
The closest that I got was this: http://www.johny.org/2007/08/upload-using-c-as-client-and-php-as-server/
But when attempting to use it I get an unhandled exception: "The remote server returned an error: (405) Method Not Allowed". This method seems the most promising so far, but I've yet to be able to debug this.
Unfortunately, I have a short amount of time to implement this, and as I said only a passing familiarity with web development. I'm not worried about maximum security or scalability as this is a temporary measure to collect feedback internally. Basically, I just need the quickest thing that works. ;)
Any help would be fantastic!
I've solved it. First off, PHP wasn't supported on my server (just now learning that PHP and ASP are can't be used on the same server, depending on whether it's on Linux or Windows - like I said, web development noob here!). I switched to ASP and, after digging through the docs, wrote this script:
<%
dim theData, theFileName
set theData=Request("data")
set theFileName=Request("filename")
dim fs,tfile
set fs=Server.CreateObject("Scripting.FileSystemObject")
set tfile=fs.CreateTextFile(Server.MapPath(theFileName+".txt"))
tfile.WriteLine(theData)
tfile.Close
set fname=nothing
set fs=nothing
set theData=nothing
set theFileName=nothing
%>
This C# code uploads the file:
const string cAddress = "http://site.com/folder/upload.asp";
string fileName = foo;
string data = bar;
string address = cAddress + "?filename=" + fileName + "&data=" + data;
uploadRequest = (HttpWebRequest) HttpWebRequest.Create(address);
uploadRequest.Method = "GET";
uploadRequest.GetResponse();
Hope this helps someone else looking for an example of how to do this!
But you have the METHOD as GET instead of POST. You can't upload a file to a website by passing the file path to the Query String.

Read csv file client side

I have to read the first couple of lines of a csv file client side to present the user with a preview before uploading a large file.
I'm using c# with ASP.Net (VS2010)
Does anyone know how this can be done? Also that it works in all browsers!?!
(It's not possible to use activeX or so, we do not want our clients to install something!)
A sample code would be great!
Thanks in advance!
Firefox 3.6+ and Chrome (at least version 6, possibly older versions) support the File API which lets you read local files from a file input.
Here's a quick sample:
function handleFile(file) {
var reader = new FileReader();
reader.onload = function(e) {
// Here's where you would parse the first few lines of the CSV file
console.log(e.target.result);
};
reader.readAsText(file);
}
in your html, you would have something like this:
<input type="file" onchange="handleFile(this.files[0])" />
Of course, in real life you should make it degrade gracefully.
The file object has name and type properties that you could use to verify that it's a CSV file if you wanted to be more strict.
There is a W3C proposal for a File API in HTML5. I ran a few browsers against this test, and found that Firefox 3.6, and Chrome 6 support File Reader API and Local Storage. Safari 5 (on Windows) supported Local Storage but not File Reader API. Sorry I couldn't test IE9 as corporate policy has IE6 (?!) nailed in place.
You can't read a local file with vanilla JavaScript.
You could use activeX as a progressive enhancement to those using IE (even though offering an enhanced experience to IE users goes against everything I stand for!).
If you can use Flash, look at Read local file in Flash.

Proxying Videos locally with a TCP Socket

I've been really interested in adding support for video podcasts to Media Browser.
I would like users to be able to navigate through the available video podcasts and stream them from the internets. That's really easy cause media player etc.. will happily play a file that lives in the cloud.
The problem is that I want cache these files locally so subsequent viewings of the same episode will not involve streaming and instead will play the local file.
So... I was thinking, why not host an HttpListener and as media player asks it for bits of the file, have the HttpListener download and store it locally. Next time a user plays the file we will already have portions of the file locally.
Does anyone know of example code that uses HttpListener for proxying?
EDIT
The idea would be only to proxy simple streamable content like MP3 or Mov.
The bounty will go to an actual implementation.
Here is the API I would like:
// will proxy a uri on the local port, if cacheFile exists it will resume the
// download from cacheFile.
// while the file is downloading it will be name cacheFile.partial, after the
// download is complete the file will be renamed to cacheFile.
// Example usage: ProxyFile("http://media.railscasts.com/videos/176_searchlogic.mov", 8000, #"c:\downloads\railscasts\176_searchlogic.mov")
//
// Directly after this call http://localhost:8000 will be the proxy stream, it will be playable locally.
void ProxyUri(Uri uri, int port, string cacheFile)
Edit 2
HttpListener is looking pretty unpromising I will probably need to do the work at a TCP socket level as HttpListeners seem to require the program runs as admin which is going to be really tricky.
I hadn't done anything with HttpListener before, so I thought this would be a nice little exercise to bring myself up to speed with it - and so it proved. I implemented it as a single ProxyListener class whose constructor takes the parameters of the ProxyUri function you specified. Once you obtain an instance, you start it listening (and potentially downloading) by calling its Start method. When you're done with it, call Cleanup.
There are one or two rough edges but basically it works as per your question. To test it, I built it up as a console application with a Program class which accepts input lines consisting of (uri, port, filename), space-separated, creates the ProxyListener instances and starts them. You can run this console application, type in a suitable line, and the downloader will start (printing out progress to console). Simultaneously you can e.g. fire up IE and fetch the file from the specified port, and you will be able to download it while the downloader is still working. The "uploader" progress will be printed to console, too.
I'm having a bit of trouble pasting it in here, maybe due to size (it's not that big, but bigger than the snippets you normally see here - the ProxyListener class is a tad under 200 lines). Does it sound interesting? If so, I'll post it to a pastebin and update this answer with a link.
Update: Posted as a gist.
Note that you will need Administrator privileges to run the program, since HttpListener requires this.
Update 2: Under certain circumstances, it is not necessary to have admin privileges to run HttpListener. See this link and this one. The idea is, if you can reserve an URL namespace during installation time, then the user does not have to have admin privileges if listening against that namespace.
Streaming was not designed to be saved, and also these protocols are very custom and very complex to implement, streaming sessions do lots of validation and synchronization which will be extremely difficult to imitate. Of course it is not an impossible task, but its fairly big task to do. Only other way is to read and save it as local media file, and use that as a reference. Because you can use windows media encoder to read stream and write stream data as local file, but it still may not allow you to do copy protected data.
Did you consider using HTTP proxy with caching features?
Like:
Apache httpd with mod_proxy and mod_cache
Squid
See also Web Cache # wikipedia
If you want your application to have such web cache component, I suggest you look for Web Cache implementation in .Net, and not code it from scratch.

Categories

Resources