6 #define _USE_MATH_DEFINES
26 constexpr
double DEFAULT_BUFFER_DELAY = 5.0;
32 m_buffer_delay(DEFAULT_BUFFER_DELAY)
47 if (device2attach.
size() != 1)
49 yCError(AUDIOPLAYERWRAPPER,
"Cannot attach more than one device");
57 Idevice2attach->
view(m_irender);
60 if (
nullptr == m_irender)
62 yCError(AUDIOPLAYERWRAPPER,
"Subdevice passed to attach method is invalid");
67 PeriodicThread::setPeriod(m_period);
68 return PeriodicThread::start();
73 if (PeriodicThread::isRunning())
75 PeriodicThread::stop();
88 if (PeriodicThread::isRunning())
90 PeriodicThread::stop();
99 bool ok = command.
read(connection);
117 else if (command.
get(0).
asString() ==
"sw_audio_gain")
127 yCError(AUDIOPLAYERWRAPPER) <<
"Invalid audio gain";
131 else if (command.
get(0).
asString() ==
"hw_audio_gain")
141 yCError(AUDIOPLAYERWRAPPER) <<
"Invalid audio gain";
161 yCError(AUDIOPLAYERWRAPPER) <<
"Invalid command";
166 if (returnToSender !=
nullptr)
168 reply.
write(*returnToSender);
183 if (config.
check(
"debug"))
185 m_debug_enabled =
true;
188 if (config.
check(
"period"))
193 std::string name =
"/audioPlayerWrapper";
194 if (config.
check(
"name"))
198 m_audioInPortName = name +
"/audio:i";
199 m_rpcPortName = name +
"/rpc:i";
200 m_statusPortName = name +
"/status:o";
202 if(!initialize_YARP(config) )
204 yCError(AUDIOPLAYERWRAPPER) <<
"Error initializing YARP ports";
208 if (config.
check(
"playback_network_buffer_size"))
210 m_buffer_delay = config.
find(
"playback_network_buffer_size").
asFloat64();
212 yCInfo(AUDIOPLAYERWRAPPER) <<
"Using a 'playback_network_buffer_size' of" << m_buffer_delay <<
"s";
213 yCInfo(AUDIOPLAYERWRAPPER) <<
"Increase this value to robustify the real-time audio stream (it will increase latency too)";
215 if(config.
check(
"subdevice"))
224 yCError(AUDIOPLAYERWRAPPER) <<
"Failed to open subdevice.. check params";
228 driverlist.
push(&m_driver,
"1");
231 yCError(AUDIOPLAYERWRAPPER) <<
"Failed to open subdevice.. check params";
234 m_isDeviceOwned =
true;
237 if (m_irender ==
nullptr)
239 yCError(AUDIOPLAYERWRAPPER,
"m_irender is null\n");
246 yCError(AUDIOPLAYERWRAPPER,
"getPlaybackAudioBufferMaxSize failed\n");
250 if (config.
check(
"start"))
261 if (!m_audioInPort.
open(m_audioInPortName))
263 yCError(AUDIOPLAYERWRAPPER,
"Failed to open port %s", m_audioInPortName.c_str());
266 if (!m_statusPort.
open(m_statusPortName))
268 yCError(AUDIOPLAYERWRAPPER,
"Failed to open port %s", m_statusPortName.c_str());
271 if (!m_rpcPort.
open(m_rpcPortName))
273 yCError(AUDIOPLAYERWRAPPER,
"Failed to open port %s", m_rpcPortName.c_str());
283 m_audioInPort.
close();
287 m_statusPort.
close();
299 yCDebug(AUDIOPLAYERWRAPPER) <<
"Received sound of:" << s->
getSamples() <<
" samples";
302 scheduled_sound_type ss;
305 ss.scheduled_time = current_time + m_buffer_delay;
308 ss.scheduled_time = current_time + 5.0 * s.
getDuration();
310 ss.scheduled_time = current_time + m_buffer_delay > 5.0 * s.
getDuration() ? (m_buffer_delay) : (5.0 * s.
getDuration());
313 m_sound_buffer.push(ss);
316 if (!m_sound_buffer.empty() && current_time > m_sound_buffer.front().scheduled_time)
318 m_irender->
renderSound(m_sound_buffer.front().sound_data);
319 m_sound_buffer.pop();
340 m_statusPort.
write(status);
345 yCTrace(AUDIOPLAYERWRAPPER,
"AudioPlayerWrapper::Close");
346 if (PeriodicThread::isRunning())
348 PeriodicThread::stop();
define control board standard interfaces
constexpr yarp::conf::vocab32_t VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_ERR
constexpr double DEFAULT_THREAD_PERIOD
bool close() override
Close the DeviceDriver.
~AudioPlayerWrapper() override
void attach(yarp::dev::IAudioRender *irend)
bool threadInit() override
Initialization method.
void run() override
Loop function.
bool attachAll(const yarp::dev::PolyDriverList &p) override
Specify which sensor this thread has to read from.
void threadRelease() override
Release method.
bool detachAll() override
Detach the object (you must have first called attach).
bool open(yarp::os::Searchable ¶ms) override
Open the DeviceDriver.
AudioPlayerStatus: A class used to describe the status of an audio player device.
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 enabled
true if the playback is currently enabled
bool view(T *&x)
Get an interface to the device driver.
virtual bool isPlaying(bool &playback_enabled)=0
Check if the playback has been enabled (e.g.
virtual bool renderSound(const yarp::sig::Sound &sound)=0
Render a sound using a device (i.e.
virtual bool resetPlaybackAudioBuffer()=0
virtual bool getPlaybackAudioBufferCurrentSize(yarp::dev::AudioBufferSize &size)=0
virtual bool startPlayback()=0
Start the playback.
virtual bool setSWGain(double gain)=0
Sets a software gain for the played audio.
virtual bool setHWGain(double gain)=0
Sets the hardware gain of the playback device (if supported by the hardware)
virtual bool stopPlayback()=0
Stop the playback.
virtual bool getPlaybackAudioBufferMaxSize(yarp::dev::AudioBufferSize &size)=0
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.
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 interrupt() override
Interrupt any current reads or writes attached to the port.
T * read(bool shouldWait=true) override
Read an available object from the port.
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.
An abstraction for a periodic thread.
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.
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.
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
virtual std::string asString() const
Get string value.
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
double getDuration() const
Get the duration of sound in seconds.
size_t getSamples() const
Get the number of samples contained in the sound.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface for the device drivers.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
An interface to the operating system, including Port based communication.