Text-to-speech tutorial

Source: Internet
Author: User
Tags sapi

 

Text-to-speech tutorial original text: From sapi5.1 Translation: seasun/openpaper/Google Forum Date: This article introduces a basic text-to-speech conversion application example. This is an example of a console-based speech program. In another article, "Event-based text-to-text conversion instance", we introduced a GUI-based and event-driven instance. In this example, we use the simplest COM framework and let the computer say something simple. It also introduces how to use XML tags to modify speech. At the end of the article, complete program code is provided. -- Step 1: create a project -- Step 2: Initialize the COM interface -- Step 3: Create a speech object -- Step 4: Start talking -- Step 5: Use the XML tag to modify the speech Step1: although building a project can write program code from scratch, it can be faster from the existing example. Therefore, we first use the Application Wizard in the development environment of Visual Studio to create a Win32 console program. Select a simple "Hello World" Application project during creation. After creation, open the "stdafx. H" header file and paste the following content in "# include" and "# End ".
Between. In this way, other Dependencies required by SAPI are included.
#define _ATL_APARTMENT_THREADED
 
Program list 1:
#include 
//You may derive a class from CComModule and use it if you want to override something, 
//but do not change the name of _Module
extern CComModule _Module;
#include 
Next, add two files (SAPI. h and SAPI. Lib) to the project. Assume that the installation of sapi5.1 follows the default path. If it is a non-default installation, or the compiler cannot find these two file paths, change them to other actual storage paths. On the menu bar, select the "project" | "Settings" | "C/C ++" tab, select "Preprocessor" from the "category" drop-down list, and click "additional"
In the include directories edit box, enter the actual header file path, for example, "C:/program files/Microsoft Speech SDK 5.1/include ". Next we will introduce how to set the SAPI. Lib path. 1. In the same "settints" dialog box, select the "Link" tab. 2. Select "input" from the "category" drop-down list ". 3. Enter the SAPI. Lib path in the "additional library path" editing box. For example, "C:/program files/Microsoft Speech SDK 5.1/lib/i386 ". 4. Add "SAPI. lib" to "Object/library modules" in the same "Link" tab. Note that the names of each library are separated by spaces. Step 2: Initialize the COM interface SAPI is based on the COM framework, so the program must initialize the COM interface before using SAPI-related functions and when the SAPI object is activated. The following code does not have any other functions except initialization, but ensures that com can be started properly. Program list 2:
#include 
#include 
int main(int argc, char* argv[])
{
    if (FAILED(::CoInitialize(NULL)))
        return FALSE;
 
    ::CoUninitialize();
    return TRUE;
}
Step 3: Create the voice object com runtime environment, and then create the voice object. A voice object is a simple COM object. In addition, speech objects use many smart default configurations. During the initialization process, SAPI automatically configures most of the default values for them. After the speech objects are created, they can be immediately used by the program. Compared with the old version, this is an important improvement of the new version. These default parameters are obtained from the "language attributes" of the "control panel", including the language type. All default parameters, including explicit and non-explicit parameters (such as speaking speed and pitch), can be changed programmatically or from the language attributes in the control panel. It is not necessary to set pvoice to null in the program. However, this helps to check for errors. It can also be used as a prompt to indicate that the space of the pointer has been allocated or released.
Program list 3: The content of the bold font is newly added.
#include 
#include 
 
int main(int argc, char* argv[])
{
    ISpVoice * pVoice = NULL;
 
    if (FAILED(::CoInitialize(NULL)))
        return FALSE;
 
    HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
    if( SUCCEEDED( hr ) )
    {
        pVoice->Release();
        pVoice = NULL;
    }
 
    ::CoUninitialize();
    return TRUE;
}
Step 4: It is quite easy for the computer to start speaking a phrase: Just call the "speak" function in a line of code! If you no longer use a voice object (VOICE) in your program, you can release it. Program list 4: The content of the bold font is newly added.
#include 
#include 
 
int main(int argc, char* argv[])
{
    ISpVoice * pVoice = NULL;
 
    if (FAILED(::CoInitialize(NULL)))
        return FALSE;
 
    HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
    if( SUCCEEDED( hr ) )
    {
        hr = pVoice->Speak(L"Hello world", 0, NULL);
        pVoice->Release();
        pVoice = NULL;
    }
 
    ::CoUninitialize();
    return TRUE;
}
Step 5: there are multiple ways to modify voice using XML tags. The most direct method is to use XML commands in voice streams. These commands are described in XML mode. In the following example, the pitch frequency of the latter part is 10 lower than that of the former part. Program list 5: The bold font section is added, which is a complete example in this article.
#include 
#include 
 
int main(int argc, char* argv[])
{
    ISpVoice * pVoice = NULL;
    if (FAILED(::CoInitialize(NULL)))
        return FALSE;
    HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
    if( SUCCEEDED( hr ) )
    {
        hr = pVoice->Speak(L"Hello world", 0, NULL);
        // Change pitch
        hr = pVoice->Speak(L"This sounds normal  but the pitch drops half way through", SPF_IS_XML, NULL );
        pVoice->Release();
        pVoice = NULL;
    }
    ::CoUninitialize();
    return TRUE;
}

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.