7#include "../utility/utility.hpp"
8#include "../macros.hpp"
14hi_export_module(hikogui.audio.pcm_format);
16hi_export
namespace hi {
inline namespace v1 {
25 [[nodiscard]]
friend constexpr bool operator==(
pcm_format const&,
pcm_format const&)
noexcept =
default;
27 [[nodiscard]]
friend constexpr auto operator<=>(
pcm_format const &lhs,
pcm_format const &rhs)
noexcept
29 if (
auto const tmp = lhs._floating_point <=> rhs._floating_point; tmp != std::strong_ordering::equal) {
32 if (
auto const tmp = lhs._num_major_bits <=> rhs._num_major_bits; tmp != std::strong_ordering::equal) {
35 if (
auto const tmp = lhs._num_minor_bits <=> rhs._num_minor_bits; tmp != std::strong_ordering::equal) {
38 if (
auto const tmp = lhs._num_bytes <=> rhs._num_bytes; tmp != std::strong_ordering::equal) {
41 if (
auto const tmp = lhs._lsb <=> rhs._lsb; tmp != std::strong_ordering::equal) {
44 return lhs._lsb <=> rhs._lsb;
47 [[nodiscard]]
constexpr friend bool equal_except_bit_depth(
pcm_format const& lhs,
pcm_format const& rhs)
noexcept
49 return lhs._floating_point == rhs._floating_point;
69 uint8_t num_major_bits,
70 uint8_t num_minor_bits) noexcept :
71 _floating_point(floating_point),
72 _little_endian(endian == std::endian::little),
75 _num_major_bits(num_major_bits),
76 _num_minor_bits(num_minor_bits)
79 hi_assert(num_major_bits >= 0 and num_major_bits <= 15);
80 hi_assert(num_minor_bits >= 1 and num_minor_bits <= 63);
83 [[nodiscard]]
constexpr static pcm_format float32() noexcept
85 return pcm_format{
true, std::endian::native,
true, 4, 8, 23};
88 [[nodiscard]]
constexpr static pcm_format float32_le() noexcept
90 return pcm_format{
true, std::endian::little,
true, 4, 8, 23};
93 [[nodiscard]]
constexpr static pcm_format float32_be() noexcept
95 return pcm_format{
true, std::endian::big,
true, 4, 8, 23};
98 [[nodiscard]]
constexpr static pcm_format sint24() noexcept
100 return pcm_format{
false, std::endian::native,
true, 3, 0, 23};
103 [[nodiscard]]
constexpr static pcm_format sint24_le() noexcept
105 return pcm_format{
false, std::endian::little,
true, 3, 0, 23};
108 [[nodiscard]]
constexpr static pcm_format sint24_be() noexcept
110 return pcm_format{
false, std::endian::big,
true, 3, 0, 23};
113 [[nodiscard]]
constexpr static pcm_format sint16() noexcept
115 return pcm_format{
false, std::endian::native,
true, 2, 0, 15};
118 [[nodiscard]]
constexpr static pcm_format sint16_le() noexcept
120 return pcm_format{
false, std::endian::little,
true, 2, 0, 15};
123 [[nodiscard]]
constexpr static pcm_format sint16_be() noexcept
125 return pcm_format{
false, std::endian::big,
true, 2, 0, 15};
128 [[nodiscard]]
constexpr bool empty() const noexcept
130 return _num_minor_bits == 0;
133 constexpr explicit operator bool() const noexcept
138 [[nodiscard]]
constexpr bool floating_point() const noexcept
140 hi_axiom(not empty());
141 return _floating_point;
144 [[nodiscard]]
constexpr bool fixed_point() const noexcept
146 hi_axiom(not empty());
147 return not floating_point();
150 [[nodiscard]]
constexpr std::endian endian() const noexcept
152 hi_axiom(not empty());
153 return _little_endian ? std::endian::little : std::endian::big;
162 hi_axiom(not empty());
163 return narrow_cast<uint8_t>(_num_bytes + 1);
168 [[nodiscard]]
constexpr bool lsb() const noexcept
170 hi_axiom(not empty());
176 [[nodiscard]]
constexpr bool msb() const noexcept
178 hi_axiom(not empty());
192 hi_axiom(not empty());
193 return narrow_cast<uint8_t>(_num_major_bits + _num_minor_bits + 1);
207 hi_axiom(floating_point());
208 return _num_major_bits;
220 hi_axiom(floating_point());
221 return _num_minor_bits;
237 hi_axiom(fixed_point());
238 return _num_major_bits;
254 hi_axiom(fixed_point());
255 return _num_minor_bits;
260 if (rhs.floating_point()) {
261 if (rhs.endian() == std::endian::native) {
262 return std::format(
"float-{}", rhs.
num_bits());
264 return std::format(
"float-{}_{}", rhs.
num_bits(), rhs.endian() == std::endian::little ?
"le" :
"be");
268 if (rhs.endian() == std::endian::native) {
269 return std::format(
"int-{}", rhs.
num_bits());
271 return std::format(
"int-{}_{}", rhs.
num_bits(), rhs.endian() == std::endian::little ?
"le" :
"be");
275 if (rhs.endian() == std::endian::native) {
282 rhs.endian() == std::endian::little ?
"le" :
"be");
288 uint16_t _floating_point : 1 = 0;
289 uint16_t _little_endian : 1 = 0;
290 uint16_t _lsb : 1 = 0;
291 uint16_t _num_bytes : 3 = 0;
292 uint16_t _num_major_bits : 4 = 0;
293 uint16_t _num_minor_bits : 6 = 0;
300struct std::formatter<
hi::pcm_format, char> : std::formatter<std::string_view, char> {
303 return std::formatter<std::string_view, char>::format(
to_string(t), fc);
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition pcm_format.hpp:18
uint8_t num_exponent_bits() const noexcept
The number of bits in the exponent.
Definition pcm_format.hpp:205
constexpr bool msb() const noexcept
The sample is stored in the most-significant-bits of the storage.
Definition pcm_format.hpp:176
uint8_t num_bits() const noexcept
The number of least significant bits of the storage that is used by the sample.
Definition pcm_format.hpp:190
uint8_t num_fraction_bits() const noexcept
The number of fractional bits.
Definition pcm_format.hpp:252
constexpr bool lsb() const noexcept
The sample is stored in the least-significant-bits of the storage.
Definition pcm_format.hpp:168
uint8_t num_mantissa_bits() const noexcept
The number of bits in the mantissa.
Definition pcm_format.hpp:218
uint8_t num_bytes() const noexcept
The number of bytes a sample is stored in.
Definition pcm_format.hpp:160
constexpr pcm_format(bool floating_point, std::endian endian, bool lsb, uint8_t num_bytes, uint8_t num_major_bits, uint8_t num_minor_bits) noexcept
Construct a PCM format.
Definition pcm_format.hpp:64
uint8_t num_integral_bits() const noexcept
The number of integral bits.
Definition pcm_format.hpp:235