1 #ifndef RAPP_CLOUD_SPEECH_TO_TEXT_SPHINX4
2 #define RAPP_CLOUD_SPEECH_TO_TEXT_SPHINX4
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,
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;
51 grammar_array.push_back(std::make_pair(
"", child));
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;
58 words_array.push_back(std::make_pair(
"", child));
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;
65 sentence_array.push_back(std::make_pair(
"", child));
67 tree.add_child(
"sentences", sentence_array);
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";
87 std::stringstream ss(json);
88 std::vector<std::string> words;
90 boost::property_tree::ptree tree;
91 boost::property_tree::read_json(ss, tree);
93 for (
auto child : tree.get_child(
"words")) {
94 words.push_back(child.second.get_value<std::string>());
97 for (
auto child : tree.get_child(
"error")) {
98 const std::string value = child.second.get_value<std::string>();
100 std::cerr <<
"speech_detection_sphinx4 error: " << value << std::endl;
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;
113 std::function<void(std::vector<std::string> words)>
delegate_;
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 ...