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 --file_name audio_out.wav --save_mode overwrite_file");
44 yCInfo(AUDIOTOFILE,
"save_mode can be overwrite_file, append_file, rename_file, break_file");
45 yCInfo(AUDIOTOFILE,
"use --add_marker option to add a marker at the beginning and at the ending of each received waveform");
50 if (!b) {
return false; }
52 if (config.
check(
"file_name"))
55 yCInfo(AUDIOTOFILE) <<
"Audio will be saved on exit to file:" << m_audio_filename;
59 yCInfo(AUDIOTOFILE) <<
"No `file_name` option specified. Audio will be saved on exit to default file:" << m_audio_filename;
62 if (config.
check(
"add_marker")) {m_add_marker=
true;
yCInfo(AUDIOTOFILE) <<
"Audio marker option enabled";}
64 if (config.
find(
"save_mode").
toString() ==
"overwrite_file") { m_save_mode = save_mode_t::save_overwrite_file;}
65 else if (config.
find(
"save_mode").
toString() ==
"append_data") { m_save_mode = save_mode_t::save_append_data; }
66 else if (config.
find(
"save_mode").
toString() ==
"rename_file") { m_save_mode = save_mode_t::save_rename_file; }
67 else if (config.
find(
"save_mode").
toString() ==
"break_file") { m_save_mode = save_mode_t::save_break_file; }
68 else if (config.
check(
"save_mode")) {
yError() <<
"Unsupported value for save_mode parameter";
return false; }
70 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"; }
71 else if (m_save_mode == save_mode_t::save_append_data) {
yCInfo(AUDIOTOFILE) <<
"append_data mode selected. File will be saved on exit only"; }
72 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 else if (m_save_mode == save_mode_t::save_break_file) {
yCInfo(AUDIOTOFILE) <<
"break_file mode selected."; }
74 else {
return false; }
79void audioToFileDevice::save_to_file()
82 if (m_sounds.size() == 0) {
88 m_audioFile.
setFrequency(m_sounds.front().getFrequency());
89 m_audioFile.
resize(0, m_sounds.front().getChannels());
90 while (!m_sounds.empty())
98 m_audioFile += curr_sound;
105 marked_sound.
resize(ss_size + 5, ch_size);
107 for (
size_t c = 0; c < ch_size; c++)
109 for (
size_t i = 0; i < ss_size; i++)
111 marked_sound.
set(curr_sound.
get(i, c), i, c);
113 for (
size_t i = ss_size; i < ss_size + 5; i++)
115 marked_sound.
set(32000, i, c);
117 marked_sound.
set(-32000, 0, c);
120 m_audioFile += marked_sound;
122 m_sounds.pop_front();
126 size_t lastindex = m_audio_filename.find_last_of(
".");
127 std::string trunc_filename = m_audio_filename.substr(0, lastindex);
128 std::string trunc_extension =
".wav";
129 if (lastindex!= std::string::npos)
131 trunc_extension = m_audio_filename.substr(lastindex, std::string::npos);
134 if (m_save_mode == save_mode_t::save_rename_file ||
135 m_save_mode == save_mode_t::save_break_file)
137 trunc_filename = trunc_filename +
std::to_string(m_filename_counter++);
140 std::string complete_filename = trunc_filename + trunc_extension;
144 yCDebug(AUDIOTOFILE) <<
"Wrote audio to:" << complete_filename;
156 std::lock_guard<std::recursive_mutex> lock(
m_mutex);
157 yCDebug(AUDIOTOFILE) <<
"start";
159 if (m_save_mode != save_mode_t::save_append_data)
168 std::lock_guard<std::recursive_mutex> lock(
m_mutex);
169 yCDebug(AUDIOTOFILE) <<
"stop";
171 if (m_save_mode != save_mode_t::save_append_data)
180 std::lock_guard<std::recursive_mutex> lock(
m_mutex);
181 if (m_save_mode == save_break_file)
183 m_sounds.push_back(sound);
189 m_sounds.push_back(sound);
196 std::lock_guard<std::recursive_mutex> lock(
m_mutex);
207 yCError(AUDIOTOFILE,
"configureDeviceAndStart() Not yet implemented");
213 yCError(AUDIOTOFILE,
"interruptDeviceAndClose() Not yet implemented");
219 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 bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
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.
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.
audio_sample get(size_t sample, size_t channel=0) const
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 yCInfo(component,...)
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
std::string to_string(IntegerType x)
For streams capable of holding different kinds of content, check what they actually have.
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)