| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #include "TtsBaidu.h"
- #include"Md5.h"
- #include"ttsapi/speech.h"
- //#include <unistd.h>
- #include <boost/filesystem.hpp>
- namespace fs = boost::filesystem;
- TtsBaidu::TtsBaidu()
- {
- }
- TtsBaidu::~TtsBaidu()
- {
- }
- void TtsBaidu::Init(std::string ttsPath)
- {
- m_ttsPath = ttsPath;
- m_ttsPath = m_ttsPath + "/";
- }
- bool TtsBaidu::TextToAudio(std::string Content, std::string& FileName)
- {
- aip::Speech g_client(app_id, api_key, secret_key);
- std::ofstream ofile;
- std::string file_ret;
- md5space::MD5 md(Content);
- std::string ContentMd5 = md.getMd5();
- ContentMd5.append(".mp3");
- /*ContentMd5.append(".wav");*/
- std::string AudioName = ContentMd5;
- std::string resultName = m_ttsPath + AudioName;
- //if ((access(resultName.c_str(), 0)) != -1) //若文件已存在不再转换
- //{
- // FileName = resultName;
- // return true;
- //}
- fs::path filePath = (resultName);
- if (fs::exists(filePath))
- {
- FileName = resultName;
- return true;
- }
- ofile.open(resultName, std::ios::out | std::ios::binary);
- g_client.text2audio(Content, aip::null, file_ret);
- Json::Value result = g_client.text2audio(Content, aip::null, file_ret);
- // 如果error_code字段为空则说明合成成功,返回mp3文件内容,否则json数据为相应的错误反回结果
- if (result["error_code"] == Json::nullValue)
- {
- ofile << file_ret;
- FileName = resultName;
- return true;
- }
- return false;
- }
|