16constexpr
size_t DEFAULT_MIN_NUMBER_OF_SAMPLES_OVER_NETWORK = 11250;
17constexpr
size_t DEFAULT_MAX_NUMBER_OF_SAMPLES_OVER_NETWORK = 11250;
18constexpr
double DEFAULT_GETSOUND_TIMEOUT = 1.0;
25 m_min_number_of_samples_over_network(DEFAULT_MIN_NUMBER_OF_SAMPLES_OVER_NETWORK),
26 m_max_number_of_samples_over_network(DEFAULT_MAX_NUMBER_OF_SAMPLES_OVER_NETWORK),
27 m_getSound_timeout(DEFAULT_GETSOUND_TIMEOUT)
42 if (config.
check(
"period"))
47 if (config.
check(
"subdevice"))
56 yCError(AUDIORECORDERWRAPPER) <<
"Failed to open subdevice.. check params";
60 driverlist.
push(&m_driver,
"1");
63 yCError(AUDIORECORDERWRAPPER) <<
"Failed to open subdevice.. check params";
66 m_isDeviceOwned =
true;
71 yCError(AUDIORECORDERWRAPPER,
"Failed to open IAudioGrabberSound interface");
76 if (config.
check(
"min_samples_over_network"))
78 m_min_number_of_samples_over_network = config.
find(
"min_samples_over_network").
asInt64();
80 if (config.
check(
"max_samples_over_network"))
82 m_max_number_of_samples_over_network = config.
find(
"max_samples_over_network").
asInt64();
84 yCInfo(AUDIORECORDERWRAPPER) <<
"Wrapper configured to produce packets with the following size (in samples): " <<
85 m_min_number_of_samples_over_network <<
" < samples < " << m_max_number_of_samples_over_network;
89 if (config.
check(
"max_samples_timeout"))
91 m_getSound_timeout = config.
find(
"max_samples_timeout").
asFloat64();
93 yCInfo(AUDIORECORDERWRAPPER) <<
"Wrapper configured with max_samples_timeout: " << m_getSound_timeout <<
"s";
96 std::string portname =
"/audioRecorderWrapper";
97 if (config.
check(
"name"))
101 if (m_streamingPort.
open(portname +
"/audio:o") ==
false)
103 yCError(AUDIORECORDERWRAPPER) <<
"Unable to open port" << portname +
"/audio:o";
106 if (m_statusPort.
open(portname +
"/status:o") ==
false)
108 yCError(AUDIORECORDERWRAPPER) <<
"Unable to open port" << portname +
"/status:o";
112 if (m_rpcPort.
open(portname +
"/rpc") ==
false)
114 yCError(AUDIORECORDERWRAPPER) <<
"Unable to open port" << portname +
"/rpc";
122 yCError(AUDIORECORDERWRAPPER,
"getPlaybackAudioBufferMaxSize failed\n");
127 if (config.
check(
"start")) {
138 if (m_mic !=
nullptr)
140 m_dataThread->
stop();
141 m_statusThread->
stop();
146 m_streamingPort.
close();
150 m_statusPort.
close();
161 bool ok = command.
read(connection);
179 else if (command.
get(0).
asString() ==
"sw_audio_gain")
189 yCError(AUDIORECORDERWRAPPER) <<
"Invalid audio gain";
193 else if (command.
get(0).
asString() ==
"hw_audio_gain")
203 yCError(AUDIORECORDERWRAPPER) <<
"Invalid audio gain";
223 yCError(AUDIORECORDERWRAPPER) <<
"Invalid command";
228 if (returnToSender !=
nullptr)
230 reply.
write(*returnToSender);
237 if (device2attach.
size() != 1)
239 yCError(AUDIORECORDERWRAPPER,
"Cannot attach more than one device");
247 Idevice2attach->
view(m_mic);
250 if (
nullptr == m_mic)
252 yCError(AUDIORECORDERWRAPPER,
"Subdevice passed to attach method is invalid");
260 m_dataThread->
start();
261 m_statusThread->
start();
269 m_dataThread->
stop();
273 m_statusThread->
stop();
288 m_dataThread->
stop();
292 m_statusThread->
stop();
300 if (
m_ARW->m_debug_enabled)
326 yCDebug(AUDIORECORDERWRAPPER) << (current_time -
m_ARW->m_debug_last_time);
327 m_ARW->m_debug_last_time = current_time;
330 if (
m_ARW->m_mic ==
nullptr)
332 yCError(AUDIORECORDERWRAPPER) <<
"The IAudioGrabberSound interface is not available yet!";
336#ifdef PRINT_DEBUG_MESSAGES
338 audio_buffer_size buf_max;
339 audio_buffer_size buf_cur;
340 mic->getRecordingAudioBufferMaxSize(buf_max);
341 mic->getRecordingAudioBufferCurrentSize(buf_cur);
342 yCDebug(AUDIORECORDERWRAPPER) <<
"BEFORE Buffer status:" << buf_cur.getBytes() <<
"/" << buf_max.getBytes() <<
"bytes";
347 m_ARW->m_mic->
getSound(snd,
m_ARW->m_min_number_of_samples_over_network,
m_ARW->m_max_number_of_samples_over_network,
m_ARW->m_getSound_timeout);
352 yCWarning(AUDIORECORDERWRAPPER) <<
"subdevice->getSound() is not producing sounds of the requested size ("
353 <<
m_ARW->m_min_number_of_samples_over_network <<
"<"
355 <<
m_ARW->m_max_number_of_samples_over_network <<
") failed";
358#ifdef PRINT_DEBUG_MESSAGES
360 audio_buffer_size buf_max;
361 audio_buffer_size buf_cur;
362 mic->getRecordingAudioBufferMaxSize(buf_max);
363 mic->getRecordingAudioBufferCurrentSize(buf_cur);
364 yCDebug(AUDIORECORDERWRAPPER) <<
"AFTER Buffer status:" << buf_cur.getBytes() <<
"/" << buf_max.getBytes() <<
"bytes";
367#ifdef PRINT_DEBUG_MESSAGES
379 yCError(AUDIORECORDERWRAPPER) <<
"Subdevice produced sound of 0 samples!";
384 yCError(AUDIORECORDERWRAPPER) <<
"Subdevice produced sound of 0 channels!";
389 yCError(AUDIORECORDERWRAPPER) <<
"Subdevice produced sound with 0 frequency!";
constexpr yarp::conf::vocab32_t VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_ERR
constexpr double DEFAULT_THREAD_PERIOD
void run() override
Loop function.
AudioRecorderWrapper * m_ARW
AudioRecorderWrapper * m_ARW
void run() override
Loop function.
AudioRecorderWrapper: A Wrapper which streams audio over the network, after grabbing it from a device
bool detachAll() override
Detach the object (you must have first called attach).
friend class AudioRecorderDataThread
bool close() override
Close the DeviceDriver.
friend class AudioRecorderStatusThread
bool attachAll(const yarp::dev::PolyDriverList &p) override
Attach to a list of objects.
~AudioRecorderWrapper() override
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.
AudioRecorderStatus: A class used to describe the status of an audio recorder device.
bool enabled
true if the playback is currently enabled
size_t current_buffer_size
the size of the audio buffer [samples]
size_t max_buffer_size
the max_size of the audio buffer [samples]
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 setSWGain(double gain)=0
Sets a software gain for the grabbed audio.
virtual bool getRecordingAudioBufferCurrentSize(yarp::dev::AudioBufferSize &size)=0
virtual bool getRecordingAudioBufferMaxSize(yarp::dev::AudioBufferSize &size)=0
virtual bool stopRecording()=0
Stop the recording.
virtual bool startRecording()=0
Start the recording.
virtual bool isRecording(bool &recording_enabled)=0
Check if the recording has been enabled (e.g.
virtual bool setHWGain(double gain)=0
Sets the hardware gain of the grabbing device (if supported by the hardware)
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.
void addVocab32(yarp::conf::vocab32_t x)
Places a vocabulary item in the bottle, at the end of the list.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
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 setPeriod(double period)
Set the (new) period of the thread.
bool isRunning() const
Returns true when the thread is started, false otherwise.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
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 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.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
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 See Audio in YARP for additional documentation on YARP audio.
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,...)
For streams capable of holding different kinds of content, check what they actually have.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
An interface to the operating system, including Port based communication.