-
Notifications
You must be signed in to change notification settings - Fork 403
/
Copy pathbasic_dsp.h
93 lines (83 loc) · 2.54 KB
/
basic_dsp.h
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
/*
* Surge XT - a free and open source hybrid synthesizer,
* built by Surge Synth Team
*
* Learn more at https://surge-synthesizer.github.io/
*
* Copyright 2018-2024, various authors, as described in the GitHub
* transaction log.
*
* Surge XT is released under the GNU General Public Licence v3
* or later (GPL-3.0-or-later). The license is found in the "LICENSE"
* file in the root of this repository, or at
* https://www.gnu.org/licenses/gpl-3.0.en.html
*
* Surge was a commercial product from 2004-2018, copyright and ownership
* held by Claes Johanson at Vember Audio during that period.
* Claes made Surge open source in September 2018.
*
* All source for Surge XT is available at
* https://github.com/surge-synthesizer/surge
*/
#ifndef SURGE_SRC_COMMON_DSP_VEMBERTECH_BASIC_DSP_H
#define SURGE_SRC_COMMON_DSP_VEMBERTECH_BASIC_DSP_H
#include <cstring>
#include <algorithm>
#include <cassert>
template <typename T> inline T limit_range(const T &x, const T &low, const T &high)
{
assert(low <= high);
return std::clamp(x, low, high);
}
template <typename T> inline T limit01(const T &x) { return limit_range(x, (T)0, (T)1); }
template <typename T> inline T clamp01(const T &x) { return limit_range(x, (T)0, (T)1); }
template <typename T> inline T limitpm1(const T &x) { return limit_range(x, (T)-1, (T)1); }
template <typename T> inline T clamp1bp(const T &x) { return limit_range(x, (T)-1, (T)1); }
inline void float2i15_block(float *f, short *s, int n)
{
for (int i = 0; i < n; i++)
{
s[i] = (short)(int)limit_range((int)((float)f[i] * 16384.f), -16384, 16383);
}
}
inline void i152float_block(short *s, float *f, int n)
{
const float scale = 1.f / 16384.f;
for (int i = 0; i < n; i++)
{
f[i] = (float)s[i] * scale;
}
}
inline void i162float_block(short *s, float *f, int n)
{
const float scale = 1.f / (16384.f * 2);
for (int i = 0; i < n; i++)
{
f[i] = (float)s[i] * scale;
}
}
inline void i16toi15_block(short *s, short *o, int n)
{
for (int i = 0; i < n; i++)
{
o[i] = s[i] >> 1;
}
}
// These pan laws are bad so don't port them
inline float megapanL(float pos) // valid range -2 .. 2 (> +- 1 is inverted phase)
{
if (pos > 2.f)
pos = 2.f;
if (pos < -2.f)
pos = -2.f;
return (1 - 0.75f * pos - 0.25f * pos * pos);
}
inline float megapanR(float pos)
{
if (pos > 2.f)
pos = 2.f;
if (pos < -2.f)
pos = -2.f;
return (1 + 0.75f * pos - 0.25f * pos * pos);
}
#endif // SURGE_SRC_COMMON_DSP_VEMBERTECH_BASIC_DSP_H