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
48
SampleFormat
format
;
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
() {
75
return
AudioFormat
(0,
SampleFormat::UNDEFINED
,0);
76
}
77
82
void
Clear
() {
83
sample_rate
= 0;
84
format
=
SampleFormat::UNDEFINED
;
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
177
AudioFormat::IsValid
()
const
178
{
179
return
audio_valid_sample_rate
(
sample_rate
) &&
180
audio_valid_sample_format
(
format
) &&
181
audio_valid_channel_count
(
channels
);
182
}
183
188
inline
bool
189
AudioFormat::IsMaskValid
()
const
190
{
191
return
(
sample_rate
== 0 ||
192
audio_valid_sample_rate
(
sample_rate
)) &&
193
(
format
==
SampleFormat::UNDEFINED
||
194
audio_valid_sample_format
(
format
)) &&
195
(
channels
== 0 ||
audio_valid_channel_count
(
channels
));
196
}
197
198
inline
unsigned
199
AudioFormat::GetSampleSize
()
const
200
{
201
return
sample_format_size
(
format
);
202
}
203
204
inline
unsigned
205
AudioFormat::GetFrameSize
()
const
206
{
207
return
GetSampleSize
() *
channels
;
208
}
209
210
inline
double
211
AudioFormat::GetTimeToSize
()
const
212
{
213
return
sample_rate
*
GetFrameSize
();
214
}
215
223
gcc_const
224
StringBuffer<24>
225
ToString
(
AudioFormat
af) noexcept;
226
227
#endif
AudioFormat::IsValid
bool IsValid() const
Returns false if the format is not valid for playback with MPD.
Definition:
AudioFormat.hxx:177
MAX_CHANNELS
static constexpr unsigned MAX_CHANNELS
Definition:
AudioFormat.hxx:31
AudioFormat
This structure describes the format of a raw PCM stream.
Definition:
AudioFormat.hxx:36
AudioFormat::sample_rate
uint32_t sample_rate
The sample rate in Hz.
Definition:
AudioFormat.hxx:42
AudioFormat::format
SampleFormat format
The format samples are stored in.
Definition:
AudioFormat.hxx:48
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:211
audio_valid_sample_rate
static constexpr bool audio_valid_sample_rate(unsigned sample_rate)
Checks whether the sample rate is valid.
Definition:
AudioFormat.hxx:158
AudioFormat::Undefined
static constexpr AudioFormat Undefined()
Definition:
AudioFormat.hxx:74
AudioFormat::IsFullyDefined
constexpr bool IsFullyDefined() const
Checks whether the object is full, i.e.
Definition:
AudioFormat.hxx:100
StringBuffer
Definition:
AudioFormat.hxx:29
AudioFormat::ApplyMask
void ApplyMask(AudioFormat mask) noexcept
AudioFormat::channels
uint8_t channels
The number of channels.
Definition:
AudioFormat.hxx:65
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:199
AudioFormat::IsMaskValid
bool IsMaskValid() const
Returns false if the format mask is not valid for playback with MPD.
Definition:
AudioFormat.hxx:189
AudioFormat::operator!=
constexpr bool operator!=(const AudioFormat other) const
Definition:
AudioFormat.hxx:122
AudioFormat::WithMask
gcc_pure AudioFormat WithMask(AudioFormat mask) const noexcept
Definition:
AudioFormat.hxx:129
AudioFormat::Clear
void Clear()
Clears the object, i.e.
Definition:
AudioFormat.hxx:82
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:91
SampleFormat
SampleFormat
Definition:
SampleFormat.hxx:33
SampleFormat.hxx
AudioFormat::operator==
constexpr bool operator==(const AudioFormat other) const
Definition:
AudioFormat.hxx:116
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:108
AudioFormat::GetFrameSize
unsigned GetFrameSize() const
Returns the size of each full frame in bytes.
Definition:
AudioFormat.hxx:205
audio_valid_channel_count
static constexpr bool audio_valid_channel_count(unsigned channels)
Checks whether the number of channels is valid.
Definition:
AudioFormat.hxx:167
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:69
Generated on Thu May 9 2019 07:58:21 for MPD by
1.8.15