MPD  0.20.23
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 <stdint.h>
27 #include <stddef.h>
28 
29 template<size_t CAPACITY> class StringBuffer;
30 
31 static constexpr unsigned MAX_CHANNELS = 8;
32 
36 struct AudioFormat {
42  uint32_t sample_rate;
43 
49 
65  uint8_t channels;
66 
67  AudioFormat() = default;
68 
69  constexpr AudioFormat(uint32_t _sample_rate,
70  SampleFormat _format, uint8_t _channels)
71  :sample_rate(_sample_rate),
72  format(_format), channels(_channels) {}
73 
74  static constexpr AudioFormat Undefined() {
76  }
77 
82  void Clear() {
83  sample_rate = 0;
85  channels = 0;
86  }
87 
91  constexpr bool IsDefined() const {
92  return sample_rate != 0;
93  }
94 
100  constexpr bool IsFullyDefined() const {
101  return sample_rate != 0 && format != SampleFormat::UNDEFINED &&
102  channels != 0;
103  }
104 
108  constexpr bool IsMaskDefined() const {
109  return sample_rate != 0 || format != SampleFormat::UNDEFINED ||
110  channels != 0;
111  }
112 
113  bool IsValid() const;
114  bool IsMaskValid() const;
115 
116  constexpr bool operator==(const AudioFormat other) const {
117  return sample_rate == other.sample_rate &&
118  format == other.format &&
119  channels == other.channels;
120  }
121 
122  constexpr bool operator!=(const AudioFormat other) const {
123  return !(*this == other);
124  }
125 
126  void ApplyMask(AudioFormat mask) noexcept;
127 
128  gcc_pure
129  AudioFormat WithMask(AudioFormat mask) const noexcept {
130  AudioFormat result = *this;
131  result.ApplyMask(mask);
132  return result;
133  }
134 
138  unsigned GetSampleSize() const;
139 
143  unsigned GetFrameSize() const;
144 
149  double GetTimeToSize() const;
150 };
151 
157 static constexpr inline bool
158 audio_valid_sample_rate(unsigned sample_rate)
159 {
160  return sample_rate > 0 && sample_rate < (1 << 30);
161 }
162 
166 static constexpr inline bool
167 audio_valid_channel_count(unsigned channels)
168 {
169  return channels >= 1 && channels <= MAX_CHANNELS;
170 }
171 
176 inline bool
178 {
182 }
183 
188 inline bool
190 {
191  return (sample_rate == 0 ||
196 }
197 
198 inline unsigned
200 {
201  return sample_format_size(format);
202 }
203 
204 inline unsigned
206 {
207  return GetSampleSize() * channels;
208 }
209 
210 inline double
212 {
213  return sample_rate * GetFrameSize();
214 }
215 
223 gcc_const
225 ToString(AudioFormat af) noexcept;
226 
227 #endif
bool IsValid() const
Returns false if the format is not valid for playback with MPD.
static constexpr unsigned MAX_CHANNELS
Definition: AudioFormat.hxx:31
This structure describes the format of a raw PCM stream.
Definition: AudioFormat.hxx:36
uint32_t sample_rate
The sample rate in Hz.
Definition: AudioFormat.hxx:42
SampleFormat format
The format samples are stored in.
Definition: AudioFormat.hxx:48
static constexpr bool audio_valid_sample_format(SampleFormat format)
Checks whether the sample format is valid.
double GetTimeToSize() const
Returns the floating point factor which converts a time span to a storage size in bytes.
static constexpr bool audio_valid_sample_rate(unsigned sample_rate)
Checks whether the sample rate is valid.
static constexpr AudioFormat Undefined()
Definition: AudioFormat.hxx:74
constexpr bool IsFullyDefined() const
Checks whether the object is full, i.e.
void ApplyMask(AudioFormat mask) noexcept
uint8_t channels
The number of channels.
Definition: AudioFormat.hxx:65
#define gcc_const
Definition: Compiler.h:109
unsigned GetSampleSize() const
Returns the size of each (mono) sample in bytes.
bool IsMaskValid() const
Returns false if the format mask is not valid for playback with MPD.
constexpr bool operator!=(const AudioFormat other) const
gcc_pure AudioFormat WithMask(AudioFormat mask) const noexcept
void Clear()
Clears the object, i.e.
Definition: AudioFormat.hxx:82
static constexpr unsigned sample_format_size(SampleFormat format)
constexpr bool IsDefined() const
Checks whether the object has a defined value.
Definition: AudioFormat.hxx:91
SampleFormat
constexpr bool operator==(const AudioFormat other) const
#define gcc_pure
Definition: Compiler.h:116
constexpr bool IsMaskDefined() const
Checks whether the object has at least one defined value.
unsigned GetFrameSize() const
Returns the size of each full frame in bytes.
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:69