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
30using 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
52void
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
74void
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. */
86void
87NaoQiSpeechSynthThread::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
97void
98NaoQiSpeechSynthThread::say(const char *text)
99{
100 tts_task_id_ = altts_->say(text);
101}
102
103void
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. */
115void
116NaoQiSpeechSynthThread::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}
virtual ~NaoQiSpeechSynthThread()
Destructor.
virtual void finalize()
Finalize the thread.
NaoQiSpeechSynthThread()
Constructor.
virtual void loop()
Code to execute in the thread.
virtual void init()
Initialize the thread.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
virtual void close(Interface *interface)=0
Close interface.
Thread aspect to use blocked timing.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void msgq_pop()
Erase first message from queue.
Definition: interface.cpp:1215
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:501
bool msgq_empty()
Check if queue is empty.
Definition: interface.cpp:1062
MessageType * msgq_first_safe(MessageType *&msg) noexcept
Get first message casted to the desired type without exceptions.
Definition: interface.h:340
AL::ALPtr< AL::ALBroker > naoqi_broker
NaoQi broker.
Definition: naoqi.h:44
SayMessage Fawkes BlackBoard Interface Message.
SpeechSynthInterface Fawkes BlackBoard Interface.
void set_msgid(const uint32_t new_msgid)
Set msgid value.
void set_final(const bool new_final)
Set final value.
Thread class encapsulation of pthreads.
Definition: thread.h:46
Fawkes library namespace.