| 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 "openmpt/base/Int24.hpp" |
| 11 |
#include "openmpt/base/Types.hpp" |
| 12 |
|
| 13 |
|
| 14 |
OPENMPT_NAMESPACE_BEGIN |
| 15 |
|
| 16 |
|
| 17 |
namespace SC |
| 18 |
{ // SC = _S_ample_C_onversion |
| 19 |
|
| 20 |
|
| 21 |
template <typename Tsample, bool clipOutput> |
| 22 |
struct Clip; |
| 23 |
|
| 24 |
template <bool clipOutput> |
| 25 |
struct Clip<uint8, clipOutput> |
| 26 |
{ |
| 27 |
using input_t = uint8; |
| 28 |
using output_t = uint8; |
| 29 |
MPT_FORCEINLINE uint8 operator()(uint8 val) |
| 30 |
{ |
| 31 |
return val; |
| 32 |
} |
| 33 |
}; |
| 34 |
|
| 35 |
template <bool clipOutput> |
| 36 |
struct Clip<int8, clipOutput> |
| 37 |
{ |
| 38 |
using input_t = int8; |
| 39 |
using output_t = int8; |
| 40 |
MPT_FORCEINLINE int8 operator()(int8 val) |
| 41 |
{ |
| 42 |
return val; |
| 43 |
} |
| 44 |
}; |
| 45 |
|
| 46 |
template <bool clipOutput> |
| 47 |
struct Clip<int16, clipOutput> |
| 48 |
{ |
| 49 |
using input_t = int16; |
| 50 |
using output_t = int16; |
| 51 |
MPT_FORCEINLINE int16 operator()(int16 val) |
| 52 |
{ |
| 53 |
return val; |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
template <bool clipOutput> |
| 58 |
struct Clip<int24, clipOutput> |
| 59 |
{ |
| 60 |
using input_t = int24; |
| 61 |
using output_t = int24; |
| 62 |
MPT_FORCEINLINE int24 operator()(int24 val) |
| 63 |
{ |
| 64 |
return val; |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
template <bool clipOutput> |
| 69 |
struct Clip<int32, clipOutput> |
| 70 |
{ |
| 71 |
using input_t = int32; |
| 72 |
using output_t = int32; |
| 73 |
MPT_FORCEINLINE int32 operator()(int32 val) |
| 74 |
{ |
| 75 |
return val; |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
template <bool clipOutput> |
| 80 |
struct Clip<int64, clipOutput> |
| 81 |
{ |
| 82 |
using input_t = int64; |
| 83 |
using output_t = int64; |
| 84 |
MPT_FORCEINLINE int64 operator()(int64 val) |
| 85 |
{ |
| 86 |
return val; |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
template <bool clipOutput> |
| 91 |
struct Clip<float, clipOutput> |
| 92 |
{ |
| 93 |
using input_t = float; |
| 94 |
using output_t = float; |
| 95 |
MPT_FORCEINLINE float operator()(float val) |
| 96 |
{ |
| 97 |
if constexpr(clipOutput) |
| 98 |
{ |
| 99 |
if(val < -1.0f) val = -1.0f; |
| 100 |
if(val > 1.0f) val = 1.0f; |
| 101 |
return val; |
| 102 |
} else |
| 103 |
{ |
| 104 |
return val; |
| 105 |
} |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
template <bool clipOutput> |
| 110 |
struct Clip<double, clipOutput> |
| 111 |
{ |
| 112 |
using input_t = double; |
| 113 |
using output_t = double; |
| 114 |
MPT_FORCEINLINE double operator()(double val) |
| 115 |
{ |
| 116 |
if constexpr(clipOutput) |
| 117 |
{ |
| 118 |
if(val < -1.0) val = -1.0; |
| 119 |
if(val > 1.0) val = 1.0; |
| 120 |
return val; |
| 121 |
} else |
| 122 |
{ |
| 123 |
return val; |
| 124 |
} |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
} // namespace SC |
| 130 |
|
| 131 |
|
| 132 |
OPENMPT_NAMESPACE_END |