19 #define _USE_MATH_DEFINES
39 constexpr
double DEFAULT_BUFFER_DELAY = 5.0;
46 m_buffer_delay(DEFAULT_BUFFER_DELAY),
47 m_isDeviceOwned(false),
48 m_debug_enabled(false)
63 if (device2attach.
size() != 1)
65 yCError(AUDIOPLAYERWRAPPER,
"Cannot attach more than one device");
73 Idevice2attach->
view(m_irender);
76 if (
nullptr == m_irender)
78 yCError(AUDIOPLAYERWRAPPER,
"Subdevice passed to attach method is invalid");
83 PeriodicThread::setPeriod(m_period);
84 return PeriodicThread::start();
89 if (PeriodicThread::isRunning())
91 PeriodicThread::stop();
104 if (PeriodicThread::isRunning())
106 PeriodicThread::stop();
115 bool ok = command.
read(connection);
145 yCError(AUDIOPLAYERWRAPPER) <<
"Invalid command";
150 if (returnToSender !=
nullptr)
152 reply.
write(*returnToSender);
167 if (config.
check(
"debug"))
169 m_debug_enabled =
true;
172 if (config.
check(
"period"))
177 string name =
"/audioPlayerWrapper";
178 if (config.
check(
"name"))
182 m_audioInPortName = name +
"/audio:i";
183 m_rpcPortName = name +
"/rpc:i";
184 m_statusPortName = name +
"/status:o";
186 if(!initialize_YARP(config) )
188 yCError(AUDIOPLAYERWRAPPER) <<
"Error initializing YARP ports";
192 if (config.
check(
"playback_network_buffer_lenght"))
194 m_buffer_delay = config.
find(
"playback_network_buffer_lenght").
asFloat64();
196 yCInfo(AUDIOPLAYERWRAPPER) <<
"Using a 'playback_network_buffer_lenght' of" << m_buffer_delay <<
"s";
197 yCInfo(AUDIOPLAYERWRAPPER) <<
"Increase this value to robustify the real-time audio stream (it will increase latency too)";
199 if(config.
check(
"subdevice"))
208 yCError(AUDIOPLAYERWRAPPER) <<
"Failed to open subdevice.. check params";
212 driverlist.
push(&m_driver,
"1");
215 yCError(AUDIOPLAYERWRAPPER) <<
"Failed to open subdevice.. check params";
218 m_isDeviceOwned =
true;
222 if (m_irender ==
nullptr)
224 yCError(AUDIOPLAYERWRAPPER,
"m_irender is null\n");
231 yCError(AUDIOPLAYERWRAPPER,
"getPlaybackAudioBufferMaxSize failed\n");
240 if (!m_audioInPort.
open(m_audioInPortName))
242 yCError(AUDIOPLAYERWRAPPER,
"Failed to open port %s", m_audioInPortName.c_str());
245 if (!m_statusPort.
open(m_statusPortName))
247 yCError(AUDIOPLAYERWRAPPER,
"Failed to open port %s", m_statusPortName.c_str());
250 if (!m_rpcPort.
open(m_rpcPortName))
252 yCError(AUDIOPLAYERWRAPPER,
"Failed to open port %s", m_rpcPortName.c_str());
262 m_audioInPort.
close();
266 m_statusPort.
close();
276 scheduled_sound_type ss;
279 ss.scheduled_time = current_time + m_buffer_delay;
282 ss.scheduled_time = current_time + 5.0 * s.
getDuration();
284 ss.scheduled_time = current_time + m_buffer_delay > 5.0 * s.
getDuration() ? (m_buffer_delay) : (5.0 * s.
getDuration());
287 m_sound_buffer.push(ss);
290 if (!m_sound_buffer.empty() && current_time > m_sound_buffer.front().scheduled_time)
292 m_irender->
renderSound(m_sound_buffer.front().sound_data);
293 m_sound_buffer.pop();
310 yCTrace(AUDIOPLAYERWRAPPER,
"AudioPlayerWrapper::Close");
311 if (PeriodicThread::isRunning())
313 PeriodicThread::stop();
#define DEFAULT_THREAD_PERIOD
define control board standard interfaces
constexpr yarp::conf::vocab32_t VOCAB_OK
constexpr yarp::conf::vocab32_t VOCAB_ERR
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.
bool view(T *&x)
Get an interface to the device driver.
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 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.
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.
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.
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.
double getDuration() const
Get the duration of sound in seconds.
#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.
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
An interface to the operating system, including Port based communication.