00001 #ifndef SPEECH_TO_TEXT_ACTION_HANDLER_H_ 00002 #define SPEECH_TO_TEXT_ACTION_HANDLER_H_ 00003 00004 #include <atomic> 00005 00006 #include "actionlib/server/simple_action_server.h" 00007 #include "gcloud_speech_msgs/SpeechToTextAction.h" 00008 #include "gcloud_speech_msgs/RecognitionHypothesis.h" 00009 #include "gcloud_speech_msgs/LinearPcm16Le16000Audio.h" 00010 00011 #include "cogrob/cloud/speech/audio_sample.h" 00012 #include "cogrob/cloud/speech/google_speech_interface.h" 00013 #include "third_party/glog.h" 00014 #include "util/simple_thread_safe_queue.h" 00015 00016 namespace gcloud_speech { 00017 00018 class SpeechToTextActionHandler { 00019 public: 00020 // Constructor: recognizer is a GoogleSpeechRecognizer that will be used to 00021 // contact Google Speech API and do speech recognition, simple_action_server 00022 // is a SimpleActionServer handle that interacts with client. 00023 SpeechToTextActionHandler( 00024 cogrob::cloud::speech::GoogleSpeechRecognizerInterface* recognizer, 00025 actionlib::SimpleActionServer<gcloud_speech_msgs::SpeechToTextAction>* 00026 simple_action_server); 00027 00028 // ROS subscriber callback for LinearPcm16Le16000Audio. 00029 void AudioMsgCallback( 00030 const gcloud_speech_msgs::LinearPcm16Le16000Audio::ConstPtr& msg); 00031 00032 // ROS callback for SimpleActionServer. 00033 void ExecuteSpeechToTextAction( 00034 const gcloud_speech_msgs::SpeechToTextGoalConstPtr& goal); 00035 00036 private: 00037 // audio_queue_ contains audio samples, and will be populated by 00038 // AudioMsgCallback function. 00039 cogrob::cloud::speech::AudioQueue audio_queue_; 00040 00041 // is_active_ indicates whether the action server is active. This handler will 00042 // only record audio if the action server is active. 00043 std::atomic_bool is_active_; 00044 00045 // result_queue_ contains results from recognizer_. 00046 util::SimpleThreadSafeQueue<cogrob::cloud::speech::RecognitionResult> 00047 result_queue_; 00048 00049 cogrob::cloud::speech::GoogleSpeechRecognizerInterface* recognizer_; 00050 actionlib::SimpleActionServer<gcloud_speech_msgs::SpeechToTextAction>* 00051 simple_action_server_; 00052 }; 00053 00054 } // namespace gcloud_speech 00055 00056 #endif // SPEECH_TO_TEXT_ACTION_HANDLER_H_