YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
AudioToFileDevice.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#include "AudioToFileDevice.h"
7
8#include <yarp/os/Thread.h>
9#include <yarp/os/Time.h>
10#include <yarp/os/Stamp.h>
12#include <yarp/os/LogStream.h>
13
14#include <mutex>
15#include <string>
16
17
18using namespace yarp::os;
19using namespace yarp::dev;
20using namespace yarp::sig;
21
22namespace {
23YARP_LOG_COMPONENT(AUDIOTOFILE, "yarp.device.audioToFileDevice")
24}
25
29
34
36{
37 bool b = false;
38 b = parseParams(config);
39 if (!b) {return false;}
40
41 b = configurePlayerAudioDevice(config.findGroup("AUDIO_BASE"), "audioToFileDevice");
42 if (!b) { return false; }
43
44 yCInfo(AUDIOTOFILE) << "Audio will be saved to file: " << m_file_name;
45
46 if (m_save_mode == "overwrite_file") { m_save_mode_enum = save_mode_t::save_overwrite_file; yCInfo(AUDIOTOFILE) << "overwrite_file mode selected. File will be saved both on exit and on stop";}
47 else if (m_save_mode == "append_data") { m_save_mode_enum = save_mode_t::save_append_data; yCInfo(AUDIOTOFILE) << "append_data mode selected. File will be saved on exit only";}
48 else if (m_save_mode == "rename_file") { m_save_mode_enum = save_mode_t::save_rename_file; yCInfo(AUDIOTOFILE) << "rename_file mode selected. File will be saved both on exit and on stop";}
49 else if (m_save_mode == "break_file") { m_save_mode_enum = save_mode_t::save_break_file; yCInfo(AUDIOTOFILE) << "break_file mode selected.";}
50 else { yError() << "Unsupported value for save_mode parameter"; return false; }
51
52 return true;
53}
54
55void AudioToFileDevice::save_to_file()
56{
57 //of the buffer is empty, there is nothing to save
58 if (m_sounds.size() == 0) {
59 return;
60 }
61
62 //we need to set the number of channels and the frequency before calling the
63 //concatenation operator
64 m_audioFile.setFrequency(m_sounds.front().getFrequency());
65 m_audioFile.resize(0, m_sounds.front().getChannels());
66 while (!m_sounds.empty())
67 {
68 yarp::sig::Sound curr_sound = m_sounds.front();
69 size_t ss_size = curr_sound.getSamples();
70 size_t ch_size = curr_sound.getChannels();
71
72 if (!m_add_marker)
73 {
74 m_audioFile += curr_sound;
75 }
76 else
77 {
78 //if required, create a sound with a marker at the beginning and at the end
80 marked_sound.setFrequency(curr_sound.getFrequency());
81 marked_sound.resize(ss_size + 5, ch_size);
82
83 for (size_t c = 0; c < ch_size; c++)
84 {
85 for (size_t i = 0; i < ss_size; i++)
86 {
87 marked_sound.set(curr_sound.get(i, c), i, c);
88 }
89 for (size_t i = ss_size; i < ss_size + 5; i++)
90 {
91 marked_sound.set(32000, i, c);
92 }
93 marked_sound.set(-32000, 0, c);
94 }
95
96 m_audioFile += marked_sound;
97 }
98 m_sounds.pop_front();
99 }
100
101 //remove the extension .wav from the filename
102 size_t lastindex = m_file_name.find_last_of(".");
103 std::string trunc_filename = m_file_name.substr(0, lastindex);
104 std::string trunc_extension =".wav";
105 if (lastindex!= std::string::npos)
106 {
107 trunc_extension = m_file_name.substr(lastindex, std::string::npos);
108 }
109
110 if (m_save_mode_enum == save_mode_t::save_rename_file ||
111 m_save_mode_enum == save_mode_t::save_break_file)
112 {
113 trunc_filename = trunc_filename +std::to_string(m_filename_counter++);
114 }
115
117 bool ok = yarp::sig::file::write(m_audioFile, complete_filename.c_str());
118 if (ok)
119 {
120 yCDebug(AUDIOTOFILE) << "Wrote audio to:" << complete_filename;
121 }
122}
123
125{
126 save_to_file();
127 return true;
128}
129
131{
132 std::lock_guard<std::recursive_mutex> lock(m_mutex);
133 yCDebug(AUDIOTOFILE) << "start";
134 m_playback_enabled = true;
135 if (m_save_mode_enum != save_mode_t::save_append_data)
136 {
137 m_sounds.clear();
138 }
139 return ReturnValue_ok;
140}
141
143{
144 std::lock_guard<std::recursive_mutex> lock(m_mutex);
145 yCDebug(AUDIOTOFILE) << "stop";
146 m_playback_enabled = false;
147 if (m_save_mode_enum != save_mode_t::save_append_data)
148 {
149 save_to_file();
150 }
151 return ReturnValue_ok;
152}
153
155{
156 std::lock_guard<std::recursive_mutex> lock(m_mutex);
157 if (m_save_mode_enum == save_break_file)
158 {
159 m_sounds.push_back(sound);
160 save_to_file();
161 return ReturnValue_ok;
162 }
164 {
165 m_sounds.push_back(sound);
166 }
167 return ReturnValue_ok;
168}
169
171{
172 std::lock_guard<std::recursive_mutex> lock(m_mutex);
173 if (gain > 0)
174 {
175 m_hw_gain = gain;
176 return ReturnValue_ok;
177 }
178 return ReturnValue::return_code::return_value_error_method_failed;
179}
180
182{
183 yCError(AUDIOTOFILE, "configureDeviceAndStart() Not yet implemented");
184 return true;
185}
186
188{
189 yCError(AUDIOTOFILE, "interruptDeviceAndClose() Not yet implemented");
190 return true;
191}
192
194{
195 yCError(AUDIOTOFILE, "waitUntilPlaybackStreamIsComplete() Not yet implemented");
196 return;
197}
#define yError(...)
Definition Log.h:361
#define ReturnValue_ok
Definition ReturnValue.h:77
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool close() override
Close the DeviceDriver.
virtual yarp::dev::ReturnValue setHWGain(double gain) override
Sets the hardware gain of the playback device (if supported by the hardware)
virtual yarp::dev::ReturnValue startPlayback() override
Start the playback.
virtual bool configureDeviceAndStart() override
virtual void waitUntilPlaybackStreamIsComplete() override
virtual yarp::dev::ReturnValue stopPlayback() override
Stop the playback.
virtual yarp::dev::ReturnValue renderSound(const yarp::sig::Sound &sound) override
Render a sound using a device (i.e.
virtual bool interruptDeviceAndClose() override
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool configurePlayerAudioDevice(yarp::os::Searchable &config, std::string device_name)
A mini-server for performing network communication in the background.
A base class for nested structures that can be searched.
Definition Searchable.h:31
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:25
void setFrequency(int freq)
Set the frequency of the sound (i.e.
Definition Sound.cpp:361
void resize(size_t samples, size_t channels=1)
Set the sound size.
Definition Sound.cpp:270
#define yCInfo(component,...)
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
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)