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
16#include <map>
17
18namespace yarp::sig {
19
21{
22public:
23 std::string label;
24 int channel=-1;
25 size_t sample_id=0;
26
27 bool operator==(const SoundMarker& other) const
28 {
29 return label == other.label &&
30 channel == other.channel &&
31 sample_id == other.sample_id;
32 }
33};
34
42{
43public:
44 typedef short int audio_sample;
45
46 Sound(size_t bytesPerSample = 2);
47
53 Sound(const Sound& alt);
54
55 virtual ~Sound();
56
62 const Sound& operator=(const Sound& alt);
63
69 bool operator==(const Sound& alt) const;
70
76 Sound& operator+=(const Sound& alt);
77
85 void overwrite (const Sound& alt, size_t offset=0, size_t len=0);
86
92 Sound subSound(size_t first_sample, size_t len);
93
94 /*
95 * Reset the sound (i.e. reset the number of samples, channels, frequency etc to zero)
96 */
97 void clear();
98
104 void resize(size_t samples, size_t channels = 1);
105
106 audio_sample get(size_t sample, size_t channel = 0) const;
107
108 void set(audio_sample value, size_t sample, size_t channel = 0);
109
110 audio_sample getSafe(size_t sample, size_t channel = 0) const {
111 if (isSample(sample,channel)) {
112 return get(sample,channel);
113 }
114 return 0;
115 }
116
117 void setSafe(audio_sample value, size_t sample, size_t channel = 0);
118
125 bool isSample(size_t sample, size_t channel = 0) const;
126
130 void zero();
131
136 void normalizeChannel(size_t channel);
137
141 void normalize();
142
148 void amplifyChannel(size_t channel, double gain);
149
154 void amplify(double gain);
155
162 void findPeakInChannel(size_t channelId, size_t& sampleId, audio_sample& sampleValue) const;
163
170 void findPeak(size_t& channelId, size_t& sampleId, audio_sample& sampleValue) const;
171
177 inline double sample2timestamp( size_t sampleid) const {return static_cast<double>(sampleid / m_frequency);}
178
184 inline size_t timestamp2sample( double time) const { return static_cast<size_t>(time * m_frequency);}
185
191 bool zeroChannel(size_t channel);
192
197 int getFrequency() const;
198
202 void setFrequency(int freq);
203
208 size_t getBytesPerSample() const;
209
214 size_t getSamples() const;
215
220 double getDuration() const;
221
226 size_t getChannels() const;
227
232 Sound extractChannelAsSound(size_t channel_id) const;
233
234 std::vector<std::reference_wrapper<audio_sample>> getChannel(size_t channel_id);
235
242 bool replaceChannel(size_t id, Sound channel);
243
250 std::vector<std::reference_wrapper<audio_sample>> getInterleavedAudioRawData() const;
251
258 std::vector<std::reference_wrapper<audio_sample>> getNonInterleavedAudioRawData() const;
259
264 std::string toString() const;
265
266public:
274 void add_marker(const std::string& marker_label, size_t sample_id, int channel=-1);
275
283 bool get_marker(const std::string& marker_label, size_t& sample_id, int& channel) const;
284
289 size_t getMarkersCount() const;
290
298 yarp::sig::SoundMarker getMarker(size_t index) const;
299
304 std::vector<yarp::sig::SoundMarker> getMarkers() const;
305
310 void remove_marker(const std::string& marker_label);
311
315 void remove_all_markers();
316
324 Sound subSound(std::string marker_start, std::string marker_end);
325
326private:
332 unsigned char *getRawData() const;
333
338 size_t getRawDataSize() const;
339
340 void delete_implementation();
341
342public:
343 bool read(yarp::os::ConnectionReader& connection) override;
344
345 bool write(yarp::os::ConnectionWriter& connection) const override;
346
347private:
348 void init(size_t bytesPerSample);
349
350 void *implementation =nullptr;
351 size_t m_samples;
352 size_t m_channels;
353 size_t m_bytesPerSample;
354 int m_frequency;
355
356 std::map<std::string,yarp::sig::SoundMarker> m_markers;
357};
358
359} // namespace yarp::sig
360
361#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:42
double sample2timestamp(size_t sampleid) const
Utility function: return the timestamp (in seconds) given the sample id.
Definition Sound.h:177
short int audio_sample
Definition Sound.h:44
size_t timestamp2sample(double time) const
Utility function: return the sample id given the timestamp (in seconds)
Definition Sound.h:184
audio_sample getSafe(size_t sample, size_t channel=0) const
Definition Sound.h:110
Vector & operator+=(Vector &a, const double &s)
Addition operator between a scalar and a vector (defined in Math.h).
Definition math.cpp:38
std::string label
Definition Sound.h:23
bool operator==(const SoundMarker &other) const
Definition Sound.h:27
#define YARP_sig_API
Definition api.h:18