RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
speech_detection_google.hpp
Go to the documentation of this file.
1 #ifndef RAPP_CLOUD_SPEECH_TO_TEXT_GOOGLE
2 #define RAPP_CLOUD_SPEECH_TO_TEXT_GOOGLE
3 #include "includes.ihh"
4 namespace rapp {
5 namespace cloud {
14 {
15 public:
25  const std::shared_ptr<rapp::object::audio> file,
26  const std::string language,
27  const std::string user,
28  std::function<void(std::vector<std::string>,
29  std::vector<std::string>)> callback
30  )
31  : rapp::services::asio_service_http (), delegate_(callback)
32  {
33  assert(file);
34  std::string boundary = random_boundary();
35  std::string fname = random_boundary() + file->extension();
36  boost::property_tree::ptree tree;
37  tree.put("language", language);
38  tree.put("user", user);
39  tree.put("audio_source", file->audio_source());
40  std::stringstream ss;
41  boost::property_tree::write_json(ss, tree, false);
42  post_ += "--" + boundary + "\r\n"
43  + "Content-Disposition: form-data; name=\"file_uri\"; filename=\""+fname+"\"\r\n"
44  + "Content-Transfer-Encoding: binary\r\n\r\n";
45  auto bytes = file->bytearray();
46  post_.insert( post_.end(), bytes.begin(), bytes.end() );
47  post_ += "\r\n" + "--" + boundary + "--";
48  header_ = "POST /hop/speech_detection_google HTTP/1.1\r\n";
49  header_ += "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n\r\n";
50  callback_ = std::bind(&speech_detection_google::handle_reply, this, std::placeholders::_1);
51  }
52 private:
56  void handle_reply(std::string json)
57  {
58  std::stringstream ss(json);
59  std::vector<std::string> words;
60  std::vector<std::string> alternatives;
61  try {
62  boost::property_tree::ptree tree;
63  boost::property_tree::read_json(ss, tree);
64  // JSON response is: { words: [], alternatives: [], error: '' }
65  for (auto child : tree.get_child("words")) {
66  words.push_back(child.second.get_value<std::string>());
67  }
68  for (auto child : tree.get_child("alternatives")) {
69  alternatives.push_back(child.second.get_value<std::string>());
70  }
71  // Check for error response from platform
72  for (auto child : tree.get_child("error")) {
73  const std::string value = child.second.get_value<std::string>();
74  if (!value.empty()) {
75  std::cerr << "speech_detection_google error: " << value << std::endl;
76  }
77  }
78  }
79  catch (boost::property_tree::json_parser::json_parser_error & je) {
80  std::cerr << "speech_detection_google::handle_reply Error parsing: " << je.filename()
81  << " on line: " << je.line() << std::endl;
82  std::cerr << je.message() << std::endl;
83  }
84  delegate_(words, alternatives);
85  }
87  std::function<void(std::vector<std::string>, std::vector<std::string>)> delegate_;
88 };
89 }
90 }
91 #endif
std::string header_
Header that will be used.
std::string random_boundary() const
Create a random boundary for the multipart/form in HTTP.
delegate speech-to-text to Google via RAPP
speech_detection_google(const std::shared_ptr< rapp::object::audio > file, const std::string language, const std::string user, std::function< void(std::vector< std::string >, std::vector< std::string >)> callback)
construct the handler which will query the Google API
std::string post_
Actual post Data.
void handle_reply(std::string json)
handle rapp-platform JSON reply
std::function< void(std::string)> callback_
Callback Handler - use with std::bind or boost variant.
base class for asynchronous http websockets used for connecting to cloud services ...
std::function< void(std::vector< std::string >, std::vector< std::string >)> delegate_
The callback called upon completion of receiving the detected words and alternatives.