YARP
Yet Another Robot Platform
Sound.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_SIG_SOUND_H
11 #define YARP_SIG_SOUND_H
12 
13 #include <yarp/os/Portable.h>
14 #include <yarp/conf/numeric.h>
15 #include <yarp/sig/api.h>
16 #include <vector>
17 #include <string>
18 
19 namespace yarp {
20 namespace sig {
21 
28 {
29 public:
30  typedef short int audio_sample;
31 
32  Sound(size_t bytesPerSample = 2);
33 
39  Sound(const Sound& alt);
40 
41  virtual ~Sound();
42 
48  const Sound& operator=(const Sound& alt);
49 
55  bool operator==(const Sound& alt) const;
56 
62  Sound& operator+=(const Sound& alt);
63 
69  Sound subSound(size_t first_sample, size_t last_sample);
70 
76  void resize(size_t samples, size_t channels = 1);
77 
78  audio_sample get(size_t sample, size_t channel = 0) const;
79 
80  void set(audio_sample value, size_t sample, size_t channel = 0);
81 
82  audio_sample getSafe(size_t sample, size_t channel = 0) const {
83  if (isSample(sample,channel)) {
84  return get(sample,channel);
85  }
86  return 0;
87  }
88 
89  void setSafe(audio_sample value, size_t sample, size_t channel = 0);
90 
97  bool isSample(size_t sample, size_t channel = 0) const;
98 
102  void clear();
103 
109  bool clearChannel(size_t channel);
110 
115  int getFrequency() const;
116 
120  void setFrequency(int freq);
121 
126  size_t getBytesPerSample() const;
127 
132  size_t getSamples() const;
133 
138  double getDuration() const;
139 
144  size_t getChannels() const;
145 
150  Sound extractChannelAsSound(size_t channel_id) const;
151 
152  std::vector<std::reference_wrapper<audio_sample>> getChannel(size_t channel_id);
153 
160  bool replaceChannel(size_t id, Sound channel);
161 
168  std::vector<std::reference_wrapper<audio_sample>> getInterleavedAudioRawData() const;
169 
176  std::vector<std::reference_wrapper<audio_sample>> getNonInterleavedAudioRawData() const;
177 
183  std::string toString() const;
184 
185 private:
191  unsigned char *getRawData() const;
192 
197  size_t getRawDataSize() const;
198 
199 public:
200  bool read(yarp::os::ConnectionReader& connection) override;
201 
202  bool write(yarp::os::ConnectionWriter& connection) const override;
203 
204 private:
205  void init(size_t bytesPerSample);
206  void synchronize();
207 
208  void *implementation;
209  size_t m_samples;
210  size_t m_channels;
211  size_t m_bytesPerSample;
212  int m_frequency;
213 };
214 
215 } // namespace sig
216 } // namespace yarp
217 
218 #endif // YARP_SIG_SOUND_H
yarp::os::Portable
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
yarp::sig::file::read
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
Definition: ImageFile.cpp:827
api.h
numeric.h
Portable.h
samples
int16_t * samples
Definition: FfmpegWriter.cpp:76
operator+=
Vector & operator+=(Vector &a, const double &s)
Addition operator between a scalar and a vector (defined in Math.h).
Definition: math.cpp:41
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
yarp::sig::Sound::getSafe
audio_sample getSafe(size_t sample, size_t channel=0) const
Definition: Sound.h:82
YARP_sig_API
#define YARP_sig_API
Definition: api.h:19
yarp::sig::Sound::audio_sample
short int audio_sample
Definition: Sound.h:30
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
yarp::sig::Sound
Class for storing sounds.
Definition: Sound.h:28
toString
std::string toString(const T &value)
convert an arbitrary type to string.
Definition: fakeMotionControl.cpp:121
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
implementation
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:20
yarp::sig::file::write
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)
Definition: ImageFile.cpp:971