MPD  0.20.18
AudioFormat.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_AUDIO_FORMAT_HXX
21 #define MPD_AUDIO_FORMAT_HXX
22 
23 #include "pcm/SampleFormat.hxx"
24 #include "Compiler.h"
25 
26 #include <assert.h>
27 #include <stdint.h>
28 #include <stddef.h>
29 
30 template<size_t CAPACITY> class StringBuffer;
31 
32 static constexpr unsigned MAX_CHANNELS = 8;
33 
37 struct AudioFormat {
43  uint32_t sample_rate;
44 
50 
66  uint8_t channels;
67 
68  AudioFormat() = default;
69 
70  constexpr AudioFormat(uint32_t _sample_rate,
71  SampleFormat _format, uint8_t _channels)
72  :sample_rate(_sample_rate),
73  format(_format), channels(_channels) {}
74 
75  static constexpr AudioFormat Undefined() {
77  }
78 
83  void Clear() {
84  sample_rate = 0;
85  format = SampleFormat::UNDEFINED;
86  channels = 0;
87  }
88 
92  constexpr bool IsDefined() const {
93  return sample_rate != 0;
94  }
95 
101  constexpr bool IsFullyDefined() const {
102  return sample_rate != 0 && format != SampleFormat::UNDEFINED &&
103  channels != 0;
104  }
105 
109  constexpr bool IsMaskDefined() const {
110  return sample_rate != 0 || format != SampleFormat::UNDEFINED ||
111  channels != 0;
112  }
113 
114  bool IsValid() const;
115  bool IsMaskValid() const;
116 
117  constexpr bool operator==(const AudioFormat other) const {
118  return sample_rate == other.sample_rate &&
119  format == other.format &&
120  channels == other.channels;
121  }
122 
123  constexpr bool operator!=(const AudioFormat other) const {
124  return !(*this == other);
125  }
126 
127  void ApplyMask(AudioFormat mask) noexcept;
128 
129  gcc_pure
130  AudioFormat WithMask(AudioFormat mask) const noexcept {
131  AudioFormat result = *this;
132  result.ApplyMask(mask);
133  return result;
134  }
135 
139  unsigned GetSampleSize() const;
140 
144  unsigned GetFrameSize() const;
145 
150  double GetTimeToSize() const;
151 };
152 
158 static constexpr inline bool
159 audio_valid_sample_rate(unsigned sample_rate)
160 {
161  return sample_rate > 0 && sample_rate < (1 << 30);
162 }
163 
167 static constexpr inline bool
168 audio_valid_channel_count(unsigned channels)
169 {
170  return channels >= 1 && channels <= MAX_CHANNELS;
171 }
172 
177 inline bool
179 {
183 }
184 
189 inline bool
191 {
192  return (sample_rate == 0 ||
197 }
198 
199 inline unsigned
201 {
202  return sample_format_size(format);
203 }
204 
205 inline unsigned
207 {
208  return GetSampleSize() * channels;
209 }
210 
211 inline double
213 {
214  return sample_rate * GetFrameSize();
215 }
216 
224 gcc_const
226 ToString(AudioFormat af) noexcept;
227 
228 #endif
constexpr bool operator==(const AudioFormat other) const
static constexpr unsigned MAX_CHANNELS
Definition: AudioFormat.hxx:32
This structure describes the format of a raw PCM stream.
Definition: AudioFormat.hxx:37
uint32_t sample_rate
The sample rate in Hz.
Definition: AudioFormat.hxx:43
double GetTimeToSize() const
Returns the floating point factor which converts a time span to a storage size in bytes...
SampleFormat format
The format samples are stored in.
Definition: AudioFormat.hxx:49
constexpr bool IsFullyDefined() const
Checks whether the object is full, i.e.
static constexpr bool audio_valid_sample_format(SampleFormat format)
Checks whether the sample format is valid.
unsigned GetFrameSize() const
Returns the size of each full frame in bytes.
bool IsValid() const
Returns false if the format is not valid for playback with MPD.
static constexpr bool audio_valid_sample_rate(unsigned sample_rate)
Checks whether the sample rate is valid.
static constexpr AudioFormat Undefined()
Definition: AudioFormat.hxx:75
void ApplyMask(AudioFormat mask) noexcept
uint8_t channels
The number of channels.
Definition: AudioFormat.hxx:66
#define gcc_const
Definition: Compiler.h:109
gcc_pure AudioFormat WithMask(AudioFormat mask) const noexcept
void Clear()
Clears the object, i.e.
Definition: AudioFormat.hxx:83
static constexpr unsigned sample_format_size(SampleFormat format)
constexpr bool operator!=(const AudioFormat other) const
SampleFormat
constexpr bool IsMaskDefined() const
Checks whether the object has at least one defined value.
bool IsMaskValid() const
Returns false if the format mask is not valid for playback with MPD.
unsigned GetSampleSize() const
Returns the size of each (mono) sample in bytes.
#define gcc_pure
Definition: Compiler.h:116
static constexpr bool audio_valid_channel_count(unsigned channels)
Checks whether the number of channels is valid.
AudioFormat()=default
gcc_const StringBuffer< 24 > ToString(AudioFormat af) noexcept
Renders the AudioFormat object into a string, e.g.
constexpr AudioFormat(uint32_t _sample_rate, SampleFormat _format, uint8_t _channels)
Definition: AudioFormat.hxx:70
constexpr bool IsDefined() const
Checks whether the object has a defined value.
Definition: AudioFormat.hxx:92