YARP
Yet Another Robot Platform
audioToFileDevice.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include "audioToFileDevice.h"
10 
11 #include <yarp/os/Thread.h>
12 #include <yarp/os/Time.h>
13 #include <yarp/os/Stamp.h>
14 #include <yarp/os/LogComponent.h>
15 #include <yarp/os/LogStream.h>
16 
17 #include <mutex>
18 #include <string>
19 
20 
21 using namespace yarp::os;
22 using namespace yarp::dev;
23 using namespace yarp::sig;
24 
25 namespace {
26 YARP_LOG_COMPONENT(AUDIOTOFILE, "yarp.device.audioToFileDevice")
27 }
28 
29 typedef unsigned short int audio_sample_16t;
30 
32  m_audio_filename("audio_out")
33 {
34 }
35 
37 {
38  close();
39 }
40 
42 {
43  if (config.check("file_name"))
44  {
45  m_audio_filename=config.find("file_name").asString();
46  yCInfo(AUDIOTOFILE) << "Audio will be saved on exit to file:" << m_audio_filename;
47  return true;
48  }
49  else
50  {
51  yCInfo(AUDIOTOFILE) << "No `file_name` option specified. Audio will be saved on exit to default file:" << m_audio_filename;
52  }
53 
54  if (config.find("save_mode").toString() == "overwrite_file") { m_save_mode = save_mode_t::save_overwrite_file;}
55  else if (config.find("save_mode").toString() == "append_data") { m_save_mode = save_mode_t::save_append_data; }
56  else if (config.find("save_mode").toString() == "rename_file") { m_save_mode = save_mode_t::save_rename_file; }
57  else if (config.check("save_mode")) {yError() << "Unsupported value for save_mode parameter"; return false;}
58 
59  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"; }
60  else if (m_save_mode == save_mode_t::save_append_data) { yCInfo(AUDIOTOFILE) << "append_data mode selected. File will be saved on exit only"; }
61  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"; }
62 
63  return true;
64 }
65 
66 void audioToFileDevice::save_to_file()
67 {
68  //of the buffer is empty, there is nothing to save
69  if (m_sounds.size() == 0) return;
70 
71  //we need to set the number of channels and the frequency before calling the
72  //concatenation operator
73  m_audioFile.setFrequency(m_sounds.front().getFrequency());
74  m_audioFile.resize(0, m_sounds.front().getChannels());
75  while (!m_sounds.empty())
76  {
77  m_audioFile += m_sounds.front();
78  m_sounds.pop_front();
79  }
80 
81  //remove the extension .wav from the filename
82  size_t lastindex = m_audio_filename.find(".wav");
83  std::string rawname = m_audio_filename.substr(0, lastindex);
84 
85  if (m_save_mode == save_mode_t::save_rename_file)
86  {
87  rawname = rawname +std::to_string(m_filename_counter++);
88  }
89  rawname = rawname +".wav";
90  bool ok = yarp::sig::file::write(m_audioFile, rawname.c_str());
91  if (ok)
92  {
93  yCDebug(AUDIOTOFILE) << "Wrote audio to:" << rawname;
94  }
95 }
96 
98 {
99  save_to_file();
100  return true;
101 }
102 
104 {
105  //no lock guard is needed here
106  //size = 0;
107  return true;
108 }
109 
111 {
112  //no lock guard is needed here
113  //size = 0;
114  return true;
115 }
116 
118 {
119  std::lock_guard<std::mutex> lock(m_mutex);
120  m_sounds.clear();
121  return true;
122 }
123 
125 {
126  std::lock_guard<std::mutex> lock(m_mutex);
127  yCDebug(AUDIOTOFILE) << "start";
128  m_playback_running = true;
129  if (m_save_mode != save_mode_t::save_append_data)
130  {
131  m_sounds.clear();
132  }
133  return true;
134 }
135 
137 {
138  std::lock_guard<std::mutex> lock(m_mutex);
139  yCDebug(AUDIOTOFILE) << "stop";
140  m_playback_running = false;
141  if (m_save_mode != save_mode_t::save_append_data)
142  {
143  save_to_file();
144  }
145  return true;
146 }
147 
149 {
150  std::lock_guard<std::mutex> lock(m_mutex);
151  if (m_playback_running)
152  {
153  m_sounds.push_back(sound);
154  }
155  return true;
156 }
LogStream.h
yarp::sig::Sound::setFrequency
void setFrequency(int freq)
Set the frequency of the sound (i.e.
Definition: Sound.cpp:226
audioToFileDevice::startPlayback
virtual bool startPlayback() override
Start the playback.
Definition: audioToFileDevice.cpp:124
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
yarp::sig
Signal processing.
Definition: Image.h:25
audioToFileDevice::resetPlaybackAudioBuffer
virtual bool resetPlaybackAudioBuffer() override
Definition: audioToFileDevice.cpp:117
yarp::dev::AudioBufferSize
Definition: AudioBufferSize.h:26
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
yError
#define yError(...)
Definition: Log.h:282
audioToFileDevice::getPlaybackAudioBufferMaxSize
virtual bool getPlaybackAudioBufferMaxSize(yarp::dev::AudioBufferSize &size) override
Definition: audioToFileDevice.cpp:110
yarp::dev
An interface for the device drivers.
Definition: audioBufferSizeData.cpp:17
audioToFileDevice::getPlaybackAudioBufferCurrentSize
virtual bool getPlaybackAudioBufferCurrentSize(yarp::dev::AudioBufferSize &size) override
Definition: audioToFileDevice.cpp:103
audioToFileDevice::close
bool close() override
Close the DeviceDriver.
Definition: audioToFileDevice.cpp:97
audioToFileDevice::stopPlayback
virtual bool stopPlayback() override
Stop the playback.
Definition: audioToFileDevice.cpp:136
Stamp.h
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
audioToFileDevice.h
audioToFileDevice::renderSound
virtual bool renderSound(const yarp::sig::Sound &sound) override
Render a sound using a device (i.e.
Definition: audioToFileDevice.cpp:148
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
Thread.h
yarp::os::Searchable::find
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
audioToFileDevice::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: audioToFileDevice.cpp:41
LogComponent.h
audio_sample_16t
unsigned short int audio_sample_16t
Definition: audioToFileDevice.cpp:29
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yCDebug
#define yCDebug(component,...)
Definition: LogComponent.h:112
yarp::sig::Sound
Class for storing sounds.
Definition: Sound.h:28
yarp::sig::Sound::resize
void resize(size_t samples, size_t channels=1)
Set the sound size.
Definition: Sound.cpp:167
Time.h
audioToFileDevice::audioToFileDevice
audioToFileDevice()
Definition: audioToFileDevice.cpp:31
yarp::os::Value::toString
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Value.cpp:359
yarp::sig::file::write
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:971
audioToFileDevice::~audioToFileDevice
~audioToFileDevice() override
Definition: audioToFileDevice.cpp:36