HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
audio_sample_format.hpp
1// Copyright Take Vos 2020.
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 "../required.hpp"
8#include "../assert.hpp"
9#include "../cast.hpp"
10#include "../rapid/numeric_array.hpp"
11#include <bit>
12
13namespace tt {
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 constexpr audio_sample_format(audio_sample_format const &) noexcept = default;
65 constexpr audio_sample_format(audio_sample_format &&) noexcept = default;
66 constexpr audio_sample_format &operator=(audio_sample_format const &) noexcept = default;
67 constexpr audio_sample_format &operator=(audio_sample_format &&) noexcept = default;
68
78 [[nodiscard]] constexpr audio_sample_format(
79 uint8_t num_bytes,
80 uint8_t num_guard_bits,
81 uint8_t num_bits,
82 bool is_float,
83 std::endian endian) noexcept :
85 {
86 tt_axiom(holds_invariant());
87 }
88
89 [[nodiscard]] static constexpr audio_sample_format float32_le() noexcept
90 {
91 return {4, 8, 23, true, std::endian::little};
92 }
93
94 [[nodiscard]] static constexpr audio_sample_format float32_be() noexcept
95 {
96 return {4, 8, 23, true, std::endian::big};
97 }
98
99 [[nodiscard]] static constexpr audio_sample_format float32() noexcept
100 {
101 return {4, 8, 23, true, std::endian::native};
102 }
103
104 [[nodiscard]] static constexpr audio_sample_format int16_le() noexcept
105 {
106 return {2, 0, 15, false, std::endian::little};
107 }
108
109 [[nodiscard]] static constexpr audio_sample_format int16_be() noexcept
110 {
111 return {2, 0, 15, false, std::endian::big};
112 }
113
114 [[nodiscard]] static constexpr audio_sample_format int16() noexcept
115 {
116 return {2, 0, 15, false, std::endian::native};
117 }
118
119 [[nodiscard]] static constexpr audio_sample_format int20_le() noexcept
120 {
121 return {3, 0, 19, false, std::endian::little};
122 }
123
124 [[nodiscard]] static constexpr audio_sample_format int20_be() noexcept
125 {
126 return {3, 0, 19, false, std::endian::big};
127 }
128
129 [[nodiscard]] static constexpr audio_sample_format int20() noexcept
130 {
131 return {3, 0, 19, false, std::endian::native};
132 }
133
134 [[nodiscard]] static constexpr audio_sample_format int24_le() noexcept
135 {
136 return {3, 0, 23, false, std::endian::little};
137 }
138
139 [[nodiscard]] static constexpr audio_sample_format int24_be() noexcept
140 {
141 return {3, 0, 23, false, std::endian::big};
142 }
143
144 [[nodiscard]] static constexpr audio_sample_format int24() noexcept
145 {
146 return {3, 0, 23, false, std::endian::native};
147 }
148
149 [[nodiscard]] static constexpr audio_sample_format int32_le() noexcept
150 {
151 return {4, 0, 31, false, std::endian::little};
152 }
153
154 [[nodiscard]] static constexpr audio_sample_format int32_be() noexcept
155 {
156 return {4, 0, 31, false, std::endian::big};
157 }
158
159 [[nodiscard]] static constexpr audio_sample_format int32() noexcept
160 {
161 return {4, 0, 31, false, std::endian::native};
162 }
163
164 [[nodiscard]] static constexpr audio_sample_format fix8_23_le() noexcept
165 {
166 return {4, 8, 23, false, std::endian::little};
167 }
168
169 [[nodiscard]] static constexpr audio_sample_format fix8_23_be() noexcept
170 {
171 return {4, 8, 23, false, std::endian::big};
172 }
173
174 [[nodiscard]] static constexpr audio_sample_format fix8_23() noexcept
175 {
176 return {4, 8, 23, false, std::endian::native};
177 }
178
179 constexpr explicit operator bool () const noexcept
180 {
181 return num_bytes != 0;
182 }
183
186 [[nodiscard]] float pack_multiplier() const noexcept;
187
190 [[nodiscard]] float unpack_multiplier() const noexcept;
191
195 [[nodiscard]] size_t num_samples_per_chunk(size_t stride) const noexcept;
196
199 [[nodiscard]] size_t chunk_stride(size_t stride) const noexcept;
200
203 [[nodiscard]] size_t num_chunks_per_quad(size_t stride) const noexcept;
204
207 [[nodiscard]] size_t num_fast_quads(size_t stride, size_t num_samples) const noexcept;
208
211 [[nodiscard]] i8x16 load_shuffle_indices(size_t stride) const noexcept;
212
215 [[nodiscard]] i8x16 store_shuffle_indices(size_t stride) const noexcept;
216
219 [[nodiscard]] i8x16 concat_shuffle_indices(size_t stride) const noexcept;
220
223 [[nodiscard]] constexpr bool holds_invariant() const noexcept
224 {
225 return (num_bytes >= 1 && num_bytes <= 4) && (num_bits + num_guard_bits <= num_bytes * 8) &&
226 (endian == std::endian::little || endian == std::endian::big);
227 }
228};
229
230} // namespace tt
STL namespace.
Audio sample format.
Definition audio_sample_format.hpp:29
std::endian endian
The endian order of the bytes in the container.
Definition audio_sample_format.hpp:59
i8x16 store_shuffle_indices(size_t stride) const noexcept
Return a shuffle indices for storing 32 bit samples into packed samples.
bool is_float
The numeric type is floating point.
Definition audio_sample_format.hpp:55
i8x16 load_shuffle_indices(size_t stride) const noexcept
Return a shuffle indices for loading samples into 32 bit integers.
constexpr bool holds_invariant() const noexcept
Is the audio sample format valid.
Definition audio_sample_format.hpp:223
size_t num_fast_quads(size_t stride, size_t num_samples) const noexcept
Calculate the number of 4 sample-quads can be handled as chunked loads and stores.
float unpack_multiplier() const noexcept
How much to multiply integer samples to create float samples.
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
size_t chunk_stride(size_t stride) const noexcept
The number of bytes to advance to the next chunk to be loaded or stored.
float pack_multiplier() const noexcept
How much to multiply float samples to create integer samples.
uint8_t num_bytes
The number of bytes of the container.
Definition audio_sample_format.hpp:33
size_t num_chunks_per_quad(size_t stride) const noexcept
The number of chunks to load or store to handle 4 samples.
i8x16 concat_shuffle_indices(size_t stride) const noexcept
Return a shuffle indices to shift previous loaded samples for concatenation.
size_t num_samples_per_chunk(size_t stride) const noexcept
The number of packed samples that are handled in a single 128 bit load or store.
uint8_t num_bits
The number of significant bits of the sample format.
Definition audio_sample_format.hpp:50