组件

TtsCore.cpp 4.9KB

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