刚刚从 Microsoft 网站下栽了 Speech SDK 5.1 和中日文发声补丁,参考 Speech SDK文档写了几个小程序,尽管我也是刚刚接触 Speech若干小时的初学者,还是拿出来共享吧:
Speech SDK 采用 COM 的形式封装,和大多数COM对象一样,下面是一个似曾相识的初始化:
CoInitialize(NULL);
CLSIDFromProgID(L"SAPI.SpVoice", &CLSID_SpVoice);
ISpVoice * pSpVoice = NULL;
if (FAILED(CoCreateInstance(CLSID_SpVoice, NULL,
CLSCTX_INPROC_SERVER, IID_ISpVoice, (void **)&pSpVoice)))
{
cout << "Failed to create instance of ISpVoice!" << endl;
return -1;
}
现在我们用它来说 “Hello World”,非常简单:
pSpVoice->Speak(L"Hello World!", SPF_DEFAULT, NULL);
最后,清除这一切
pSpVoice->Release();
CoUninitialize();
很容易,是不是?下面我们稍微改变一下:
IEnumSpObjectTokens *pSpEnumTokens = NULL;
if (SUCCEEDED(SpEnumTokens(SPCAT_VOICES, L"language=409", NULL, &pSpEnumTokens)))
{
ISpObjectToken *pSpToken = NULL;
while (SUCCEEDED(pSpEnumTokens->Next(1, &pSpToken, NULL)) &&
pSpToken != NULL)
{
pSpVoice->SetVoice(pSpToken);
pSpVoice->Speak(L"Hello World!", SPF_DEFAULT, NULL);
pSpToken->Release();
}
pSpEnumTokens->Release();
}
如果只需要一种 Voice,SpFindBestToken 更加方便:
ISpObjectToken * pSpObjectToken = NULL;
if (SUCCEEDED(SpFindBestToken(SPCAT_VOICES, L"gender=female",
NULL, &pSpObjectToken)))
{
pSpVoice->SetVoice(pSpObjectToken);
pSpVoice->Speak(L"Hello World!", SPF_DEFAULT, NULL);
pSpObjectToken->Release();
}
现在改为使用中文:
ISpObjectToken * pSpObjectToken = NULL;
if (SUCCEEDED(SpFindBestToken(SPCAT_VOICES, L"language=804",
NULL, &pSpObjectToken)))
{
pSpVoice->SetVoice(pSpObjectToken);
pSpVoice->Speak(L"世界你好!", SPF_DEFAULT, NULL);
pSpObjectToken->Release();
}
ISpVoice::Speak 可以允许朗读的句子中包含 XML 标记,其实以上这一切用 XML 标记将变得十分简单:
pSpVoice->Speak(L"<lang langid=\"804\">世界你好!</lang>", SPF_DEFAULT, NULL);
pSpVoice->Speak(L"<voice required=\"gender=female\">Hello World!</voice>", SPF_DEFAULT, NULL);
最后,以一段简单的报时结束这篇文章:
SYSTEMTIME st;
GetLocalTime(&st);
ISpObjectToken * pSpObjectToken = NULL;
if (SUCCEEDED(SpFindBestToken(SPCAT_VOICES, L"language=804", NULL, &pSpObjectToken)))
{
pSpVoice->SetVoice(pSpObjectToken);
WCHAR wsz[128];
swprintf(wsz,
L"现在是<context ID = \"CHS_Date_ymdhm\">%.4d-%.02d-%.2d %.2d:%.02d</context>",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute);
pSpVoice->Speak(wsz, SPF_IS_XML, NULL);
pSpObjectToken->Release();
}
■ 到此为止,希望这篇文章能带给初学者一点启发。
本文地址:http://com.8s8s.com/it/it1662.htm