RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
speech_detection_sphinx4.hpp
Go to the documentation of this file.
1 #ifndef RAPP_CLOUD_SPEECH_TO_TEXT_SPHINX4
2 #define RAPP_CLOUD_SPEECH_TO_TEXT_SPHINX4
3 #include "includes.ihh"
4 namespace rapp {
5 namespace cloud {
14 {
15 public:
28  const std::shared_ptr<rapp::object::audio> file,
29  const std::string language,
30  const std::string user,
31  const std::vector<std::string> grammar,
32  const std::vector<std::string> words,
33  const std::vector<std::string> sentences,
34  std::function<void(std::vector<std::string> words)> callback,
35  std::string token
36  )
37  : asio_service_http(token), delegate_(callback)
38  {
39  assert(file);
40  // Create a new random boundary
41  std::string boundary = random_boundary();
42  std::string fname = random_boundary() + file->extension();
43  boost::property_tree::ptree tree;
44  tree.put("language", language);
45  tree.put("user", user);
46  tree.put("audio_source", file->audio_source());
47  boost::property_tree::ptree grammar_array;
48  for (const auto gram : grammar) {
49  boost::property_tree::ptree child;
50  child.put("", gram);
51  grammar_array.push_back(std::make_pair("", child));
52  }
53  tree.add_child("grammar", grammar_array);
54  boost::property_tree::ptree words_array;
55  for (const auto word : words) {
56  boost::property_tree::ptree child;
57  child.put("", word);
58  words_array.push_back(std::make_pair("", child));
59  }
60  tree.add_child("words", words_array);
61  boost::property_tree::ptree sentence_array;
62  for (const auto sents : sentences) {
63  boost::property_tree::ptree child;
64  child.put("", sents);
65  sentence_array.push_back(std::make_pair("", child));
66  }
67  tree.add_child("sentences", sentence_array);
68  std::stringstream ss;
69  boost::property_tree::write_json(ss, tree, false);
70  post_ += "--" + boundary + "\r\n"
71  + "Content-Disposition: form-data; name=\"file_uri\"; filename=\""+fname+"\"\r\n"
72  + "Content-Transfer-Encoding: binary\r\n\r\n";
73  auto bytes = file->bytearray();
74  post_.insert( post_.end(), bytes.begin(), bytes.end() );
75  post_ += "\r\n--" + boundary + "--";
76  header_ = "POST /hop/speech_detection_sphinx4 HTTP/1.1\r\n";
77  header_ += "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n\r\n";
78  callback_ = std::bind(&speech_detection_sphinx4::handle_reply, this, std::placeholders::_1);
79  }
80 
81 private:
85  void handle_reply(std::string json)
86  {
87  std::stringstream ss(json);
88  std::vector<std::string> words;
89  try {
90  boost::property_tree::ptree tree;
91  boost::property_tree::read_json(ss, tree);
92  // JSON response is: { words: [], error: '' }
93  for (auto child : tree.get_child("words")) {
94  words.push_back(child.second.get_value<std::string>());
95  }
96  // Check for error response from api.rapp.cloud
97  for (auto child : tree.get_child("error")) {
98  const std::string value = child.second.get_value<std::string>();
99  if (!value.empty()) {
100  std::cerr << "speech_detection_sphinx4 error: " << value << std::endl;
101  }
102  }
103  }
104  catch(boost::property_tree::json_parser::json_parser_error & je) {
105  std::cerr << "speech_detection_sphinx4::handle_reply Error parsing: " << je.filename()
106  << " on line: " << je.line() << std::endl;
107  std::cerr << je.message() << std::endl;
108  }
109  delegate_(words);
110  }
111 
113  std::function<void(std::vector<std::string> words)> delegate_;
114 };
115 }
116 }
117 #endif
std::function< void(std::vector< std::string > words)> delegate_
The callback called upon completion of receiving the detected words.
std::string header_
Header that will be used.
std::string random_boundary() const
Create a random boundary for the multipart/form in HTTP.
speech_detection_sphinx4(const std::shared_ptr< rapp::object::audio > file, const std::string language, const std::string user, const std::vector< std::string > grammar, const std::vector< std::string > words, const std::vector< std::string > sentences, std::function< void(std::vector< std::string > words)> callback, std::string token)
construct a speechToText handler
void handle_reply(std::string json)
handle the rappl-platform JSON reply
std::string post_
Actual post Data.
speech-to-text recognition using CMU sphinx4
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 ...