Text to voice, TTS voice implementation

Source: Internet
Author: User
Tags sapi

In a recent chess and card game, the leader said that the chat content on the client should be able to be pronounced, that is, the text sent by the player should be automatically read, if the voice package is integrated into the client, the Client installation file will inevitably increase, and the business volume will come up with this solution: B/S side implementation, the general process is that the client requests B/S side, b/S generates the voice file and the client downloads it.

In the process of converting Text to Speech, we used anonymous + reflection. However, the generated speech file sometimes has no sound, and the file size is only a few bytes. The file is not generated successfully and the cause is unknown.
!CodeAs follows:

If (! String . Isnullorempty (context. Request. querystring [ " Txt " ]) &! String . Isnullorempty (context. Request. querystring [ " Type " ])
{
Try
{
String Type = context. Request. querystring [ " Type " ];
String TXT = context. Request. querystring [ " Txt " ];
String Filename = system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (Type + txt, " MD5 " );
String Filepath = " /Files/ " + Filename + " . Wav " ;
If (File. exists (context. server. mappath (filepath )))
{
Context. response. Write (filename );;
}
Else
{
Dynamic synth = system. activator. createinstance (system. type. gettypefromprogid ( " SAPI. spvoice " ));
Dynamic filestream = system. activator. createinstance (system. type. gettypefromprogid ( " SAPI. spfilestream " ));

If (Type = " 2 " )
{
Synth. Voice = synth. getvoices ( " Name = VW Lily " ). Item ( 0 );
}
Else
{
Synth. Voice = synth. getvoices ( " Name = VW Liang " ). Item ( 0 );
}
Synth. Rate =- 1 ;

Filestream. Open (context. server. mappath (filepath), speechstreamfilemode. ssfmcreateforwrite, False );
Synth. audiooutputstream = filestream;
Synth. Speak (txt );
Synth. waituntildone ( 1000 );
Synth. Dispose ();
Filestream. Close ();

context. response. Write (filename);
context. response. End ();

}< BR >}< br> catch
{< br> context. response. write ( " 0 " );
context. response. end ();
}

}

 

 

Later, I used the. NET 3.0 generation method. Currently, the test is normal. The Code is as follows:

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Speech. synthesis;
Using System. IO;
Using System. Threading;

namespace dokeetts
{< br> ///


/// _ default summary
///
Public class _ default: ihttphandler
{

Public void processrequest (httpcontext context)
{< br> context. response. contenttype = " text/plain " ;

StringTXT = context. Request. querystring ["Txt"];
StringType = context. Request. querystring ["Type"];
StringFilename ="";

Filename = system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (Type + txt," MD5 " );
Thread t = New Thread () =>
{
Speechsynthesizer syth = New Speechsynthesizer ();
If (Type = " 2 " )
{
Syth. selectvoice ( " VW Lily " );
}
Else
{
Syth. selectvoice ( " VW Liang " );
}
String Filepath = " /Files/ " + Filename + " . Wav " ;
If (File. exists (context. server. mappath (filepath )))
{
Context. response. Write (filename );;
}
Else
{
Syth. setoutputtowavefile (context. server. mappath (filepath ));
Syth. Speak (txt );
}
Syth. Dispose ();
});
T. Start ();
T. Join ();

Context. response. Write (filename );
Context. response. Flush ();
Context. response. End ();

}

Public BoolIsreusable
{
Get
{
Return False;
}
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.