Skip to content

Commit

Permalink
Documentation and parameters cleanup. Added --help option
Browse files Browse the repository at this point in the history
  • Loading branch information
randaz81 committed Mar 9, 2021
1 parent 23f38df commit 999dfcd
Show file tree
Hide file tree
Showing 19 changed files with 196 additions and 80 deletions.
15 changes: 12 additions & 3 deletions src/devices/audioFromFileDevice/audioFromFileDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ bool audioFromFileDevice::setHWGain(double gain)

bool audioFromFileDevice::open(yarp::os::Searchable &config)
{
if (config.check("help"))
{
yCInfo(AUDIOFROMFILE, "Some examples:");
yCInfo(AUDIOFROMFILE, "yarpdev --device audioFromFileDevice --help");
yCInfo(AUDIOFROMFILE, "yarpdev --device AudioRecorderWrapper --subdevice audioFromFileDevice --start");
yCInfo(AUDIOFROMFILE, "yarpdev --device AudioRecorderWrapper --subdevice audioFromFileDevice --start --file_name myaudio.wav");
return false;
}

bool b = configureRecorderAudioDevice(config, "audioFromFileDevice");
if (!b) { return false; }

Expand All @@ -64,10 +73,10 @@ bool audioFromFileDevice::open(yarp::os::Searchable &config)
yCInfo(AUDIOFROMFILE) << "Using default period of " << c_DEFAULT_PERIOD << " s";
}

//sets the number of samples period
if (config.check("samples"))
//sets the number of samples processed atomically every thread iteration
if (config.check("driver_frame_size"))
{
m_samples_to_be_copied = config.find("samples").asFloat64();
m_samples_to_be_copied = config.find("driver_frame_size").asFloat64();
}
yCDebug(AUDIOFROMFILE) << m_samples_to_be_copied << " will be processed every iteration";

Expand Down
10 changes: 5 additions & 5 deletions src/devices/audioFromFileDevice/audioFromFileDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
* is used to read data from a file and stream it to the network.
*
* Parameters required by this device are:
* | Parameter name | SubParameter | Type | Units | Default Value | Required | Description | Notes |
* |:--------------:|:--------------:|:-------:|:--------------:|:------------------------:|:--------------------------: |:-----------------------------------------------------------------:|:-----:|
* | file_name | - | string | - | audio.wav | No | The name of the file opened by the module | Only .wav files supported |
* | period | - | double | s | 0.010 | No | the period of thread which processes the file | On each iteration xxx samples are processed |
* | samples | - | int | samples | 512 | No | the number of samples to process on each iteration of the thread | - |
* | Parameter name | SubParameter | Type | Units | Default Value | Required | Description | Notes |
* |:-----------------:|:--------------:|:-------:|:--------------:|:------------------------:|:--------------------------: |:-----------------------------------------------------------------:|:-----:|
* | file_name | - | string | - | audio.wav | No | The name of the file opened by the module | Only .wav files supported |
* | period | - | double | s | 0.010 | No | the period of thread which processes the file | On each iteration xxx samples are processed |
* | driver_frame_size | - | int | samples | 512 | No | the number of samples to process on each iteration of the thread | - |
*/

class audioFromFileDevice :
Expand Down
2 changes: 1 addition & 1 deletion src/devices/audioPlayerWrapper/AudioPlayerWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class AudioPlayerWrapper :

struct scheduled_sound_type
{
double scheduled_time;
double scheduled_time=0;
yarp::sig::Sound sound_data;
};

Expand Down
9 changes: 9 additions & 0 deletions src/devices/audioToFileDevice/audioToFileDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ audioToFileDevice::~audioToFileDevice()

bool audioToFileDevice::open(yarp::os::Searchable &config)
{
if (config.check("help"))
{
yCInfo(AUDIOTOFILE, "Some examples:");
yCInfo(AUDIOTOFILE, "yarpdev --device audioToFileDevice --help");
yCInfo(AUDIOTOFILE, "yarpdev --device AudioPlayerWrapper --subdevice audioToFileDevice --start");
yCInfo(AUDIOTOFILE, "yarpdev --device AudioPlayerWrapper --subdevice audioToFileDevice --start --audio_out.wav --save_mode overwrite_file");
return false;
}

if (config.check("file_name"))
{
m_audio_filename=config.find("file_name").asString();
Expand Down
1 change: 0 additions & 1 deletion src/devices/audioToFileDevice/audioToFileDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class audioToFileDevice :
yarp::sig::Sound m_audioFile;
std::string m_audio_filename = "audio_out.wav";
std::deque<yarp::sig::Sound> m_sounds;
double m_hw_gain = 1.0;
size_t m_filename_counter = 0;

enum save_mode_t
Expand Down
63 changes: 44 additions & 19 deletions src/devices/fakeMicrophone/fakeMicrophone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ using namespace yarp::os;
using namespace yarp::dev;
using namespace yarp::sig;

constexpr size_t c_SAMPLES_TO_BE_COPIED=512; //samples
constexpr double c_DEFAULT_PERIOD=0.01; //s

namespace {
YARP_LOG_COMPONENT(FAKEMICROPHONE, "yarp.device.fakeMicrophone")
Expand All @@ -34,7 +34,7 @@ YARP_LOG_COMPONENT(FAKEMICROPHONE, "yarp.device.fakeMicrophone")
typedef unsigned short int audio_sample_16t;

fakeMicrophone::fakeMicrophone() :
PeriodicThread(DEFAULT_PERIOD)
PeriodicThread(c_DEFAULT_PERIOD)
{
}

Expand All @@ -45,6 +45,18 @@ fakeMicrophone::~fakeMicrophone()

bool fakeMicrophone::open(yarp::os::Searchable &config)
{
if (config.check("help"))
{
yCInfo(FAKEMICROPHONE, "Some examples:");
yCInfo(FAKEMICROPHONE, "yarpdev --device fakeMicrophone --help");
yCInfo(FAKEMICROPHONE, "yarpdev --device AudioRecorderWrapper --subdevice fakeMicrophone --start");
yCInfo(FAKEMICROPHONE, "yarpdev --device AudioRecorderWrapper --subdevice fakeMicrophone --start --signal_frequency 400 --waveform sine");
yCInfo(FAKEMICROPHONE, "yarpdev --device AudioRecorderWrapper --subdevice fakeMicrophone --start --signal_frequency 400 --waveform sawtooth");
yCInfo(FAKEMICROPHONE, "yarpdev --device AudioRecorderWrapper --subdevice fakeMicrophone --start --signal_frequency 400 --waveform square");
yCInfo(FAKEMICROPHONE, "yarpdev --device AudioRecorderWrapper --subdevice fakeMicrophone --start --waveform constant");
return false;
}

bool b = configureRecorderAudioDevice(config, "fakeMicrophone");
if (!b) { return false; }

Expand All @@ -57,14 +69,26 @@ bool fakeMicrophone::open(yarp::os::Searchable &config)
}
else
{
yCInfo(FAKEMICROPHONE) << "Using default period of " << DEFAULT_PERIOD << " s";
yCInfo(FAKEMICROPHONE) << "Using default period of " << c_DEFAULT_PERIOD << " s";
}

if (config.check("signal_frequency"))
{
m_sig_freq = config.find("signal_frequency").asInt32();
}

if (config.check("amplitude"))
{
m_wave_amplitude = config.find("amplitude").asInt32();
}

//sets the number of samples processed atomically every thread iteration
if (config.check("driver_frame_size"))
{
m_samples_to_be_copied = config.find("driver_frame_size").asFloat64();
}
yCDebug(FAKEMICROPHONE) << m_samples_to_be_copied << " will be processed every iteration";

if (config.check("waveform"))
{
std::string waveform = config.find("waveform").asString();
Expand All @@ -88,6 +112,7 @@ bool fakeMicrophone::open(yarp::os::Searchable &config)

m_max_count.resize(m_audiorecorder_cfg.numChannels);
m_counter.resize(m_audiorecorder_cfg.numChannels);

//start the capture thread
start();
return true;
Expand All @@ -97,19 +122,19 @@ bool fakeMicrophone::close()
{
fakeMicrophone::stop();

//wait until the thread is stopped
//wait until the thread is stopped...

if (m_inputBuffer)
{
delete m_inputBuffer;
m_inputBuffer = 0;
}
return true;
}

bool fakeMicrophone::setHWGain(double gain)
{
yCInfo(FAKEMICROPHONE) << "Not yet implemented";
std::lock_guard<std::mutex> lock(m_mutex);
if (gain > 0)
{
m_hw_gain = gain;
return true;
}
return false;
}

Expand All @@ -129,7 +154,7 @@ void fakeMicrophone::run()

//fill the buffer with a sine tone
//each iteration, which occurs every xxx ms, I copy a bunch of samples in the buffer.
for (size_t i = 0; i < c_SAMPLES_TO_BE_COPIED; i++)
for (size_t i = 0; i < m_samples_to_be_copied; i++)
{
// Default values:
// this signal has amplitude (-32000,32000)
Expand All @@ -141,19 +166,19 @@ void fakeMicrophone::run()
for (size_t i = 0; i < m_audiorecorder_cfg.numChannels; i++)
{
m_max_count[i] = double(m_audiorecorder_cfg.frequency) / m_sig_freq / double(i + 1);
unsigned short elem1 = double(m_wave_amplitude * sin(double(m_counter[i]) / m_max_count[i] * 2 * M_PI));
m_inputBuffer->write(elem1);
double elem1 = double(m_wave_amplitude * sin(double(m_counter[i]) / m_max_count[i] * 2 * M_PI));
m_inputBuffer->write(elem1 * m_hw_gain);
m_counter[i]++;
if (m_counter[i] >= m_max_count[i]) m_counter[i] = 0;
}
}
}
else if(m_waveform == waveform_t::sawtooth)
{
for (size_t i = 0; i < m_audiorecorder_cfg.numChannels; i++)
{
m_max_count[i] = double(m_audiorecorder_cfg.frequency) / m_sig_freq / double(i + 1);
unsigned short elem1 = m_wave_amplitude * 2 * (double(m_counter[i])/ m_max_count[i]) - m_wave_amplitude;
m_inputBuffer->write(elem1);
double elem1 = m_wave_amplitude * 2 * (double(m_counter[i])/ m_max_count[i]) - m_wave_amplitude;
m_inputBuffer->write(elem1 * m_hw_gain);
m_counter[i]++;
if (m_counter[i] >= m_max_count[i]) m_counter[i] = 0;
}
Expand All @@ -163,8 +188,8 @@ void fakeMicrophone::run()
for (size_t i = 0; i < m_audiorecorder_cfg.numChannels; i++)
{
m_max_count[i] = double(m_audiorecorder_cfg.frequency) / m_sig_freq / double(i + 1);
unsigned short elem1 = m_counter[i] < m_max_count[i]/2 ? m_wave_amplitude : 0;
m_inputBuffer->write(elem1);
double elem1 = m_counter[i] < m_max_count[i]/2 ? m_wave_amplitude : 0;
m_inputBuffer->write(elem1 * m_hw_gain);
m_counter[i]++;
if (m_counter[i] >= m_max_count[i]) m_counter[i] = 0;
}
Expand All @@ -173,7 +198,7 @@ void fakeMicrophone::run()
{
for (size_t i = 0; i < m_audiorecorder_cfg.numChannels; i++)
{
m_inputBuffer->write(m_wave_amplitude / double(i + 1));
m_inputBuffer->write(m_wave_amplitude * m_hw_gain / double(i + 1));
m_counter[i]++;
if (m_counter[i] >= m_max_count[i]) m_counter[i] = 0;
}
Expand Down
21 changes: 10 additions & 11 deletions src/devices/fakeMicrophone/fakeMicrophone.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@
#include <string>
#include <mutex>

#define DEFAULT_PERIOD 0.01 //s

/**
* @ingroup dev_impl_fake dev_impl_media
*
* \brief `fakeMicrophone` : fake microphone device implementing the IAudioGrabberSound interface to generate a test sound.
* It can generate various signals, i.e. sine, sawtooth, square wave, constant.
*
* Parameters required by this device are:
* | Parameter name | SubParameter | Type | Units | Default Value | Required | Description | Notes |
* |:--------------:|:--------------:|:-------:|:--------------:|:------------------------:|:--------------------------: |:-----------------------------------------------------------------:|:-----:|
* | period | - | double | s | 0.010 | No | the period of processing thread | A value of 10ms is recommended. Do to not modify it |
* | channels | - | size_t | - | 2 | No | Number of channels (e.g. 1=mono, 2-stereo etc) | - |
* | waveform | - | string | - | sine | No | Defines the shape of the waveform. Can be one of the following: sine,sawtooth,square,constant | - |
* | sampling_frequency | - | int | Hz | 44100 | No | Sampling frequency | - |
* | signal_frequency | - | int | Hz | 440 | No | Frequency of the generated signal | - |
* Parameters used by this device are:
* | Parameter name | SubParameter | Type | Units | Default Value | Required | Description | Notes |
* |:-----------------:|:--------------:|:-------:|:--------------:|:------------------------:|:--------------------------: |:-----------------------------------------------------------------:|:-----:|
* | period | - | double | s | 0.010 | No | the period of processing thread | A value of 10ms is recommended. Do to not modify it |
* | waveform | - | string | - | sine | No | Defines the shape of the waveform. Can be one of the following: sine,sawtooth,square,constant | - |
* | signal_frequency | - | int | Hz | 440 | No | Frequency of the generated signal | - |
* | signal_amplitude | - | int | | 32000 | No | Amplitude of the generated signal | - |
* | driver_frame_size | - | int | samples | 512 | No | the number of samples to process on each iteration of the thread | - |
*/

class fakeMicrophone :
public yarp::dev::DeviceDriver,
public yarp::dev::AudioRecorderDeviceBase,
Expand Down Expand Up @@ -64,6 +62,7 @@ class fakeMicrophone :
std::vector<size_t> m_max_count;
size_t m_wave_amplitude = 32000;
double m_sig_freq=440; //Hz
size_t m_samples_to_be_copied = 512;

enum waveform_t
{
Expand Down
25 changes: 15 additions & 10 deletions src/devices/fakeSpeaker/fakeSpeaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ using namespace yarp::os;
using namespace yarp::dev;
using namespace yarp::sig;

#define HW_CHANNELS 2
#define SAMPLING_RATE 44100
constexpr double c_DEFAULT_PERIOD = 0.01; //s

namespace {
YARP_LOG_COMPONENT(FAKESPEAKER, "yarp.device.fakeSpeaker")
Expand All @@ -30,7 +29,7 @@ YARP_LOG_COMPONENT(FAKESPEAKER, "yarp.device.fakeSpeaker")
typedef unsigned short int audio_sample_16t;

fakeSpeaker::fakeSpeaker() :
PeriodicThread(DEFAULT_PERIOD)
PeriodicThread(c_DEFAULT_PERIOD)
{
}

Expand All @@ -41,6 +40,14 @@ fakeSpeaker::~fakeSpeaker()

bool fakeSpeaker::open(yarp::os::Searchable &config)
{
if (config.check("help"))
{
yCInfo(FAKESPEAKER, "Some examples:");
yCInfo(FAKESPEAKER, "yarpdev --device fakeSpeaker --help");
yCInfo(FAKESPEAKER, "yarpdev --device AudioPlayerWrapper --subdevice fakeSpeaker --start");
return false;
}

bool b = configurePlayerAudioDevice(config, "fakeSpeaker");
if (!b) { return false; }

Expand All @@ -53,7 +60,7 @@ bool fakeSpeaker::open(yarp::os::Searchable &config)
}
else
{
yCInfo(FAKESPEAKER) << "Using default period of " << DEFAULT_PERIOD << " s";
yCInfo(FAKESPEAKER) << "Using default period of " << c_DEFAULT_PERIOD << " s";
}

//start the capture thread
Expand All @@ -64,11 +71,9 @@ bool fakeSpeaker::open(yarp::os::Searchable &config)
bool fakeSpeaker::close()
{
fakeSpeaker::stop();
if (m_outputBuffer)
{
delete m_outputBuffer;
m_outputBuffer = 0;
}

//wait until the thread is stopped...

return true;
}

Expand All @@ -94,7 +99,7 @@ void fakeSpeaker::run()
for (size_t i = 0; i<buffer_size; i++)
{
audio_sample_16t s = m_outputBuffer->read();
s=s*m_hw_gain;
s= audio_sample_16t(double(s)*m_hw_gain);
}
yCDebug(FAKESPEAKER) << "Sound Playback complete";
yCDebug(FAKESPEAKER) << "Played " << siz_sam << " samples, " << siz_chn << " channels, " << siz_byt << " bytes";
Expand Down
22 changes: 10 additions & 12 deletions src/devices/fakeSpeaker/fakeSpeaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
#include <yarp/sig/SoundFile.h>
#include <yarp/dev/AudioPlayerDeviceBase.h>


#define DEFAULT_PERIOD 0.01 //s

/**
* @ingroup dev_impl_fake dev_impl_media
*
* \brief `fakeSpeaker` : fake device implementing the IAudioRender device interface to play sound
*
* Documentation to be added
*/
* @ingroup dev_impl_fake dev_impl_media
*
* \brief `fakeSpeaker` : fake device implementing the IAudioRender device interface to play sound
*
* Parameters used by this device are:
* | Parameter name | SubParameter | Type | Units | Default Value | Required | Description | Notes |
* |:----------------:|:--------------:|:-------:|:--------------:|:------------------------:|:--------------------------: |:-----------------------------------------------------------------:|:-----:|
* | period | - | double | s | 0.010 | No | the period of processing thread | A value of 10ms is recommended. Do to not modify it |
*/

class fakeSpeaker :
public yarp::dev::DeviceDriver,
public yarp::dev::AudioPlayerDeviceBase,
Expand All @@ -37,7 +38,6 @@ class fakeSpeaker :
fakeSpeaker(fakeSpeaker&&) = delete;
fakeSpeaker& operator=(const fakeSpeaker&) = delete;
fakeSpeaker& operator=(fakeSpeaker&&) = delete;

~fakeSpeaker() override;

// Device Driver interface
Expand All @@ -53,6 +53,4 @@ class fakeSpeaker :
private:
bool threadInit() override;
void run() override;

double m_hw_gain = 1.0;
};
Loading

0 comments on commit 999dfcd

Please # to comment.