YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
SpeechSynthesizer_nws_yarp.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
9#include <yarp/os/Os.h>
10#include <memory>
11
12namespace {
13YARP_LOG_COMPONENT(SPEECHSYNTH_NWS, "yarp.devices.speechSynthesizer_nws_yarp")
14}
15
20
22{
23 return closeMain();
24}
25
27{
28 if (!parseParams(config)) { return false; }
29
30 m_inputPort.open(m_name +"/text:i");
31 m_outputPort.open(m_name + "/sound:o");
32 if (!m_rpcPort.open(m_name +"/rpc"))
33 {
34 yCError(SPEECHSYNTH_NWS, "Failed to open rpc port");
35 return false;
36 }
37 m_rpcPort.setReader(*this);
38
39 yCInfo(SPEECHSYNTH_NWS, "Device waiting for attach...");
40 return true;
41}
42
44{
45 yCInfo(SPEECHSYNTH_NWS, "Starting");
46
47 //double before, now;
48 while (!isStopping())
49 {
51 //do nothing (for now..)
52 }
53 yCInfo(SPEECHSYNTH_NWS, "Stopping");
54}
55
56bool SpeechSynthesizer_nws_yarp::attach(yarp::dev::PolyDriver* deviceToAttach)
57{
58 if (deviceToAttach->isValid())
59 {
60 deviceToAttach->view(m_isptr);
61 }
62
63 if (nullptr == m_isptr)
64 {
65 yCError(SPEECHSYNTH_NWS, "Subdevice passed to attach method is invalid");
66 return false;
67 }
68
69 yCInfo(SPEECHSYNTH_NWS, "Attach done");
70
71 callback_impl = std::make_unique<SpeechSynthesizer_CallbackHelper>(m_isptr,&m_outputPort);
72 m_inputPort.useCallback(*callback_impl);
73 m_rpc.setInterfaces(m_isptr);
74 m_rpc.setOutputPort(&m_outputPort);
75
76 start();
77 return true;
78}
79
80bool SpeechSynthesizer_nws_yarp::detach()
81{
82 m_isptr = nullptr;
83 return true;
84}
85
86bool SpeechSynthesizer_nws_yarp::closeMain()
87{
88 if (Thread::isRunning()) {
90 }
91 //close the port connection here
92 m_inputPort.disableCallback();
93 m_inputPort.close();
94 m_outputPort.close();
95 m_rpcPort.close();
96 return true;
97}
98
100{
101 if (!connection.isValid()) { return false;}
102
103 bool b = m_rpc.read(connection);
104 if (b)
105 {
106 return true;
107 }
108 else
109 {
110 yCError(SPEECHSYNTH_NWS, "read() Command failed");
111 return false;
112 }
113}
114
115//--------------------------------------------------
116// RPC methods
118{
119 std::lock_guard <std::mutex> lg(m_mutex);
121 if (m_isptr)
122 {
123 return m_isptr->setLanguage(language);
124 }
125 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
126 ret.ret = ReturnValue::return_code::return_value_error_generic;
127 return ret;
128}
129
131{
132 std::lock_guard <std::mutex> lg(m_mutex);
134 if (m_isptr)
135 {
136 std::string language;
137 ReturnValue b = m_isptr->getLanguage(language);
138 ret.ret = b;
139 ret.language = language;
140 return ret;
141 }
142 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
143 ret.ret = ReturnValue::return_code::return_value_error_generic;
144 return ret;
145}
146
148{
149 std::lock_guard <std::mutex> lg(m_mutex);
151 if (m_isptr)
152 {
153 return m_isptr->setVoice(voice);
154 }
155 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
156 return ret;
157}
158
160{
161 std::lock_guard <std::mutex> lg(m_mutex);
163 if (m_isptr)
164 {
165 std::string voice;
166 ReturnValue b = m_isptr->getVoice(voice);
167 ret.ret = b;
168 ret.voice = voice;
169 return ret;
170 }
171 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
172 ret.ret = ReturnValue::return_code::return_value_error_generic;
173 return ret;
174}
175
177{
178 std::lock_guard <std::mutex> lg(m_mutex);
180 if (m_isptr)
181 {
182 yarp::sig::VectorOfString available_voices;
183 ReturnValue b = m_isptr->getAvailableVoices(available_voices);
184 ret.ret = b;
185 ret.available_voices = available_voices;
186 return ret;
187 }
188 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
189 ret.ret = ReturnValue::return_code::return_value_error_generic;
190 return ret;
191}
192
194{
195 std::lock_guard <std::mutex> lg(m_mutex);
197 if (m_isptr)
198 {
199 return m_isptr->setPitch(pitch);
200 }
201 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
202 ret.ret = ReturnValue::return_code::return_value_error_generic;
203 return ret;
204}
205
207{
208 std::lock_guard <std::mutex> lg(m_mutex);
210 if (m_isptr)
211 {
212 double pitch;
213 ReturnValue b = m_isptr->getPitch(pitch);
214 ret.ret = b;
215 ret.pitch = pitch;
216 return ret;
217 }
218 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
219 return ret;
220}
221
223{
224 std::lock_guard <std::mutex> lg(m_mutex);
226 if (m_isptr)
227 {
228 return m_isptr->setSpeed(speed);
229 }
230 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
231 ret.ret = ReturnValue::return_code::return_value_error_generic;
232 return ret;
233}
234
236{
237 std::lock_guard <std::mutex> lg(m_mutex);
239 if (m_isptr)
240 {
241 double speed;
242 ReturnValue b = m_isptr->getSpeed(speed);
243 ret.ret = b;
244 ret.speed = speed;
245 return ret;
246 }
247 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
248 ret.ret = ReturnValue::return_code::return_value_error_generic;
249 return ret;
250}
251
253{
254 std::lock_guard <std::mutex> lg(m_mutex);
256 if (m_isptr)
257 {
259 double score;
260 ReturnValue b = m_isptr->synthesize(text, snd);
261 ret.ret = b;
262 ret.sound = snd;
263
264 if (m_output_port && m_output_port->getOutputCount()>0)
265 {
266 m_output_port->write(snd);
267 }
268
269 return ret;
270 }
271 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
272 ret.ret = ReturnValue::return_code::return_value_error_generic;
273 return ret;
274}
275
276//--------------------------------------------------
277// ImplementCallbackHelper class.
279{
280 if (x ==nullptr || p==nullptr)
281 {
282 yCError(SPEECHSYNTH_NWS, "Could not get ISpeechSynthesizer interface/output port");
283 std::exit(1);
284 }
285 m_isptr = x;
286 m_output_port = p;
287}
288
290{
291 if (m_isptr)
292 {
294 bool ok = m_isptr->synthesize(b.get(0).asString(), snd);
295 if (!ok)
296 {
297 yCError(SPEECHSYNTH_NWS, "Problems during speech synthesis");
298 }
299 else
300 {
302 {
304 }
305 }
306 }
307 else
308 {
309 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
310 }
311}
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
virtual return_set_pitch set_pitch(double pitch) override
virtual return_get_pitch get_pitch() override
virtual return_set_language set_language(const std::string &language) override
virtual return_get_speed get_speed() override
virtual return_get_voice get_voice() override
virtual return_get_available_voices get_available_voices() override
virtual return_get_language get_language() override
virtual return_set_speed set_speed(double speed) override
virtual return_set_voice set_voice(const std::string &language) override
void setOutputPort(yarp::os::Port *port)
void setInterfaces(yarp::dev::ISpeechSynthesizer *iser)
virtual return_synthesize synthesize(const std::string &text) override
void onRead(yarp::os::Bottle &b) override
yarp::dev::ISpeechSynthesizer * m_isptr
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
void run() override
Main body of the new thread.
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool close() override
Close the DeviceDriver.
yarp::sig::VectorOfString available_voices
yarp::dev::ReturnValue ret
yarp::dev::ReturnValue ret
yarp::dev::ReturnValue ret
yarp::dev::ReturnValue ret
yarp::dev::ReturnValue ret
yarp::dev::ReturnValue ret
yarp::dev::ReturnValue ret
yarp::dev::ReturnValue ret
yarp::sig::Sound sound
A generic interface for speech synthesis.
virtual yarp::dev::ReturnValue synthesize(const std::string &text, yarp::sig::Sound &sound)=0
Performs the speech synthesis.
virtual yarp::dev::ReturnValue setPitch(const double pitch)=0
Sets the pitch for speech synthesis.
virtual yarp::dev::ReturnValue setVoice(const std::string &voice_name="auto")=0
Sets the voice set for speech synthesis.
virtual yarp::dev::ReturnValue setSpeed(const double speed=0)=0
Sets the voice speed for speech synthesis.
virtual yarp::dev::ReturnValue getAvailableVoices(yarp::sig::VectorOfString &available_voices)=0
Returns the list of available voices for the currently set language.
virtual yarp::dev::ReturnValue getLanguage(std::string &language)=0
Gets the current language set for speech synthesis.
virtual yarp::dev::ReturnValue setLanguage(const std::string &language="auto")=0
Sets the language for speech synthesis.
virtual yarp::dev::ReturnValue getSpeed(double &speed)=0
Gets the current voice speed.
virtual yarp::dev::ReturnValue getVoice(std::string &voice_name)=0
Gets the current voice set for speech synthesis.
virtual yarp::dev::ReturnValue getPitch(double &pitch)=0
Gets the current pitch set for speech synthesis.
A container for a device driver.
Definition PolyDriver.h:23
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:247
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
void disableCallback() override
Remove a callback set up with useCallback()
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
An interface for reading from a network connection.
virtual bool isValid() const =0
A mini-server for network communication.
Definition Port.h:46
int getOutputCount() override
Determine how many output connections this port has.
Definition Port.cpp:526
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition Port.cpp:395
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition Port.cpp:470
void close() override
Stop port activity.
Definition Port.cpp:330
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
A base class for nested structures that can be searched.
Definition Searchable.h:31
static void delaySystem(double seconds)
bool stop()
Stop the thread.
Definition Thread.cpp:81
bool isStopping()
Returns true if the thread is stopping (Thread::stop has been called).
Definition Thread.cpp:99
bool isRunning()
Returns true if the thread is running (Thread::start has been called successfully and the thread has ...
Definition Thread.cpp:105
bool start()
Start the new thread running.
Definition Thread.cpp:93
virtual std::string asString() const
Get string value.
Definition Value.cpp:240
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:39
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)