ITPub博客

首页 > 应用开发 > IT综合 > Hello World by Microsoft Speech SDK 5.1 (转)

Hello World by Microsoft Speech SDK 5.1 (转)

原创 IT综合 作者:amyz 时间:2007-11-13 15:26:46 0 删除 编辑
Hello World by Microsoft Speech SDK 5.1 (转)[@more@]

Hello World by microsoft Speech SDK 5.1

 刚刚从 Microsoft 网站下栽了 Speech SDK 5.1 和中日文发声补丁,参考 Speech SDK文档写了几个小程序,尽管我也是刚刚接触 Speech若干小时的初学者,还是拿出来共享吧:

 XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

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();

}

 

 ■ 到此为止,希望这篇文章能带给初学者一点启发。

 


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-982288/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    3984
  • 访问量
    7335585