6 #define _USE_MATH_DEFINES
23 #define DEBUG_TIME_SPENT 0
26 #define DEFAULT_SAMPLE_RATE (44100)
27 #define DEFAULT_NUM_CHANNELS (2)
28 #define DEFAULT_SAMPLE_SIZE (2)
32 if (m_outputBuffer ==
nullptr)
34 yCError(
AUDIOPLAYER_BASE) <<
"getPlaybackAudioBufferCurrentSize() called, but no audio buffer is allocated yet";
38 size = this->m_outputBuffer->size();
44 if (m_outputBuffer ==
nullptr)
50 size = this->m_outputBuffer->getMaxSize();
54 AudioPlayerDeviceBase::~AudioPlayerDeviceBase()
56 delete m_outputBuffer;
59 bool AudioPlayerDeviceBase::setSWGain(
double gain)
61 std::lock_guard<std::recursive_mutex> lock(m_mutex);
70 bool AudioPlayerDeviceBase::startPlayback()
72 std::lock_guard<std::recursive_mutex> lock(m_mutex);
73 m_playback_enabled =
true;
74 if (m_enable_buffer_autoclear && this->m_outputBuffer)
75 {this->m_outputBuffer->clear();}
80 bool AudioPlayerDeviceBase::stopPlayback()
82 std::lock_guard<std::recursive_mutex> lock(m_mutex);
83 m_playback_enabled =
false;
84 if (m_enable_buffer_autoclear && this->m_outputBuffer)
85 {this->m_outputBuffer->clear();}
90 bool AudioPlayerDeviceBase::isPlaying(
bool& playback_enabled)
92 playback_enabled = m_playback_enabled;
96 bool AudioPlayerDeviceBase::resetPlaybackAudioBuffer()
98 if (m_outputBuffer ==
nullptr)
103 std::lock_guard<std::recursive_mutex> lock(m_mutex);
104 this->m_outputBuffer->clear();
114 for (
size_t i = 0; i < num_samples; i++) {
115 for (
size_t j = 0; j < num_channels; j++) {
116 m_outputBuffer->write(sound.
get(i, j));
126 m_outputBuffer->clear();
131 for (
size_t i = 0; i < num_samples; i++) {
132 for (
size_t j = 0; j < num_channels; j++) {
133 m_outputBuffer->write(sound.
get(i, j));
143 std::lock_guard<std::recursive_mutex> lock(m_mutex);
160 if (m_sw_gain != 1.0)
165 if (freq != this->m_audioplayer_cfg.frequency ||
166 chans != this->m_audioplayer_cfg.numChannels)
169 waitUntilPlaybackStreamIsComplete();
173 yCInfo(
AUDIOPLAYER_BASE) <<
"changing from: " << this->m_audioplayer_cfg.numChannels <<
"channels, " << this->m_audioplayer_cfg.frequency <<
" Hz, ->" <<
174 chans <<
"channels, " << freq <<
" Hz";
176 bool was_playing = this->m_playback_enabled;
178 this->interruptDeviceAndClose();
181 m_audioplayer_cfg.numChannels = (int)(chans);
182 m_audioplayer_cfg.frequency = (int)(freq);
183 bool ok = configureDeviceAndStart();
192 this->startPlayback();
196 if (m_renderMode == RENDER_IMMEDIATE) {
197 return immediateSound(procsound);
198 }
else if (m_renderMode == RENDER_APPEND) {
199 return appendSound(procsound);
207 m_audioplayer_cfg.frequency = config.
check(
"rate",
Value(0),
"audio sample rate (0=automatic)").asInt32();
208 m_audioplayer_cfg.numSamples = config.
check(
"samples",
Value(0),
"number of samples per network packet (0=automatic). For chunks of 1 second of recording set samples=rate. Channels number is handled internally.").asInt32();
209 m_audioplayer_cfg.numChannels = config.
check(
"channels",
Value(0),
"number of audio channels (0=automatic, max is 2)").asInt32();
210 m_hw_gain = config.
check(
"hw_gain",
Value(1.0),
"HW gain").asFloat32();
211 m_sw_gain = config.
check(
"sw_gain",
Value(1.0),
"SW gain").asFloat32();
213 if (m_audioplayer_cfg.numChannels == 0) {
216 if (m_audioplayer_cfg.frequency == 0) {
219 if (m_audioplayer_cfg.numSamples == 0) {
220 m_audioplayer_cfg.numSamples = m_audioplayer_cfg.frequency;
223 if (config.
check(
"render_mode_append"))
225 m_renderMode = RENDER_APPEND;
227 if (config.
check(
"render_mode_immediate"))
229 m_renderMode = RENDER_IMMEDIATE;
242 AudioBufferSize buffer_size(m_audioplayer_cfg.numSamples, m_audioplayer_cfg.numChannels, m_audioplayer_cfg.bytesPerSample);
243 if (m_outputBuffer ==
nullptr)
249 m_enable_buffer_autoclear = config.
check(
"buffer_autoclear",
Value(
false),
"Automatically clear the buffer every time the devices is started/stopped").asBool();
250 m_audiobase_debug = config.
check(
"debug",
Value(
false),
"Enable debug mode").asBool();
255 void AudioPlayerDeviceBase::waitUntilPlaybackStreamIsComplete()
257 if (m_outputBuffer ==
nullptr)
259 yCError(
AUDIOPLAYER_BASE) <<
"waitUntilPlaybackStreamIsComplete() called, but no audio buffer is allocated yet";
263 while (m_outputBuffer->size().getSamples() != 0)
#define DEFAULT_NUM_CHANNELS
const yarp::os::LogComponent & AUDIOPLAYER_BASE()
#define DEFAULT_SAMPLE_RATE
constexpr double c_sleep_time
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.
A single value (typically within a Bottle).
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
size_t getChannels() const
Get the number of channels of the sound.
void amplify(double gain)
amplify a sound
int getFrequency() const
Get the frequency of the sound (i.e.
audio_sample get(size_t sample, size_t channel=0) const
size_t getSamples() const
Get the number of samples contained in the sound.
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface for the device drivers.
yarp::dev::CircularAudioBuffer< unsigned short int > CircularAudioBuffer_16t
void delay(double seconds)
Wait for a certain number of seconds.
An interface to the operating system, including Port based communication.