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 
29 #include <interfaces/SpeechSynthInterface.h>
30 
31 using namespace fawkes;
32 
33 /** @class NaoQiSpeechSynthThread "motion_thread.h"
34  * Thread to provide NaoQi motions to Fawkes.
35  * This thread holds an ALMotion proxy and provides its capabilities via
36  * the blackboard to other Fawkes threads.
37  *
38  * @author Tim Niemueller
39  */
40 
41 /** Constructor. */
43  : Thread("NaoQiSpeechSynthThread", Thread::OPMODE_WAITFORWAKEUP),
45 {
46 }
47 
48 
49 /** Destructor. */
51 {
52 }
53 
54 
55 void
57 {
58  __tts_task_id = -1;
59 
60  // Is ALTextToSpeech available?
61  try {
62  AL::ALPtr<AL::ALLauncherProxy> launcher(new AL::ALLauncherProxy(naoqi_broker));
63  bool is_tts_available = launcher->isModulePresent("ALTextToSpeech");
64 
65  if (! is_tts_available) {
66  throw Exception("NaoQi ALTextToSpeech is not available");
67  }
68  } catch (AL::ALError& e) {
69  throw Exception("Checking ALTextToSpeech aliveness failed: %s",
70  e.toString().c_str());
71  }
72 
73  __altts =
74  AL::ALPtr<AL::ALTextToSpeechProxy>(new AL::ALTextToSpeechProxy(naoqi_broker));
75 
76  __speechsynth_if =
78 }
79 
80 
81 void
83 {
84  stop_speech();
85 
86  blackboard->close(__speechsynth_if);
87  __speechsynth_if = NULL;
88 
89  __altts.reset();
90 }
91 
92 
93 /** Stop currently running speech synthesis. */
94 void
95 NaoQiSpeechSynthThread::stop_speech()
96 {
97  if (__tts_task_id != -1) {
98  if (__altts->isRunning(__tts_task_id)) {
99  __altts->stop(__tts_task_id);
100  }
101  __tts_task_id = -1;
102  }
103 }
104 
105 void
106 NaoQiSpeechSynthThread::say(const char *text)
107 {
108  __tts_task_id = __altts->say(text);
109 }
110 
111 void
113 {
114  bool working = (__tts_task_id != -1) && __altts->isRunning(__tts_task_id);
115  if (! working) {
116  process_messages();
117  }
118  __speechsynth_if->set_final( ! working );
119  __speechsynth_if->write();
120 }
121 
122 
123 /** Process incoming BB messages. */
124 void
125 NaoQiSpeechSynthThread::process_messages()
126 {
127  // process bb messages
128  if ( ! __speechsynth_if->msgq_empty() ) {
130  __speechsynth_if->msgq_first_safe(msg))
131  {
132  say(msg->text());
133  __speechsynth_if->set_msgid(msg->id());
134  }
135 
136  __speechsynth_if->msgq_pop();
137  }
138 }
virtual void loop()
Code to execute in the thread.
virtual void finalize()
Finalize the thread.
void set_final(const bool new_final)
Set final value.
bool msgq_empty()
Check if queue is empty.
Definition: interface.cpp:1048
Fawkes library namespace.
AL::ALPtr< AL::ALBroker > naoqi_broker
NaoQi broker.
Definition: naoqi.h:45
virtual void init()
Initialize the thread.
NaoQiSpeechSynthThread()
Constructor.
SayMessage Fawkes BlackBoard Interface Message.
Thread class encapsulation of pthreads.
Definition: thread.h:42
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:500
void set_msgid(const uint32_t new_msgid)
Set msgid value.
Thread aspect to use blocked timing.
void msgq_pop()
Erase first message from queue.
Definition: interface.cpp:1193
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual ~NaoQiSpeechSynthThread()
Destructor.
MessageType * msgq_first_safe(MessageType *&msg)
Get first message casted to the desired type without exceptions.
Definition: interface.h:302
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
SpeechSynthInterface Fawkes BlackBoard Interface.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
virtual void close(Interface *interface)=0
Close interface.