27 #define SAMPLING_RATE 44100
28 #define CHUNK_SIZE 512
29 #define SLEEP_TIME 0.005
30 #define SAMPLES_TO_BE_COPIED 512
44 m_cfg_bytesPerSample(0),
45 m_audio_filename(
"audio.wav"),
47 m_inputBuffer(nullptr),
48 m_getSoundIsNotBlocking(false)
60 if(config.
check(
"period"))
64 yCInfo(FAKEMICROPHONE) <<
"Using chosen period of " << period <<
" s";
72 if (config.
check(
"audio_file"))
78 yCInfo(FAKEMICROPHONE) <<
"--audio_file option not found. Using default:" << m_audio_filename;
85 yCError(FAKEMICROPHONE) <<
"Unable to open file" << m_audio_filename.c_str();
94 const size_t EXTRA_SPACE = 2;
95 AudioBufferSize buffer_size(m_cfg_numSamples*EXTRA_SPACE, m_cfg_numChannels, m_cfg_bytesPerSample);
108 delete m_inputBuffer;
115 bool fakeMicrophone::threadInit()
121 void fakeMicrophone::run()
131 size_t fsize_in_samples = m_audioFile.
getSamples();
138 if (m_bpnt >= fsize_in_samples)
142 m_inputBuffer->
write((
unsigned short)(p.at(m_bpnt).get()));
145 #ifdef ADVANCED_DEBUG
146 yCDebug(FAKEMICROPHONE) <<
"b_pnt" << m_bpnt <<
"/" << fsize_in_bytes <<
" bytes";
152 std::lock_guard<std::mutex> lock(m_mutex);
153 m_isRecording =
true;
154 #ifdef BUFFER_AUTOCLEAR
155 this->m_recDataBuffer->clear();
157 yCInfo(FAKEMICROPHONE) <<
"Recording started";
164 std::lock_guard<std::mutex> lock(m_mutex);
165 m_isRecording =
false;
166 #ifdef BUFFER_AUTOCLEAR
167 this->m_recDataBuffer->clear();
169 yCInfo(FAKEMICROPHONE) <<
"Recording stopped";
185 size = this->m_inputBuffer->
size();
192 std::lock_guard<std::mutex> lock(m_mutex);
193 m_inputBuffer->
clear();
194 yCDebug(FAKEMICROPHONE) <<
"resetRecordingAudioBuffer";
202 #ifdef AUTOMATIC_REC_START
203 if (m_isRecording ==
false)
209 while (m_isRecording ==
false)
213 yCInfo(FAKEMICROPHONE) <<
"getSound() is currently waiting. Use startRecording() to start the audio stream";
225 if (max_number_of_samples < min_number_of_samples)
227 yCError(FAKEMICROPHONE) <<
"max_number_of_samples must be greater than min_number_of_samples!";
230 if (max_number_of_samples > this->m_cfg_numSamples)
232 yCWarning(FAKEMICROPHONE) <<
"max_number_of_samples bigger than the internal audio buffer! It will be truncated to:" << this->m_cfg_numSamples;
233 max_number_of_samples = this->m_cfg_numSamples;
237 size_t buff_size = 0;
243 if (buff_size >= max_number_of_samples) {
break; }
244 if (buff_size >= min_number_of_samples &&
yarp::os::Time::now() - start_time > max_samples_timeout_s) {
break; }
245 if (m_isRecording ==
false) {
break; }
250 yCDebug(FAKEMICROPHONE) <<
"getSound() Buffer size is " << buff_size <<
"/" << max_number_of_samples <<
" after 1s";
258 size_t samples_to_be_copied = buff_size;
259 if (samples_to_be_copied > max_number_of_samples) samples_to_be_copied = max_number_of_samples;
262 sound.
resize(samples_to_be_copied, this->m_cfg_numChannels);
267 #ifdef DEBUG_TIME_SPENT
270 for (
size_t i = 0; i< samples_to_be_copied; i++)
271 for (
size_t j = 0; j<this->m_cfg_numChannels; j++)
273 int16_t s = (int16_t)(m_inputBuffer->
read());
278 #ifdef DEBUG_TIME_SPENT
280 yCDebug(FAKEMICROPHONE) << ct2 - ct1;
virtual bool getSound(yarp::sig::Sound &sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s) override
Get a sound from a device.
virtual bool resetRecordingAudioBuffer() override
virtual bool startRecording() override
Start the recording.
bool close() override
Close the DeviceDriver.
virtual bool getRecordingAudioBufferCurrentSize(yarp::dev::AudioBufferSize &size) override
virtual bool stopRecording() override
Stop the recording.
virtual bool getRecordingAudioBufferMaxSize(yarp::dev::AudioBufferSize &size) override
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
~fakeMicrophone() override
yarp::dev::AudioBufferSize getMaxSize()
An abstraction for a periodic thread.
bool setPeriod(double period)
Set the (new) period of the thread.
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...
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.
static void delaySystem(double seconds)
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.
void setFrequency(int freq)
Set the frequency of the sound (i.e.
size_t getBytesPerSample() const
Get the number of bytes per sample.
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 resize(size_t samples, size_t channels=1)
Set the sound size.
int getFrequency() const
Get the frequency of the sound (i.e.
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 SAMPLES_TO_BE_COPIED
unsigned short int audio_sample_16t
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface for the device drivers.
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.
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)