1 #ifndef RAPP_CLOUD_SPEECH_TO_TEXT_GOOGLE
2 #define RAPP_CLOUD_SPEECH_TO_TEXT_GOOGLE
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
36 boost::property_tree::ptree tree;
37 tree.put(
"language", language);
38 tree.put(
"user", user);
39 tree.put(
"audio_source", file->audio_source());
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";
58 std::stringstream ss(json);
59 std::vector<std::string> words;
60 std::vector<std::string> alternatives;
62 boost::property_tree::ptree tree;
63 boost::property_tree::read_json(ss, tree);
65 for (
auto child : tree.get_child(
"words")) {
66 words.push_back(child.second.get_value<std::string>());
68 for (
auto child : tree.get_child(
"alternatives")) {
69 alternatives.push_back(child.second.get_value<std::string>());
72 for (
auto child : tree.get_child(
"error")) {
73 const std::string value = child.second.get_value<std::string>();
75 std::cerr <<
"speech_detection_google error: " << value << std::endl;
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;
87 std::function<void(std::vector<std::string>, std::vector<std::string>)>
delegate_;
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.