2010-02-14 21:11:26 +00:00
|
|
|
#include "StdAfx.h"
|
2010-11-24 22:39:20 +00:00
|
|
|
|
|
|
|
#ifdef USE_SPEECH_API
|
2010-02-14 21:11:26 +00:00
|
|
|
#include <sapi.h>
|
|
|
|
#include "Speech.h"
|
|
|
|
|
|
|
|
CSpeech::~CSpeech(void)
|
|
|
|
{
|
|
|
|
// Don't do this: causes crash on app exit!
|
|
|
|
//if (m_cpVoice)
|
|
|
|
// m_cpVoice->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpeech::Init(void)
|
|
|
|
{
|
|
|
|
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_INPROC, IID_ISpVoice, (LPVOID*)&m_cpVoice);
|
|
|
|
return hr == S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpeech::Reset(void)
|
|
|
|
{
|
|
|
|
if (!m_cpVoice)
|
|
|
|
return;
|
|
|
|
|
|
|
|
HRESULT hr = m_cpVoice->Speak(NULL, SPF_PURGEBEFORESPEAK, 0);
|
|
|
|
_ASSERT(hr == S_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpeech::Speak(const char* const pBuffer)
|
|
|
|
{
|
|
|
|
if (!m_cpVoice)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t uSize;
|
|
|
|
errno_t err = mbstowcs_s(&uSize, NULL, 0, pBuffer, 0);
|
|
|
|
if (err)
|
|
|
|
return;
|
|
|
|
|
|
|
|
WCHAR* pszWTextString = new WCHAR[uSize];
|
|
|
|
if (!pszWTextString)
|
|
|
|
return;
|
|
|
|
|
|
|
|
err = mbstowcs_s(&uSize, pszWTextString, uSize, pBuffer, uSize);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
delete [] pszWTextString;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT hr = m_cpVoice->Speak(pszWTextString, SPF_ASYNC | SPF_IS_NOT_XML, 0);
|
|
|
|
_ASSERT(hr == S_OK);
|
|
|
|
|
|
|
|
hr = m_cpVoice->Resume();
|
|
|
|
_ASSERT(hr == S_OK);
|
|
|
|
|
|
|
|
delete [] pszWTextString;
|
|
|
|
}
|
2010-11-24 22:39:20 +00:00
|
|
|
|
|
|
|
#endif // USE_SPEECH_API
|