aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/src/openmpt/soundbase/SampleClip.hpp
blob: 733d61decaf1a5b5634e2e5e845ec186ae8f9389 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* SPDX-License-Identifier: BSD-3-Clause */
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */


#pragma once

#include "openmpt/all/BuildSettings.hpp"

#include "mpt/base/macros.hpp"
#include "openmpt/base/Int24.hpp"
#include "openmpt/base/Types.hpp"


OPENMPT_NAMESPACE_BEGIN


namespace SC
{  // SC = _S_ample_C_onversion


template <typename Tsample, bool clipOutput>
struct Clip;

template <bool clipOutput>
struct Clip<uint8, clipOutput>
{
	using input_t = uint8;
	using output_t = uint8;
	MPT_FORCEINLINE uint8 operator()(uint8 val)
	{
		return val;
	}
};

template <bool clipOutput>
struct Clip<int8, clipOutput>
{
	using input_t = int8;
	using output_t = int8;
	MPT_FORCEINLINE int8 operator()(int8 val)
	{
		return val;
	}
};

template <bool clipOutput>
struct Clip<int16, clipOutput>
{
	using input_t = int16;
	using output_t = int16;
	MPT_FORCEINLINE int16 operator()(int16 val)
	{
		return val;
	}
};

template <bool clipOutput>
struct Clip<int24, clipOutput>
{
	using input_t = int24;
	using output_t = int24;
	MPT_FORCEINLINE int24 operator()(int24 val)
	{
		return val;
	}
};

template <bool clipOutput>
struct Clip<int32, clipOutput>
{
	using input_t = int32;
	using output_t = int32;
	MPT_FORCEINLINE int32 operator()(int32 val)
	{
		return val;
	}
};

template <bool clipOutput>
struct Clip<int64, clipOutput>
{
	using input_t = int64;
	using output_t = int64;
	MPT_FORCEINLINE int64 operator()(int64 val)
	{
		return val;
	}
};

template <bool clipOutput>
struct Clip<float, clipOutput>
{
	using input_t = float;
	using output_t = float;
	MPT_FORCEINLINE float operator()(float val)
	{
		if constexpr(clipOutput)
		{
			if(val < -1.0f) val = -1.0f;
			if(val > 1.0f) val = 1.0f;
			return val;
		} else
		{
			return val;
		}
	}
};

template <bool clipOutput>
struct Clip<double, clipOutput>
{
	using input_t = double;
	using output_t = double;
	MPT_FORCEINLINE double operator()(double val)
	{
		if constexpr(clipOutput)
		{
			if(val < -1.0) val = -1.0;
			if(val > 1.0) val = 1.0;
			return val;
		} else
		{
			return val;
		}
	}
};


}  // namespace SC


OPENMPT_NAMESPACE_END