MPD: AudioFormat.hxx Source File

MPD 0.20.15

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
49 SampleFormat format;
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() {
76 return AudioFormat(0, SampleFormat::UNDEFINED,0);
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
178 AudioFormat::IsValid() const
179 {
180 return audio_valid_sample_rate(sample_rate) &&
181 audio_valid_sample_format(format) &&
182 audio_valid_channel_count(channels);
183 }
184
189 inline bool
190 AudioFormat::IsMaskValid() const
191 {
192 return (sample_rate == 0 ||
193 audio_valid_sample_rate(sample_rate)) &&
194 (format == SampleFormat::UNDEFINED ||
195 audio_valid_sample_format(format)) &&
196 (channels == 0 || audio_valid_channel_count(channels));
197 }
198
199 inline unsigned
200 AudioFormat::GetSampleSize() const
201 {
202 return sample_format_size(format);
203 }
204
205 inline unsigned
206 AudioFormat::GetFrameSize() const
207 {
208 return GetSampleSize() * channels;
209 }
210
211 inline double
212 AudioFormat::GetTimeToSize() const
213 {
214 return sample_rate * GetFrameSize();
215 }
216
224 gcc_const
225 StringBuffer<24>
226 ToString(AudioFormat af) noexcept;
227
228 #endif
AudioFormat::IsValid
bool IsValid() const
Returns false if the format is not valid for playback with MPD.
Definition: AudioFormat.hxx:178
MAX_CHANNELS
static constexpr unsigned MAX_CHANNELS
Definition: AudioFormat.hxx:32
AudioFormat
This structure describes the format of a raw PCM stream.
Definition: AudioFormat.hxx:37
AudioFormat::sample_rate
uint32_t sample_rate
The sample rate in Hz.
Definition: AudioFormat.hxx:43
AudioFormat::format
SampleFormat format
The format samples are stored in.
Definition: AudioFormat.hxx:49
audio_valid_sample_format
static constexpr bool audio_valid_sample_format(SampleFormat format)
Checks whether the sample format is valid.
Definition: SampleFormat.hxx:71
AudioFormat::GetTimeToSize
double GetTimeToSize() const
Returns the floating point factor which converts a time span to a storage size in bytes...
Definition: AudioFormat.hxx:212
audio_valid_sample_rate
static constexpr bool audio_valid_sample_rate(unsigned sample_rate)
Checks whether the sample rate is valid.
Definition: AudioFormat.hxx:159
AudioFormat::Undefined
static constexpr AudioFormat Undefined()
Definition: AudioFormat.hxx:75
AudioFormat::IsFullyDefined
constexpr bool IsFullyDefined() const
Checks whether the object is full, i.e.
Definition: AudioFormat.hxx:101
StringBuffer
Definition: AudioFormat.hxx:30
AudioFormat::ApplyMask
void ApplyMask(AudioFormat mask) noexcept
AudioFormat::channels
uint8_t channels
The number of channels.
Definition: AudioFormat.hxx:66
Compiler.h
gcc_const
#define gcc_const
Definition: Compiler.h:109
AudioFormat::GetSampleSize
unsigned GetSampleSize() const
Returns the size of each (mono) sample in bytes.
Definition: AudioFormat.hxx:200
AudioFormat::IsMaskValid
bool IsMaskValid() const
Returns false if the format mask is not valid for playback with MPD.
Definition: AudioFormat.hxx:190
AudioFormat::operator!=
constexpr bool operator!=(const AudioFormat other) const
Definition: AudioFormat.hxx:123
AudioFormat::WithMask
gcc_pure AudioFormat WithMask(AudioFormat mask) const noexcept
Definition: AudioFormat.hxx:130
AudioFormat::Clear
void Clear()
Clears the object, i.e.
Definition: AudioFormat.hxx:83
sample_format_size
static constexpr unsigned sample_format_size(SampleFormat format)
Definition: SampleFormat.hxx:93
AudioFormat::IsDefined
constexpr bool IsDefined() const
Checks whether the object has a defined value.
Definition: AudioFormat.hxx:92
SampleFormat
SampleFormat
Definition: SampleFormat.hxx:33
SampleFormat.hxx
AudioFormat::operator==
constexpr bool operator==(const AudioFormat other) const
Definition: AudioFormat.hxx:117
SampleFormat::UNDEFINED
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
AudioFormat::IsMaskDefined
constexpr bool IsMaskDefined() const
Checks whether the object has at least one defined value.
Definition: AudioFormat.hxx:109
AudioFormat::GetFrameSize
unsigned GetFrameSize() const
Returns the size of each full frame in bytes.
Definition: AudioFormat.hxx:206
audio_valid_channel_count
static constexpr bool audio_valid_channel_count(unsigned channels)
Checks whether the number of channels is valid.
Definition: AudioFormat.hxx:168
AudioFormat::AudioFormat
AudioFormat()=default
ToString
gcc_const StringBuffer< 24 > ToString(AudioFormat af) noexcept
Renders the AudioFormat object into a string, e.g.
AudioFormat::AudioFormat
constexpr AudioFormat(uint32_t _sample_rate, SampleFormat _format, uint8_t _channels)
Definition: AudioFormat.hxx:70