HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
audio_sample_format.hpp
1// Copyright Take Vos 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../utility.hpp"
8#include "../assert.hpp"
9#include "../cast.hpp"
10#include "../rapid/numeric_array.hpp"
11#include <bit>
12
13namespace hi::inline v1 {
14
33 uint8_t num_bytes;
34
39
50 uint8_t num_bits;
51
56
59 std::endian endian;
60
61 constexpr audio_sample_format() noexcept :
62 num_bytes(0), num_guard_bits(0), num_bits(0), is_float(false), endian(std::endian::native)
63 {
64 }
65
66 constexpr audio_sample_format(audio_sample_format const &) noexcept = default;
67 constexpr audio_sample_format(audio_sample_format &&) noexcept = default;
68 constexpr audio_sample_format &operator=(audio_sample_format const &) noexcept = default;
69 constexpr audio_sample_format &operator=(audio_sample_format &&) noexcept = default;
70
79 [[nodiscard]] constexpr audio_sample_format(
80 uint8_t num_bytes,
81 uint8_t num_guard_bits,
82 uint8_t num_bits,
83 bool is_float,
84 std::endian endian) noexcept :
85 num_bytes(num_bytes), num_guard_bits(num_guard_bits), num_bits(num_bits), is_float(is_float), endian(endian)
86 {
87 hi_axiom(holds_invariant());
88 }
89
90 [[nodiscard]] static constexpr audio_sample_format float32_le() noexcept
91 {
92 return {4, 8, 23, true, std::endian::little};
93 }
94
95 [[nodiscard]] static constexpr audio_sample_format float32_be() noexcept
96 {
97 return {4, 8, 23, true, std::endian::big};
98 }
99
100 [[nodiscard]] static constexpr audio_sample_format float32() noexcept
101 {
102 return {4, 8, 23, true, std::endian::native};
103 }
104
105 [[nodiscard]] static constexpr audio_sample_format int16_le() noexcept
106 {
107 return {2, 0, 15, false, std::endian::little};
108 }
109
110 [[nodiscard]] static constexpr audio_sample_format int16_be() noexcept
111 {
112 return {2, 0, 15, false, std::endian::big};
113 }
114
115 [[nodiscard]] static constexpr audio_sample_format int16() noexcept
116 {
117 return {2, 0, 15, false, std::endian::native};
118 }
119
120 [[nodiscard]] static constexpr audio_sample_format int20_le() noexcept
121 {
122 return {3, 0, 19, false, std::endian::little};
123 }
124
125 [[nodiscard]] static constexpr audio_sample_format int20_be() noexcept
126 {
127 return {3, 0, 19, false, std::endian::big};
128 }
129
130 [[nodiscard]] static constexpr audio_sample_format int20() noexcept
131 {
132 return {3, 0, 19, false, std::endian::native};
133 }
134
135 [[nodiscard]] static constexpr audio_sample_format int24_le() noexcept
136 {
137 return {3, 0, 23, false, std::endian::little};
138 }
139
140 [[nodiscard]] static constexpr audio_sample_format int24_be() noexcept
141 {
142 return {3, 0, 23, false, std::endian::big};
143 }
144
145 [[nodiscard]] static constexpr audio_sample_format int24() noexcept
146 {
147 return {3, 0, 23, false, std::endian::native};
148 }
149
150 [[nodiscard]] static constexpr audio_sample_format int32_le() noexcept
151 {
152 return {4, 0, 31, false, std::endian::little};
153 }
154
155 [[nodiscard]] static constexpr audio_sample_format int32_be() noexcept
156 {
157 return {4, 0, 31, false, std::endian::big};
158 }
159
160 [[nodiscard]] static constexpr audio_sample_format int32() noexcept
161 {
162 return {4, 0, 31, false, std::endian::native};
163 }
164
165 [[nodiscard]] static constexpr audio_sample_format fix8_23_le() noexcept
166 {
167 return {4, 8, 23, false, std::endian::little};
168 }
169
170 [[nodiscard]] static constexpr audio_sample_format fix8_23_be() noexcept
171 {
172 return {4, 8, 23, false, std::endian::big};
173 }
174
175 [[nodiscard]] static constexpr audio_sample_format fix8_23() noexcept
176 {
177 return {4, 8, 23, false, std::endian::native};
178 }
179
180 constexpr explicit operator bool() const noexcept
181 {
182 return num_bytes != 0;
183 }
184
187 [[nodiscard]] float pack_multiplier() const noexcept;
188
191 [[nodiscard]] float unpack_multiplier() const noexcept;
192
196 [[nodiscard]] std::size_t num_samples_per_chunk(std::size_t stride) const noexcept;
197
200 [[nodiscard]] std::size_t chunk_stride(std::size_t stride) const noexcept;
201
204 [[nodiscard]] std::size_t num_chunks_per_quad(std::size_t stride) const noexcept;
205
208 [[nodiscard]] std::size_t num_fast_quads(std::size_t stride, std::size_t num_samples) const noexcept;
209
212 [[nodiscard]] i8x16 load_shuffle_indices(std::size_t stride) const noexcept;
213
216 [[nodiscard]] i8x16 store_shuffle_indices(std::size_t stride) const noexcept;
217
220 [[nodiscard]] i8x16 concat_shuffle_indices(std::size_t stride) const noexcept;
221
224 [[nodiscard]] constexpr bool holds_invariant() const noexcept
225 {
226 return (num_bytes >= 1 && num_bytes <= 4) && (num_bits + num_guard_bits <= num_bytes * 8) &&
227 (endian == std::endian::little || endian == std::endian::big);
228 }
229};
230
231} // namespace hi::inline v1
Utilities to assert and bound check.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
Utilities used by the HikoGUI library itself.
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:15
Audio sample format.
Definition audio_sample_format.hpp:29
bool is_float
The numeric type is floating point.
Definition audio_sample_format.hpp:55
uint8_t num_guard_bits
The number of bits used for the integer part of a fixed point number.
Definition audio_sample_format.hpp:38
uint8_t num_bits
The number of significant bits of the sample format.
Definition audio_sample_format.hpp:50
float pack_multiplier() const noexcept
How much to multiply float samples to create integer samples.
std::endian endian
The endian order of the bytes in the container.
Definition audio_sample_format.hpp:59
uint8_t num_bytes
The number of bytes of the container.
Definition audio_sample_format.hpp:33