C # Introduction to voice Functions

Source: Internet
Author: User
Tags sapi microsoft website


However, the method itself does not know the language of the string you are giving, so we need to read the string in what language.
The voice attribute of the spvoiceclass class is used to set the language. We can use the getvoices method of spvoiceclass to obtain a list of all languages,
Then select the appropriate language based on the parameter. For example, set the language to Chinese as follows:

Private void setchinavoice ()
{
Voice. Voice = voice. getvoices (string. Empty, String. Empty). Item (0 );
} 0 indicates Chinese, and 1234 indicates English, indicating different accents. In this way, we have set the language. If we combine the pronunciation method, we can design a method for sending only Chinese speech private void speakchina (string strspeak)
{
Setchinavoice ();
Speak (strspeak );
}
The method for sending only English speech is similar. Program . For a mix of Chinese and English languages, the method for the program to read the Mixed Speech is: programming separates the English and Chinese of the language,
The speakchina method is called in Chinese, and the speakengishi method is called in English. How can I determine whether a character is English or Chinese?
ASC code method. The specific class method is implemented through analysespeak. In this way, for a piece of mixed Chinese and English text, we only need to pass it as a parameter to analysespeak, which can complete both Chinese and English
. Of course, for the tentative, continue, stop and other operations of pronunciation, the above also provides a simple method call, it is easy to understand. The following describes the methods for Chinese speech recognition:

First, the speech recognition class Source code Paste it below and explain it again: As the. NET technology is deeply rooted in the hearts of the people, more and more programmers begin to transfer to the. NET platform for development. However, in the newly released. the net speech SDK does not support Chinese speech. Currently, the highest version of the speech SDK that supports Chinese is SAPI 5.1 () on Windows. use sapi5.1 to develop Chinese speech applications.
--------------------------------------------------------------------------------
Directory:
1. Analysis and installation of sapi.51 SDK
2. Import COM objects to. net
3. Use C # To develop a Chinese TTS application example
4. Conclusion
5. References
--------------------------------------------------------------------------------
1. Analysis and installation of sapi.51 SDK
Sapi sdk is a free speech application development kit provided by Microsoft. It contains the speech application design interface (SAPI) and Microsoft's continuous speech recognition engine (MCSR) and Microsoft's speech synthesis (TTS) engine. The current version 5.1 supports recognition in three languages (English, Chinese and Japanese) and synthesis in two languages (English and Chinese ). SAPI also includes direct speech management, training wizard, events, syntax compilation, resources, and speech recognition (SR) for low-level control and high adaptability) management, TTS management, and other powerful design interfaces. Its structure (1 ):
Figure (1)
The Speech Engine interacts with SAPI through the DDI layer (Device Driver Interface), and applications communicate with SAPI through the API layer. By using these APIs, you can quickly develop applications for speech recognition or speech synthesis.
Sapi5.1 sdks can be downloaded from the Microsoft Website: The http://www.microsoft.com/speech/download/sdk51/ requires the setup of speech SDK 5.1 (68 m) and 5.1 Language Pack (81.5 m ).
--------------------------------------------------------------------------------
2. Import COM objects to. net
Sapi5.1 is based on the Windows platform and called through the COM interface. To use sapi5.1 on the. NET platform, we can use the. NET framework's powerful tool tlbimp.exe to import the COM Object of the sapi sdk to. net. Tlbimp.exe generates a controlled packaging class, which can be used by the management client. Number of reference packages for managing the actual COM object. When the packaging class is used as the collection garbage, the packaging class releases the COM object it wraps. Of course, you can select a COM object from the project reference dialog box in the Vs. NET environment to import a COM Object. This process is also completed through tlbimp.exe.
The following shows how to import the COM Object of SAPI:
D: \ Program Files \ common files \ microsoft shared \ speech> tlbimp SAPI. dll/out: dotnetspeech. dll

After the SDK is installed, you can find sapi.dllin the D: \ Program Files \ common files \ microsoft shared \ speech \ directory, which defines the sapicomobject and converts the DLL. NET platform Assembly --- dotnetspeech. DLL, the conversion process will prompt a lot of warnings (warning), but this affects our development, can ignore. Finally, we can use ildasm to view the objects in dotnetspeech. dll.
--------------------------------------------------------------------------------
3. Use C # To develop a Chinese TTS application example
The following example shows how to use C # To develop a speech application. The development environment is:
Operating System: Windows 2000 Chinese version + SP3
. NET Framework: 1.0.3705 (English version)
Visual Studio. NET 7.0.9466 (English version)
First, create a C # windows application project speechapp, and add the dotnetspeech Object Library in Solution Explorer on the right of the development environment. Right-click "Reference" and choose "add reference". In the displayed dialog box, find the generated dotnetspeech. dll.

Figure (2)
Open form1.csCodeFile, add a namespace (case sensitive) at the beginning of the Code ).
Using dotnetspeech;
In this way, the sapi sdk is imported. Now we can write the application code. This example shows how to read the text through the speaker and convert the text into a voice signal (wave audio file), the program interface (3 ):

Reference content is as follows:

// Read aloud
Private void buttonsynthesis_click (Object sender, system. eventargs E)
{
Try
{
Speechvoicespeakflags spflags = speechvoicespeakflags. svsflagsasync;
Spvoice voice = new spvoice ();
Voice. Speak (this. textboxtext. Text, spflags );
}
Catch (exception ER)
{
MessageBox. Show ("an error occured! "," Speechapp ", messageboxbuttons. OK, messageboxicon. Error );
}
}
// Generate a sound file (WAV)
Private void buttonttstowave_click (Object sender, system. eventargs E)
{
Try
{
Speechvoicespeakflags spflags = speechvoicespeakflags. svsflagsasync;
Spvoice voice = new spvoice ();
Savefiledialog SFD = new savefiledialog ();
SFD. Filter = "all files (*. *) | *. * | WAV Files (*. wav) | *. wav ";
SFD. Title = "Save to a wave file ";
SFD. filterindex = 2;
SFD. restoredirectory = true;
If (SFD. showdialog () = dialogresult. OK)
{
Speechstreamfilemode spfilemode = speechstreamfilemode. ssfmcreateforwrite;
Spfilestream = new spfilestream ();
Spfilestream. Open (SFD. filename, spfilemode, false );
Voice. audiooutputstream = spfilestream;
Voice. Speak (txtspeaktext. Text, spflags );
Voice. waituntildone (timeout. Infinite );
Spfilestream. Close ();
}
}
Catch (exception ER)
{
MessageBox. Show ("an error occured! "," Speechapp ", messageboxbuttons. OK, messageboxicon. Error );
}
}

Next, configure the current language of the speech SDK engine on the control panel. Open "Control Panel", open the "Voice" configuration item, and you can see where we can identify or synthesize the current language, you can also configure related hardware devices and control the language speed.

In the "text-speech conversion" "speech selection" combo box, select Simplified Chinese (Microsoft Simplified Chinese ). In this way, Chinese characters can be merged.

Return to vs. net, F5 compile and run the application just now, enter Chinese characters in the text box, put on headphones, and click "read aloud" to experience the new generation of intelligent man-machine interfaces.

From http://blog.csdn.net/rmb147/archive/2009/02/05/3864108.aspx

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.