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 input_buffer.attach(m_inputPort);
30 m_inputPort.open(m_name +"/sound:i");
31 m_outputPort.open(m_name + "/text:o");
32 if (!m_rpcPort.open(m_name +"/rpc"))
33 {
34 yCError(SPEECHTR_NWS, "Failed to open rpc port");
35 return false;
36 }
37 m_rpcPort.setReader(*this);
38
39 yCInfo(SPEECHTR_NWS, "Device waiting for attach...");
40 return true;
41}
42
44{
45 yCInfo(SPEECHTR_NWS, "Starting");
46
47 //double before, now;
48 while (!isStopping())
49 {
51 //do nothing (for now..)
52 }
53 yCInfo(SPEECHTR_NWS, "Stopping");
54}
55
56bool SpeechTranscription_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(SPEECHTR_NWS, "Subdevice passed to attach method is invalid");
66 return false;
67 }
68
69 yCInfo(SPEECHTR_NWS, "Attach done");
70
71 callback_impl = std::make_unique<SpeechTranscription_CallbackHelper>(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 SpeechTranscription_nws_yarp::detach()
81{
82 m_isptr = nullptr;
83 return true;
84}
85
86bool SpeechTranscription_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 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(SPEECHTR_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(SPEECHTR_NWS, "ISpeechTranscription 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(SPEECHTR_NWS, "ISpeechTranscription 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 std::string transcription;
154 double score;
155 ReturnValue b = m_isptr->transcribe(sound, transcription, score);
156 ret.ret = b;
157 ret.transcription = transcription;
158 ret.score = score;
159
160 if (m_output_port && m_output_port->getOutputCount()>0)
161 {
163 bot.addString(transcription);
164 bot.addFloat64(score);
165 m_output_port->write(bot);
166 }
167
168 return ret;
169 }
170 yCError(SPEECHTR_NWS, "ISpeechTranscription interface was not set");
171 ret.ret = ReturnValue::return_code::return_value_error_generic;
172 return ret;
173}
174
175//--------------------------------------------------
176// ImplementCallbackHelper class.
178{
179 if (x ==nullptr || p==nullptr)
180 {
181 yCError(SPEECHTR_NWS, "Could not get ISpeechTranscription interface/output port");
182 std::exit(1);
183 }
184 m_isptr = x;
185 m_output_port = p;
186}
187
189{
190 if (m_isptr)
191 {
192 std::string transcription;
193 double score=0;
194 bool ok = m_isptr->transcribe(b, transcription, score);
195 if (!ok)
196 {
197 yCError(SPEECHTR_NWS, "Problems during transcription");
198 }
199 else
200 {
202 {
204 b.addString(transcription);
205 b.addFloat64(score);
207 }
208 }
209 }
210 else
211 {
212 yCError(SPEECHTR_NWS, "ISpeechTranscription interface was not set");
213 }
214}
bool ret
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.
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:170
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
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,...)