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 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
80 [[nodiscard]] constexpr audio_sample_format(
81 uint8_t num_bytes,
82 uint8_t num_guard_bits,
83 uint8_t num_bits,
84 bool is_float,
85 std::endian endian) noexcept :
86 num_bytes(num_bytes), num_guard_bits(num_guard_bits), num_bits(num_bits), is_float(is_float), endian(endian)
87 {
88 hi_axiom(holds_invariant());
89 }
90
91 [[nodiscard]] static constexpr audio_sample_format float32_le() noexcept
92 {
93 return {4, 8, 23, true, std::endian::little};
94 }
95
96 [[nodiscard]] static constexpr audio_sample_format float32_be() noexcept
97 {
98 return {4, 8, 23, true, std::endian::big};
99 }
100
101 [[nodiscard]] static constexpr audio_sample_format float32() noexcept
102 {
103 return {4, 8, 23, true, std::endian::native};
104 }
105
106 [[nodiscard]] static constexpr audio_sample_format int16_le() noexcept
107 {
108 return {2, 0, 15, false, std::endian::little};
109 }
110
111 [[nodiscard]] static constexpr audio_sample_format int16_be() noexcept
112 {
113 return {2, 0, 15, false, std::endian::big};
114 }
115
116 [[nodiscard]] static constexpr audio_sample_format int16() noexcept
117 {
118 return {2, 0, 15, false, std::endian::native};
119 }
120
121 [[nodiscard]] static constexpr audio_sample_format int20_le() noexcept
122 {
123 return {3, 0, 19, false, std::endian::little};
124 }
125
126 [[nodiscard]] static constexpr audio_sample_format int20_be() noexcept
127 {
128 return {3, 0, 19, false, std::endian::big};
129 }
130
131 [[nodiscard]] static constexpr audio_sample_format int20() noexcept
132 {
133 return {3, 0, 19, false, std::endian::native};
134 }
135
136 [[nodiscard]] static constexpr audio_sample_format int24_le() noexcept
137 {
138 return {3, 0, 23, false, std::endian::little};
139 }
140
141 [[nodiscard]] static constexpr audio_sample_format int24_be() noexcept
142 {
143 return {3, 0, 23, false, std::endian::big};
144 }
145
146 [[nodiscard]] static constexpr audio_sample_format int24() noexcept
147 {
148 return {3, 0, 23, false, std::endian::native};
149 }
150
151 [[nodiscard]] static constexpr audio_sample_format int32_le() noexcept
152 {
153 return {4, 0, 31, false, std::endian::little};
154 }
155
156 [[nodiscard]] static constexpr audio_sample_format int32_be() noexcept
157 {
158 return {4, 0, 31, false, std::endian::big};
159 }
160
161 [[nodiscard]] static constexpr audio_sample_format int32() noexcept
162 {
163 return {4, 0, 31, false, std::endian::native};
164 }
165
166 [[nodiscard]] static constexpr audio_sample_format fix8_23_le() noexcept
167 {
168 return {4, 8, 23, false, std::endian::little};
169 }
170
171 [[nodiscard]] static constexpr audio_sample_format fix8_23_be() noexcept
172 {
173 return {4, 8, 23, false, std::endian::big};
174 }
175
176 [[nodiscard]] static constexpr audio_sample_format fix8_23() noexcept
177 {
178 return {4, 8, 23, false, std::endian::native};
179 }
180
181 constexpr explicit operator bool() const noexcept
182 {
183 return num_bytes != 0;
184 }
185
188 [[nodiscard]] float pack_multiplier() const noexcept;
189
192 [[nodiscard]] float unpack_multiplier() const noexcept;
193
197 [[nodiscard]] std::size_t num_samples_per_chunk(std::size_t stride) const noexcept;
198
201 [[nodiscard]] std::size_t chunk_stride(std::size_t stride) const noexcept;
202
205 [[nodiscard]] std::size_t num_chunks_per_quad(std::size_t stride) const noexcept;
206
209 [[nodiscard]] std::size_t num_fast_quads(std::size_t stride, std::size_t num_samples) const noexcept;
210
213 [[nodiscard]] i8x16 load_shuffle_indices(std::size_t stride) const noexcept;
214
217 [[nodiscard]] i8x16 store_shuffle_indices(std::size_t stride) const noexcept;
218
221 [[nodiscard]] i8x16 concat_shuffle_indices(std::size_t stride) const noexcept;
222
225 [[nodiscard]] constexpr bool holds_invariant() const noexcept
226 {
227 return (num_bytes >= 1 && num_bytes <= 4) && (num_bits + num_guard_bits <= num_bytes * 8) &&
228 (endian == std::endian::little || endian == std::endian::big);
229 }
230};
231
232} // namespace hi::inline v1
This file includes required definitions.
STL namespace.
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