27 constexpr
size_t DEFAULT_MIN_NUMBER_OF_SAMPLES_OVER_NETWORK = 11250;
28 constexpr
size_t DEFAULT_MAX_NUMBER_OF_SAMPLES_OVER_NETWORK = 11250;
29 constexpr
double DEFAULT_GETSOUND_TIMEOUT = 1.0;
31 #ifdef DEBUG_TIME_SPENT
41 m_min_number_of_samples_over_network(DEFAULT_MIN_NUMBER_OF_SAMPLES_OVER_NETWORK),
42 m_max_number_of_samples_over_network(DEFAULT_MAX_NUMBER_OF_SAMPLES_OVER_NETWORK),
43 m_getSound_timeout(DEFAULT_GETSOUND_TIMEOUT),
44 m_isDeviceOwned(false)
45 #ifdef DEBUG_TIME_SPENT
62 if (config.
check(
"period"))
67 if (config.
check(
"subdevice"))
76 yCError(AUDIORECORDERWRAPPER) <<
"Failed to open subdevice.. check params";
80 driverlist.
push(&m_driver,
"1");
83 yCError(AUDIORECORDERWRAPPER) <<
"Failed to open subdevice.. check params";
86 m_isDeviceOwned =
true;
91 yCError(AUDIORECORDERWRAPPER,
"Failed to open IAudioGrabberSound interface");
96 if (config.
check(
"min_samples_over_network"))
98 m_min_number_of_samples_over_network = config.
find(
"min_samples_over_network").
asInt64();
100 if (config.
check(
"max_samples_over_network"))
102 m_max_number_of_samples_over_network = config.
find(
"max_samples_over_network").
asInt64();
104 yCInfo(AUDIORECORDERWRAPPER) <<
"Wrapper configured to produce packets with the following size (in samples): " <<
105 m_min_number_of_samples_over_network <<
" < samples < " << m_max_number_of_samples_over_network;
109 if (config.
check(
"max_samples_timeout"))
111 m_getSound_timeout = config.
find(
"max_samples_timeout").
asFloat64();
113 yCInfo(AUDIORECORDERWRAPPER) <<
"Wrapper configured with max_samples_timeout: " << m_getSound_timeout <<
"s";
116 std::string portname =
"/audioRecorderWrapper";
117 if (config.
check(
"name"))
121 if (m_streamingPort.
open(portname +
"/audio:o") ==
false)
123 yCError(AUDIORECORDERWRAPPER) <<
"Unable to open port" << portname;
128 if (m_rpcPort.
open(portname +
"/rpc") ==
false)
130 yCError(AUDIORECORDERWRAPPER) <<
"Unable to open port" << portname +
"/rpc";
136 if (config.
check(
"start")) {
146 if (m_mic !=
nullptr)
148 PeriodicThread::stop();
153 m_streamingPort.
close();
164 #ifdef DEBUG_TIME_SPENT
166 yCDebug(AUDIORECORDERWRAPPER) << current_time - m_last_time;
167 m_last_time = current_time;
170 if (m_mic ==
nullptr)
172 yCError(AUDIORECORDERWRAPPER) <<
"The IAudioGrabberSound interface is not available yet!";
176 #ifdef PRINT_DEBUG_MESSAGES
178 audio_buffer_size buf_max;
179 audio_buffer_size buf_cur;
180 mic->getRecordingAudioBufferMaxSize(buf_max);
181 mic->getRecordingAudioBufferCurrentSize(buf_cur);
182 yCDebug(AUDIORECORDERWRAPPER) <<
"BEFORE Buffer status:" << buf_cur.getBytes() <<
"/" << buf_max.getBytes() <<
"bytes";
187 m_mic->
getSound(snd, m_min_number_of_samples_over_network, m_max_number_of_samples_over_network, m_getSound_timeout);
189 if (snd.
getSamples() < m_min_number_of_samples_over_network ||
190 snd.
getSamples() < m_max_number_of_samples_over_network)
192 yCWarning(AUDIORECORDERWRAPPER) <<
"subdevice->getSound() is not producing sounds of the requested size ("
193 << m_min_number_of_samples_over_network <<
"<"
195 << m_max_number_of_samples_over_network <<
") failed";
198 #ifdef PRINT_DEBUG_MESSAGES
200 audio_buffer_size buf_max;
201 audio_buffer_size buf_cur;
202 mic->getRecordingAudioBufferMaxSize(buf_max);
203 mic->getRecordingAudioBufferCurrentSize(buf_cur);
204 yCDebug(AUDIORECORDERWRAPPER) <<
"AFTER Buffer status:" << buf_cur.getBytes() <<
"/" << buf_max.getBytes() <<
"bytes";
207 #ifdef PRINT_DEBUG_MESSAGES
219 yCError(AUDIORECORDERWRAPPER) <<
"Subdevice produced sound of 0 samples!";
224 yCError(AUDIORECORDERWRAPPER) <<
"Subdevice produced sound of 0 channels!";
229 yCError(AUDIORECORDERWRAPPER) <<
"Subdevice produced sound with 0 frequency!";
234 m_streamingPort.
write(snd);
241 bool ok = command.
read(connection);
242 if (!ok)
return false;
269 yCError(AUDIORECORDERWRAPPER) <<
"Invalid command";
274 if (returnToSender !=
nullptr)
276 reply.
write(*returnToSender);
283 if (device2attach.
size() != 1)
285 yCError(AUDIORECORDERWRAPPER,
"Cannot attach more than one device");
293 Idevice2attach->
view(m_mic);
296 if (
nullptr == m_mic)
298 yCError(AUDIORECORDERWRAPPER,
"Subdevice passed to attach method is invalid");
303 PeriodicThread::setPeriod(m_period);
304 return PeriodicThread::start();
309 if (PeriodicThread::isRunning())
311 PeriodicThread::stop();
324 if (PeriodicThread::isRunning())
326 PeriodicThread::stop();
#define DEFAULT_THREAD_PERIOD
constexpr yarp::conf::vocab32_t VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_ERR
bool detachAll() override
Detach the object (you must have first called attach).
AudioRecorderWrapper()
Constructor.
void run() override
Loop function.
bool close() override
Close the DeviceDriver.
bool threadInit() override
Initialization method.
bool attachAll(const yarp::dev::PolyDriverList &p) override
Attach to a list of objects.
~AudioRecorderWrapper() override
void threadRelease() override
Release method.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
void attach(yarp::dev::IAudioGrabberSound *igrab)
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
bool view(T *&x)
Get an interface to the device driver.
Read a YARP-format sound block from a device.
virtual bool getSound(yarp::sig::Sound &sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s)=0
Get a sound from a device.
virtual bool resetRecordingAudioBuffer()=0
virtual bool stopRecording()=0
Stop the recording.
virtual bool startRecording()=0
Start the recording.
void push(PolyDriver *p, const char *k)
A container for a device driver.
bool isValid() const
Check if device is valid.
bool open(const std::string &txt)
Construct and configure a device by its common name.
A simple collection of objects that can be described and transmitted in a portable way.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
void clear()
Empties the bottle of any objects it contains.
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
An interface for reading from a network connection.
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
An interface for writing to a network connection.
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
void setReader(PortReader &reader) override
Set an external reader for port data.
void interrupt() override
Interrupt any current reads or writes attached to the port.
bool setEnvelope(PortWriter &envelope) override
Set an envelope (e.g., a timestamp) to the next message which will be sent.
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.
A class for storing options and configuration information.
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
A base class for nested structures that can be searched.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
static void delaySystem(double seconds)
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
virtual std::int64_t asInt64() const
Get 64-bit integer value.
virtual std::string asString() const
Get string value.
Class for storing sounds.
size_t getBytesPerSample() const
Get the number of bytes per sample.
size_t getChannels() const
Get the number of channels of the sound.
int getFrequency() const
Get the frequency of the sound (i.e.
size_t getSamples() const
Get the number of samples contained in the sound.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
yarp::rosmsg::std_msgs::Time Time
An interface for the device drivers.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
The main, catch-all namespace for YARP.