升龙物业 老版本 ocx IPO, 加密狗 转值班电话

TtsCore.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "StdAfx.h"
  2. #include "TtsCore.h"
  3. #include "md5c.h"
  4. #include "TtsIbm.h"
  5. #include "TTSjt.h"
  6. SINGLETON_IMPLEMENT(CTtsCore)
  7. ITtsInterface& ITtsInterface::getInstance(void)
  8. {
  9. return CTtsCore::GetInstance();
  10. }
  11. CTtsCore::CTtsCore(void) : m_TTSImmplement(NULL)
  12. {
  13. }
  14. CTtsCore::~CTtsCore(void)
  15. {
  16. close();
  17. }
  18. /*****************************************************************
  19. **【函数名称】 __createTTSPath
  20. **【函数功能】 创建TTS文件路径
  21. **【参数】
  22. **【返回值】
  23. ****************************************************************/
  24. void CTtsCore::__createTTSPath( LPCSTR BufferPath )
  25. {
  26. if(BufferPath != NULL)
  27. {
  28. m_BufferPath = BufferPath;
  29. if(m_BufferPath.Right(1) != _T("\\"))
  30. m_BufferPath += _T("\\");
  31. }
  32. else
  33. m_BufferPath = TTS_WAVE_FILE_PATH;
  34. CFileFind fileFind;
  35. if(!fileFind.FindFile(m_BufferPath))
  36. {
  37. ::CreateDirectory(m_BufferPath, NULL);
  38. }
  39. }
  40. /*****************************************************************
  41. **【函数名称】 __isAudioExisted
  42. **【函数功能】 查找是否存在当前文本串的语音文件
  43. **【参数】 szStr 要转换的文本串
  44. nFileNameLen 语音文件名缓冲区长度
  45. isFile 要转换的音源是否为文件
  46. **【返回值】 szFileName 转换成的语音文件名(全路径)
  47. ****************************************************************/
  48. BOOL CTtsCore::__isAudioExisted( LPTSTR szStr, LPTSTR szFileName, int nFileNameLen, bool isFile )
  49. {
  50. CHAR szMd5String[1024] = { 0 };
  51. // 得到当前文本串的MD5值
  52. if(isFile)
  53. {
  54. MD5File(szStr, szMd5String);
  55. }
  56. else
  57. {
  58. MD5String((UCHAR*)szStr, strlen(szStr), szMd5String);
  59. }
  60. // 生成语音文件名
  61. memset(szFileName, 0, nFileNameLen);
  62. sprintf_s(szFileName, nFileNameLen, _T("%s%s.wav"), m_BufferPath, szMd5String);
  63. // 查找是否存在当前文本串的语音文件
  64. CFileFind fileFind;
  65. if(fileFind.FindFile(szFileName))
  66. return TRUE;
  67. else
  68. return FALSE;
  69. }
  70. /*****************************************************************
  71. **【函数名称】 init
  72. **【函数功能】 初始化TTS
  73. **【参数】 TtsType:TTS类型
  74. ErrorInfo:如果初始化失败,返回失败原因
  75. **【返回值】 true:初始化成功
  76. ****************************************************************/
  77. bool CTtsCore::init( TTS_TYPE TtsType, LPCSTR BufferPath, LPTSTR ErrorInfo )
  78. {
  79. switch (TtsType)
  80. {
  81. case TTS_IFLY:
  82. break;
  83. case TTS_IBM:
  84. m_TTSImmplement = new CTtsIbm;
  85. break;
  86. case TTS_JT:
  87. m_TTSImmplement = new CTtsJt;
  88. break;
  89. default:
  90. ASSERT(FALSE);
  91. }
  92. if(m_TTSImmplement == NULL)
  93. {
  94. lstrcpy(ErrorInfo, _T("生成TTS实例对象失败!"));
  95. return false;
  96. }
  97. if(!m_TTSImmplement->load())
  98. {
  99. lstrcpy(ErrorInfo, _T("加载TTS引擎失败!"));
  100. return false;
  101. }
  102. if(!m_TTSImmplement->init(ErrorInfo))
  103. return false;
  104. __createTTSPath(BufferPath);
  105. return true;
  106. }
  107. /*****************************************************************
  108. **【函数名称】 close
  109. **【函数功能】 接口关闭
  110. **【参数】
  111. **【返回值】
  112. ****************************************************************/
  113. void CTtsCore::close( void )
  114. {
  115. if(m_TTSImmplement == NULL)
  116. return;
  117. m_TTSImmplement->close();
  118. delete m_TTSImmplement;
  119. m_TTSImmplement = NULL;
  120. }
  121. /*****************************************************************
  122. **【函数名称】 setTTSParam
  123. **【函数功能】 设置字符串读得方式比如一零一,一百零一,语速,音量
  124. **【参数】
  125. **【返回值】
  126. ****************************************************************/
  127. bool CTtsCore::setTTSParam( int NumReadType, int Speed, int Volume )
  128. {
  129. if(m_TTSImmplement == NULL)
  130. return false;
  131. return m_TTSImmplement->setParam(NumReadType, Speed, Volume);
  132. }
  133. /*****************************************************************
  134. **【函数名称】 string2Audio
  135. **【函数功能】 Text:需要转换的字符串
  136. AudioName:转换后的文件名
  137. Len:添入的文件名的长度
  138. **【参数】
  139. **【返回值】 成功TRUE,失败FALSE
  140. ****************************************************************/
  141. bool CTtsCore::string2Audio( LPTSTR Text, LPTSTR AudioName, int Len )
  142. {
  143. // 如果已存在当前文本串的语音文件
  144. if(__isAudioExisted(Text, AudioName, Len, false))
  145. return true;
  146. if(m_TTSImmplement == NULL)
  147. return false;
  148. return m_TTSImmplement->string2Audio(Text, AudioName);
  149. }
  150. /*****************************************************************
  151. **【函数名称】 file2Audio
  152. **【函数功能】 FileName:需要转换的文本文件名
  153. AudioName:转换后的文件名
  154. Len:添入的文件名的长度
  155. **【参数】
  156. **【返回值】
  157. ****************************************************************/
  158. bool CTtsCore::file2Audio( LPTSTR FileName, LPTSTR AudioName, int Len )
  159. {
  160. // 如果已存在当前文件的语音文件
  161. if(__isAudioExisted(FileName, AudioName, Len, true))
  162. return true;
  163. if(m_TTSImmplement == NULL)
  164. return false;
  165. return m_TTSImmplement->file2Audio(FileName, AudioName);
  166. }