Content Introduction
Text speech (text-to-speech, hereinafter referred to as TTS), which is used to convert text to speech output through the TTS engine. Instead of building your own TTS engine, this article describes how to use the Microsoft Speech SDK to build your own text-to-speech application.
Introduction to the Microsoft Speech SDK
The Microsoft Speech SDK is a software development package provided by Microsoft, providing a Speech API (SAPI) that includes two main areas:
1. API for Text-to-Speech
2. API for Speech recognition
API for text-to-speech, is the Microsoft TTS engine interface, through which we can easily build powerful text speech program, PowerWord Word reading function to use this writing API, and now almost all text reading tools are developed using this SDK. As for the API for Speech recognition is the speech recognition which corresponds with TTS, the speech technology is an exciting technology, but because of the present speech recognition technology accuracy and the recognition speed is not very ideal, has not achieved the widespread application request.
Microsoft Speech SDK can be downloaded at Microsoft's website for free, the current version is 5.1, in order to support Chinese, but also to download the additional language packs (Langpack).
In order to use this SDK in VC, must add the SDK in the project include and Lib directory, in order to avoid each project to add a catalog, the best way is in VC
Option->directoris Plus SDK's include and Lib directories.
One of the simplest examples
Let's look at an example of getting started:
#include <sapi.h>
#pragma comment(lib,"ole32.lib") //CoInitialize CoCreateInstance需要调用ole32.dll
#pragma comment(lib,"sapi.lib") //sapi.lib在SDK的lib目录,必需正确配置
int main(int argc, char* argv[])
{
ISpVoice * pVoice = NULL;
//COM初始化:
if (FAILED(::CoInitialize(NULL)))
return FALSE;
//获取ISpVoice接口:
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;
}
Just 20 lines of code to achieve the text-to-speech text, it's magical enough. The SDK provides SAPI is based on COM encapsulation, whether you are familiar with COM, as long as the step-by-step CoInitialize (), CoCreateInstance () to obtain the Ispvoice interface is enough, it should be noted that after the initialization of COM, Be sure to use CoUninitialize () to release resources before the program ends.