YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
AudioRecorder_nwc_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
8#include <yarp/os/Log.h>
10#include <yarp/os/LogStream.h>
11
12using namespace yarp::dev;
13using namespace yarp::os;
14using namespace yarp::sig;
15
16namespace {
17YARP_LOG_COMPONENT(AUDIORECORDER_NWC, "yarp.device.audioRecorder_nwc_yarp")
18} // namespace
19
23
25{
26 mutex.lock();
27
28 lastSound = b;
31
32 //initialization (first received data)
33 if (lastStamp.isValid()==false)
34 {
35 lastStamp = newStamp;
36 }
37
38 lastStamp = newStamp;
39
40 mutex.unlock();
41}
42
44{
45 mutex.lock();
46 data = lastSound;
47 stmp = lastStamp;
48 mutex.unlock();
49 return true;
50}
51
53{
54 if (!this->parseParams(config)) { return false; }
55
56 std::string local_stream = m_local;
57 local_stream += "/data:i";
58 std::string local_rpc = m_local;
59 local_rpc += "/rpc";
60 std::string remote_stream = m_remote;
61 remote_stream += "/data:o";
62 std::string remote_rpc = m_remote;
63 remote_rpc += "/rpc";
64
65 if (m_useStream)
66 {
68 {
69 yCError(AUDIORECORDER_NWC, "open(): Could not open port %s, check network", local_stream.c_str());
70 return false;
71 }
73 }
74
76 {
77 yCError(AUDIORECORDER_NWC, "open(): Could not open rpc port %s, check network", local_rpc.c_str());
78 return false;
79 }
80
81 bool ok;
82 if (m_useStream)
83 {
84 ok=Network::connect(remote_stream.c_str(), local_stream.c_str(), m_carrier);
85 if (!ok)
86 {
87 yCError(AUDIORECORDER_NWC, "open(): Could not connect %s -> %s", remote_stream.c_str(), local_stream.c_str());
88 return false;
89 }
90 }
91
92 ok=Network::connect(local_rpc, remote_rpc);
93 if (!ok)
94 {
95 yCError(AUDIORECORDER_NWC, "open() Could not connect %s -> %s", remote_rpc.c_str(), local_rpc.c_str());
96 return false;
97 }
98
100 {
101 yCError(AUDIORECORDER_NWC, "Error! Cannot attach the port as a client");
102 return false;
103 }
104
105 return true;
106}
107
109{
111 if (m_useStream)
112 {
114 }
115 return true;
116}
117
123
129
135
141
147
149{
150 std::lock_guard <std::mutex> lg(m_mutex);
152 if (!ret.ret)
153 {
154 yCError(AUDIORECORDER_NWC, "Unable to: isRecording()");
155 return ret.ret;
156 }
157 recording_enabled = ret.isRecording;
158 return ret.ret;
159}
160
162{
163 std::lock_guard <std::mutex> lg(m_mutex);
165 if (!ret.ret)
166 {
167 yCError(AUDIORECORDER_NWC, "Unable to: getRecordingAudioBufferMaxSize()");
168 return ret.ret;
169 }
170 size = ret.bufsize;
171 return ret.ret;
172}
173
175{
176 std::lock_guard <std::mutex> lg(m_mutex);
178 if (!ret.ret)
179 {
180 yCError(AUDIORECORDER_NWC, "Unable to: getRecordingAudioBufferCurrentSize()");
181 return ret.ret;
182 }
183 size = ret.bufsize;
184 return ret.ret;
185}
186
187ReturnValue AudioRecorder_nwc_yarp::getSound(yarp::sig::Sound& sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s)
188{
189 if (m_useStream)
190 {
191 yCError(AUDIORECORDER_NWC, "Unable to: getSound() streaming version not yet implemented");
192 return ReturnValue::return_code::return_value_error_not_implemented_by_device;
193 }
194 std::lock_guard <std::mutex> lg(m_mutex);
195 auto ret = m_audiograb_RPC.getSound_RPC(min_number_of_samples,max_number_of_samples, max_samples_timeout_s);
196 if (!ret.ret)
197 {
198 yCError(AUDIORECORDER_NWC, "Unable to: getSound()");
199 return ret.ret;
200 }
201 sound = ret.sound;
202 return ret.ret;
203}
bool ret
bool getLast(yarp::sig::Sound &data, yarp::os::Stamp &stmp)
void onRead(yarp::sig::Sound &v) override
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
IAudioGrabberMsgs m_audiograb_RPC
bool close() override
Close the DeviceDriver.
virtual yarp::dev::ReturnValue startRecording() override
Start the recording.
virtual yarp::dev::ReturnValue getRecordingAudioBufferMaxSize(yarp::sig::AudioBufferSize &size) override
virtual yarp::dev::ReturnValue resetRecordingAudioBuffer() override
virtual yarp::dev::ReturnValue isRecording(bool &recording_enabled) override
Check if the recording has been enabled (e.g.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
virtual yarp::dev::ReturnValue getSound(yarp::sig::Sound &sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s) override
Get a sound from a device.
virtual yarp::dev::ReturnValue setHWGain(double gain) override
Sets the hardware gain of the grabbing device (if supported by the hardware)
AudioRecorder_InputPortProcessor m_inputPort
virtual yarp::dev::ReturnValue stopRecording() override
Stop the recording.
virtual yarp::dev::ReturnValue setSWGain(double gain) override
Sets a software gain for the grabbed audio.
virtual yarp::dev::ReturnValue getRecordingAudioBufferCurrentSize(yarp::sig::AudioBufferSize &size) override
virtual return_isRecording isRecording_RPC()
virtual yarp::dev::ReturnValue setHWGain_RPC(const double gain)
virtual return_getRecordingAudioBufferMaxSize getRecordingAudioBufferMaxSize_RPC()
virtual return_getRecordingAudioBufferCurrentSize getRecordingAudioBufferCurrentSize_RPC()
virtual return_getSound getSound_RPC(const size_t min_number_of_samples, const size_t max_number_of_samples, const double max_samples_timeout_s)
virtual yarp::dev::ReturnValue startRecording_RPC()
virtual yarp::dev::ReturnValue setSWGain_RPC(const double gain)
virtual yarp::dev::ReturnValue resetRecordingAudioBuffer_RPC()
virtual yarp::dev::ReturnValue stopRecording_RPC()
A mini-server for performing network communication in the background.
bool getEnvelope(PortReader &envelope) override
void close() override
Stop port activity.
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.
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
An abstraction for a time stamp and/or sequence number.
Definition Stamp.h:21
bool isValid() const
Check if this Stamp is valid.
Definition Stamp.cpp:39
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition Wire.h:28
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:25
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.