Skip to content

Commit

Permalink
Merge pull request robotology#3060 from randaz81/IDeviceDriverParams
Browse files Browse the repository at this point in the history
IDeviceDriverParams
  • Loading branch information
randaz81 authored Dec 12, 2023
2 parents 5da2e15 + cd9519d commit 6e5676b
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 62 deletions.
2 changes: 2 additions & 0 deletions src/devices/audioToFileDevice/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ if(NOT SKIP_audioToFileDevice)
PRIVATE
audioToFileDevice.cpp
audioToFileDevice.h
audioToFileDeviceParams.cpp
audioToFileDeviceParams.h
)

target_link_libraries(yarp_audioToFileDevice
Expand Down
46 changes: 10 additions & 36 deletions src/devices/audioToFileDevice/audioToFileDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ namespace {
YARP_LOG_COMPONENT(AUDIOTOFILE, "yarp.device.audioToFileDevice")
}

audioToFileDevice::audioToFileDevice() :
m_audio_filename("audio_out.wav")
audioToFileDevice::audioToFileDevice()
{
}

Expand All @@ -35,43 +34,18 @@ 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 --file_name audio_out.wav --save_mode overwrite_file");
yCInfo(AUDIOTOFILE, "save_mode can be overwrite_file, append_file, rename_file, break_file");
yCInfo(AUDIOTOFILE, "use --add_marker option to add a marker at the beginning and at the ending of each received waveform");
return false;
}
bool b = false;
b = parseParams(config);
if (!b) {return false;}

bool b = configurePlayerAudioDevice(config.findGroup("AUDIO_BASE"), "audioToFileDevice");
b = configurePlayerAudioDevice(config.findGroup("AUDIO_BASE"), "audioToFileDevice");
if (!b) { return false; }

if (config.check("file_name"))
{
m_audio_filename=config.find("file_name").asString();
yCInfo(AUDIOTOFILE) << "Audio will be saved on exit to file:" << m_audio_filename;
}
else
{
yCInfo(AUDIOTOFILE) << "No `file_name` option specified. Audio will be saved on exit to default file:" << m_audio_filename;
}

if (config.check("add_marker")) {m_add_marker=true; yCInfo(AUDIOTOFILE) << "Audio marker option enabled";}

if (config.find("save_mode").toString() == "overwrite_file") { m_save_mode = save_mode_t::save_overwrite_file;}
else if (config.find("save_mode").toString() == "append_data") { m_save_mode = save_mode_t::save_append_data; }
else if (config.find("save_mode").toString() == "rename_file") { m_save_mode = save_mode_t::save_rename_file; }
else if (config.find("save_mode").toString() == "break_file") { m_save_mode = save_mode_t::save_break_file; }
else if (config.check("save_mode")) { yError() << "Unsupported value for save_mode parameter"; return false; }

if (m_save_mode == save_mode_t::save_overwrite_file) { yCInfo(AUDIOTOFILE) << "overwrite_file mode selected. File will be saved both on exit and on stop"; }
else if (m_save_mode == save_mode_t::save_append_data) { yCInfo(AUDIOTOFILE) << "append_data mode selected. File will be saved on exit only"; }
else if (m_save_mode == save_mode_t::save_rename_file) { yCInfo(AUDIOTOFILE) << "rename_file mode selected. File will be saved both on exit and on stop"; }
else if (m_save_mode == save_mode_t::save_break_file) { yCInfo(AUDIOTOFILE) << "break_file mode selected."; }
else { return false; }
if (m_save_mode_s == "overwrite_file") { m_save_mode = save_mode_t::save_overwrite_file; yCInfo(AUDIOTOFILE) << "overwrite_file mode selected. File will be saved both on exit and on stop";}
else if (m_save_mode_s == "append_data") { m_save_mode = save_mode_t::save_append_data; yCInfo(AUDIOTOFILE) << "append_data mode selected. File will be saved on exit only";}
else if (m_save_mode_s == "rename_file") { m_save_mode = save_mode_t::save_rename_file; yCInfo(AUDIOTOFILE) << "rename_file mode selected. File will be saved both on exit and on stop";}
else if (m_save_mode_s == "break_file") { m_save_mode = save_mode_t::save_break_file; yCInfo(AUDIOTOFILE) << "break_file mode selected.";}
else { yError() << "Unsupported value for save_mode parameter"; return false; }

return true;
}
Expand Down
34 changes: 8 additions & 26 deletions src/devices/audioToFileDevice/audioToFileDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <yarp/sig/Sound.h>
#include <yarp/sig/SoundFile.h>
#include <yarp/dev/AudioPlayerDeviceBase.h>
#include "audioToFileDeviceParams.h"

#include <string>
#include <mutex>
Expand All @@ -17,37 +18,20 @@
* @ingroup dev_impl_media
*
* \brief `audioToFileDevice` : This device driver, wrapped by default by AudioPlayerWrapper,
* is used to save to a file an audio stream.
* is used to save to an audio stream to a file on disk.
*
* Three different operating modes are available, defined by the optional string parameter `save_mode`:
* if save_mode == "append_data", the file is written only when the module terminates.
* Every start/stop operation just pauses the module. On resume, the new data is concatenated at the end of the file.
* See audioToFileDeviceParams for the documentation of the accepted parameters.
*
* if save_mode == "overwrite_file", the output file is written every time the stop() method is called or when the module terminates.
* If the file already exists, it will be overwritten with the new data.
*
* if save_mode = "rename_file", the output file is written to a NEW file every time the stop() method is called or when the module terminates.
* The file name is modified, using an incremental counter appended at the end of the file name.
*
* if save_mode = "break_file", the output file is written to a NEW file every time a yarp::sig::sound is received or when the module terminates.
* The file name is modified, using an incremental counter appended at the end of the file name.
*
* This device driver derives from AudioPlayerDeviceBase base class. Please check its documentation for additional details.
*
* Parameters required by this device are:
* | Parameter name | SubParameter | Type | Units | Default Value | Required | Description | Notes |
* |:--------------:|:--------------:|:-------:|:--------------:|:------------------------:|:--------------------------: |:-----------------------------------------------------------------:|:-----:|
* | AUDIO_BASE | *** | | - | - | No | For the documentation of AUDIO_BASE group, please refer to the documentation of the base class AudioPlayerDeviceBase | |
* | file_name | - | string | - | audio_out.wav | No | The name of the file written by the module | Only .wav and .mp3 files are supported |
* | save_mode | - | string | - | overwrite_file | No | Affects the behavior of the module and defines the save mode, as described in the documentation. | |
* | add_marker | - | bool | - | - | No | If set, it will add a marker at the beginning and at the ending of each received waveform. | |
* This device driver derives from yarp::dev::AudioPlayerDeviceBase base class.
* Please check its documentation for additional configuration parameters.
*
* See \ref AudioDoc for additional documentation on YARP audio.
*/

class audioToFileDevice :
public yarp::dev::DeviceDriver,
public yarp::dev::AudioPlayerDeviceBase
public yarp::dev::AudioPlayerDeviceBase,
public audioToFileDevice_params
{
public:
audioToFileDevice();
Expand All @@ -72,10 +56,8 @@ class audioToFileDevice :

private:
yarp::sig::Sound m_audioFile;
std::string m_audio_filename = "audio_out.wav";
std::deque<yarp::sig::Sound> m_sounds;
size_t m_filename_counter = 0;
bool m_add_marker=false;
size_t m_filename_counter = 0;

enum save_mode_t
{
Expand Down
91 changes: 91 additions & 0 deletions src/devices/audioToFileDevice/audioToFileDeviceParams.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: LGPL-2.1-or-later
*/

#include "audioToFileDeviceParams.h"
#include <yarp/os/LogStream.h>
#include <yarp/os/Value.h>

namespace {
YARP_LOG_COMPONENT(AUDIOTOFILE, "yarp.device.audioToFileDevice")
}

bool audioToFileDevice_params::parseParams(const 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 --file_name audio_out.wav --save_mode overwrite_file");
yCInfo(AUDIOTOFILE, "save_mode can be overwrite_file, append_file, rename_file, break_file");
yCInfo(AUDIOTOFILE, "use --add_marker option to add a marker at the beginning and at the ending of each received waveform");
return false;
}

if (config.check("file_name"))
{
m_audio_filename = config.find("file_name").asString();
yCInfo(AUDIOTOFILE) << "Audio will be saved on exit to file:" << m_audio_filename;
}
else
{
yCInfo(AUDIOTOFILE) << "No `file_name` option specified. Audio will be saved on exit to default file:" << m_audio_filename;
}

if (config.check("add_marker"))
{
m_add_marker = true;
yCInfo(AUDIOTOFILE) << "`add_marker` option enabled";
}
else
{
yCInfo(AUDIOTOFILE) << "No `add_marker` option specified. Default is:" << m_add_marker;
}

if (config.check("save_mode"))
{
m_save_mode_s = config.find("save_mode").toString();
yCInfo(AUDIOTOFILE) << "`save_mode` selected:" << m_save_mode_s;
}
else
{
yCInfo(AUDIOTOFILE) << "No `save_mode` option specified. Default is:" << m_audio_filename;
}
return true;
}

std::string audioToFileDevice_params::getDocumentationOfDeviceParams() const
{
return std::string ("\
Parameters required by this device are :\n\
| Parameter name | SubParameter | Type | Units | Default Value | Required | Description | Notes |\n\
| : ---------- : | : -------- : | : ----- : | : ------: | : ---------- : | : ---- : | : ---------------------------------------------------------------- - : | : ---- - : |\n\
| file_name | - | string | - | audio_out.wav | No | The name of the file written by the module | Only.wav and .mp3 files are supported |\n\
| save_mode | - | string | - | overwrite_file | No | Affects the behavior of the module and defines the save mode, as described in the documentation. | |\n\
| add_marker | - | bool | - | - | No | If set, it will add a marker at the beginning and at the ending of each received waveform. | |\n\
\n\
Three different operating modes are available, defined by the optional string parameter `save_mode`:\n\
if save_mode == append_data, the file is written only when the module terminates.\n\
Every start / stop operation just pauses the module.On resume, the new data is concatenated at the end of the file.\n\
\n\
If save_mode == overwrite_file, the output file is written every time the stop() method is called or when the module terminates.\n\
If the file already exists, it will be overwritten with the new data.\n\
\n\
If save_mode == rename_file, the output file is written to a NEW file every time the stop() method is called or when the module terminates.\n\
The file name is modified, using an incremental counter appended at the end of the file name.\n\
\n\
If save_mode = break_file, the output file is written to a NEW file every time a yarp::sig::sound is received or when the module terminates.\n\
The file name is modified, using an incremental counter appended at the end of the file name.\n\
");
}

std::vector<std::string> audioToFileDevice_params::getListOfParams() const
{
std::vector<std::string> params;
params.push_back("file_name");
params.push_back("save_mode");
params.push_back("add_marker");
return params;
}
52 changes: 52 additions & 0 deletions src/devices/audioToFileDevice/audioToFileDeviceParams.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <yarp/os/Searchable.h>
#include <yarp/dev/IDeviceDriverParams.h>
#include <string>

/**
* @ingroup dev_impl_media
*
* Parameters required by this device are:
* | Parameter name | SubParameter | Type | Units | Default Value | Required | Description | Notes |
* |:--------------:|:--------------:|:-------:|:--------------:|:------------------------:|:--------------------------: |:-----------------------------------------------------------------:|:-----:|
* | file_name | - | string | - | audio_out.wav | No | The name of the file written by the module | Only .wav and .mp3 files are supported |
* | save_mode | - | string | - | overwrite_file | No | Affects the behavior of the module and defines the save mode, as described in the documentation. | |
* | add_marker | - | bool | - | - | No | If set, it will add a marker at the beginning and at the ending of each received waveform. | |
*
* Three different operating modes are available, defined by the optional string parameter `save_mode`:
* if save_mode == "append_data", the file is written only when the module terminates.
* Every start/stop operation just pauses the module. On resume, the new data is concatenated at the end of the file.
*
* if save_mode == "overwrite_file", the output file is written every time the stop() method is called or when the module terminates.
* If the file already exists, it will be overwritten with the new data.
*
* if save_mode = "rename_file", the output file is written to a NEW file every time the stop() method is called or when the module terminates.
* The file name is modified, using an incremental counter appended at the end of the file name.
*
* if save_mode = "break_file", the output file is written to a NEW file every time a yarp::sig::sound is received or when the module terminates.
* The file name is modified, using an incremental counter appended at the end of the file name.
*
*/
class audioToFileDevice_params : public yarp::dev::IDeviceDriverParams
{
public:
~audioToFileDevice_params() override = default;

private:
std::string m_device_type = "audioToFileDevice";

public:
bool m_add_marker = false;
std::string m_audio_filename = "audio_out.wav";
std::string m_save_mode_s = "overwrite_file";

public:
bool parseParams(const yarp::os::Searchable& config) override;
std::string getDeviceType() const override { return m_device_type; }
std::string getDocumentationOfDeviceParams() const override;
std::vector<std::string> getListOfParams() const override;
};
2 changes: 2 additions & 0 deletions src/libYARP_dev/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ set(YARP_dev_HDRS
yarp/dev/IControlMode.h
yarp/dev/ICurrentControl.h
yarp/dev/IDepthVisualParams.h
yarp/dev/IDeviceDriverParams.h
yarp/dev/IEncoders.h
yarp/dev/IEncodersTimed.h
yarp/dev/IFrameGrabberControls.h
Expand Down Expand Up @@ -175,6 +176,7 @@ set(YARP_dev_SRCS
yarp/dev/IBattery.cpp
yarp/dev/IChatBot.cpp
yarp/dev/IDepthVisualParams.cpp
yarp/dev/IDeviceDriverParams.cpp
yarp/dev/IFrameGrabberControls.cpp
yarp/dev/IFrameGrabberControlsDC1394.cpp
yarp/dev/IFrameGrabberImage.cpp
Expand Down
8 changes: 8 additions & 0 deletions src/libYARP_dev/src/yarp/dev/IDeviceDriverParams.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <yarp/dev/IDeviceDriverParams.h>

yarp::dev::IDeviceDriverParams::~IDeviceDriverParams() = default;
55 changes: 55 additions & 0 deletions src/libYARP_dev/src/yarp/dev/IDeviceDriverParams.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/

#ifndef YARP_DEV_IDEVICEDRIVERPARAMETERS_H
#define YARP_DEV_IDEVICEDRIVERPARAMETERS_H

#include <yarp/dev/api.h>
#include <yarp/os/Searchable.h>
#include <string>

#include <vector>

namespace yarp::dev {
class IDeviceDriverParams;
}

/**
* \ingroup dev_class
*
* An interface for the management of the parameters of a DeviceDriver.
* Methods should be reimplemented independently by each DeviceDriver.
*/
class YARP_dev_API yarp::dev::IDeviceDriverParams
{
public:
virtual ~IDeviceDriverParams();

/**
* Parse the DeviceDriver parameters
* @return true if the parsing is successful, false otherwise
*/
virtual bool parseParams(const yarp::os::Searchable& config) = 0;

/**
* Get the name of the DeviceDriver class.
* @return A string containing the name of the DeviceDriver.
*/
virtual std::string getDeviceType() const = 0;

/**
* Get the documentation of the DeviceDriver's parameters.
* @return A string containing the documentation.
*/
virtual std::string getDocumentationOfDeviceParams() const = 0;

/**
* Return a list of all params used by the device.
* @return A vector containing the names of parameters used by the device.
*/
virtual std::vector<std::string> getListOfParams() const = 0;
};

#endif //YARP_DEV_IDEVICEDRIVERPARAMETERS_H

0 comments on commit 6e5676b

Please sign in to comment.