YARP
Yet Another Robot Platform
Sound.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_SIG_SOUND_H
8 #define YARP_SIG_SOUND_H
9 
10 #include <yarp/os/Portable.h>
11 #include <yarp/conf/numeric.h>
12 #include <yarp/sig/api.h>
13 #include <vector>
14 #include <string>
15 
16 namespace yarp {
17 namespace sig {
18 
26 {
27 public:
28  typedef short int audio_sample;
29 
30  Sound(size_t bytesPerSample = 2);
31 
37  Sound(const Sound& alt);
38 
39  virtual ~Sound();
40 
46  const Sound& operator=(const Sound& alt);
47 
53  bool operator==(const Sound& alt) const;
54 
60  Sound& operator+=(const Sound& alt);
61 
67  Sound subSound(size_t first_sample, size_t last_sample);
68 
74  void resize(size_t samples, size_t channels = 1);
75 
76  audio_sample get(size_t sample, size_t channel = 0) const;
77 
78  void set(audio_sample value, size_t sample, size_t channel = 0);
79 
80  audio_sample getSafe(size_t sample, size_t channel = 0) const {
81  if (isSample(sample,channel)) {
82  return get(sample,channel);
83  }
84  return 0;
85  }
86 
87  void setSafe(audio_sample value, size_t sample, size_t channel = 0);
88 
95  bool isSample(size_t sample, size_t channel = 0) const;
96 
100  void clear();
101 
106  void normalizeChannel(size_t channel);
107 
111  void normalize();
112 
118  void amplifyChannel(size_t channel, double gain);
119 
124  void amplify(double gain);
125 
132  void findPeakInChannel(size_t channelId, size_t& sampleId, audio_sample& sampleValue) const;
133 
140  void findPeak(size_t& channelId, size_t& sampleId, audio_sample& sampleValue) const;
141 
147  inline double sample2timestamp( size_t sampleid) const {return static_cast<double>(sampleid / m_frequency);}
148 
154  inline size_t timestamp2sample( double time) const { return static_cast<size_t>(time * m_frequency);}
155 
161  bool clearChannel(size_t channel);
162 
167  int getFrequency() const;
168 
172  void setFrequency(int freq);
173 
178  size_t getBytesPerSample() const;
179 
184  size_t getSamples() const;
185 
190  double getDuration() const;
191 
196  size_t getChannels() const;
197 
202  Sound extractChannelAsSound(size_t channel_id) const;
203 
204  std::vector<std::reference_wrapper<audio_sample>> getChannel(size_t channel_id);
205 
212  bool replaceChannel(size_t id, Sound channel);
213 
220  std::vector<std::reference_wrapper<audio_sample>> getInterleavedAudioRawData() const;
221 
228  std::vector<std::reference_wrapper<audio_sample>> getNonInterleavedAudioRawData() const;
229 
235  std::string toString() const;
236 
237 private:
243  unsigned char *getRawData() const;
244 
249  size_t getRawDataSize() const;
250 
251 public:
252  bool read(yarp::os::ConnectionReader& connection) override;
253 
254  bool write(yarp::os::ConnectionWriter& connection) const override;
255 
256 private:
257  void init(size_t bytesPerSample);
258  void synchronize();
259 
260  void *implementation;
261  size_t m_samples;
262  size_t m_channels;
263  size_t m_bytesPerSample;
264  int m_frequency;
265 };
266 
267 } // namespace sig
268 } // namespace yarp
269 
270 #endif // YARP_SIG_SOUND_H
int16_t * samples
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:17
An interface for reading from a network connection.
An interface for writing to a network connection.
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:26
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition: Sound.h:26
double sample2timestamp(size_t sampleid) const
Utility function: return the timestamp (in seconds) given the sample id.
Definition: Sound.h:147
short int audio_sample
Definition: Sound.h:28
size_t timestamp2sample(double time) const
Utility function: return the sample id given the timestamp (in seconds)
Definition: Sound.h:154
audio_sample getSafe(size_t sample, size_t channel=0) const
Definition: Sound.h:80
std::string toString(const T &value)
convert an arbitrary type to string.
Vector & operator+=(Vector &a, const double &s)
Addition operator between a scalar and a vector (defined in Math.h).
Definition: math.cpp:38
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:923
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1099
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_sig_API
Definition: api.h:18