Fawkes API  Fawkes Development Version
speechsynth_thread.cpp
1 
2 /***************************************************************************
3  * speechsynth_thread.cpp - Provide NaoQi speech synthesis to Fawkes
4  *
5  * Created: Tue Jun 21 17:32:14 2011
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "speechsynth_thread.h"
24 
25 #include <alcore/alerror.h>
26 #include <alproxies/allauncherproxy.h>
27 #include <alproxies/altexttospeechproxy.h>
28 #include <interfaces/SpeechSynthInterface.h>
29 
30 using namespace fawkes;
31 
32 /** @class NaoQiSpeechSynthThread "motion_thread.h"
33  * Thread to provide NaoQi motions to Fawkes.
34  * This thread holds an ALMotion proxy and provides its capabilities via
35  * the blackboard to other Fawkes threads.
36  *
37  * @author Tim Niemueller
38  */
39 
40 /** Constructor. */
42 : Thread("NaoQiSpeechSynthThread", Thread::OPMODE_WAITFORWAKEUP),
44 {
45 }
46 
47 /** Destructor. */
49 {
50 }
51 
52 void
54 {
55  tts_task_id_ = -1;
56 
57  // Is ALTextToSpeech available?
58  try {
59  AL::ALPtr<AL::ALLauncherProxy> launcher(new AL::ALLauncherProxy(naoqi_broker));
60  bool is_tts_available = launcher->isModulePresent("ALTextToSpeech");
61 
62  if (!is_tts_available) {
63  throw Exception("NaoQi ALTextToSpeech is not available");
64  }
65  } catch (AL::ALError &e) {
66  throw Exception("Checking ALTextToSpeech aliveness failed: %s", e.toString().c_str());
67  }
68 
69  altts_ = AL::ALPtr<AL::ALTextToSpeechProxy>(new AL::ALTextToSpeechProxy(naoqi_broker));
70 
71  speechsynth_if_ = blackboard->open_for_writing<SpeechSynthInterface>("NaoQi TTS");
72 }
73 
74 void
76 {
77  stop_speech();
78 
79  blackboard->close(speechsynth_if_);
80  speechsynth_if_ = NULL;
81 
82  altts_.reset();
83 }
84 
85 /** Stop currently running speech synthesis. */
86 void
87 NaoQiSpeechSynthThread::stop_speech()
88 {
89  if (tts_task_id_ != -1) {
90  if (altts_->isRunning(tts_task_id_)) {
91  altts_->stop(tts_task_id_);
92  }
93  tts_task_id_ = -1;
94  }
95 }
96 
97 void
98 NaoQiSpeechSynthThread::say(const char *text)
99 {
100  tts_task_id_ = altts_->say(text);
101 }
102 
103 void
105 {
106  bool working = (tts_task_id_ != -1) && altts_->isRunning(tts_task_id_);
107  if (!working) {
108  process_messages();
109  }
110  speechsynth_if_->set_final(!working);
111  speechsynth_if_->write();
112 }
113 
114 /** Process incoming BB messages. */
115 void
116 NaoQiSpeechSynthThread::process_messages()
117 {
118  // process bb messages
119  if (!speechsynth_if_->msgq_empty()) {
120  if (SpeechSynthInterface::SayMessage *msg = speechsynth_if_->msgq_first_safe(msg)) {
121  say(msg->text());
122  speechsynth_if_->set_msgid(msg->id());
123  }
124 
125  speechsynth_if_->msgq_pop();
126  }
127 }
fawkes::Interface::msgq_pop
void msgq_pop()
Erase first message from queue.
Definition: interface.cpp:1182
fawkes::Interface::msgq_empty
bool msgq_empty()
Check if queue is empty.
Definition: interface.cpp:1029
fawkes::SpeechSynthInterface::set_final
void set_final(const bool new_final)
Set final value.
Definition: SpeechSynthInterface.cpp:177
fawkes::Interface::msgq_first_safe
MessageType * msgq_first_safe(MessageType *&msg)
Get first message casted to the desired type without exceptions.
Definition: interface.h:302
NaoQiSpeechSynthThread::~NaoQiSpeechSynthThread
virtual ~NaoQiSpeechSynthThread()
Destructor.
Definition: speechsynth_thread.cpp:47
fawkes::BlockedTimingAspect
Definition: blocked_timing.h:54
NaoQiSpeechSynthThread::loop
virtual void loop()
Code to execute in the thread.
Definition: speechsynth_thread.cpp:103
fawkes::SpeechSynthInterface::set_msgid
void set_msgid(const uint32_t new_msgid)
Set msgid value.
Definition: SpeechSynthInterface.cpp:142
fawkes::BlackBoard::close
virtual void close(Interface *interface)=0
fawkes::NaoQiAspect::naoqi_broker
AL::ALPtr< AL::ALBroker > naoqi_broker
Definition: naoqi.h:48
fawkes
NaoQiSpeechSynthThread::NaoQiSpeechSynthThread
NaoQiSpeechSynthThread()
Constructor.
Definition: speechsynth_thread.cpp:40
fawkes::SpeechSynthInterface
Definition: SpeechSynthInterface.h:37
fawkes::SpeechSynthInterface::SayMessage
Definition: SpeechSynthInterface.h:76
NaoQiSpeechSynthThread::finalize
virtual void finalize()
Finalize the thread.
Definition: speechsynth_thread.cpp:74
fawkes::Thread
Definition: thread.h:44
fawkes::BlackBoardAspect::blackboard
BlackBoard * blackboard
Definition: blackboard.h:47
fawkes::Interface::write
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:497
fawkes::BlackBoard::open_for_writing
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
NaoQiSpeechSynthThread::init
virtual void init()
Initialize the thread.
Definition: speechsynth_thread.cpp:52
fawkes::Exception
Definition: exception.h:39