27 m_audio_filename(
"audio_out.wav")
38 if (config.
check(
"help"))
40 yCInfo(AUDIOTOFILE,
"Some examples:");
41 yCInfo(AUDIOTOFILE,
"yarpdev --device audioToFileDevice --help");
42 yCInfo(AUDIOTOFILE,
"yarpdev --device AudioPlayerWrapper --subdevice audioToFileDevice --start");
43 yCInfo(AUDIOTOFILE,
"yarpdev --device AudioPlayerWrapper --subdevice audioToFileDevice --start --audio_out.wav --save_mode overwrite_file");
48 if (!b) {
return false; }
50 if (config.
check(
"file_name"))
53 yCInfo(AUDIOTOFILE) <<
"Audio will be saved on exit to file:" << m_audio_filename;
58 yCInfo(AUDIOTOFILE) <<
"No `file_name` option specified. Audio will be saved on exit to default file:" << m_audio_filename;
61 if (config.
find(
"save_mode").
toString() ==
"overwrite_file") { m_save_mode = save_mode_t::save_overwrite_file;}
62 else if (config.
find(
"save_mode").
toString() ==
"append_data") { m_save_mode = save_mode_t::save_append_data; }
63 else if (config.
find(
"save_mode").
toString() ==
"rename_file") { m_save_mode = save_mode_t::save_rename_file; }
64 else if (config.
check(
"save_mode")) {
yError() <<
"Unsupported value for save_mode parameter";
return false;}
66 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"; }
67 else if (m_save_mode == save_mode_t::save_append_data) {
yCInfo(AUDIOTOFILE) <<
"append_data mode selected. File will be saved on exit only"; }
68 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"; }
73 void audioToFileDevice::save_to_file()
76 if (m_sounds.size() == 0) {
82 m_audioFile.
setFrequency(m_sounds.front().getFrequency());
83 m_audioFile.
resize(0, m_sounds.front().getChannels());
84 while (!m_sounds.empty())
86 m_audioFile += m_sounds.front();
91 size_t lastindex = m_audio_filename.find_last_of(
".");
92 std::string trunc_filename = m_audio_filename.substr(0, lastindex);
93 std::string trunc_extension =
".wav";
94 if (lastindex!= std::string::npos)
96 trunc_extension = m_audio_filename.substr(lastindex, std::string::npos);
99 if (m_save_mode == save_mode_t::save_rename_file)
101 trunc_filename = trunc_filename +
std::to_string(m_filename_counter++);
104 std::string complete_filename = trunc_filename + trunc_extension;
108 yCDebug(AUDIOTOFILE) <<
"Wrote audio to:" << complete_filename;
120 std::lock_guard<std::recursive_mutex> lock(
m_mutex);
121 yCDebug(AUDIOTOFILE) <<
"start";
123 if (m_save_mode != save_mode_t::save_append_data)
132 std::lock_guard<std::recursive_mutex> lock(
m_mutex);
133 yCDebug(AUDIOTOFILE) <<
"stop";
135 if (m_save_mode != save_mode_t::save_append_data)
144 std::lock_guard<std::recursive_mutex> lock(
m_mutex);
147 m_sounds.push_back(sound);
154 std::lock_guard<std::recursive_mutex> lock(
m_mutex);
165 yCError(AUDIOTOFILE,
"configureDeviceAndStart() Not yet implemented");
171 yCError(AUDIOTOFILE,
"interruptDeviceAndClose() Not yet implemented");
177 yCError(AUDIOTOFILE,
"waitUntilPlaybackStreamIsComplete() Not yet implemented");
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
virtual bool renderSound(const yarp::sig::Sound &sound) override
Render a sound using a device (i.e.
virtual bool configureDeviceAndStart() override
virtual bool interruptDeviceAndClose() override
bool close() override
Close the DeviceDriver.
virtual void waitUntilPlaybackStreamIsComplete() override
virtual bool setHWGain(double gain) override
Sets the hardware gain of the playback device (if supported by the hardware)
virtual bool startPlayback() override
Start the playback.
virtual bool stopPlayback() override
Stop the playback.
~audioToFileDevice() override
std::recursive_mutex m_mutex
bool configurePlayerAudioDevice(yarp::os::Searchable &config, std::string device_name)
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.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
std::string toString() const override
Return a standard text representation of the content of the object.
virtual std::string asString() const
Get string value.
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.
void resize(size_t samples, size_t channels=1)
Set the sound size.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
std::string to_string(IntegerType x)
An interface for the device drivers.
An interface to the operating system, including Port based communication.
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)