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>
14#include <vector>
15#include <string>
16
17#include <map>
18
19namespace yarp::sig {
20
22{
23public:
24 bool operator==(const SoundMarker& other) const
25 {
26 return label == other.label &&
27 channel == other.channel &&
28 sample_id == other.sample_id;
29 }
30};
31
39{
40public:
41 typedef short int audio_sample;
42
43 Sound(size_t bytesPerSample = 2);
44
50 Sound(const Sound& alt);
51
52 virtual ~Sound();
53
59 const Sound& operator=(const Sound& alt);
60
66 bool operator==(const Sound& alt) const;
67
73 Sound& operator+=(const Sound& alt);
74
82 void overwrite (const Sound& alt, size_t offset=0, size_t len=0);
83
89 Sound subSound(size_t first_sample, size_t len);
90
91 /*
92 * Reset the sound (i.e. reset the number of samples, channels, frequency etc to zero)
93 */
94 void clear();
95
101 void resize(size_t samples, size_t channels = 1);
102
103 audio_sample get(size_t sample, size_t channel = 0) const;
104
105 void set(audio_sample value, size_t sample, size_t channel = 0);
106
107 audio_sample getSafe(size_t sample, size_t channel = 0) const {
108 if (isSample(sample,channel)) {
109 return get(sample,channel);
110 }
111 return 0;
112 }
113
114 void setSafe(audio_sample value, size_t sample, size_t channel = 0);
115
122 bool isSample(size_t sample, size_t channel = 0) const;
123
127 void zero();
128
133 void normalizeChannel(size_t channel);
134
138 void normalize();
139
145 void amplifyChannel(size_t channel, double gain);
146
151 void amplify(double gain);
152
159 void findPeakInChannel(size_t channelId, size_t& sampleId, audio_sample& sampleValue) const;
160
167 void findPeak(size_t& channelId, size_t& sampleId, audio_sample& sampleValue) const;
168
174 inline double sample2timestamp( size_t sampleid) const {return static_cast<double>(sampleid / m_frequency);}
175
181 inline size_t timestamp2sample( double time) const { return static_cast<size_t>(time * m_frequency);}
182
188 bool zeroChannel(size_t channel);
189
194 int getFrequency() const;
195
199 void setFrequency(int freq);
200
205 size_t getBytesPerSample() const;
206
211 size_t getSamples() const;
212
217 double getDuration() const;
218
223 size_t getChannels() const;
224
229 Sound extractChannelAsSound(size_t channel_id) const;
230
231 std::vector<std::reference_wrapper<audio_sample>> getChannel(size_t channel_id);
232
239 bool replaceChannel(size_t id, Sound channel);
240
247 std::vector<std::reference_wrapper<audio_sample>> getInterleavedAudioRawData() const;
248
255 std::vector<std::reference_wrapper<audio_sample>> getNonInterleavedAudioRawData() const;
256
261 std::string toString() const;
262
263public:
271 void add_marker(const std::string& marker_label, size_t sample_id, int channel=-1);
272
280 bool get_marker(const std::string& marker_label, size_t& sample_id, int& channel) const;
281
286 size_t getMarkersCount() const;
287
295 yarp::sig::SoundMarker getMarker(size_t index) const;
296
301 std::vector<yarp::sig::SoundMarker> getMarkers() const;
302
307 void remove_marker(const std::string& marker_label);
308
312 void remove_all_markers();
313
321 Sound subSound(std::string marker_start, std::string marker_end);
322
323private:
329 unsigned char *getRawData() const;
330
335 size_t getRawDataSize() const;
336
337 void delete_implementation();
338
339public:
340 bool read(yarp::os::ConnectionReader& connection) override;
341
342 bool write(yarp::os::ConnectionWriter& connection) const override;
343
344private:
345 void init(size_t bytesPerSample);
346
347 void *implementation =nullptr;
348 size_t m_samples;
349 size_t m_channels;
350 size_t m_bytesPerSample;
351 int m_frequency;
352
353 std::map<std::string,yarp::sig::SoundMarker> m_markers;
354};
355
356} // namespace yarp::sig
357
358#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:39
double sample2timestamp(size_t sampleid) const
Utility function: return the timestamp (in seconds) given the sample id.
Definition Sound.h:174
short int audio_sample
Definition Sound.h:41
size_t timestamp2sample(double time) const
Utility function: return the sample id given the timestamp (in seconds)
Definition Sound.h:181
audio_sample getSafe(size_t sample, size_t channel=0) const
Definition Sound.h:107
Vector & operator+=(Vector &a, const double &s)
Addition operator between a scalar and a vector (defined in Math.h).
Definition math.cpp:38
bool operator==(const SoundMarker &other) const
Definition Sound.h:24
#define YARP_sig_API
Definition api.h:18