linux版本中间件

TtsBaidu.cpp 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "TtsBaidu.h"
  2. #include"Md5.h"
  3. #include"ttsapi/speech.h"
  4. //#include <unistd.h>
  5. #include <boost/filesystem.hpp>
  6. namespace fs = boost::filesystem;
  7. TtsBaidu::TtsBaidu()
  8. {
  9. }
  10. TtsBaidu::~TtsBaidu()
  11. {
  12. }
  13. void TtsBaidu::Init(std::string ttsPath)
  14. {
  15. m_ttsPath = ttsPath;
  16. m_ttsPath = m_ttsPath + "/";
  17. }
  18. bool TtsBaidu::TextToAudio(std::string Content, std::string& FileName)
  19. {
  20. aip::Speech g_client(app_id, api_key, secret_key);
  21. std::ofstream ofile;
  22. std::string file_ret;
  23. md5space::MD5 md(Content);
  24. std::string ContentMd5 = md.getMd5();
  25. ContentMd5.append(".mp3");
  26. /*ContentMd5.append(".wav");*/
  27. std::string AudioName = ContentMd5;
  28. std::string resultName = m_ttsPath + AudioName;
  29. //if ((access(resultName.c_str(), 0)) != -1) //若文件已存在不再转换
  30. //{
  31. // FileName = resultName;
  32. // return true;
  33. //}
  34. fs::path filePath = (resultName);
  35. if (fs::exists(filePath))
  36. {
  37. FileName = resultName;
  38. return true;
  39. }
  40. ofile.open(resultName, std::ios::out | std::ios::binary);
  41. g_client.text2audio(Content, aip::null, file_ret);
  42. Json::Value result = g_client.text2audio(Content, aip::null, file_ret);
  43. // 如果error_code字段为空则说明合成成功,返回mp3文件内容,否则json数据为相应的错误反回结果
  44. if (result["error_code"] == Json::nullValue)
  45. {
  46. ofile << file_ret;
  47. FileName = resultName;
  48. return true;
  49. }
  50. return false;
  51. }