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 input_buffer.attach(m_inputPort);
31 m_inputPort.open(m_name +"/text:i");
32 m_outputPort.open(m_name + "/sound:o");
33 if (!m_rpcPort.open(m_name +"/rpc"))
34 {
35 yCError(SPEECHSYNTH_NWS, "Failed to open rpc port");
36 return false;
37 }
38 m_rpcPort.setReader(*this);
39
40 yCInfo(SPEECHSYNTH_NWS, "Device waiting for attach...");
41 return true;
42}
43
45{
46 yCInfo(SPEECHSYNTH_NWS, "Starting");
47
48 //double before, now;
49 while (!isStopping())
50 {
52 //do nothing (for now..)
53 }
54 yCInfo(SPEECHSYNTH_NWS, "Stopping");
55}
56
57bool SpeechSynthesizer_nws_yarp::attach(yarp::dev::PolyDriver* deviceToAttach)
58{
59 if (deviceToAttach->isValid())
60 {
61 deviceToAttach->view(m_isptr);
62 }
63
64 if (nullptr == m_isptr)
65 {
66 yCError(SPEECHSYNTH_NWS, "Subdevice passed to attach method is invalid");
67 return false;
68 }
69
70 yCInfo(SPEECHSYNTH_NWS, "Attach done");
71
72 callback_impl = std::make_unique<SpeechSynthesizer_CallbackHelper>(m_isptr,&m_outputPort);
73 input_buffer.useCallback(*callback_impl);
74 m_rpc.setInterfaces(m_isptr);
75 m_rpc.setOutputPort(&m_outputPort);
76
77 start();
78 return true;
79}
80
81bool SpeechSynthesizer_nws_yarp::detach()
82{
83 m_isptr = nullptr;
84 return true;
85}
86
87bool SpeechSynthesizer_nws_yarp::closeMain()
88{
89 if (Thread::isRunning()) {
91 }
92 //close the port connection here
93 input_buffer.disableCallback();
94 m_inputPort.close();
95 m_outputPort.close();
96 m_rpcPort.close();
97 return true;
98}
99
101{
102 if (!connection.isValid()) { return false;}
103
104 bool b = m_rpc.read(connection);
105 if (b)
106 {
107 return true;
108 }
109 else
110 {
111 yCError(SPEECHSYNTH_NWS, "read() Command failed");
112 return false;
113 }
114}
115
116//--------------------------------------------------
117// RPC methods
119{
120 std::lock_guard <std::mutex> lg(m_mutex);
122 if (m_isptr)
123 {
124 return m_isptr->setLanguage(language);
125 }
126 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
127 ret.ret = ReturnValue::return_code::return_value_error_generic;
128 return ret;
129}
130
132{
133 std::lock_guard <std::mutex> lg(m_mutex);
135 if (m_isptr)
136 {
137 std::string language;
138 ReturnValue b = m_isptr->getLanguage(language);
139 ret.ret = b;
140 ret.language = language;
141 return ret;
142 }
143 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
144 ret.ret = ReturnValue::return_code::return_value_error_generic;
145 return ret;
146}
147
149{
150 std::lock_guard <std::mutex> lg(m_mutex);
152 if (m_isptr)
153 {
154 return m_isptr->setVoice(voice);
155 }
156 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
157 return ret;
158}
159
161{
162 std::lock_guard <std::mutex> lg(m_mutex);
164 if (m_isptr)
165 {
166 std::string voice;
167 ReturnValue b = m_isptr->getVoice(voice);
168 ret.ret = b;
169 ret.voice = voice;
170 return ret;
171 }
172 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
173 ret.ret = ReturnValue::return_code::return_value_error_generic;
174 return ret;
175}
176
178{
179 std::lock_guard <std::mutex> lg(m_mutex);
181 if (m_isptr)
182 {
183 return m_isptr->setPitch(pitch);
184 }
185 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
186 ret.ret = ReturnValue::return_code::return_value_error_generic;
187 return ret;
188}
189
191{
192 std::lock_guard <std::mutex> lg(m_mutex);
194 if (m_isptr)
195 {
196 double pitch;
197 ReturnValue b = m_isptr->getPitch(pitch);
198 ret.ret = b;
199 ret.pitch = pitch;
200 return ret;
201 }
202 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
203 return ret;
204}
205
207{
208 std::lock_guard <std::mutex> lg(m_mutex);
210 if (m_isptr)
211 {
212 return m_isptr->setSpeed(speed);
213 }
214 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
215 ret.ret = ReturnValue::return_code::return_value_error_generic;
216 return ret;
217}
218
220{
221 std::lock_guard <std::mutex> lg(m_mutex);
223 if (m_isptr)
224 {
225 double speed;
226 ReturnValue b = m_isptr->getSpeed(speed);
227 ret.ret = b;
228 ret.speed = speed;
229 return ret;
230 }
231 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
232 ret.ret = ReturnValue::return_code::return_value_error_generic;
233 return ret;
234}
235
237{
238 std::lock_guard <std::mutex> lg(m_mutex);
240 if (m_isptr)
241 {
243 double score;
244 ReturnValue b = m_isptr->synthesize(text, snd);
245 ret.ret = b;
246 ret.sound = snd;
247
248 if (m_output_port && m_output_port->getOutputCount()>0)
249 {
250 m_output_port->write(snd);
251 }
252
253 return ret;
254 }
255 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
256 ret.ret = ReturnValue::return_code::return_value_error_generic;
257 return ret;
258}
259
260//--------------------------------------------------
261// ImplementCallbackHelper class.
263{
264 if (x ==nullptr || p==nullptr)
265 {
266 yCError(SPEECHSYNTH_NWS, "Could not get ISpeechSynthesizer interface/output port");
267 std::exit(1);
268 }
269 m_isptr = x;
270 m_output_port = p;
271}
272
274{
275 if (m_isptr)
276 {
278 bool ok = m_isptr->synthesize(b.get(0).asString(), snd);
279 if (!ok)
280 {
281 yCError(SPEECHSYNTH_NWS, "Problems during speech synthesis");
282 }
283 else
284 {
286 {
287 m_output_port->write(snd);
288 }
289 }
290 }
291 else
292 {
293 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
294 }
295}
bool ret
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_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
Callback method.
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.
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 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 getPitch(double &voice)=0
Gets the current pitch set 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.
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:246
A mini-server for performing network communication in the background.
An interface for reading from a network connection.
virtual bool isValid() const =0
void attach(Port &port)
Attach this buffer to a particular port.
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
void disableCallback() override
Remove a callback set up with useCallback()
A mini-server for network communication.
Definition Port.h:46
int getOutputCount() override
Determine how many output connections this port has.
Definition Port.cpp:567
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition Port.cpp:436
void setReader(PortReader &reader) override
Set an external reader for port data.
Definition Port.cpp:511
void close() override
Stop port activity.
Definition Port.cpp:363
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:234
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:25
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)