123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- // Text2Speeck.cpp : 定义 DLL 的初始化例程。
- //
- #include "stdafx.h"
- #include "Text2SpeeckApp.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- //
- //TODO: 如果此 DLL 相对于 MFC DLL 是动态链接的,
- // 则从此 DLL 导出的任何调入
- // MFC 的函数必须将 AFX_MANAGE_STATE 宏添加到
- // 该函数的最前面。
- //
- // 例如:
- //
- // extern "C" BOOL PASCAL EXPORT ExportedFunction()
- // {
- // AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // // 此处为普通函数体
- // }
- //
- // 此宏先于任何 MFC 调用
- // 出现在每个函数中十分重要。这意味着
- // 它必须作为函数中的第一个语句
- // 出现,甚至先于所有对象变量声明,
- // 这是因为它们的构造函数可能生成 MFC
- // DLL 调用。
- //
- // 有关其他详细信息,
- // 请参阅 MFC 技术说明 33 和 58。
- //
- // CText2SpeeckApp
- BEGIN_MESSAGE_MAP(CText2SpeeckApp, CWinApp)
- END_MESSAGE_MAP()
- // CText2SpeeckApp 构造
- CText2SpeeckApp::CText2SpeeckApp()
- {
- // TODO: 在此处添加构造代码,
- // 将所有重要的初始化放置在 InitInstance 中
- }
- // 唯一的一个 CText2SpeeckApp 对象
- CText2SpeeckApp theApp;
- CText2Speech *g_pText2Speeck = NULL;
- // CText2SpeeckApp 初始化
- BOOL CText2SpeeckApp::InitInstance()
- {
- CWinApp::InitInstance();
- return TRUE;
- }
- extern "C" BOOL PASCAL EXPORT OpenSapi()
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // 此处为普通函数体
- if ( g_pText2Speeck == NULL )
- {
- g_pText2Speeck = new CText2Speech();
- if ( g_pText2Speeck->Initialize() )
- {
- // 默认语速;
- g_pText2Speeck->SetRate(0);
- // 默认最大音;
- g_pText2Speeck->SetVolume(100);
- }
- else
- {
- delete g_pText2Speeck;
- g_pText2Speeck = NULL;
- }
- }
- return g_pText2Speeck == NULL ? FALSE : TRUE;
- }
- extern "C" void PASCAL EXPORT CloseSapi()
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // 此处为普通函数体
- if ( g_pText2Speeck != NULL )
- delete g_pText2Speeck;
- g_pText2Speeck = NULL;
- }
- // nSpStreamFormat = -1时, 表示的是默认系统;
- // nSpStreamFormat = 4时, 表示的是8khsz, 8bit mono;
- // nSpStreamFormat = 6时, 表示的是8khsz, 16bit mono;
- extern "C" INT PASCAL EXPORT Text2Speech( IN LPCTSTR lpText, IN LPCTSTR lpWavFileName )
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // 此处为普通函数体
- if ( g_pText2Speeck == NULL )
- return -1;
- HRESULT hr = g_pText2Speeck->Save2Wav(lpText, lpWavFileName);
- if (FAILED(hr))
- return -2;
- return 0;
- }
- extern "C" INT PASCAL EXPORT SetSpeech(IN INT nVoiceIndex, IN INT nSpeeckRate, IN INT nSpeeckVolume, IN INT nSpStreamFormat)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // 此处为普通函数体
- if ( g_pText2Speeck == NULL )
- {
- WriteTextLog(_T("g_pText2Speeck = null"));
- return -1;
- }
- WriteTextLog(_T("参数:%d,%d,%d,%d"), nVoiceIndex, nSpeeckRate, nSpeeckVolume, nSpStreamFormat);
- HRESULT hr = S_OK;
- hr = g_pText2Speeck->SetVoice(nVoiceIndex);
- if (FAILED(hr))
- {
- WriteTextLog(_T("g_pText2Speeck->SetVoice(nVoiceIndex),%ld"), hr);
- return -2;
- }
- hr = g_pText2Speeck->SetRate(nSpeeckRate);
- if (FAILED(hr))
- {
- WriteTextLog(_T("g_pText2Speeck->SetRate(nVoiceIndex),%ld"), hr);
- return -3;
- }
- hr = g_pText2Speeck->SetVolume(nSpeeckVolume);
- if (FAILED(hr))
- {
- WriteTextLog(_T("g_pText2Speeck->SetVolume(nVoiceIndex),%ld"), hr);
- return -4;
- }
- SPSTREAMFORMAT spsf = (SPSTREAMFORMAT)nSpStreamFormat;
- hr = g_pText2Speeck->SetOutAudio(spsf);
- if (FAILED(hr))
- {
- WriteTextLog(_T("g_pText2Speeck->SetOutAudio(nVoiceIndex),%ld"), hr);
- return -5;
- }
- return 0;
- }
- extern "C" INT PASCAL EXPORT Speak(IN LPCTSTR lpText)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // 此处为普通函数体
- if ( g_pText2Speeck == NULL || lpText == NULL )
- return -1;
- HRESULT hr = S_OK;
- hr = g_pText2Speeck->Speak(lpText);
- if (FAILED(hr))
- return -2;
- return 0;
- }
- extern "C" INT PASCAL EXPORT Play(IN LPCTSTR lpWavFileName)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // 此处为普通函数体
- if ( g_pText2Speeck == NULL || lpWavFileName == NULL || !PathFileExists(lpWavFileName))
- return -1;
- HRESULT hr = S_OK;
- hr = g_pText2Speeck->PlayWav(lpWavFileName);
- if (FAILED(hr))
- return -2;
- return 0;
- }
- extern "C" INT PASCAL EXPORT GetVoiceCount()
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // 此处为普通函数体
- if ( g_pText2Speeck == NULL )
- return -1;
- return g_pText2Speeck->GetVoiceCount();
- }
- extern "C" BOOL PASCAL EXPORT GetVoiceName(OUT LPTSTR lpVoiceName, IN INT nBuffLen, IN INT nVoiceIndex )
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // 此处为普通函数体
- if ( lpVoiceName == NULL || nBuffLen <= 0 )
- return FALSE;
- if ( g_pText2Speeck == NULL )
- return FALSE;
- WCHAR *pVoiceName = NULL;
- HRESULT hr = g_pText2Speeck->GetVoice(&pVoiceName, nVoiceIndex);
- if ( FAILED(hr) || pVoiceName == NULL)
- return FALSE;
- #ifdef UNICODE
- _stprintf_s(lpVoiceName, nBuffLen, _T("%s"), pVoiceName);
- #else
- INT cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, pVoiceName, -1, NULL, 0, NULL, NULL);
- if ( cbMultiByte == 0 )
- return FALSE;
- CHAR *pMultiByte = new CHAR[cbMultiByte+1];
- memset(pMultiByte, 0, cbMultiByte+1);
- INT nRealWrite = WideCharToMultiByte(CP_OEMCP, 0, pVoiceName, -1, pMultiByte, cbMultiByte, NULL, NULL);
- if ( nRealWrite != cbMultiByte )
- {
- if (pMultiByte)
- delete []pMultiByte;
- return FALSE;
- }
- _stprintf_s(lpVoiceName, nBuffLen, _T("%s"), pMultiByte);
- if (pMultiByte)
- delete []pMultiByte;
- #endif
- return TRUE;
- }
|