20 #include <diagnostic_msgs/DiagnosticArray.h> 21 #include <visualization_msgs/Marker.h> 22 #include <std_msgs/String.h> 23 #include <std_srvs/Trigger.h> 24 #include <cob_sound/SayAction.h> 25 #include <cob_sound/PlayAction.h> 26 #include <cob_srvs/SetString.h> 29 #include <boost/filesystem.hpp> 30 #include <boost/lexical_cast.hpp> 63 as_play_(nh_,
ros::this_node::
getName() +
"/play", false)
70 libvlc_media_player_stop(vlc_player_);
71 libvlc_media_player_release(vlc_player_);
72 libvlc_release(vlc_inst_);
77 vlc_inst_ = libvlc_new(0,NULL);
78 if(!vlc_inst_){
ROS_ERROR(
"failed to create libvlc instance");
return false;}
79 vlc_player_ = libvlc_media_player_new(vlc_inst_);
80 if(!vlc_player_){
ROS_ERROR(
"failed to create vlc media player object");
return false;}
91 diagnostics_pub_ = nh_.
advertise<diagnostic_msgs::DiagnosticArray>(
"diagnostics", 1);
94 pubMarker_ = nh_.
advertise<visualization_msgs::Marker>(
"marker",1);
95 fade_volume_ = nh_.
param<
bool>(
"fade_volume",
true);
96 fade_duration_ = nh_.
param<
double>(
"fade_duration", 0.5);
109 if(
play(filename, message))
111 play_feedback_timer_.
start();
115 cob_sound::PlayResult
result;
116 result.success =
false;
117 result.message = message;
127 play_feedback_timer_.
stop();
128 libvlc_media_player_stop(vlc_player_);
130 cob_sound::PlayResult
result;
131 result.success =
false;
132 result.message =
"Action has been preempted";
139 bool ret =
say(goal->text, message);
141 cob_sound::SayResult
result;
142 result.success = ret;
143 result.message = message;
155 cob_srvs::SetString::Response &res )
157 res.success =
say(req.data, res.message);
162 cob_srvs::SetString::Response &res )
164 res.success =
play(req.data, res.message);
166 while((libvlc_media_player_is_playing(vlc_player_) == 1))
169 ROS_DEBUG(
"still playing %s", req.data.c_str());
175 std_srvs::Trigger::Response &res )
180 play_feedback_timer_.
stop();
181 cob_sound::PlayResult
result;
182 result.success =
false;
183 result.message =
"Action has been aborted";
185 libvlc_media_player_stop(vlc_player_);
187 res.message =
"aborted running action";
191 if(libvlc_media_player_is_playing(vlc_player_) == 1)
193 libvlc_media_player_stop(vlc_player_);
195 res.message =
"stopped sound play";
200 res.message =
"nothing there to stop";
207 std_srvs::Trigger::Response &res )
211 res.message =
"Sound muted successfully";
216 std_srvs::Trigger::Response &res )
220 res.message =
"Sound un-muted successfully";
227 say(msg->data.c_str(), message);
233 play(msg->data.c_str(), message);
236 bool say(std::string data, std::string& message)
240 message =
"Sound is set to mute. You will hear nothing.";
245 ROS_INFO(
"Saying: %s", data.c_str());
251 std::string cepstral_voice;
252 std::string cepstral_conf;
253 nh_.
param<std::string>(
"mode",mode,
"festival");
254 nh_.
param<std::string>(
"cepstral_voice",cepstral_voice,
"David");
255 nh_.
param<std::string>(
"cepstral_settings",cepstral_conf,
"\"speech/rate=170\"");
256 if (mode ==
"cepstral")
258 command =
"aoss swift -p " + cepstral_conf +
" -n " + cepstral_voice +
" " + data;
262 command =
"echo " + data +
" | text2wave | aplay -q";
265 int ret = system(command.c_str());
268 message =
"Command say failed to say '" + data +
"' using mode " + mode +
" (system return value: " + boost::lexical_cast<std::string>(ret) +
")";
274 message =
"Say successfull";
278 bool play(std::string filename, std::string &message)
282 message =
"Sound is set to mute. You will hear nothing.";
287 if (filename.empty())
289 message =
"Cannot play because filename is empty.";
294 if ( !boost::filesystem::exists(filename) )
296 message =
"Cannot play '" + filename +
"' because file does not exist.";
301 vlc_media_ = libvlc_media_new_path(vlc_inst_, filename.c_str());
304 message =
"failed to create media for filepath %s", filename.c_str();
309 libvlc_media_player_set_media(vlc_player_, vlc_media_);
310 libvlc_media_release(vlc_media_);
314 message =
"failed to fade out";
321 message =
"failed to fade in";
326 message =
"Play successfull";
339 if (libvlc_media_player_is_playing(vlc_player_) == 1)
341 float perc_done = libvlc_media_player_get_position(vlc_player_);
342 int64_t t = libvlc_media_player_get_time(vlc_player_);
343 cob_sound::PlayFeedback feedback;
344 feedback.position = perc_done;
350 play_feedback_timer_.
stop();
351 cob_sound::PlayResult
result;
352 result.success =
true;
353 result.message =
"Action succeeded";
361 visualization_msgs::Marker marker;
362 marker.header.frame_id =
"base_link";
366 marker.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
367 marker.action = visualization_msgs::Marker::ADD;
368 marker.lifetime =
ros::Duration(std::max(data.size()*0.15, 2.0));
370 marker.pose.position.x = 0.0;
371 marker.pose.position.y = 0.0;
372 marker.pose.position.z = 1.8;
373 marker.pose.orientation.x = 0.0;
374 marker.pose.orientation.y = 0.0;
375 marker.pose.orientation.z = 0.0;
376 marker.pose.orientation.w = 1.0;
377 marker.scale.x = 0.1;
378 marker.scale.y = 0.1;
379 marker.scale.z = 0.1;
380 marker.color.a = 1.0;
381 marker.color.r = 1.0;
382 marker.color.g = 1.0;
383 marker.color.b = 1.0;
389 diagnostic_msgs::DiagnosticStatus status;
390 status.level = level;
391 status.name =
"sound";
392 status.hardware_id =
"sound";
393 status.message = message;
394 diagnostics_.status.push_back(status);
397 diagnostics_pub_.
publish(diagnostics_);
399 diagnostics_.status.resize(0);
404 if(libvlc_media_player_play(vlc_player_) >= 0)
408 while(libvlc_audio_set_volume(vlc_player_,0) != 0)
410 for(
int i = 0; i < 100; i+=5)
412 libvlc_audio_set_volume(vlc_player_,i);
418 while(libvlc_audio_set_volume(vlc_player_,100) != 0)
432 int volume = libvlc_audio_get_volume(vlc_player_);
433 if(libvlc_media_player_is_playing(vlc_player_))
437 for(
int i = volume - (volume%5); i>=0; i-=5)
439 libvlc_audio_set_volume(vlc_player_,i);
449 int main(
int argc,
char** argv)
boost::shared_ptr< const Goal > acceptNewGoal()
void publishFeedback(const FeedbackConstPtr &feedback)
diagnostic_msgs::DiagnosticArray diagnostics_
ros::Subscriber sub_play_
void timer_cb(const ros::TimerEvent &)
void publish(const boost::shared_ptr< M > &message) const
bool service_cb_say(cob_srvs::SetString::Request &req, cob_srvs::SetString::Response &res)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void publish_marker(std::string data)
void as_preempt_cb_play_()
void timer_play_feedback_cb(const ros::TimerEvent &)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
std::string getName(void *handle)
libvlc_media_player_t * vlc_player_
ros::Publisher pubMarker_
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
void publish_diagnostics(unsigned char level, std::string message)
void setPreempted(const Result &result=Result(), const std::string &text=std::string(""))
ROSLIB_DECL std::string command(const std::string &cmd)
void as_cb_say_(const cob_sound::SayGoalConstPtr &goal)
ros::Publisher diagnostics_pub_
bool service_cb_stop(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
bool service_cb_play(cob_srvs::SetString::Request &req, cob_srvs::SetString::Response &res)
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
ros::ServiceServer srvServer_unmute_
libvlc_instance_t * vlc_inst_
libvlc_media_t * vlc_media_
void registerPreemptCallback(boost::function< void()> cb)
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
bool say(std::string data, std::string &message)
actionlib::SimpleActionServer< cob_sound::PlayAction > as_play_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool service_cb_unmute(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
#define ROS_WARN_STREAM(args)
ros::ServiceServer srvServer_play_
void setAborted(const Result &result=Result(), const std::string &text=std::string(""))
bool play(std::string filename, std::string &message)
ros::Timer play_feedback_timer_
bool service_cb_mute(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
int main(int argc, char **argv)
ros::ServiceServer srvServer_mute_
ros::ServiceServer srvServer_stop_
void topic_cb_play(const std_msgs::String::ConstPtr &msg)
#define ROS_ERROR_STREAM(args)
void registerGoalCallback(boost::function< void()> cb)
void topic_cb_say(const std_msgs::String::ConstPtr &msg)
ros::Timer diagnostics_timer_
ros::ServiceServer srvServer_say_
actionlib::SimpleActionServer< cob_sound::SayAction > as_say_