linux版本中间件

speech.h 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #pragma once
  2. #ifndef __AIP_SPEECH_H__
  3. #define __AIP_SPEECH_H__
  4. #include "base/base.h"
  5. #include "json/json.h"
  6. namespace aip {
  7. class Speech: public AipBase
  8. {
  9. public:
  10. std::string _asr = "https://vop.baidu.com/server_api";
  11. std::string _tts = "http://tsn.baidu.com/text2audio";
  12. Speech(const std::string app_id, const std::string & ak, const std::string & sk): AipBase(app_id, ak, sk)
  13. {
  14. }
  15. Json::Value request_asr(
  16. std::string const & url,
  17. Json::Value & data)
  18. {
  19. std::string response;
  20. Json::Value obj;
  21. int status_code = this->client.post(url, nullptr, data, nullptr, &response);
  22. if (status_code != CURLcode::CURLE_OK) {
  23. obj[aip::CURL_ERROR_CODE] = status_code;
  24. return obj;
  25. }
  26. std::string error;
  27. std::unique_ptr<Json::CharReader> reader(crbuilder.newCharReader());
  28. reader->parse(response.data(), response.data() + response.size(), &obj, &error);
  29. return obj;
  30. }
  31. Json::Value request_tts(
  32. const std::string url,
  33. std::map<std::string, std::string> & data,
  34. std::string & file_content)
  35. {
  36. std::string response;
  37. Json::Value obj;
  38. Json::Value file_json;
  39. int status_code = this->client.post(url, nullptr, data, nullptr, &response);
  40. if (status_code != CURLcode::CURLE_OK) {
  41. obj[aip::CURL_ERROR_CODE] = status_code;
  42. return obj;
  43. }
  44. file_content = response;
  45. return obj;
  46. }
  47. Json::Value recognize(const std::string voice_binary, const std::string & format, const int & rate, std::map<std::string, std::string> const & options)
  48. {
  49. Json::Value data;
  50. std::map<std::string, std::string>::const_iterator it;
  51. for(it=options.begin(); it!=options.end(); it++)
  52. {
  53. data[it->first] = it->second;
  54. }
  55. std::string token = this->getAccessToken();
  56. data["speech"] = base64_encode(voice_binary.c_str(), (int) voice_binary.size());
  57. data["format"] = format;
  58. data["rate"] = std::to_string(rate);
  59. data["channel"] = "1";
  60. data["token"] = token;
  61. data["cuid"] = this->getAk();
  62. data["len"] = (int) voice_binary.size();
  63. Json::Value result = this->request_asr(_asr, data);
  64. return result;
  65. }
  66. Json::Value recognize_url(const std::string & url,
  67. const std::string & callback, const std::string & format,
  68. const int & rate,
  69. std::map<std::string, std::string> options)
  70. {
  71. Json::Value data;
  72. std::map<std::string, std::string>::iterator it;
  73. for(it=options.begin(); it!=options.end(); it++)
  74. {
  75. data[it->first] = it->second;
  76. }
  77. std::string token = this->getAccessToken();
  78. data["url"] = url;
  79. data["callback"] = callback;
  80. data["format"] = format;
  81. data["rate"] = std::to_string(rate);
  82. data["channel"] = 1;
  83. data["token"] = token;
  84. data["cuid"] = this->getAk();
  85. Json::Value result = this->request_asr(_asr, data);
  86. return result;
  87. }
  88. Json::Value text2audio(const std::string & text, std::map<std::string, std::string> const & options, std::string & file_content)
  89. {
  90. std::map<std::string, std::string> data;
  91. std::map<std::string, std::string>::const_iterator it;
  92. for(it = options.begin(); it !=options.end(); it++)
  93. {
  94. data[it->first] = it->second;
  95. }
  96. std::string token = this->getAccessToken();
  97. data["tex"] = text;
  98. data["lan"] = "zh";
  99. data["ctp"] = "1";
  100. data["tok"] = token;
  101. data["cuid"] = this->getAk();
  102. Json::Value result = this->request_tts(_tts, data, file_content);
  103. return result;
  104. }
  105. };
  106. }
  107. #endif