YARP
Yet Another Robot Platform
audioFromFileDevice.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include "audioFromFileDevice.h"
10 
11 #include <yarp/os/Thread.h>
12 #include <yarp/os/Time.h>
13 #include <yarp/os/Semaphore.h>
14 #include <yarp/os/Stamp.h>
15 #include <yarp/os/LogComponent.h>
16 #include <yarp/os/LogStream.h>
17 
18 #include <mutex>
19 #include <string>
20 
21 
22 using namespace yarp::os;
23 using namespace yarp::dev;
24 using namespace yarp::sig;
25 
26 constexpr double c_DEFAULT_PERIOD=0.01; //s
27 
28 namespace {
29 YARP_LOG_COMPONENT(AUDIOFROMFILE, "yarp.device.audioFromFileDevice")
30 }
31 
32 typedef unsigned short int audio_sample_16t;
33 
36 {
37 }
38 
40 {
41  delete m_inputBuffer;
42 }
43 
45 {
46  //sets the thread period
47  if(config.check("period"))
48  {
49  double period = config.find("period").asFloat64();
50  setPeriod(period);
51  yCInfo(AUDIOFROMFILE) << "Using chosen period of " << period << " s";
52  }
53  else
54  {
55  yCInfo(AUDIOFROMFILE) << "Using default period of " << c_DEFAULT_PERIOD << " s";
56  }
57 
58  //sets the number of samples period
59  if (config.check("samples"))
60  {
61  m_samples_to_be_copied = config.find("samples").asFloat64();
62  }
63  yCDebug(AUDIOFROMFILE) << m_samples_to_be_copied << " will be processed every iteration";
64 
65  //sets the filename
66  if (config.check("file_name"))
67  {
68  m_audio_filename = config.find("file_name").asString();
69  yCInfo(AUDIOFROMFILE) << "Audio will loaded from file:" << m_audio_filename;
70  }
71  else
72  {
73  yCInfo(AUDIOFROMFILE) << "No `file_name` option specified. Audio will be loaded from default file:" << m_audio_filename;
74  }
75 
76  //opens the file
77  bool ret = yarp::sig::file::read(m_audioFile, m_audio_filename.c_str());
78  if (ret == false)
79  {
80  yCError(AUDIOFROMFILE) << "Unable to open file" << m_audio_filename.c_str();
81  return false;
82  }
83 
84  //sets the audio configuration equal to the audio file
89  constexpr size_t c_EXTRA_SPACE = 2;
91  m_inputBuffer = new yarp::dev::CircularAudioBuffer_16t("fake_mic_buffer", buffer_size);
92 
93  //start the capture thread
94  start();
95  return true;
96 }
97 
99 {
101  return true;
102 }
103 
104 
105 bool audioFromFileDevice::threadInit()
106 {
107  return true;
108 }
109 
110 
111 void audioFromFileDevice::run()
112 {
113  // when not recording, do nothing
114  if (!m_isRecording)
115  {
116  return;
117  }
118 
119  // Just acquire raw data and put them in the buffer
120  auto p = m_audioFile.getInterleavedAudioRawData();
121  size_t fsize_in_samples = m_audioFile.getSamples();
122 // size_t bps = m_audioFile.getBytesPerSample();
123 
124  //each iteration, which occurs every xxx ms, I copy a bunch of samples in the buffer.
125  //When the pointer reaches the end of the sound (audioFile), just restart from the beginning in an endless loop
126  for (size_t i = 0; i < m_samples_to_be_copied; i++)
127  {
128  if (m_bpnt >= fsize_in_samples)
129  {
130  m_bpnt = 0;
131  }
132  m_inputBuffer->write((unsigned short)(p.at(m_bpnt).get()));
133  m_bpnt++;
134  }
135 #ifdef ADVANCED_DEBUG
136  yCDebug(AUDIOFROMFILE) << "b_pnt" << m_bpnt << "/" << fsize_in_bytes << " bytes";
137 #endif
138 }
LogStream.h
yarp::dev::CircularAudioBuffer::write
void write(SAMPLE elem)
Definition: CircularAudioBuffer.h:48
yarp::dev::AudioDeviceDriverSettings::bytesPerSample
size_t bytesPerSample
Definition: AudioRecorderDeviceBase.h:30
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::sig::Sound::getFrequency
int getFrequency() const
Get the frequency of the sound (i.e.
Definition: Sound.cpp:221
yarp::sig::file::read
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:827
yarp::sig
Signal processing.
Definition: Image.h:25
audioFromFileDevice::close
bool close() override
Close the DeviceDriver.
Definition: audioFromFileDevice.cpp:98
c_DEFAULT_PERIOD
constexpr double c_DEFAULT_PERIOD
Definition: audioFromFileDevice.cpp:26
yarp::sig::Sound::getSamples
size_t getSamples() const
Get the number of samples contained in the sound.
Definition: Sound.cpp:404
yarp::dev::AudioBufferSize
Definition: AudioBufferSize.h:26
yarp::dev::AudioDeviceDriverSettings::numChannels
size_t numChannels
Definition: AudioRecorderDeviceBase.h:28
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
yarp::dev::AudioDeviceDriverSettings::numSamples
size_t numSamples
Definition: AudioRecorderDeviceBase.h:27
audioFromFileDevice::~audioFromFileDevice
~audioFromFileDevice() override
Definition: audioFromFileDevice.cpp:39
yarp::dev::AudioRecorderDeviceBase::m_isRecording
bool m_isRecording
Definition: AudioRecorderDeviceBase.h:36
yarp::sig::Sound::getChannels
size_t getChannels() const
Get the number of channels of the sound.
Definition: Sound.cpp:409
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
audioFromFileDevice::audioFromFileDevice
audioFromFileDevice()
Definition: audioFromFileDevice.cpp:34
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
yarp::dev::AudioDeviceDriverSettings::frequency
size_t frequency
Definition: AudioRecorderDeviceBase.h:29
audioFromFileDevice::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: audioFromFileDevice.cpp:44
yarp::dev::AudioRecorderDeviceBase::m_audiorecorder_cfg
AudioDeviceDriverSettings m_audiorecorder_cfg
Definition: AudioRecorderDeviceBase.h:40
Stamp.h
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
yarp::os::PeriodicThread::start
bool start()
Call this to start the thread.
Definition: PeriodicThread.cpp:311
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
yarp::os::PeriodicThread::setPeriod
bool setPeriod(double period)
Set the (new) period of the thread.
Definition: PeriodicThread.cpp:281
Thread.h
yarp::os::Searchable::find
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
Semaphore.h
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
LogComponent.h
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
audio_sample_16t
unsigned short int audio_sample_16t
Definition: audioFromFileDevice.cpp:32
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
yarp::dev::AudioRecorderDeviceBase::m_inputBuffer
yarp::dev::CircularAudioBuffer_16t * m_inputBuffer
Definition: AudioRecorderDeviceBase.h:38
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yCDebug
#define yCDebug(component,...)
Definition: LogComponent.h:112
yarp::dev::CircularAudioBuffer_16t
yarp::dev::CircularAudioBuffer< unsigned short int > CircularAudioBuffer_16t
Definition: CircularAudioBuffer.h:118
yarp::sig::Sound::getInterleavedAudioRawData
std::vector< std::reference_wrapper< audio_sample > > getInterleavedAudioRawData() const
Returns a serialized version of the sound, in interleaved format, e.g.
Definition: Sound.cpp:343
yarp::os::PeriodicThread::stop
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
Definition: PeriodicThread.cpp:296
Time.h
yarp::sig::Sound::getBytesPerSample
size_t getBytesPerSample() const
Get the number of bytes per sample.
Definition: Sound.cpp:399
audioFromFileDevice.h
yarp::os::Value::asFloat64
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:225