Ocean
SoundMedium.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_MEDIA_SOUND_MEDIUM_H
9 #define META_OCEAN_MEDIA_SOUND_MEDIUM_H
10 
11 #include "ocean/media/Media.h"
12 #include "ocean/media/Medium.h"
13 #include "ocean/media/MediumRef.h"
14 
15 #include "ocean/base/Timestamp.h"
16 
17 namespace Ocean
18 {
19 
20 namespace Media
21 {
22 
23 // Forward declaration.
24 class SoundMedium;
25 
26 /**
27  * Definition of a smart medium reference holding a sound medium object.
28  * @see SmartMediumRef, SoundMedium.
29  * @ingroup media
30  */
32 
33 /**
34  * This class is the base class for all sound mediums.
35  * @ingroup media
36  */
37 class OCEAN_MEDIA_EXPORT SoundMedium : public virtual Medium
38 {
39  public:
40 
41  /**
42  * Definition of a sound frequency defined in Hz.
43  */
44  typedef float SoundFrequency;
45 
46  /**
47  * Definition of a sound type composed by the sound frequency and channels.
48  */
49  class OCEAN_MEDIA_EXPORT SoundType
50  {
51  public:
52 
53  /**
54  * Creates a new sound type with invalid parameters.
55  */
56  inline SoundType();
57 
58  /**
59  * Creates a new sound type.
60  * @param frequency Sound frequency
61  * @param channels Sound channels
62  * @param bitsPerSample Bits per sound sample
63  */
64  inline SoundType(const SoundFrequency frequency, const unsigned int channels, const unsigned int bitsPerSample);
65 
66  /**
67  * Returns the frequency of the sound format in Hz.
68  * @return Sound frequency
69  */
70  inline SoundFrequency frequency() const;
71 
72  /**
73  * Returns the channels of the sound format.
74  * @return Sound channels
75  */
76  inline unsigned int channels() const;
77 
78  /**
79  * Returns the bits per sound sample.
80  * @return Bits per sample
81  */
82  inline unsigned int bitsPerSample() const;
83 
84  /**
85  * Sets the frequency of the sound format in Hz.
86  * @param frequency Sound frequency
87  */
88  inline void setFrequency(const SoundFrequency frequency);
89 
90  /**
91  * Sets the channels of the sound format.
92  * @param channels Sound channels
93  */
94  inline void setChannels(const unsigned int channels);
95 
96  /**
97  * Sets the bits per sound sample.
98  * @param bits Bits per sound sample
99  */
100  inline void setBitsPerSoundSample(const unsigned int bits);
101 
102  /**
103  * Returns whether two sound types are equal.
104  * @param right Right sound type
105  * @return True, if so
106  */
107  inline bool operator==(const SoundType& right) const;
108 
109  /**
110  * Returns whether the left sound type is 'smaller' than the right one.
111  * @param right Right sound type
112  * @return True, if so
113  */
114  inline bool operator<(const SoundType& right) const;
115 
116  private:
117 
118  /// Sound frequency in Hz.
120 
121  /// Sound channels.
122  unsigned int typeChannels;
123 
124  /// Bits per sound sample.
125  unsigned int typeBitsPerSample;
126  };
127 
128  protected:
129 
130  /**
131  * Class allowing the sorting of several sound media types according their popularity.
132  */
133  class OCEAN_MEDIA_EXPORT SortableSoundType
134  {
135  public:
136 
137  /**
138  * Creates a new sortable sound media type.
139  * @param soundType Preferable sound type
140  */
141  SortableSoundType(const SoundType& soundType);
142 
143  /**
144  * Creates a new sortable sound type.
145  * @param frequency Preferable reference frequency
146  * @param channels Preferable reference channels
147  * @param bitsPerSample Preferable reference bits per sample
148  */
149  SortableSoundType(const SoundFrequency frequency, const unsigned int channels, const unsigned int bitsPerSample);
150 
151  /**
152  * Returns whether the left sortable sound type is more prefered than the right one.
153  * @param right Right sortable sound type
154  * @return True, if so
155  */
156  bool operator<(const SortableSoundType& right) const;
157 
158  protected:
159 
160  /// Actual sound type.
162 
163  /// Preferable sound type.
165  };
166 
167  public:
168 
169  /**
170  * Returns whether the object holds a sound.
171  * @return True, if so
172  */
173  inline bool hasSound() const;
174 
175  /**
176  * Returns the number of sound channels.
177  * @return Number of channels
178  */
179  inline unsigned int soundChannels() const;
180 
181  /**
182  * Returns the frequency of the sound in Hz.
183  * @return Sound frequency in Hz
184  */
185  inline SoundFrequency soundFrequency() const;
186 
187  /**
188  * Returns the number of bits per sample.
189  * @return Number of bits per sample
190  */
191  inline unsigned int soundBitsPerSample() const;
192 
193  /**
194  * Returns the volume of the sound in db.
195  * @return Sound volume in db.
196  * @see setSoundVolume().
197  */
198  virtual float soundVolume() const = 0;
199 
200  /**
201  * Returns whether the sound medium is in a mute state.
202  * @return True, if so
203  * @see setSoundMute().
204  */
205  virtual bool soundMute() const = 0;
206 
207  /**
208  * Returns the preferred number of sound channels.
209  * @return Preferred sound channels
210  */
211  inline unsigned int preferredSoundChannels() const;
212 
213  /**
214  * Returns the preferred sound frequency in Hz.
215  * @return Preferred sound frequency
216  */
217  inline SoundFrequency preferredSoundFrequency() const;
218 
219  /**
220  * Returns the preferred bits per sound sample.
221  * @return Preferred bits per sound sample
222  */
223  inline unsigned int preferredSoundBitsPerSample() const;
224 
225  /**
226  * Sets the volume of the sound in db.
227  * The volume must be in range [-100db, 0db]
228  * @param volume New volume in db
229  * @return True, if succeeded
230  * @see soundVolume().
231  */
232  virtual bool setSoundVolume(const float volume) = 0;
233 
234  /**
235  * Sets or unsets the sound medium to a mute state.
236  * @param mute True, to mute the sound
237  * @return True, if succeeded
238  * @see soundMute().
239  */
240  virtual bool setSoundMute(const bool mute) = 0;
241 
242  /**
243  * Sets the preferred number of sound channels.
244  * @param channels Preferred sound channels
245  * @return True, if succeeded
246  */
247  virtual bool setPreferredSoundChannels(const unsigned int channels);
248 
249  /**
250  * Sets the preferred sound frequency in Hz.
251  * @param frequency Preferred sound frequency
252  * @return True, if succeeded
253  */
254  virtual bool setPreferredSoundFrequency(const SoundFrequency frequency);
255 
256  /**
257  * Sets the preferred bits per sound sample.
258  * @param bits Preferred bits per sound sample
259  * @return True, if succeeded
260  */
261  virtual bool setPreferredSoundBitsPerSample(const unsigned int bits);
262 
263  protected:
264 
265  /**
266  * Creates a new sound medium by a given url.
267  * @param url Url of the sound medium
268  */
269  explicit SoundMedium(const std::string& url);
270 
271  protected:
272 
273  /// Actual sound type.
275 
276  /// Preferred sound type.
278 
279  /// Timestamp of the recent sound frame.
281 
282  /// Timestamp of the recent sound frame type.
284 };
285 
287  typeFrequency(0),
288  typeChannels(0),
289  typeBitsPerSample(0)
290 {
291  // nothing to do here
292 }
293 
294 inline SoundMedium::SoundType::SoundType(const SoundFrequency frequency, const unsigned int channels, const unsigned int bitsPerSample) :
295  typeFrequency(frequency),
296  typeChannels(channels),
297  typeBitsPerSample(bitsPerSample)
298 {
299  // nothing to do here
300 }
301 
303 {
304  return typeFrequency;
305 }
306 
307 inline unsigned int SoundMedium::SoundType::channels() const
308 {
309  return typeChannels;
310 }
311 
312 inline unsigned int SoundMedium::SoundType::bitsPerSample() const
313 {
314  return typeBitsPerSample;
315 }
316 
318 {
319  typeFrequency = frequency;
320 }
321 
322 inline void SoundMedium::SoundType::setChannels(const unsigned int channels)
323 {
324  typeChannels = channels;
325 }
326 
327 inline void SoundMedium::SoundType::setBitsPerSoundSample(const unsigned int bits)
328 {
329  typeBitsPerSample = bits;
330 }
331 
332 inline bool SoundMedium::SoundType::operator==(const SoundType& right) const
333 {
334  return typeChannels == right.typeChannels && typeFrequency == right.typeFrequency
335  && typeBitsPerSample == right.typeBitsPerSample;
336 }
337 
338 inline bool SoundMedium::SoundType::operator<(const SoundType& right) const
339 {
340  if (typeChannels < right.typeChannels)
341  return true;
342  if (typeChannels > right.typeChannels)
343  return false;
344 
345  if (typeFrequency < right.typeFrequency)
346  return true;
347  if (typeFrequency > right.typeFrequency)
348  return false;
349 
350  return typeBitsPerSample < right.typeBitsPerSample;
351 }
352 
353 inline bool SoundMedium::hasSound() const
354 {
355  return mediumSoundTimestamp.isInvalid() == false;
356 }
357 
358 inline unsigned int SoundMedium::soundChannels() const
359 {
360  return mediumSoundType.channels();
361 }
362 
364 {
365  return mediumSoundType.frequency();
366 }
367 
368 inline unsigned int SoundMedium::soundBitsPerSample() const
369 {
371 }
372 
373 inline unsigned int SoundMedium::preferredSoundChannels() const
374 {
376 }
377 
379 {
381 }
382 
383 inline unsigned int SoundMedium::preferredSoundBitsPerSample() const
384 {
386 }
387 
388 }
389 
390 }
391 
392 #endif // META_OCEAN_MEDIA_SOUND_MEDIUM_H
This is the base class for all mediums.
Definition: Medium.h:48
This class implements a smart medium reference.
Definition: MediumRef.h:33
Class allowing the sorting of several sound media types according their popularity.
Definition: SoundMedium.h:134
bool operator<(const SortableSoundType &right) const
Returns whether the left sortable sound type is more prefered than the right one.
SortableSoundType(const SoundFrequency frequency, const unsigned int channels, const unsigned int bitsPerSample)
Creates a new sortable sound type.
SoundType actualSoundType
Actual sound type.
Definition: SoundMedium.h:161
SoundType preferableSoundType
Preferable sound type.
Definition: SoundMedium.h:164
SortableSoundType(const SoundType &soundType)
Creates a new sortable sound media type.
Definition of a sound type composed by the sound frequency and channels.
Definition: SoundMedium.h:50
bool operator<(const SoundType &right) const
Returns whether the left sound type is 'smaller' than the right one.
Definition: SoundMedium.h:338
SoundFrequency frequency() const
Returns the frequency of the sound format in Hz.
Definition: SoundMedium.h:302
unsigned int typeChannels
Sound channels.
Definition: SoundMedium.h:122
void setBitsPerSoundSample(const unsigned int bits)
Sets the bits per sound sample.
Definition: SoundMedium.h:327
SoundFrequency typeFrequency
Sound frequency in Hz.
Definition: SoundMedium.h:119
void setChannels(const unsigned int channels)
Sets the channels of the sound format.
Definition: SoundMedium.h:322
unsigned int typeBitsPerSample
Bits per sound sample.
Definition: SoundMedium.h:125
void setFrequency(const SoundFrequency frequency)
Sets the frequency of the sound format in Hz.
Definition: SoundMedium.h:317
bool operator==(const SoundType &right) const
Returns whether two sound types are equal.
Definition: SoundMedium.h:332
SoundType()
Creates a new sound type with invalid parameters.
Definition: SoundMedium.h:286
unsigned int bitsPerSample() const
Returns the bits per sound sample.
Definition: SoundMedium.h:312
unsigned int channels() const
Returns the channels of the sound format.
Definition: SoundMedium.h:307
This class is the base class for all sound mediums.
Definition: SoundMedium.h:38
Timestamp mediumSoundTimestamp
Timestamp of the recent sound frame.
Definition: SoundMedium.h:280
SoundFrequency soundFrequency() const
Returns the frequency of the sound in Hz.
Definition: SoundMedium.h:363
bool hasSound() const
Returns whether the object holds a sound.
Definition: SoundMedium.h:353
virtual float soundVolume() const =0
Returns the volume of the sound in db.
virtual bool setSoundVolume(const float volume)=0
Sets the volume of the sound in db.
virtual bool setSoundMute(const bool mute)=0
Sets or unsets the sound medium to a mute state.
Timestamp mediumSoundTypeTimestamp
Timestamp of the recent sound frame type.
Definition: SoundMedium.h:283
SoundMedium(const std::string &url)
Creates a new sound medium by a given url.
unsigned int preferredSoundChannels() const
Returns the preferred number of sound channels.
Definition: SoundMedium.h:373
virtual bool setPreferredSoundChannels(const unsigned int channels)
Sets the preferred number of sound channels.
float SoundFrequency
Definition of a sound frequency defined in Hz.
Definition: SoundMedium.h:44
unsigned int preferredSoundBitsPerSample() const
Returns the preferred bits per sound sample.
Definition: SoundMedium.h:383
unsigned int soundBitsPerSample() const
Returns the number of bits per sample.
Definition: SoundMedium.h:368
SoundType mediumPreferredSoundType
Preferred sound type.
Definition: SoundMedium.h:277
unsigned int soundChannels() const
Returns the number of sound channels.
Definition: SoundMedium.h:358
virtual bool setPreferredSoundBitsPerSample(const unsigned int bits)
Sets the preferred bits per sound sample.
SoundFrequency preferredSoundFrequency() const
Returns the preferred sound frequency in Hz.
Definition: SoundMedium.h:378
virtual bool soundMute() const =0
Returns whether the sound medium is in a mute state.
SoundType mediumSoundType
Actual sound type.
Definition: SoundMedium.h:274
virtual bool setPreferredSoundFrequency(const SoundFrequency frequency)
Sets the preferred sound frequency in Hz.
This class implements a timestamp.
Definition: Timestamp.h:36
bool isInvalid() const
Returns whether the timestamp holds an invalid time.
Definition: Timestamp.h:308
SmartMediumRef< SoundMedium > SoundMediumRef
Definition of a smart medium reference holding a sound medium object.
Definition: SoundMedium.h:24
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15