| 1 |
/* SPDX-License-Identifier: BSD-3-Clause */ |
| 2 |
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */ |
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include "openmpt/all/BuildSettings.hpp" |
| 8 |
|
| 9 |
#include "mpt/base/macros.hpp" |
| 10 |
#include "mpt/base/utility.hpp" |
| 11 |
#include "openmpt/base/Int24.hpp" |
| 12 |
#include "openmpt/base/Types.hpp" |
| 13 |
|
| 14 |
#include <type_traits> |
| 15 |
|
| 16 |
|
| 17 |
OPENMPT_NAMESPACE_BEGIN |
| 18 |
|
| 19 |
|
| 20 |
class SampleFormat |
| 21 |
{ |
| 22 |
public: |
| 23 |
enum class Enum : uint8 |
| 24 |
{ |
| 25 |
Unsigned8 = 9, // do not change value (for compatibility with old configuration settings) |
| 26 |
Int8 = 8, // do not change value (for compatibility with old configuration settings) |
| 27 |
Int16 = 16, // do not change value (for compatibility with old configuration settings) |
| 28 |
Int24 = 24, // do not change value (for compatibility with old configuration settings) |
| 29 |
Int32 = 32, // do not change value (for compatibility with old configuration settings) |
| 30 |
Float32 = 32 + 128, // do not change value (for compatibility with old configuration settings) |
| 31 |
Float64 = 64 + 128, // do not change value (for compatibility with old configuration settings) |
| 32 |
Default = Float32 |
| 33 |
}; |
| 34 |
static constexpr SampleFormat::Enum Unsigned8 = SampleFormat::Enum::Unsigned8; |
| 35 |
static constexpr SampleFormat::Enum Int8 = SampleFormat::Enum::Int8; |
| 36 |
static constexpr SampleFormat::Enum Int16 = SampleFormat::Enum::Int16; |
| 37 |
static constexpr SampleFormat::Enum Int24 = SampleFormat::Enum::Int24; |
| 38 |
static constexpr SampleFormat::Enum Int32 = SampleFormat::Enum::Int32; |
| 39 |
static constexpr SampleFormat::Enum Float32 = SampleFormat::Enum::Float32; |
| 40 |
static constexpr SampleFormat::Enum Float64 = SampleFormat::Enum::Float64; |
| 41 |
static constexpr SampleFormat::Enum Default = SampleFormat::Enum::Default; |
| 42 |
|
| 43 |
private: |
| 44 |
SampleFormat::Enum value; |
| 45 |
|
| 46 |
template <typename T> |
| 47 |
static MPT_CONSTEXPRINLINE SampleFormat::Enum Sanitize(T x) noexcept |
| 48 |
{ |
| 49 |
using uT = typename std::make_unsigned<T>::type; |
| 50 |
uT val = static_cast<uT>(x); |
| 51 |
if(!val) |
| 52 |
{ |
| 53 |
return SampleFormat::Enum::Default; |
| 54 |
} |
| 55 |
if(val == static_cast<uT>(-8)) |
| 56 |
{ |
| 57 |
val = 8 + 1; |
| 58 |
} |
| 59 |
// float|64|32|16|8|?|?|unsigned |
| 60 |
val &= 0b1'1111'00'1; |
| 61 |
auto is_float = [](uT val) -> bool |
| 62 |
{ |
| 63 |
return (val & 0b1'0000'00'0) ? true : false; |
| 64 |
}; |
| 65 |
auto is_unsigned = [](uT val) -> bool |
| 66 |
{ |
| 67 |
return (val & 0b0'0000'00'1) ? true : false; |
| 68 |
}; |
| 69 |
auto has_size = [](uT val) -> bool |
| 70 |
{ |
| 71 |
return (val & 0b0'1111'00'0) ? true : false; |
| 72 |
}; |
| 73 |
auto unique_size = [](uT val) -> bool |
| 74 |
{ |
| 75 |
val &= 0b0'1111'00'0; |
| 76 |
if(val == 0b0'0001'00'0) |
| 77 |
{ |
| 78 |
return true; |
| 79 |
} else if(val == 0b0'0010'00'0) |
| 80 |
{ |
| 81 |
return true; |
| 82 |
} else if(val == 0b0'0011'00'0) |
| 83 |
{ |
| 84 |
return true; |
| 85 |
} else if(val == 0b0'0100'00'0) |
| 86 |
{ |
| 87 |
return true; |
| 88 |
} else if(val == 0b0'1000'00'0) |
| 89 |
{ |
| 90 |
return true; |
| 91 |
} else |
| 92 |
{ |
| 93 |
return false; |
| 94 |
} |
| 95 |
}; |
| 96 |
auto get_size = [](uT val) -> std::size_t |
| 97 |
{ |
| 98 |
val &= 0b0'1111'00'0; |
| 99 |
if(val == 0b0'0001'00'0) |
| 100 |
{ |
| 101 |
return 1; |
| 102 |
} else if(val == 0b0'0010'00'0) |
| 103 |
{ |
| 104 |
return 2; |
| 105 |
} else if(val == 0b0'0011'00'0) |
| 106 |
{ |
| 107 |
return 3; |
| 108 |
} else if(val == 0b0'0100'00'0) |
| 109 |
{ |
| 110 |
return 4; |
| 111 |
} else if(val == 0b0'1000'00'0) |
| 112 |
{ |
| 113 |
return 8; |
| 114 |
} else |
| 115 |
{ |
| 116 |
return 2; // default to 16bit |
| 117 |
} |
| 118 |
}; |
| 119 |
if(!has_size(val)) |
| 120 |
{ |
| 121 |
if(is_unsigned(val) && is_float(val)) |
| 122 |
{ |
| 123 |
val = mpt::to_underlying(Enum::Default); |
| 124 |
} else if(is_unsigned(val)) |
| 125 |
{ |
| 126 |
val = mpt::to_underlying(Enum::Unsigned8); |
| 127 |
} else if(is_float(val)) |
| 128 |
{ |
| 129 |
val = mpt::to_underlying(Enum::Float32); |
| 130 |
} else |
| 131 |
{ |
| 132 |
val = mpt::to_underlying(Enum::Default); |
| 133 |
} |
| 134 |
} else if(!unique_size(val)) |
| 135 |
{ |
| 136 |
// order of size check matters |
| 137 |
if((val & 0b0'0011'00'0) == 0b0'0011'00'0) |
| 138 |
{ |
| 139 |
val = mpt::to_underlying(Enum::Int24); |
| 140 |
} else if(val & 0b0'0010'00'0) |
| 141 |
{ |
| 142 |
val = mpt::to_underlying(Enum::Int16); |
| 143 |
} else if(val & 0b0'0100'00'0) |
| 144 |
{ |
| 145 |
if(is_float(val)) |
| 146 |
{ |
| 147 |
val = mpt::to_underlying(Enum::Float32); |
| 148 |
} else |
| 149 |
{ |
| 150 |
val = mpt::to_underlying(Enum::Int32); |
| 151 |
} |
| 152 |
} else if(val & 0b0'1000'00'0) |
| 153 |
{ |
| 154 |
val = mpt::to_underlying(Enum::Float64); |
| 155 |
} else if(val & 0b0'0001'00'0) |
| 156 |
{ |
| 157 |
if(is_unsigned(val)) |
| 158 |
{ |
| 159 |
val = mpt::to_underlying(Enum::Unsigned8); |
| 160 |
} else |
| 161 |
{ |
| 162 |
val = mpt::to_underlying(Enum::Int8); |
| 163 |
} |
| 164 |
} |
| 165 |
} else |
| 166 |
{ |
| 167 |
if(is_unsigned(val) && (get_size(val) > 1)) |
| 168 |
{ |
| 169 |
val &= ~0b0'0000'00'1; // remove unsigned |
| 170 |
} |
| 171 |
if(is_float(val) && (get_size(val) < 4)) |
| 172 |
{ |
| 173 |
val &= ~0b1'0000'00'0; // remove float |
| 174 |
} |
| 175 |
if(!is_float(val) && (get_size(val) == 8)) |
| 176 |
{ |
| 177 |
val |= 0b1'0000'00'0; // add float |
| 178 |
} |
| 179 |
} |
| 180 |
return static_cast<SampleFormat::Enum>(val); |
| 181 |
} |
| 182 |
|
| 183 |
public: |
| 184 |
MPT_CONSTEXPRINLINE SampleFormat() noexcept |
| 185 |
: value(SampleFormat::Default) |
| 186 |
{ |
| 187 |
} |
| 188 |
|
| 189 |
MPT_CONSTEXPRINLINE SampleFormat(SampleFormat::Enum v) noexcept |
| 190 |
: value(Sanitize(v)) |
| 191 |
{ |
| 192 |
} |
| 193 |
|
| 194 |
friend MPT_CONSTEXPRINLINE bool operator==(const SampleFormat &a, const SampleFormat &b) noexcept |
| 195 |
{ |
| 196 |
return a.value == b.value; |
| 197 |
} |
| 198 |
friend MPT_CONSTEXPRINLINE bool operator!=(const SampleFormat &a, const SampleFormat &b) noexcept |
| 199 |
{ |
| 200 |
return a.value != b.value; |
| 201 |
} |
| 202 |
friend MPT_CONSTEXPRINLINE bool operator==(const SampleFormat::Enum &a, const SampleFormat &b) noexcept |
| 203 |
{ |
| 204 |
return a == b.value; |
| 205 |
} |
| 206 |
friend MPT_CONSTEXPRINLINE bool operator!=(const SampleFormat::Enum &a, const SampleFormat &b) noexcept |
| 207 |
{ |
| 208 |
return a != b.value; |
| 209 |
} |
| 210 |
friend MPT_CONSTEXPRINLINE bool operator==(const SampleFormat &a, const SampleFormat::Enum &b) noexcept |
| 211 |
{ |
| 212 |
return a.value == b; |
| 213 |
} |
| 214 |
friend MPT_CONSTEXPRINLINE bool operator!=(const SampleFormat &a, const SampleFormat::Enum &b) noexcept |
| 215 |
{ |
| 216 |
return a.value != b; |
| 217 |
} |
| 218 |
|
| 219 |
MPT_CONSTEXPRINLINE bool IsUnsigned() const noexcept |
| 220 |
{ |
| 221 |
return false |
| 222 |
|| (value == SampleFormat::Unsigned8); |
| 223 |
} |
| 224 |
MPT_CONSTEXPRINLINE bool IsFloat() const noexcept |
| 225 |
{ |
| 226 |
return false |
| 227 |
|| (value == SampleFormat::Float32) |
| 228 |
|| (value == SampleFormat::Float64); |
| 229 |
} |
| 230 |
MPT_CONSTEXPRINLINE bool IsInt() const noexcept |
| 231 |
{ |
| 232 |
return false |
| 233 |
|| (value == SampleFormat::Unsigned8) |
| 234 |
|| (value == SampleFormat::Int8) |
| 235 |
|| (value == SampleFormat::Int16) |
| 236 |
|| (value == SampleFormat::Int24) |
| 237 |
|| (value == SampleFormat::Int32); |
| 238 |
} |
| 239 |
MPT_CONSTEXPRINLINE uint8 GetSampleSize() const noexcept |
| 240 |
{ |
| 241 |
return false ? 0 |
| 242 |
: (value == SampleFormat::Unsigned8) ? 1 |
| 243 |
: (value == SampleFormat::Int8) ? 1 |
| 244 |
: (value == SampleFormat::Int16) ? 2 |
| 245 |
: (value == SampleFormat::Int24) ? 3 |
| 246 |
: (value == SampleFormat::Int32) ? 4 |
| 247 |
: (value == SampleFormat::Float32) ? 4 |
| 248 |
: (value == SampleFormat::Float64) ? 8 |
| 249 |
: 0; |
| 250 |
} |
| 251 |
MPT_CONSTEXPRINLINE uint8 GetBitsPerSample() const noexcept |
| 252 |
{ |
| 253 |
return false ? 0 |
| 254 |
: (value == SampleFormat::Unsigned8) ? 8 |
| 255 |
: (value == SampleFormat::Int8) ? 8 |
| 256 |
: (value == SampleFormat::Int16) ? 16 |
| 257 |
: (value == SampleFormat::Int24) ? 24 |
| 258 |
: (value == SampleFormat::Int32) ? 32 |
| 259 |
: (value == SampleFormat::Float32) ? 32 |
| 260 |
: (value == SampleFormat::Float64) ? 64 |
| 261 |
: 0; |
| 262 |
} |
| 263 |
|
| 264 |
MPT_CONSTEXPRINLINE operator SampleFormat::Enum() const noexcept |
| 265 |
{ |
| 266 |
return value; |
| 267 |
} |
| 268 |
|
| 269 |
// backward compatibility, conversion to/from integers |
| 270 |
static MPT_CONSTEXPRINLINE SampleFormat FromInt(int x) noexcept |
| 271 |
{ |
| 272 |
return SampleFormat(Sanitize(x)); |
| 273 |
} |
| 274 |
static MPT_CONSTEXPRINLINE int ToInt(SampleFormat x) noexcept |
| 275 |
{ |
| 276 |
return mpt::to_underlying(x.value); |
| 277 |
} |
| 278 |
}; |
| 279 |
|
| 280 |
|
| 281 |
template <typename Container> |
| 282 |
Container AllSampleFormats() |
| 283 |
{ |
| 284 |
return {SampleFormat::Float64, SampleFormat::Float32, SampleFormat::Int32, SampleFormat::Int24, SampleFormat::Int16, SampleFormat::Int8, SampleFormat::Unsigned8}; |
| 285 |
} |
| 286 |
|
| 287 |
template <typename Container> |
| 288 |
Container DefaultSampleFormats() |
| 289 |
{ |
| 290 |
return {SampleFormat::Float32, SampleFormat::Int32, SampleFormat::Int24, SampleFormat::Int16, SampleFormat::Int8}; |
| 291 |
} |
| 292 |
|
| 293 |
template <typename Tsample> |
| 294 |
struct SampleFormatTraits; |
| 295 |
template <> |
| 296 |
struct SampleFormatTraits<uint8> |
| 297 |
{ |
| 298 |
static MPT_CONSTEXPRINLINE SampleFormat sampleFormat() { return SampleFormat::Unsigned8; } |
| 299 |
}; |
| 300 |
template <> |
| 301 |
struct SampleFormatTraits<int8> |
| 302 |
{ |
| 303 |
static MPT_CONSTEXPRINLINE SampleFormat sampleFormat() { return SampleFormat::Int8; } |
| 304 |
}; |
| 305 |
template <> |
| 306 |
struct SampleFormatTraits<int16> |
| 307 |
{ |
| 308 |
static MPT_CONSTEXPRINLINE SampleFormat sampleFormat() { return SampleFormat::Int16; } |
| 309 |
}; |
| 310 |
template <> |
| 311 |
struct SampleFormatTraits<int24> |
| 312 |
{ |
| 313 |
static MPT_CONSTEXPRINLINE SampleFormat sampleFormat() { return SampleFormat::Int24; } |
| 314 |
}; |
| 315 |
template <> |
| 316 |
struct SampleFormatTraits<int32> |
| 317 |
{ |
| 318 |
static MPT_CONSTEXPRINLINE SampleFormat sampleFormat() { return SampleFormat::Int32; } |
| 319 |
}; |
| 320 |
template <> |
| 321 |
struct SampleFormatTraits<float> |
| 322 |
{ |
| 323 |
static MPT_CONSTEXPRINLINE SampleFormat sampleFormat() { return SampleFormat::Float32; } |
| 324 |
}; |
| 325 |
template <> |
| 326 |
struct SampleFormatTraits<double> |
| 327 |
{ |
| 328 |
static MPT_CONSTEXPRINLINE SampleFormat sampleFormat() { return SampleFormat::Float64; } |
| 329 |
}; |
| 330 |
|
| 331 |
template <SampleFormat::Enum sampleFormat> |
| 332 |
struct SampleFormatToType; |
| 333 |
template <> |
| 334 |
struct SampleFormatToType<SampleFormat::Unsigned8> |
| 335 |
{ |
| 336 |
typedef uint8 type; |
| 337 |
}; |
| 338 |
template <> |
| 339 |
struct SampleFormatToType<SampleFormat::Int8> |
| 340 |
{ |
| 341 |
typedef int8 type; |
| 342 |
}; |
| 343 |
template <> |
| 344 |
struct SampleFormatToType<SampleFormat::Int16> |
| 345 |
{ |
| 346 |
typedef int16 type; |
| 347 |
}; |
| 348 |
template <> |
| 349 |
struct SampleFormatToType<SampleFormat::Int24> |
| 350 |
{ |
| 351 |
typedef int24 type; |
| 352 |
}; |
| 353 |
template <> |
| 354 |
struct SampleFormatToType<SampleFormat::Int32> |
| 355 |
{ |
| 356 |
typedef int32 type; |
| 357 |
}; |
| 358 |
template <> |
| 359 |
struct SampleFormatToType<SampleFormat::Float32> |
| 360 |
{ |
| 361 |
typedef float type; |
| 362 |
}; |
| 363 |
template <> |
| 364 |
struct SampleFormatToType<SampleFormat::Float64> |
| 365 |
{ |
| 366 |
typedef double type; |
| 367 |
}; |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
OPENMPT_NAMESPACE_END |