Text2SpeeckApp.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Text2Speeck.cpp : 定义 DLL 的初始化例程。
  2. //
  3. #include "stdafx.h"
  4. #include "Text2SpeeckApp.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #endif
  8. //
  9. //TODO: 如果此 DLL 相对于 MFC DLL 是动态链接的,
  10. // 则从此 DLL 导出的任何调入
  11. // MFC 的函数必须将 AFX_MANAGE_STATE 宏添加到
  12. // 该函数的最前面。
  13. //
  14. // 例如:
  15. //
  16. // extern "C" BOOL PASCAL EXPORT ExportedFunction()
  17. // {
  18. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  19. // // 此处为普通函数体
  20. // }
  21. //
  22. // 此宏先于任何 MFC 调用
  23. // 出现在每个函数中十分重要。这意味着
  24. // 它必须作为函数中的第一个语句
  25. // 出现,甚至先于所有对象变量声明,
  26. // 这是因为它们的构造函数可能生成 MFC
  27. // DLL 调用。
  28. //
  29. // 有关其他详细信息,
  30. // 请参阅 MFC 技术说明 33 和 58。
  31. //
  32. // CText2SpeeckApp
  33. BEGIN_MESSAGE_MAP(CText2SpeeckApp, CWinApp)
  34. END_MESSAGE_MAP()
  35. // CText2SpeeckApp 构造
  36. CText2SpeeckApp::CText2SpeeckApp()
  37. {
  38. // TODO: 在此处添加构造代码,
  39. // 将所有重要的初始化放置在 InitInstance 中
  40. }
  41. // 唯一的一个 CText2SpeeckApp 对象
  42. CText2SpeeckApp theApp;
  43. CText2Speech *g_pText2Speeck = NULL;
  44. // CText2SpeeckApp 初始化
  45. BOOL CText2SpeeckApp::InitInstance()
  46. {
  47. CWinApp::InitInstance();
  48. return TRUE;
  49. }
  50. extern "C" BOOL PASCAL EXPORT OpenSapi()
  51. {
  52. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  53. // 此处为普通函数体
  54. if ( g_pText2Speeck == NULL )
  55. {
  56. g_pText2Speeck = new CText2Speech();
  57. if ( g_pText2Speeck->Initialize() )
  58. {
  59. // 默认语速;
  60. g_pText2Speeck->SetRate(0);
  61. // 默认最大音;
  62. g_pText2Speeck->SetVolume(100);
  63. }
  64. else
  65. {
  66. delete g_pText2Speeck;
  67. g_pText2Speeck = NULL;
  68. }
  69. }
  70. return g_pText2Speeck == NULL ? FALSE : TRUE;
  71. }
  72. extern "C" void PASCAL EXPORT CloseSapi()
  73. {
  74. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  75. // 此处为普通函数体
  76. if ( g_pText2Speeck != NULL )
  77. delete g_pText2Speeck;
  78. g_pText2Speeck = NULL;
  79. }
  80. // nSpStreamFormat = -1时, 表示的是默认系统;
  81. // nSpStreamFormat = 4时, 表示的是8khsz, 8bit mono;
  82. // nSpStreamFormat = 6时, 表示的是8khsz, 16bit mono;
  83. extern "C" INT PASCAL EXPORT Text2Speech( IN LPCTSTR lpText, IN LPCTSTR lpWavFileName )
  84. {
  85. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  86. // 此处为普通函数体
  87. if ( g_pText2Speeck == NULL )
  88. return -1;
  89. HRESULT hr = g_pText2Speeck->Save2Wav(lpText, lpWavFileName);
  90. if (FAILED(hr))
  91. return -2;
  92. return 0;
  93. }
  94. extern "C" INT PASCAL EXPORT SetSpeech(IN INT nVoiceIndex, IN INT nSpeeckRate, IN INT nSpeeckVolume, IN INT nSpStreamFormat)
  95. {
  96. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  97. // 此处为普通函数体
  98. if ( g_pText2Speeck == NULL )
  99. {
  100. WriteTextLog(_T("g_pText2Speeck = null"));
  101. return -1;
  102. }
  103. WriteTextLog(_T("参数:%d,%d,%d,%d"), nVoiceIndex, nSpeeckRate, nSpeeckVolume, nSpStreamFormat);
  104. HRESULT hr = S_OK;
  105. hr = g_pText2Speeck->SetVoice(nVoiceIndex);
  106. if (FAILED(hr))
  107. {
  108. WriteTextLog(_T("g_pText2Speeck->SetVoice(nVoiceIndex),%ld"), hr);
  109. return -2;
  110. }
  111. hr = g_pText2Speeck->SetRate(nSpeeckRate);
  112. if (FAILED(hr))
  113. {
  114. WriteTextLog(_T("g_pText2Speeck->SetRate(nVoiceIndex),%ld"), hr);
  115. return -3;
  116. }
  117. hr = g_pText2Speeck->SetVolume(nSpeeckVolume);
  118. if (FAILED(hr))
  119. {
  120. WriteTextLog(_T("g_pText2Speeck->SetVolume(nVoiceIndex),%ld"), hr);
  121. return -4;
  122. }
  123. SPSTREAMFORMAT spsf = (SPSTREAMFORMAT)nSpStreamFormat;
  124. hr = g_pText2Speeck->SetOutAudio(spsf);
  125. if (FAILED(hr))
  126. {
  127. WriteTextLog(_T("g_pText2Speeck->SetOutAudio(nVoiceIndex),%ld"), hr);
  128. return -5;
  129. }
  130. return 0;
  131. }
  132. extern "C" INT PASCAL EXPORT Speak(IN LPCTSTR lpText)
  133. {
  134. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  135. // 此处为普通函数体
  136. if ( g_pText2Speeck == NULL || lpText == NULL )
  137. return -1;
  138. HRESULT hr = S_OK;
  139. hr = g_pText2Speeck->Speak(lpText);
  140. if (FAILED(hr))
  141. return -2;
  142. return 0;
  143. }
  144. extern "C" INT PASCAL EXPORT Play(IN LPCTSTR lpWavFileName)
  145. {
  146. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  147. // 此处为普通函数体
  148. if ( g_pText2Speeck == NULL || lpWavFileName == NULL || !PathFileExists(lpWavFileName))
  149. return -1;
  150. HRESULT hr = S_OK;
  151. hr = g_pText2Speeck->PlayWav(lpWavFileName);
  152. if (FAILED(hr))
  153. return -2;
  154. return 0;
  155. }
  156. extern "C" INT PASCAL EXPORT GetVoiceCount()
  157. {
  158. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  159. // 此处为普通函数体
  160. if ( g_pText2Speeck == NULL )
  161. return -1;
  162. return g_pText2Speeck->GetVoiceCount();
  163. }
  164. extern "C" BOOL PASCAL EXPORT GetVoiceName(OUT LPTSTR lpVoiceName, IN INT nBuffLen, IN INT nVoiceIndex )
  165. {
  166. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  167. // 此处为普通函数体
  168. if ( lpVoiceName == NULL || nBuffLen <= 0 )
  169. return FALSE;
  170. if ( g_pText2Speeck == NULL )
  171. return FALSE;
  172. WCHAR *pVoiceName = NULL;
  173. HRESULT hr = g_pText2Speeck->GetVoice(&pVoiceName, nVoiceIndex);
  174. if ( FAILED(hr) || pVoiceName == NULL)
  175. return FALSE;
  176. #ifdef UNICODE
  177. _stprintf_s(lpVoiceName, nBuffLen, _T("%s"), pVoiceName);
  178. #else
  179. INT cbMultiByte = WideCharToMultiByte(CP_OEMCP, 0, pVoiceName, -1, NULL, 0, NULL, NULL);
  180. if ( cbMultiByte == 0 )
  181. return FALSE;
  182. CHAR *pMultiByte = new CHAR[cbMultiByte+1];
  183. memset(pMultiByte, 0, cbMultiByte+1);
  184. INT nRealWrite = WideCharToMultiByte(CP_OEMCP, 0, pVoiceName, -1, pMultiByte, cbMultiByte, NULL, NULL);
  185. if ( nRealWrite != cbMultiByte )
  186. {
  187. if (pMultiByte)
  188. delete []pMultiByte;
  189. return FALSE;
  190. }
  191. _stprintf_s(lpVoiceName, nBuffLen, _T("%s"), pMultiByte);
  192. if (pMultiByte)
  193. delete []pMultiByte;
  194. #endif
  195. return TRUE;
  196. }