C # convert Text to Speech and save wav and MP3 files

Source: Internet
Author: User
Tags sapi

It has been more than four months since I wrote my blog. I have been busy with my recent work and have no time to write my blog. In the future, I will write more blog posts to share my recent problems with you. Recently, due to project requirements, I have studied how to use C # To develop TTS. The following describes the general idea and hopes to help you.

First, you need to understand the ms sapi, Which is Microsoft's voice API. It includes Speech Recognition SR engine and speech synthesis SS engine. Let's take a look at the speech synthesis SS engine. It varies by version and operating system, but I like to use other synthetic speech packages, such as NeoSpeech. Looking back, the SAPI versions for MS are: 5.1, 5.3, and 5.4. The specific usage is as follows:

1. Windows Speech SDK 5.1 supports XP and server 2003 systems. You need to download and install the SDK. Windows Speech SDK 5.1 is installed for the Chinese language engine. : Http://www.microsoft.com/download/en/details.aspx? Id = 10121

2. Windows Speech SDK 5.3 supports Vista and Server 2008 systems and has been integrated into the system. Vista and Server 2003 use Microsoft lili voice library and Microsoft Anna voice library by default.

3. Windows Speech SDK 5.4 supports Windows 7 and has been integrated into the system. You do not need to download and install it. Microsoft lili Chinese female voice library and Microsoft Anna female voice Library are also included in Windows 7. Microsoft lili supports mixed reading between Chinese and English.

Now the environment is ready. Next we will discuss the classes used to develop TTS. What I use is.. net class library. speech. synthesis. speechSynthesizer, which must be referenced before use: System. speech. The main methods, attributes, and events of this class are as follows:

Method:

GetInstalledVoices (): gets the speaker installed in the current system, and returns a VoiceInfo object set. The specific object is in the control panel, and the voice item can be viewed. Xp is Microsoft Sam by default.

SelectVoice (string): select the target reader. The parameter is the reader name, for example, Microsoft Sam.

SpeakAsync (string): starts asynchronous reading. The parameter is the text to be read.

Speak (string): starts to read aloud. The parameter is the text to read. I didn't understand the difference between SpeakAsync and Speak here. I tried the same effect.

Pause (): Pause playing and reading aloud.

Resume (): Resume the pause of reading.

SetOutputToWaveFile (string): saves the voice file. After calling this method, you must call the Speak method. The parameter is the path to save the file. For example, d:/124.wav or d:/123. MP3

SetOutputToNull (): Specifies the end statement for saving the file. This statement must be called. Otherwise, the produced audio file cannot be played.

Attribute:

Rate: playback speed,-10 ~ 10

Volume: Volume adjustment: 0 ~ 100

Voice: the video is being played by a person. The VoiceInfo object is returned.

Event:

SpeakCompleted: Read completion event. This time is triggered after the read is completed. You can process the completed playback process in this event.

SpeakStarted: Read the start event.

SpeakProgress: Read process events. You can continue to process progress bars.

....

Other attributes, methods, and events are rarely used. You can check the API. If you have any questions, please leave a message for us to discuss. The interface is as follows:

Paste the Code as follows:

Private SpeechSynthesizer synth = null; // voice object

/// <Summary>
/// Return a SpeechSynthesizer object
/// </Summary>
/// <Returns> </returns>
Private SpeechSynthesizer GetSpeechSynthesizerInstance ()
{
If (synth = null)
{
Synth = new SpeechSynthesizer ();
}
Return synth;
}

/// <Summary>
/// Get the reading setting object
/// </Summary>
/// <Returns> </returns>
Private VoiceClass GetVoiceClass ()
{
VoiceClass setViceClass = new VoiceClass ();
SetViceClass. VoiceName = cboTokens. Text;
SetViceClass. Rate = int. Parse (cboSpeed. Text );
SetViceClass. Volume = tbVoice. Value;
SetViceClass. VoiceText = txtInput. Text;
Return setViceClass;
}

/// <Summary>
/// Play
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void btnGo_Click (object sender, EventArgs e)
{
If (IsVaild ())
{
VoiceClass setViceClass = GetVoiceClass (); // VoiceClass is a class set up by the user. It contains the stream-to-speech volume stream playback text.
Thread thread = new Thread (RingVoice );
Thread. Start (setViceClass );
}
}
/// <Summary>
/// Start reading aloud and put it in the thread
/// </Summary>
/// <Param name = "VoiceObject"> </param>
Public void RingVoice (object VoiceObject)
{
Try
{
VoiceClass voiceClass = (VoiceClass) VoiceObject;
Synth = GetSpeechSynthesizerInstance ();
Synth. SelectVoice (voiceClass. VoiceName );
Synth. Rate = voiceClass. Rate;
Synth. Volume = voiceClass. Volume;
Synth. SpeakAsync (voiceClass. VoiceText );
}
Catch (Exception er)
{
MessageBox. Show (er. ToString (), "prompt", MessageBoxButtons. OK, MessageBoxIcon. Error );
}
}

/// <Summary>
/// Save
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void btnSave_Click (object sender, EventArgs e)
{
If (IsVaild ())
{
Synth = GetSpeechSynthesizerInstance ();
VoiceClass voiceClass = GetVoiceClass ();
Synth. SelectVoice (voiceClass. VoiceName );
Synth. Rate = voiceClass. Rate;
Synth. Volume = voiceClass. Volume;

SaveFileDialog saveFileDialog = new SaveFileDialog ();
SaveFileDialog. Title = "Save document ";
SaveFileDialog. Filter = "*. wav | *. wav | *. mp3 | *. mp3 ";
SaveFileDialog. InitialDirectory = Environment. GetFolderPath (Environment. SpecialFolder. MyDocuments );
DialogResult saveDialog = saveFileDialog. ShowDialog ();
Try
{

If (saveDialog = System. Windows. Forms. DialogResult. OK)
{
Synth. SetOutputToWaveFile (saveFileDialog. FileName );
Synth. Speak (voiceClass. VoiceText );
Synth. SetOutputToNull ();
System. Diagnostics. Process. Start ("assumer.exe", string. Format (@ "/select, {0}", saveFileDialog. FileName); // open the wav directory and select the file
}

}
Catch (Exception er)
{
MessageBox. Show (er. ToString (), "prompt", MessageBoxButtons. OK, MessageBoxIcon. Error );
}
}

}

/// <Summary>
/// Continue
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void btnContinum_Click (object sender, EventArgs e)
{
Synth. Resume ();
}
/// <Summary>
/// Pause
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void btnStop_Click (object sender, EventArgs e)
{
Synth. Pause ();
}

I have a problem. I don't know why. The content to be played: Please call the 12345 customer service system and read 12345 to me as 10 thousand, and. The problem is still being solved. Who knows the trouble? Thank you.

 

Related Article

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.