YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
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
68 void overwrite (const Sound& alt, size_t offset=0, size_t len=0);
69
75 Sound subSound(size_t first_sample, size_t last_sample);
76
82 void resize(size_t samples, size_t channels = 1);
83
84 audio_sample get(size_t sample, size_t channel = 0) const;
85
86 void set(audio_sample value, size_t sample, size_t channel = 0);
87
88 audio_sample getSafe(size_t sample, size_t channel = 0) const {
89 if (isSample(sample,channel)) {
90 return get(sample,channel);
91 }
92 return 0;
93 }
94
95 void setSafe(audio_sample value, size_t sample, size_t channel = 0);
96
103 bool isSample(size_t sample, size_t channel = 0) const;
104
108 void clear();
109
114 void normalizeChannel(size_t channel);
115
119 void normalize();
120
126 void amplifyChannel(size_t channel, double gain);
127
132 void amplify(double gain);
133
140 void findPeakInChannel(size_t channelId, size_t& sampleId, audio_sample& sampleValue) const;
141
148 void findPeak(size_t& channelId, size_t& sampleId, audio_sample& sampleValue) const;
149
155 inline double sample2timestamp( size_t sampleid) const {return static_cast<double>(sampleid / m_frequency);}
156
162 inline size_t timestamp2sample( double time) const { return static_cast<size_t>(time * m_frequency);}
163
169 bool clearChannel(size_t channel);
170
175 int getFrequency() const;
176
180 void setFrequency(int freq);
181
186 size_t getBytesPerSample() const;
187
192 size_t getSamples() const;
193
198 double getDuration() const;
199
204 size_t getChannels() const;
205
210 Sound extractChannelAsSound(size_t channel_id) const;
211
212 std::vector<std::reference_wrapper<audio_sample>> getChannel(size_t channel_id);
213
220 bool replaceChannel(size_t id, Sound channel);
221
228 std::vector<std::reference_wrapper<audio_sample>> getInterleavedAudioRawData() const;
229
236 std::vector<std::reference_wrapper<audio_sample>> getNonInterleavedAudioRawData() const;
237
243 std::string toString() const;
244
245private:
251 unsigned char *getRawData() const;
252
257 size_t getRawDataSize() const;
258
259 void delete_implementation();
260
261public:
262 bool read(yarp::os::ConnectionReader& connection) override;
263
264 bool write(yarp::os::ConnectionWriter& connection) const override;
265
266private:
267 void init(size_t bytesPerSample);
268
269 void *implementation =nullptr;
270 size_t m_samples;
271 size_t m_channels;
272 size_t m_bytesPerSample;
273 int m_frequency;
274};
275
276} // namespace yarp::sig
277
278#endif // YARP_SIG_SOUND_H
bool operator==(const struct v4l2_control &left, const struct v4l2_control &right)
Definition CApiMock.h:65
std::string toString(const T &value)
convert an arbitrary type to string.
RandScalar * implementation(void *t)
A mini-server for performing network communication in the background.
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:155
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:162
audio_sample getSafe(size_t sample, size_t channel=0) const
Definition Sound.h:88
Vector & operator+=(Vector &a, const double &s)
Addition operator between a scalar and a vector (defined in Math.h).
Definition math.cpp:38
#define YARP_sig_API
Definition api.h:18