20 #define AUTOMATIC_REC_START 0
21 #define DEBUG_TIME_SPENT 0
24 #define DEFAULT_SAMPLE_RATE (44100)
25 #define DEFAULT_NUM_CHANNELS (2)
26 #define DEFAULT_SAMPLE_SIZE (2)
28 bool AudioRecorderDeviceBase::getSound(
yarp::sig::Sound& sound,
size_t min_number_of_samples,
size_t max_number_of_samples,
double max_samples_timeout_s)
32 #if AUTOMATIC_REC_START
33 if (m_isRecording ==
false)
35 this->startRecording();
39 while (m_recording_enabled ==
false)
55 if (max_number_of_samples < min_number_of_samples)
60 if (max_number_of_samples > this->m_audiorecorder_cfg.numSamples)
62 yCWarning(
AUDIORECORDER_BASE) <<
"max_number_of_samples ("<< max_number_of_samples <<
") is bigger than the internal audio buffer! It will be truncated to:" << this->m_audiorecorder_cfg.numSamples;
63 max_number_of_samples = this->m_audiorecorder_cfg.numSamples;
72 buff_size = m_inputBuffer->size().getSamples();
73 if (buff_size >= max_number_of_samples) {
break; }
74 if (buff_size >= min_number_of_samples &&
yarp::os::Time::now() - start_time > max_samples_timeout_s) {
break; }
75 if (m_recording_enabled ==
false) {
break; }
86 size_t samples_to_be_copied = buff_size;
87 if (samples_to_be_copied > max_number_of_samples) {
88 samples_to_be_copied = max_number_of_samples;
90 if (sound.
getChannels() != this->m_audiorecorder_cfg.numChannels && sound.
getSamples() != samples_to_be_copied)
92 sound.
resize(samples_to_be_copied, this->m_audiorecorder_cfg.numChannels);
100 for (
size_t i = 0; i < samples_to_be_copied; i++)
102 for (
size_t j = 0; j < this->m_audiorecorder_cfg.numChannels; j++)
104 int16_t s = (int16_t)(m_inputBuffer->read());
105 if (s > (std::numeric_limits<int16_t>::max() - m_cliptol) ||
106 s < (std::numeric_limits<int16_t>::min() + m_cliptol))
115 if (m_sw_gain!=1.0) {sound.
amplify(m_sw_gain);}
127 if (m_inputBuffer ==
nullptr)
133 size = this->m_inputBuffer->getMaxSize();
140 if (m_inputBuffer ==
nullptr)
146 size = this->m_inputBuffer->size();
150 bool AudioRecorderDeviceBase::setSWGain(
double gain)
152 std::lock_guard<std::mutex> lock(m_mutex);
161 bool AudioRecorderDeviceBase::resetRecordingAudioBuffer()
163 if (m_inputBuffer ==
nullptr)
168 std::lock_guard<std::mutex> lock(m_mutex);
169 m_inputBuffer->clear();
174 bool AudioRecorderDeviceBase::startRecording()
176 std::lock_guard<std::mutex> lock(m_mutex);
177 m_recording_enabled =
true;
178 if (m_enable_buffer_autoclear && this->m_inputBuffer)
180 this->m_inputBuffer->clear();
186 bool AudioRecorderDeviceBase::stopRecording()
188 std::lock_guard<std::mutex> lock(m_mutex);
189 m_recording_enabled =
false;
190 if (m_enable_buffer_autoclear && this->m_inputBuffer)
200 bool AudioRecorderDeviceBase::isRecording(
bool& recording_enabled)
202 recording_enabled = m_recording_enabled;
206 AudioRecorderDeviceBase::~AudioRecorderDeviceBase()
208 delete m_inputBuffer;
211 bool AudioRecorderDeviceBase::configureRecorderAudioDevice(
yarp::os::Searchable& config, std::string device_name)
213 m_audiorecorder_cfg.frequency = config.
check(
"rate",
Value(0),
"audio sample rate (0=automatic)").asInt32();
214 m_audiorecorder_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();
215 m_audiorecorder_cfg.numChannels = config.
check(
"channels",
Value(0),
"number of audio channels (0=automatic, max is 2)").asInt32();
216 m_hw_gain = config.
check(
"hw_gain",
Value(1.0),
"HW gain").asFloat32();
217 m_sw_gain = config.
check(
"sw_gain",
Value(1.0),
"SW gain").asFloat32();
219 if (m_audiorecorder_cfg.frequency == 0) {
222 if (m_audiorecorder_cfg.numChannels == 0) {
225 if (m_audiorecorder_cfg.numSamples == 0) {
226 m_audiorecorder_cfg.numSamples = m_audiorecorder_cfg.frequency;
237 AudioBufferSize rec_buffer_size(m_audiorecorder_cfg.numSamples, m_audiorecorder_cfg.numChannels, m_audiorecorder_cfg.bytesPerSample);
238 if (m_inputBuffer ==
nullptr)
244 m_enable_buffer_autoclear = config.
check(
"buffer_autoclear",
Value(
true),
"Automatically clear the buffer every time the devices is started/stopped").asBool();
245 m_audiobase_debug = config.
check(
"debug",
Value(
false),
"Enable debug mode").asBool();
const yarp::os::LogComponent & AUDIORECORDER_BASE()
#define DEFAULT_NUM_CHANNELS
#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.
static void delaySystem(double seconds)
A single value (typically within a Bottle).
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
void setFrequency(int freq)
Set the frequency of the sound (i.e.
std::vector< std::reference_wrapper< audio_sample > > getInterleavedAudioRawData() const
Returns a serialized version of the sound, in interleaved format, e.g.
size_t getChannels() const
Get the number of channels of the sound.
void amplify(double gain)
amplify a sound
void resize(size_t samples, size_t channels=1)
Set the sound size.
void set(audio_sample value, size_t sample, size_t channel=0)
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,...)
#define yCWarningThrottle(component, period,...)
For streams capable of holding different kinds of content, check what they actually have.
yarp::dev::CircularAudioBuffer< unsigned short int > CircularAudioBuffer_16t
double now()
Return the current time in seconds, relative to an arbitrary starting point.
An interface to the operating system, including Port based communication.