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
11namespace {
12YARP_LOG_COMPONENT(SPEECHSYNTH_NWS, "yarp.devices.speechSynthesizer_nws_yarp")
13}
14
19
21{
22 return closeMain();
23}
24
26{
27 if (!parseParams(config)) { return false; }
28
29 input_buffer.attach(m_inputPort);
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 = new ImplementCallbackHelper2(m_isptr,&m_outputPort);
72 input_buffer.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 input_buffer.disableCallback();
93 m_inputPort.close();
94 m_outputPort.close();
95 m_rpcPort.close();
96 if (callback_impl) {delete callback_impl; callback_impl=nullptr;}
97 return true;
98}
99
101{
102 bool b = m_rpc.read(connection);
103 if (b)
104 {
105 return true;
106 }
107 else
108 {
109 yCError(SPEECHSYNTH_NWS, "read() Command failed");
110 return false;
111 }
112}
113
114//--------------------------------------------------
115// RPC methods
116bool ISpeechSynthesizerMsgsd::set_language(const std::string& language)
117{
118 std::lock_guard <std::mutex> lg(m_mutex);
119 if (m_isptr)
120 {
121 return m_isptr->setLanguage(language);
122 }
123 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
124 return false;
125}
126
128{
129 std::lock_guard <std::mutex> lg(m_mutex);
131 if (m_isptr)
132 {
133 std::string language;
134 bool b = m_isptr->getLanguage(language);
135 ret.ret = b;
136 ret.language = language;
137 return ret;
138 }
139 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
140 return ret;
141}
142
143bool ISpeechSynthesizerMsgsd::set_voice(const std::string& voice)
144{
145 std::lock_guard <std::mutex> lg(m_mutex);
146 if (m_isptr)
147 {
148 return m_isptr->setVoice(voice);
149 }
150 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
151 return false;
152}
153
155{
156 std::lock_guard <std::mutex> lg(m_mutex);
158 if (m_isptr)
159 {
160 std::string voice;
161 bool b = m_isptr->getVoice(voice);
162 ret.ret = b;
163 ret.voice = voice;
164 return ret;
165 }
166 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
167 return ret;
168}
169
171{
172 std::lock_guard <std::mutex> lg(m_mutex);
173 if (m_isptr)
174 {
175 return m_isptr->setPitch(pitch);
176 }
177 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
178 return false;
179}
180
182{
183 std::lock_guard <std::mutex> lg(m_mutex);
185 if (m_isptr)
186 {
187 double pitch;
188 bool b = m_isptr->getPitch(pitch);
189 ret.ret = b;
190 ret.pitch = pitch;
191 return ret;
192 }
193 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
194 return ret;
195}
196
198{
199 std::lock_guard <std::mutex> lg(m_mutex);
200 if (m_isptr)
201 {
202 return m_isptr->setSpeed(speed);
203 }
204 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
205 return false;
206}
207
209{
210 std::lock_guard <std::mutex> lg(m_mutex);
212 if (m_isptr)
213 {
214 double speed;
215 bool b = m_isptr->getSpeed(speed);
216 ret.ret = b;
217 ret.speed = speed;
218 return ret;
219 }
220 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
221 return ret;
222}
223
225{
226 std::lock_guard <std::mutex> lg(m_mutex);
228 if (m_isptr)
229 {
231 double score;
232 bool b = m_isptr->synthesize(text, snd);
233 ret.ret = b;
234 ret.sound = snd;
235
236 if (m_output_port && m_output_port->getOutputCount()>0)
237 {
238 m_output_port->write(snd);
239 }
240
241 return ret;
242 }
243 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
244 return ret;
245}
246
247//--------------------------------------------------
248// ImplementCallbackHelper class.
250{
251 if (x ==nullptr || p==nullptr)
252 {
253 yCError(SPEECHSYNTH_NWS, "Could not get ISpeechSynthesizer interface/output port");
254 std::exit(1);
255 }
256 m_isptr = x;
258}
259
261{
262 if (m_isptr)
263 {
265 bool ok = m_isptr->synthesize(b.get(0).asString(), snd);
266 if (!ok)
267 {
268 yCError(SPEECHSYNTH_NWS, "Problems during speech synthesis");
269 }
270 else
271 {
273 {
274 m_output_port->write(snd);
275 }
276 }
277 }
278 else
279 {
280 yCError(SPEECHSYNTH_NWS, "ISpeechSynthesizer interface was not set");
281 }
282}
bool ret
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
virtual return_get_pitch get_pitch() override
virtual bool set_speed(double speed) override
virtual return_get_speed get_speed() override
virtual bool set_voice(const std::string &language) override
virtual return_get_voice get_voice() override
virtual return_get_language get_language() override
virtual bool set_pitch(double pitch) override
virtual bool set_language(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
Callback implementation after buffered input.
void onRead(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 bool setSpeed(const double speed=0)=0
Sets the voice speed for speech synthesis.
virtual bool getLanguage(std::string &language)=0
Gets the current language set for speech synthesis.
virtual bool setVoice(const std::string &voice_name="auto")=0
Sets the voice set for speech synthesis.
virtual bool getVoice(std::string &voice_name)=0
Gets the current voice set for speech synthesis.
virtual bool setLanguage(const std::string &language="auto")=0
Sets the language for speech synthesis.
virtual bool getPitch(double &voice)=0
Gets the current pitch set for speech synthesis.
virtual bool synthesize(const std::string &text, yarp::sig::Sound &sound)=0
Performs the speech synthesis.
virtual bool getSpeed(double &speed)=0
Gets the current voice speed.
virtual bool setPitch(const double pitch)=0
Sets the pitch 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.
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,...)