本文转摘自网络,写得不错。
Hello World by Microsoft Speech SDK 5.1
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"
世界你好!", SPF_DEFAULT, NULL);
pSpVoice->Speak(L"
Hello World!", 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"现在是
%.4d-%.02d-%.2d %.2d:%.02d",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute);
pSpVoice->Speak(wsz, SPF_IS_XML, NULL);
pSpObjectToken->Release();
}
http://blog.csdn.net/lkjx82/archive/2006/07/23/965120.aspx
现在语音技术应用十分广泛,本人也一直在从事这方面的工作,利用MS TTS开发产品,针对中文发音设置的问题,在这里阐述一下自己的实现方法。
void CIMEInfoAcquisitionDlg::SpeakVoice(LPCTSTR csVoice)
{
//com初期化
if (FAILED(CoInitialize(NULL)))
{
AfxMessageBox("Error to intiliaze COM");
return;
}
//文字変換
LPOLESTR lpw = new WCHAR [256];
MultiByteToWideChar(CP_ACP,0,csVoice,-1,lpw,256);
HRESULT hr = S_OK;
if( SUCCEEDED( hr ) )
{
CComPtr cpVoiceToken;
CComPtr cpEnum;
CComPtr cpVoice;
ULONG ulCount = 0;
// 建立
hr = cpVoice.CoCreateInstance(CLSID_SpVoice);
LPCSTR name;
name = "Microsoft Simplified Chinese";
CString s; s.Format("Name=%s", name);
WCHAR wName[1024];
MultiByteToWideChar(CP_ACP, 0, s, -1, wName, sizeof(wName));
ISpObjectToken* pToken = NULL;
if(SUCCEEDED(SpFindBestToken(SPCAT_VOICES, NULL, wName, &pToken))){
cpVoice->SetVoice(pToken);
}
//文读
if (SUCCEEDED (hr))
{
hr = cpVoice->Speak( lpw, SPF_DEFAULT, NULL );
}
}
//com的结束
delete [] lpw;
CoUninitialize();
return;
}
以上利用ISpObjectToken接口实现了对中文的阅读,是不是很简单明了:)
阅读(2363) | 评论(0) | 转发(0) |