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
16namespace yarp::sig {
17
25{
26public:
27 typedef short int audio_sample;
28
29 Sound(size_t bytesPerSample = 2);
30
36 Sound(const Sound& alt);
37
38 virtual ~Sound();
39
45 const Sound& operator=(const Sound& alt);
46
52 bool operator==(const Sound& alt) const;
53
59 Sound& operator+=(const Sound& alt);
60
66 Sound subSound(size_t first_sample, size_t last_sample);
67
73 void resize(size_t samples, size_t channels = 1);
74
75 audio_sample get(size_t sample, size_t channel = 0) const;
76
77 void set(audio_sample value, size_t sample, size_t channel = 0);
78
79 audio_sample getSafe(size_t sample, size_t channel = 0) const {
80 if (isSample(sample,channel)) {
81 return get(sample,channel);
82 }
83 return 0;
84 }
85
86 void setSafe(audio_sample value, size_t sample, size_t channel = 0);
87
94 bool isSample(size_t sample, size_t channel = 0) const;
95
99 void clear();
100
105 void normalizeChannel(size_t channel);
106
110 void normalize();
111
117 void amplifyChannel(size_t channel, double gain);
118
123 void amplify(double gain);
124
131 void findPeakInChannel(size_t channelId, size_t& sampleId, audio_sample& sampleValue) const;
132
139 void findPeak(size_t& channelId, size_t& sampleId, audio_sample& sampleValue) const;
140
146 inline double sample2timestamp( size_t sampleid) const {return static_cast<double>(sampleid / m_frequency);}
147
153 inline size_t timestamp2sample( double time) const { return static_cast<size_t>(time * m_frequency);}
154
160 bool clearChannel(size_t channel);
161
166 int getFrequency() const;
167
171 void setFrequency(int freq);
172
177 size_t getBytesPerSample() const;
178
183 size_t getSamples() const;
184
189 double getDuration() const;
190
195 size_t getChannels() const;
196
201 Sound extractChannelAsSound(size_t channel_id) const;
202
203 std::vector<std::reference_wrapper<audio_sample>> getChannel(size_t channel_id);
204
211 bool replaceChannel(size_t id, Sound channel);
212
219 std::vector<std::reference_wrapper<audio_sample>> getInterleavedAudioRawData() const;
220
227 std::vector<std::reference_wrapper<audio_sample>> getNonInterleavedAudioRawData() const;
228
234 std::string toString() const;
235
236private:
242 unsigned char *getRawData() const;
243
248 size_t getRawDataSize() const;
249
250public:
251 bool read(yarp::os::ConnectionReader& connection) override;
252
253 bool write(yarp::os::ConnectionWriter& connection) const override;
254
255private:
256 void init(size_t bytesPerSample);
257 void synchronize();
258
259 void *implementation;
260 size_t m_samples;
261 size_t m_channels;
262 size_t m_bytesPerSample;
263 int m_frequency;
264};
265
266} // namespace yarp::sig
267
268#endif // YARP_SIG_SOUND_H
bool operator==(const struct v4l2_control &left, const struct v4l2_control &right)
Definition: CApiMock.h:65
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:25
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition: Sound.h:25
double sample2timestamp(size_t sampleid) const
Utility function: return the timestamp (in seconds) given the sample id.
Definition: Sound.h:146
short int audio_sample
Definition: Sound.h:27
size_t timestamp2sample(double time) const
Utility function: return the sample id given the timestamp (in seconds)
Definition: Sound.h:153
audio_sample getSafe(size_t sample, size_t channel=0) const
Definition: Sound.h:79
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:915
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:1091
#define YARP_sig_API
Definition: api.h:18