YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
SpeechTranscription_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(SPEECHTR_NWS, "yarp.devices.speechTranscription_nws_yarp")
13}
14
19
21{
22 return closeMain();
23}
24
26{
27 if (!parseParams(config)) { return false; }
28
29 m_inputPort.open(m_name +"/sound:i");
30 m_outputPort.open(m_name + "/text:o");
31 if (!m_rpcPort.open(m_name +"/rpc"))
32 {
33 yCError(SPEECHTR_NWS, "Failed to open rpc port");
34 return false;
35 }
36 m_rpcPort.setReader(*this);
37
38 yCInfo(SPEECHTR_NWS, "Device waiting for attach...");
39 return true;
40}
41
43{
44 yCInfo(SPEECHTR_NWS, "Starting");
45
46 //double before, now;
47 while (!isStopping())
48 {
50 //do nothing (for now..)
51 }
52 yCInfo(SPEECHTR_NWS, "Stopping");
53}
54
55bool SpeechTranscription_nws_yarp::attach(yarp::dev::PolyDriver* deviceToAttach)
56{
57 if (deviceToAttach->isValid())
58 {
59 deviceToAttach->view(m_isptr);
60 }
61
62 if (nullptr == m_isptr)
63 {
64 yCError(SPEECHTR_NWS, "Subdevice passed to attach method is invalid");
65 return false;
66 }
67
68 yCInfo(SPEECHTR_NWS, "Attach done");
69
70 callback_impl = std::make_unique<SpeechTranscription_CallbackHelper>(m_isptr,&m_outputPort);
71 m_inputPort.useCallback(*callback_impl);
72 m_rpc.setInterfaces(m_isptr);
73 m_rpc.setOutputPort(&m_outputPort);
74
75 start();
76 return true;
77}
78
79bool SpeechTranscription_nws_yarp::detach()
80{
81 m_isptr = nullptr;
82 return true;
83}
84
85bool SpeechTranscription_nws_yarp::closeMain()
86{
87 if (Thread::isRunning()) {
89 }
90 //close the port connection here
91 m_inputPort.disableCallback();
92 m_inputPort.close();
93 m_outputPort.close();
94 m_rpcPort.close();
95 return true;
96}
97
99{
100 if (!connection.isValid()) { return false;}
101
102 bool b = m_rpc.read(connection);
103 if (b)
104 {
105 return true;
106 }
107 else
108 {
109 yCError(SPEECHTR_NWS, "read() Command failed");
110 return false;
111 }
112}
113
114//--------------------------------------------------
115// RPC methods
117{
118 std::lock_guard <std::mutex> lg(m_mutex);
120 if (m_isptr)
121 {
122 return m_isptr->setLanguage(language);
123 }
124 yCError(SPEECHTR_NWS, "ISpeechTranscription interface was not set");
125 ret.ret = ReturnValue::return_code::return_value_error_generic;
126 return ret;
127}
128
130{
131 std::lock_guard <std::mutex> lg(m_mutex);
133 if (m_isptr)
134 {
135 std::string language;
136 ReturnValue b = m_isptr->getLanguage(language);
137 ret.ret = b;
138 ret.language = language;
139 return ret;
140 }
141 yCError(SPEECHTR_NWS, "ISpeechTranscription interface was not set");
142 ret.ret = ReturnValue::return_code::return_value_error_generic;
143 return ret;
144}
145
147{
148 std::lock_guard <std::mutex> lg(m_mutex);
150 if (m_isptr)
151 {
152 std::string transcription;
153 double score;
154 ReturnValue b = m_isptr->transcribe(sound, transcription, score);
155 ret.ret = b;
156 ret.transcription = transcription;
157 ret.score = score;
158
159 if (m_output_port && m_output_port->getOutputCount()>0)
160 {
162 bot.addString(transcription);
163 bot.addFloat64(score);
164 m_output_port->write(bot);
165 }
166
167 return ret;
168 }
169 yCError(SPEECHTR_NWS, "ISpeechTranscription interface was not set");
170 ret.ret = ReturnValue::return_code::return_value_error_generic;
171 return ret;
172}
173
174//--------------------------------------------------
175// ImplementCallbackHelper class.
177{
178 if (x ==nullptr || p==nullptr)
179 {
180 yCError(SPEECHTR_NWS, "Could not get ISpeechTranscription interface/output port");
181 std::exit(1);
182 }
183 m_isptr = x;
184 m_output_port = p;
185}
186
188{
189 if (m_isptr)
190 {
191 std::string transcription;
192 double score=0;
193 bool ok = m_isptr->transcribe(b, transcription, score);
194 if (!ok)
195 {
196 yCError(SPEECHTR_NWS, "Problems during transcription");
197 }
198 else
199 {
201 {
203 b.addString(transcription);
204 b.addFloat64(score);
206 }
207 }
208 }
209 else
210 {
211 yCError(SPEECHTR_NWS, "ISpeechTranscription interface was not set");
212 }
213}
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
virtual return_set_language set_language(const std::string &language) override
virtual return_transcribe transcribe(const yarp::sig::Sound &sound) override
void setInterfaces(yarp::dev::ISpeechTranscription *iser)
virtual return_get_language get_language() override
void setOutputPort(yarp::os::Port *port)
yarp::dev::ISpeechTranscription * m_isptr
void onRead(yarp::sig::Sound &b) override
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
bool close() override
Close the DeviceDriver.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
void run() override
Main body of the new thread.
yarp::dev::ReturnValue ret
yarp::dev::ReturnValue ret
std::string transcription
yarp::dev::ReturnValue ret
A generic interface for speech transcription.
virtual yarp::dev::ReturnValue transcribe(const yarp::sig::Sound &sound, std::string &transcription, double &score)=0
Performs the speech transcription.
virtual yarp::dev::ReturnValue getLanguage(std::string &language)=0
Gets the current language set for speech transcription.
virtual yarp::dev::ReturnValue setLanguage(const std::string &language="auto")=0
Sets the language for speech transcription.
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
void addFloat64(yarp::conf::float64_t x)
Places a 64-bit floating point number in the bottle, at the end of the list.
Definition Bottle.cpp:158
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:176
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
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,...)