icub-client
subSystem_speech.cpp
Go to the documentation of this file.
1 #include <yarp/os/all.h>
3 
4 icubclient::SubSystem_Speech::SubSystem_Speech(const std::string &masterName) :SubSystem(masterName)
5 {
6  tts.open(("/" + m_masterName + "/tts:o").c_str());
7  portRPC.open(("/" + m_masterName + "/tts:rpc").c_str());
9 }
10 
12 {
13  if(!yarp::os::Network::isConnected("/iSpeak/emotions:o", "/icub/face/emotions/in")) {
14  yarp::os::Network::connect("/iSpeak/emotions:o", "/icub/face/emotions/in");
15  }
16  bool connected = true;
17  if(!yarp::os::Network::isConnected(tts.getName(), "/iSpeak")) {
18  connected &= yarp::os::Network::connect(tts.getName(), "/iSpeak");
19  }
20  if(!yarp::os::Network::isConnected(portRPC.getName(), "/iSpeak/rpc")) {
21  connected &= yarp::os::Network::connect(portRPC.getName(), "/iSpeak/rpc");
22  }
23 
24  return connected;
25 }
26 
27 void icubclient::SubSystem_Speech::TTS(const std::string &text, bool shouldWait) {
28  if(text=="") {
29  yWarning() << "[SubSystem_Speech] Text is empty, not going to say anything";
30  return;
31  }
32  //Clean the input of underscores.
33  std::string tmpText = text;
34  replace_all(tmpText, "_", " ");
35  yarp::os::Bottle txt; txt.addString(tmpText.c_str());
36  tts.write(txt);
37  //int words = countWordsInString(text);
38  //double durationMn = words / (double)m_speed;
39  //double durationS = durationMn *60.0;
40  //yarp::os::Time::delay(durationS);
41  yarp::os::Bottle cmd, reply;
42  cmd.addVocab(yarp::os::createVocab('s', 't', 'a', 't'));
43  std::string status = "speaking";
44  bool speechStarted = false;
45 
46  while (shouldWait && (!speechStarted || status == "speaking"))
47  {
48  portRPC.write(cmd, reply);
49  status = reply.get(0).asString();
50  if (!speechStarted && status != "quiet")
51  {
52  speechStarted = true;
53  }
54  yarp::os::Time::delay(0.2);
55  }
56 }
57 
58 void icubclient::SubSystem_Speech::SetOptions(const std::string &custom) {
59  if(custom!="iCub") {
60  yarp::os::Bottle param;
61  param.addString("set");
62  param.addString("opt");
63  param.addString(custom.c_str());
64  portRPC.write(param);
65  } else {
66  yWarning() << "SetOptions called with none for iSpeak";
67  }
68 }
69 
71  yarp::os::Bottle cmd, reply;
72  cmd.addVocab(yarp::os::createVocab('s', 't', 'a', 't'));
73  portRPC.write(cmd, reply);
74  return (reply.get(0).asString() != "quiet");
75 }
76 
78 {
79  tts.interrupt();
80  tts.close();
81  portRPC.interrupt();
82  portRPC.close();
83 }
std::string m_type
Definition: subSystem.h:47
virtual void TTS(const std::string &text, bool shouldWait=true)
Produce text to speech output.
yarp::os::RpcClient portRPC
Definition: subSystem.h:48
void replace_all(std::string &in, const std::string &plain, const std::string &tok)
Simple search and replace function for strings;.
Definition: functions.cpp:42
#define SUBSYSTEM_SPEECH
Abstract class to handle sub-systems of the icub-client.
Definition: subSystem.h:43
SubSystem_Speech(const std::string &masterName)
Default constructor.
yarp::os::Port tts
Port to /iSpeak.
void SetOptions(const std::string &custom)
Set the command line options sent by iSpeak.
bool isSpeaking()
Check if iSpeak is currently speaking.
std::string m_masterName
Definition: subSystem.h:46
virtual void Close()
Clean up resources.