rospeex.cpp
Go to the documentation of this file.
00001 #include "rospeex_if/rospeex.h"
00002 
00003 #include <stdio.h>
00004 #include <stdint.h>
00005 #include <sstream>
00006 
00007 #include <sys/stat.h>
00008 
00009 #include <ros/ros.h>
00010 #include <ros/console.h>
00011 #include <ros/package.h>
00012 
00013 #include <boost/format.hpp>
00014 #include <boost/filesystem.hpp>
00015 
00016 // include messages
00017 #include "rospeex_msgs/SpeechSynthesisResponse.h"
00018 #include "rospeex_msgs/SpeechSynthesisRequest.h"
00019 #include "rospeex_msgs/SpeechSynthesisHeader.h"
00020 #include "rospeex_msgs/SpeechSynthesisState.h"
00021 #include "rospeex_msgs/SpeechRecognitionResponse.h"
00022 #include "rospeex_msgs/SpeechRecognitionRequest.h"
00023 #include "rospeex_msgs/SignalProcessingResponse.h"
00024 #include "rospeex_msgs/SpeechRecognitionConfig.h"
00025 
00026 using namespace rospeex;
00027 using namespace rospeex_msgs;
00028 
00029 #define SR_RESPONSE_TOPIC_NAME "sr_res"
00030 #define SS_RESPONSE_TOPIC_NAME "ss_res"
00031 #define SPI_RESPONSE_TOPIC_NAME "spi_res"
00032 #define SR_REQUEST_TOPIC_NAME "sr_req"
00033 #define SS_REQUEST_TOPIC_NAME "ss_req"
00034 #define SS_STATE_TOPIC_NAME "ss_state"
00035 #define SPI_CONFIG_SERVICE_NAME "spi_config"
00036 
00037 
00038 class Interface::Impl
00039 {
00040 public:
00041         Impl()
00042                 : sr_func_()
00043                 , ss_func_()
00044                 , ss_request_id_(0)
00045                 , sr_request_id_(0)
00046                 , sr_pub_()
00047                 , ss_pub_()
00048                 , ss_state_pub_()
00049                 , sr_sub_()
00050                 , ss_sub_()
00051                 , spi_sub_()
00052                 , ss_enable_()
00053                 , sr_enable_()
00054                 , spi_enable_()
00055         {}
00056 
00061         void SPIResponse( const SignalProcessingResponse::ConstPtr& response );
00062 
00067         void SRResponse( const SpeechRecognitionResponse::ConstPtr& response );
00068 
00073         void SSResponse( const SpeechSynthesisResponse::ConstPtr& response );
00074 
00080         void playPackageSound( const std::string& package_name );
00081 
00086         void playSound( const std::string& file_name );
00087 
00092         void sendSpeechSynthesisState( bool state );
00093 
00094         uint32_t ss_request_id_;                                        
00095         uint32_t sr_request_id_;                                        
00096 
00097         SpeechRecognizeCallback sr_func_;                       
00098         SpeechSynthesisCallback ss_func_;                       
00099 
00100         ros::Publisher sr_pub_;                                         
00101         ros::Publisher ss_pub_;                                         
00102         ros::Publisher ss_state_pub_;                           
00103 
00104         ros::Subscriber sr_sub_;                                        
00105         ros::Subscriber ss_sub_;                                        
00106         ros::Subscriber spi_sub_;                                       
00107 
00108         std::string spi_engine_;                                        
00109         std::string spi_language_;                                      
00110 
00111         bool ss_enable_, sr_enable_, spi_enable_;
00112 };
00113 
00114 
00115 void Interface::Impl::SPIResponse( const SignalProcessingResponse::ConstPtr& response )
00116 {
00117         if ( sr_func_.empty() == false ) {
00118                 playPackageSound("accept.wav");
00119                 sr_request_id_++;
00120 
00121         } else {
00122                 // Do Nothing
00123         }
00124 }
00125 
00126 
00127 void Interface::Impl::SRResponse( const SpeechRecognitionResponse::ConstPtr& response )
00128 {
00129 
00130         if( response->message == "" ) {
00131                 playPackageSound("nomessage.wav");
00132 
00133         } else if ( spi_enable_ != true ) {
00134                 playPackageSound("accept.wav");
00135         }
00136 
00137         ros::NodeHandle n;
00138         std::string user = response->header.user;
00139         bool check_user = ( user == "spi" || user == n.getNamespace() );
00140         if ( sr_func_.empty() == false && check_user == true) {
00141                 sr_func_( response->message );
00142 
00143         } else {
00144                 // Do Nothing
00145         }
00146 }
00147 
00148 
00149 void Interface::Impl::SSResponse( const SpeechSynthesisResponse::ConstPtr& response )
00150 {
00151         ros::NodeHandle n;
00152         if ( ss_func_.empty() == false && response->header.user == n.getNamespace() ) {
00153                 ss_func_( response->data );
00154 
00155         } else {
00156                 // Do Nothing
00157         }
00158 }
00159 
00160 
00161 Interface::Interface()
00162         : impl_( new Impl() )
00163 {}
00164 
00165 
00166 Interface::~Interface()
00167 {}
00168 
00169 
00170 bool Interface::init( bool ss_enable, bool sr_enable, bool spi_enable )
00171 {
00172         impl_ = boost::shared_ptr<Impl>(new Impl());
00173 
00174         ros::NodeHandle n;
00175         impl_->ss_enable_ = ss_enable;
00176         impl_->sr_enable_ = sr_enable;
00177         impl_->spi_enable_ = spi_enable;
00178 
00179         // init speech recognize
00180         if ( sr_enable == true ) {
00181                 ROS_INFO("enable speeech recognition.");
00182                 impl_->sr_pub_ = n.advertise<SpeechRecognitionRequest>(SR_REQUEST_TOPIC_NAME, 10);
00183                 boost::function<void(const SpeechRecognitionResponse::ConstPtr&)> sr_func = boost::bind(&Impl::SRResponse, this->impl_, _1);
00184                 impl_->sr_sub_ = n.subscribe(SR_RESPONSE_TOPIC_NAME, 10, sr_func);
00185         }
00186 
00187         // init speech synthesis
00188         if ( ss_enable == true ) {
00189                 ROS_INFO("enable speech synthsis.");
00190                 impl_->ss_pub_ = n.advertise<SpeechSynthesisRequest>(SS_REQUEST_TOPIC_NAME, 10);
00191                 boost::function<void(const SpeechSynthesisResponse::ConstPtr&)> ss_func = boost::bind(&Impl::SSResponse, this->impl_, _1);
00192                 impl_->ss_sub_ = n.subscribe(SS_RESPONSE_TOPIC_NAME, 10, ss_func);
00193         }
00194 
00195         // init signal processing interface
00196         if ( spi_enable == true ) {
00197                 ROS_INFO("enable signal processing interface.");
00198                 boost::function<void(const SignalProcessingResponse::ConstPtr&)> spi_func = boost::bind(&Impl::SPIResponse, this->impl_, _1);
00199                 impl_->ss_state_pub_ = n.advertise<SpeechSynthesisState>(SS_STATE_TOPIC_NAME, 10);
00200                 impl_->spi_sub_ = n.subscribe(SPI_RESPONSE_TOPIC_NAME, 10, spi_func);
00201         }
00202 
00203         return true;
00204 }
00205 
00206 
00207 void Interface::Impl::playPackageSound( const std::string& sound_file )
00208 {
00209         // set spi input off
00210         sendSpeechSynthesisState(true);
00211 
00212         // get sound path
00213         std::string package_path = ros::package::getPath("rospeex_if");
00214         boost::filesystem::path sound_path(package_path + "/sound/" + sound_file);
00215         playSound(sound_path.string());
00216 
00217         // set spi input on
00218         sendSpeechSynthesisState(false);
00219 }
00220 
00221 
00222 void Interface::Impl::sendSpeechSynthesisState( bool state )
00223 {
00224         // create header
00225         if ( ss_state_pub_ ) {
00226                 ros::NodeHandle n;
00227                 SpeechSynthesisState ss_state;
00228                 ss_state.header.language = "";
00229                 ss_state.header.voice_font = "";
00230                 ss_state.header.engine = "";
00231                 ss_state.header.user = n.getNamespace();
00232                 ss_state.header.request_id = "0";
00233                 ss_state.header.request_type = SpeechSynthesisHeader::REQUEST_TYPE_SAY;
00234                 ss_state.play_state = state;
00235                 ss_state_pub_.publish(ss_state);
00236         }
00237 }
00238 
00239 
00240 void Interface::Impl::playSound( const std::string& file_path )
00241 {
00242         // check file exists
00243         boost::system::error_code error;
00244         bool is_exist = boost::filesystem::exists(file_path, error);
00245 
00246         if ( error ) {
00247                 ROS_ERROR("file[%s] open error. %s",file_path.c_str() ,error.message().c_str());
00248                 return;
00249         }
00250 
00251         if ( !is_exist ) {
00252                 ROS_ERROR("[%s] is not exist.", file_path.c_str());
00253                 return;
00254         }
00255 
00256         // play sound if sound file exist.
00257         std::string command = (boost::format("aplay -q %s") % file_path).str();
00258         int ret = system(command.c_str());
00259         if(ret != 0){
00260                 ROS_ERROR("playing sound error.");
00261         }
00262 }
00263 
00264 
00265 void Interface::say( const std::string& msg,
00266                                 const std::string& language,
00267                                 const std::string& engine,
00268                                 const std::string& voice_font)
00269 {
00270         if ( impl_->ss_pub_ ) {
00271                 ros::NodeHandle n;
00272                 std::stringstream ss;
00273                 ss << impl_->ss_request_id_;
00274                 SpeechSynthesisRequest request;
00275                 request.header.language = language;
00276                 request.header.voice_font = voice_font;
00277                 request.header.engine = engine;
00278                 request.header.user = n.getNamespace();
00279                 request.header.request_id = ss.str();
00280                 request.header.request_type = SpeechSynthesisHeader::REQUEST_TYPE_SAY;
00281                 request.message = msg;
00282                 impl_->ss_pub_.publish(request);
00283                 impl_->ss_request_id_++;
00284 
00285         } else {
00286                 ROS_INFO("ss interface is disabled.");
00287         }
00288 }
00289 
00290 
00291 void Interface::tts( const std::string& msg,
00292                                 const std::string& file,
00293                                 const std::string& language,
00294                                 const std::string& engine,
00295                                 const std::string& voice_font )
00296 {
00297         if ( impl_->ss_pub_ ) {
00298                 ros::NodeHandle n;
00299                 std::stringstream ss;
00300                 ss << impl_->ss_request_id_;
00301                 SpeechSynthesisRequest request;
00302                 request.header.language = language;
00303                 request.header.voice_font = voice_font;
00304                 request.header.engine = engine;
00305                 request.header.user = n.getNamespace();
00306                 request.header.request_id = ss.str();
00307                 request.header.request_type = SpeechSynthesisHeader::REQUEST_TYPE_TTS;
00308                 request.message = msg;
00309                 impl_->ss_pub_.publish(request);
00310                 impl_->ss_request_id_++;
00311 
00312         } else {
00313                 ROS_INFO("ss interface is disabled.");
00314         }
00315 }
00316 
00317 
00318 void Interface::recognize( const std::string& data,
00319                                                 const std::string& language,
00320                                                 const std::string& engine )
00321 {
00322         if ( impl_->sr_pub_ ) {
00323                 ros::NodeHandle n;
00324                 std::stringstream ss;
00325                 ss << impl_->sr_request_id_;
00326                 SpeechRecognitionRequest request;
00327                 request.header.language = language;
00328                 request.header.engine = engine;
00329                 request.header.user = n.getNamespace();
00330                 request.header.request_id = ss.str();
00331                 request.data = data;
00332                 impl_->sr_pub_.publish(request);
00333                 impl_->sr_request_id_++;
00334 
00335         } else {
00336                 ROS_INFO("sr interface is disabled.");
00337         }
00338 }
00339 
00340 
00341 void Interface::registerSRResponse( const SpeechRecognizeCallback& callback )
00342 {
00343         impl_->sr_func_ = callback;
00344 }
00345 
00346 
00347 void Interface::registerSRResponse( void(*fp)(const std::string& msg) )
00348 {
00349         impl_->sr_func_ = SpeechRecognizeCallback(fp);
00350 }
00351 
00352 
00353 void Interface::registerSSResponse( const SpeechSynthesisCallback& callback )
00354 {
00355         impl_->ss_func_ = callback;
00356 }
00357 
00358 
00359 void Interface::registerSSResponse( void(*fp)(const std::string& data) )
00360 {
00361         impl_->ss_func_ = SpeechSynthesisCallback(fp);
00362 }
00363 
00364 
00365 void Interface::setSPIConfig( const std::string& language, const std::string& engine)
00366 {
00367         impl_->spi_language_ = language;
00368         impl_->spi_engine_ = engine;
00369 
00370         ros::service::waitForService(SPI_CONFIG_SERVICE_NAME, -1);
00371         ros::NodeHandle nh;
00372         ros::ServiceClient client = nh.serviceClient<SpeechRecognitionConfig>(SPI_CONFIG_SERVICE_NAME);
00373         SpeechRecognitionConfig srv;
00374         srv.request.engine = engine;
00375         srv.request.language = language;
00376         if ( client.call(srv) ) {
00377                 ROS_DEBUG("Success to call service %s", SPI_CONFIG_SERVICE_NAME);
00378 
00379         } else {
00380                 ROS_ERROR("Failed to call service %s", SPI_CONFIG_SERVICE_NAME);
00381         }
00382 }
00383 
00384 
00385 void Interface::playSound( const std::string& file_name )
00386 {
00387         impl_->playSound(file_name);
00388 }


rospeex_if
Author(s): Komei Sugiura
autogenerated on Thu Jun 6 2019 18:53:13