annotate CloneChannel/plugins/common/gen_dsp/genlib_ops.h @ 23:489244127359

kopieckreta-0.240903-0
author Przemyslaw <prymula76@outlook.com>
date Mon, 01 Apr 2024 07:15:00 +0200
parents 24d60bdea349
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1 /*******************************************************************************************************************
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
2 Copyright (c) 2012 Cycling '74
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
3
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
5 and associated documentation files (the "Software"), to deal in the Software without restriction,
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
6 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
7 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
8 subject to the following conditions:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
9
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
10 The above copyright notice and this permission notice shall be included in all copies
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
11 or substantial portions of the Software.
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
12
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
14 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
15 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
16 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
17 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
18 *******************************************************************************************************************/
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
19
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
20 #ifndef GENLIB_OPS_H
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
21 #define GENLIB_OPS_H 1
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
22
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
23 #include "genlib_common.h" // common to common code and any host code
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
24 #include "genlib.h" // this file is different for different "hosts"
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
25
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
26 #include <cmath>
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
27
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
28 //////////// genlib_ops.h ////////////
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
29
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
30 // system constants
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
31 #define GENLIB_DBL_EPSILON (__DBL_EPSILON__)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
32
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
33 #define GENLIB_PI (3.14159265358979323846264338327950288f)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
34 #define GENLIB_PI_OVER_2 (1.57079632679489661923132169163975144f)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
35 #define GENLIB_PI_OVER_4 (0.785398163397448309615660845819875721f)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
36 #define GENLIB_1_OVER_LOG_2 (1.442695040888963f)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
37
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
38 #define GENLIB_NO_DENORM_TEST 1
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
39
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
40 // assumes v is a 64-bit double:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
41 #define GENLIB_IS_NAN_DOUBLE(v) (((((uint32_t *)&(v))[1])&0x7fe00000)==0x7fe00000)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
42 #define GENLIB_FIX_NAN_DOUBLE(v) ((v)=GENLIB_IS_NAN_DOUBLE(v)?0.:(v))
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
43
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
44 #ifdef GENLIB_NO_DENORM_TEST
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
45 #define GENLIB_IS_DENORM_DOUBLE(v) (v)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
46 #define GENLIB_FIX_DENORM_DOUBLE(v) (v)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
47 #else
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
48 #define GENLIB_IS_DENORM_DOUBLE(v) ((((((uint32_t *)&(v))[1])&0x7fe00000)==0)&&((v)!=0.))
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
49 #define GENLIB_FIX_DENORM_DOUBLE(v) ((v)=GENLIB_IS_DENORM_DOUBLE(v)?0.f:(v))
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
50 #endif
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
51
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
52 #define GENLIB_QUANT(f1,f2) (floor((f1)*(f2)+0.5)/(f2))
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
53
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
54 inline double genlib_isnan(double v) { return GENLIB_IS_NAN_DOUBLE(v); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
55 inline double fixnan(double v) { return GENLIB_FIX_NAN_DOUBLE(v); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
56 inline double fixdenorm(double v) { return GENLIB_FIX_DENORM_DOUBLE(v); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
57 inline double isdenorm(double v) { return GENLIB_IS_DENORM_DOUBLE(v); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
58
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
59 inline double safemod(double f, double m) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
60 if (m > GENLIB_DBL_EPSILON || m < -GENLIB_DBL_EPSILON) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
61 if (m<0)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
62 m = -m; // modulus needs to be absolute value
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
63 if (f>=m) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
64 if (f>=(m*2.)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
65 double d = f / m;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
66 d = d - (long) d;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
67 f = d * m;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
68 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
69 else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
70 f -= m;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
71 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
72 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
73 else if (f<=(-m)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
74 if (f<=(-m*2.)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
75 double d = f / m;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
76 d = d - (long) d;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
77 f = d * m;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
78 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
79 else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
80 f += m;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
81 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
82 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
83 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
84 f = 0.0; //don't divide by zero
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
85 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
86 return f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
87 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
88
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
89
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
90 inline double safediv(double num, double denom) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
91 return denom == 0. ? 0. : num/denom;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
92 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
93
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
94 // fixnan for case of negative base and non-integer exponent:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
95 inline double safepow(double base, double exponent) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
96 return fixnan(pow(base, exponent));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
97 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
98
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
99 inline double absdiff(double a, double b) { return fabs(a-b); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
100
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
101 inline double exp2(double v) { return pow(2., v); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
102
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
103 inline double trunc(double v) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
104 double epsilon = (v<0.0) * -2 * 1E-9 + 1E-9;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
105 // copy to long so it gets truncated (probably cheaper than floor())
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
106 long val = v + epsilon;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
107 return val;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
108 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
109
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
110 inline t_sample sign(t_sample v) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
111 return v > t_sample(0) ? t_sample(1) : v < t_sample(0) ? t_sample(-1) : t_sample(0);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
112 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
113
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
114 inline long is_poweroftwo(long x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
115 return (x & (x - 1)) == 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
116 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
117
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
118 inline uint64_t next_power_of_two(uint64_t v) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
119 v--;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
120 v |= v >> 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
121 v |= v >> 2;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
122 v |= v >> 4;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
123 v |= v >> 8;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
124 v |= v >> 16;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
125 v |= v >> 32;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
126 v++;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
127 return v;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
128 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
129
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
130 inline t_sample fold(t_sample v, t_sample lo1, t_sample hi1){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
131 t_sample lo;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
132 t_sample hi;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
133 if(lo1 == hi1){ return lo1; }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
134 if (lo1 > hi1) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
135 hi = lo1; lo = hi1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
136 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
137 lo = lo1; hi = hi1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
138 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
139 const t_sample range = hi - lo;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
140 long numWraps = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
141 if(v >= hi){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
142 v -= range;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
143 if(v >= hi){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
144 numWraps = (long)((v - lo)/range);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
145 v -= range * (t_sample)numWraps;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
146 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
147 numWraps++;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
148 } else if(v < lo){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
149 v += range;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
150 if(v < lo){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
151 numWraps = (long)((v - lo)/range) - 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
152 v -= range * (t_sample)numWraps;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
153 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
154 numWraps--;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
155 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
156 if(numWraps & 1) v = hi + lo - v; // flip sign for odd folds
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
157 return v;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
158 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
159
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
160 inline double wrap(double v, double lo1, double hi1){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
161 double lo;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
162 double hi;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
163 if(lo1 == hi1) return lo1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
164 if (lo1 > hi1) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
165 hi = lo1; lo = hi1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
166 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
167 lo = lo1; hi = hi1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
168 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
169 const double range = hi - lo;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
170 if (v >= lo && v < hi) return v;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
171 if (range <= 0.000000001) return lo; // no point...
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
172 const long numWraps = long((v-lo)/range) - (v < lo);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
173 return v - range * double(numWraps);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
174 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
175
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
176 // this version gives far better performance when wrapping is relatively rare
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
177 // and typically double of wraps is very low (>1%)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
178 // but catastrophic if wraps is high (1000%+)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
179 inline t_sample genlib_wrapfew(t_sample v, t_sample lo, t_sample hi){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
180 const t_sample range = hi - lo;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
181 while (v >= hi) v -= range;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
182 while (v < lo) v += range;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
183 return v;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
184 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
185
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
186 inline t_sample phasewrap(t_sample val) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
187 const t_sample twopi = GENLIB_PI*2.;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
188 const t_sample oneovertwopi = 1./twopi;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
189 if (val>= twopi || val <= twopi) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
190 t_sample d = val * oneovertwopi; //multiply faster
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
191 d = d - (long)d;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
192 val = d * twopi;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
193 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
194 if (val > GENLIB_PI) val -= twopi;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
195 if (val < -GENLIB_PI) val += twopi;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
196 return val;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
197 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
198
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
199 /// 8th order Taylor series approximation to a cosine.
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
200 /// r must be in [-pi, pi].
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
201 inline t_sample genlib_cosT8(t_sample r) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
202 const t_sample t84 = 56.;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
203 const t_sample t83 = 1680.;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
204 const t_sample t82 = 20160.;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
205 const t_sample t81 = 2.4801587302e-05;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
206 const t_sample t73 = 42.;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
207 const t_sample t72 = 840.;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
208 const t_sample t71 = 1.9841269841e-04;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
209 if(r < GENLIB_PI_OVER_4 && r > -GENLIB_PI_OVER_4){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
210 t_sample rr = r*r;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
211 return 1. - rr * t81 * (t82 - rr * (t83 - rr * (t84 - rr)));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
212 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
213 else if(r > 0.){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
214 r -= GENLIB_PI_OVER_2;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
215 t_sample rr = r*r;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
216 return -r * (1. - t71 * rr * (t72 - rr * (t73 - rr)));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
217 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
218 else{
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
219 r += GENLIB_PI_OVER_2;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
220 t_sample rr = r*r;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
221 return r * (1. - t71 * rr * (t72 - rr * (t73 - rr)));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
222 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
223 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
224
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
225 //inline double genlib_sin_fast(const double r){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
226 // const double y = (4./GENLIB_PI) * r + (-4./(GENLIB_PI*GENLIB_PI)) * r * fabs(r);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
227 // return 0.225 * (y * fabs(y) - y) + y; // Q * y + P * y * abs(y)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
228 //}
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
229 //
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
230 //inline t_sample genlib_sinP7(t_sample n){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
231 // t_sample nn = n*n;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
232 // return n * (t_sample(3.138982) + nn * (t_sample(-5.133625) + nn * (t_sample(2.428288) - nn * t_sample(0.433645))));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
233 //}
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
234 //
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
235 //inline t_sample genlib_sinP9(t_sample n){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
236 // t_sample nn = n*n;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
237 // return n * (GENLIB_PI + nn * (t_sample(-5.1662729) + nn * (t_sample(2.5422065) + nn * (t_sample(-0.5811243) + nn * t_sample(0.0636716)))));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
238 //}
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
239 //
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
240 //inline t_sample genlib_sinT7(t_sample r){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
241 // const t_sample t84 = 56.;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
242 // const t_sample t83 = 1680.;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
243 // const t_sample t82 = 20160.;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
244 // const t_sample t81 = 2.4801587302e-05;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
245 // const t_sample t73 = 42.;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
246 // const t_sample t72 = 840.;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
247 // const t_sample t71 = 1.9841269841e-04;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
248 // if(r < GENLIB_PI_OVER_4 && r > -GENLIB_PI_OVER_4){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
249 // t_sample rr = r*r;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
250 // return r * (1. - t71 * rr * (t72 - rr * (t73 - rr)));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
251 // }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
252 // else if(r > 0.){
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
253 // r -= GENLIB_PI_OVER_2;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
254 // t_sample rr = r*r;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
255 // return t_sample(1.) - rr * t81 * (t82 - rr * (t83 - rr * (t84 - rr)));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
256 // }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
257 // else{
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
258 // r += GENLIB_PI_OVER_2;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
259 // t_sample rr = r*r;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
260 // return t_sample(-1.) + rr * t81 * (t82 - rr * (t83 - rr * (t84 - rr)));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
261 // }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
262 //}
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
263
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
264 // use these if r is not known to be in [-pi, pi]:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
265 inline t_sample genlib_cosT8_safe(t_sample r) { return genlib_cosT8(phasewrap(r)); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
266 //inline double genlib_sin_fast_safe(double r) { return genlib_sin_fast(phasewrap(r)); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
267 //inline t_sample genlib_sinP7_safe(t_sample r) { return genlib_sinP7(phasewrap(r)); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
268 //inline t_sample genlib_sinP9_safe(t_sample r) { return genlib_sinP9(phasewrap(r)); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
269 //inline t_sample genlib_sinT7_safe(t_sample r) { return genlib_sinT7(phasewrap(r)); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
270
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
271
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
272
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
273 /*=====================================================================*
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
274 * Copyright (C) 2011 Paul Mineiro *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
275 * All rights reserved. *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
276 * *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
277 * Redistribution and use in source and binary forms, with *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
278 * or without modification, are permitted provided that the *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
279 * following conditions are met: *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
280 * *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
281 * * Redistributions of source code must retain the *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
282 * above copyright notice, this list of conditions and *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
283 * the following disclaimer. *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
284 * *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
285 * * Redistributions in binary form must reproduce the *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
286 * above copyright notice, this list of conditions and *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
287 * the following disclaimer in the documentation and/or *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
288 * other materials provided with the distribution. *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
289 * *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
290 * * Neither the name of Paul Mineiro nor the names *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
291 * of other contributors may be used to endorse or promote *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
292 * products derived from this software without specific *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
293 * prior written permission. *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
294 * *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
295 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
296 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
297 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
298 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
299 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
300 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
301 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
302 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
303 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
304 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
305 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
306 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
307 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
308 * POSSIBILITY OF SUCH DAMAGE. *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
309 * *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
310 * Contact: Paul Mineiro <paul@mineiro.com> *
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
311 *=====================================================================*/
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
312
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
313 inline float genlib_fastersin (float x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
314 static const float fouroverpi = 1.2732395447351627f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
315 static const float fouroverpisq = 0.40528473456935109f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
316 static const float q = 0.77633023248007499f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
317 union { float f; uint32_t i; } p = { 0.22308510060189463f };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
318 union { float f; uint32_t i; } vx = { x };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
319 uint32_t sign = vx.i & 0x80000000;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
320 vx.i &= 0x7FFFFFFF;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
321 float qpprox = fouroverpi * x - fouroverpisq * x * vx.f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
322 p.i |= sign;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
323 return qpprox * (q + p.f * qpprox);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
324 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
325
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
326 inline float genlib_fastercos (float x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
327 static const float twooverpi = 0.63661977236758134f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
328 static const float p = 0.54641335845679634f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
329 union { float f; uint32_t i; } vx = { x };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
330 vx.i &= 0x7FFFFFFF;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
331 float qpprox = 1.0f - twooverpi * vx.f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
332 return qpprox + p * qpprox * (1.0f - qpprox * qpprox);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
333 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
334
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
335 inline float genlib_fastersinfull (float x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
336 static const float twopi = 6.2831853071795865f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
337 static const float invtwopi = 0.15915494309189534f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
338 int k = x * invtwopi;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
339 float half = (x < 0) ? -0.5f : 0.5f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
340 return genlib_fastersin ((half + k) * twopi - x);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
341 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
342
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
343 inline float genlib_fastercosfull (float x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
344 static const float halfpi = 1.5707963267948966f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
345 return genlib_fastersinfull (x + halfpi);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
346 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
347
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
348 inline float genlib_fastertanfull (float x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
349 static const float twopi = 6.2831853071795865f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
350 static const float invtwopi = 0.15915494309189534f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
351 int k = x * invtwopi;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
352 float half = (x < 0) ? -0.5f : 0.5f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
353 float xnew = x - (half + k) * twopi;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
354 return genlib_fastersin (xnew) / genlib_fastercos (xnew);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
355 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
356
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
357
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
358 #define cast_uint32_t static_cast<uint32_t>
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
359 inline float genlib_fasterpow2 (float p) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
360 float clipp = (p < -126) ? -126.0f : p;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
361 union { uint32_t i; float f; } v = { cast_uint32_t ( (1 << 23) * (clipp + 126.94269504f) ) };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
362 return v.f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
363 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
364
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
365 inline float genlib_fasterexp (float p) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
366 return genlib_fasterpow2 (1.442695040f * p);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
367 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
368
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
369 inline float genlib_fasterlog2 (float x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
370 union { float f; uint32_t i; } vx = { x };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
371 float y = vx.i;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
372 y *= 1.1920928955078125e-7f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
373 return y - 126.94269504f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
374 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
375
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
376 inline float genlib_fasterpow (float x, float p) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
377 return genlib_fasterpow2(p * genlib_fasterlog2 (x));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
378 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
379
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
380 ////////////////////////////////////////////////////////////////
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
381
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
382 inline double fastertanfull(double x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
383 return (double)genlib_fastertanfull((float)x);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
384 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
385
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
386 inline double fastersinfull(double x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
387 return (double)genlib_fastersinfull((float)x);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
388 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
389
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
390 inline double fastercosfull(double x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
391 return (double)genlib_fastercosfull((float)x);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
392 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
393
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
394 inline double fasterexp(double x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
395 return (double)genlib_fasterexp((float)x);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
396 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
397
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
398 inline double fasterpow(double x, double p) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
399 return (double)genlib_fasterpow((float)x, (float)p);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
400 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
401 /****************************************************************/
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
402
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
403
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
404
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
405 inline double minimum(double x, double y) { return (y<x?y:x); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
406 inline double maximum(double x, double y) { return (x<y?y:x); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
407
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
408 inline t_sample clamp(t_sample x, t_sample minVal, t_sample maxVal) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
409 return minimum(maximum(x,minVal),maxVal);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
410 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
411
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
412 template<typename T>
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
413 inline T smoothstep(double e0, double e1, T x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
414 T t = clamp( safediv(x-T(e0),T(e1-e0)), 0., 1. );
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
415 return t*t*(T(3) - T(2)*t);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
416 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
417
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
418 inline t_sample mix(t_sample x, t_sample y, t_sample a) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
419 return x+a*(y-x);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
420 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
421
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
422 inline double scale(double in, double inlow, double inhigh, double outlow, double outhigh, double power)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
423 {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
424 double value;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
425 double inscale = safediv(1., inhigh - inlow);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
426 double outdiff = outhigh - outlow;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
427
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
428 value = (in - inlow) * inscale;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
429 if (value > 0.0)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
430 value = pow(value, power);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
431 else if (value < 0.0)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
432 value = -pow(-value, power);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
433 value = (value * outdiff) + outlow;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
434
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
435 return value;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
436 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
437
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
438 inline t_sample linear_interp(t_sample a, t_sample x, t_sample y) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
439 return x+a*(y-x);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
440 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
441
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
442 inline t_sample cosine_interp(t_sample a, t_sample x, t_sample y) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
443 const t_sample a2 = (t_sample(1.)-genlib_cosT8_safe(a*t_sample(GENLIB_PI)))/t_sample(2.);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
444 return(x*(t_sample(1.)-a2)+y*a2);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
445 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
446
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
447 inline t_sample cubic_interp(t_sample a, t_sample w, t_sample x, t_sample y, t_sample z) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
448 const t_sample a2 = a*a;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
449 const t_sample f0 = z - y - w + x;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
450 const t_sample f1 = w - x - f0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
451 const t_sample f2 = y - w;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
452 const t_sample f3 = x;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
453 return(f0*a*a2 + f1*a2 + f2*a + f3);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
454 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
455
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
456 // Breeuwsma catmull-rom spline interpolation
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
457 inline t_sample spline_interp(t_sample a, t_sample w, t_sample x, t_sample y, t_sample z) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
458 const t_sample a2 = a*a;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
459 const t_sample f0 = t_sample(-0.5)*w + t_sample(1.5)*x - t_sample(1.5)*y + t_sample(0.5)*z;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
460 const t_sample f1 = w - t_sample(2.5)*x + t_sample(2)*y - t_sample(0.5)*z;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
461 const t_sample f2 = t_sample(-0.5)*w + t_sample(0.5)*y;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
462 return(f0*a*a2 + f1*a2 + f2*a + x);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
463 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
464
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
465 template<typename T1, typename T2>
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
466 inline T1 neqp(T1 x, T2 y) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
467 return ((((x) != T1(y))) ? (x) : T1(0));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
468 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
469
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
470 template<typename T1, typename T2>
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
471 inline T1 gtp(T1 x, T2 y) { return ((((x) > T1(y))) ? (x) : T1(0)); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
472 template<typename T1, typename T2>
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
473 inline T1 gtep(T1 x, T2 y) { return ((((x) >= T1(y))) ? (x) : T1(0)); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
474 template<typename T1, typename T2>
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
475 inline T1 ltp(T1 x, T2 y) { return ((((x) < T1(y))) ? (x) : T1(0)); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
476 template<typename T1, typename T2>
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
477 inline T1 ltep(T1 x, T2 y) { return ((((x) <= T1(y))) ? (x) : T1(0)); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
478
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
479 inline double fract(double x) { double unused; return modf(x, &unused); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
480
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
481 // log2(x) = log(x)/log(2)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
482 template<typename T>
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
483 inline T log2(T x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
484 return log(x)*GENLIB_1_OVER_LOG_2;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
485 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
486
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
487 inline double atodb(double in) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
488 return (in <=0.) ? -999. : (20. * log10(in));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
489 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
490
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
491 inline double dbtoa(double in) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
492 return pow(10., in * 0.05);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
493 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
494
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
495 inline double ftom(double in, double tuning=440.) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
496 return 69. + 17.31234050465299 * log(safediv(in, tuning));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
497 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
498
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
499 inline double mtof(double in, double tuning=440.) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
500 return tuning * exp(.057762265 * (in - 69.0));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
501 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
502
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
503 inline t_sample mstosamps(t_sample ms, t_sample samplerate=44100.) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
504 return samplerate * ms * t_sample(0.001);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
505 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
506
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
507 inline t_sample sampstoms(t_sample s, t_sample samplerate=44100.) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
508 return t_sample(1000.) * s / samplerate;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
509 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
510
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
511 inline double triangle(double phase, double p1) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
512 phase = wrap(phase, 0., 1.);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
513 p1 = clamp(p1, 0., 1.);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
514 if (phase < p1)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
515 return (p1) ? phase/p1 : 0.;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
516 else
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
517 return (p1==1.) ? phase : 1. - ((phase - p1) / (1. - p1));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
518 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
519
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
520 struct Delta {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
521 t_sample history;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
522 Delta() { reset(); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
523 inline void reset(t_sample init=0) { history=init; }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
524
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
525 inline t_sample operator()(t_sample in1) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
526 t_sample ret = in1 - history;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
527 history = in1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
528 return ret;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
529 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
530 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
531 struct Change {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
532 t_sample history;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
533 Change() { reset(); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
534 inline void reset(t_sample init=0) { history=init; }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
535
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
536 inline t_sample operator()(t_sample in1) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
537 t_sample ret = in1 - history;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
538 history = in1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
539 return sign(ret);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
540 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
541 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
542
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
543 struct Rate {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
544 t_sample phase, diff, mult, invmult, prev;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
545 int wantlock, quant;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
546
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
547 Rate() { reset(); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
548
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
549 inline void reset() {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
550 phase = diff = prev = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
551 mult = invmult = 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
552 wantlock = 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
553 quant = 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
554 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
555
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
556 inline t_sample perform_lock(t_sample in1, t_sample in2) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
557 // did multiplier change?
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
558 if (in2 != mult && !genlib_isnan(in2)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
559 mult = in2;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
560 invmult = safediv(1., mult);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
561 wantlock = 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
562 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
563 t_sample diff = in1 - prev;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
564
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
565 if (diff < t_sample(-0.5)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
566 diff += t_sample(1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
567 } else if (diff > t_sample(0.5)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
568 diff -= t_sample(1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
569 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
570
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
571 if (wantlock) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
572 // recalculate phase
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
573 phase = (in1 - GENLIB_QUANT(in1, quant)) * invmult
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
574 + GENLIB_QUANT(in1, quant * mult);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
575 diff = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
576 wantlock = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
577 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
578 // diff is always between -0.5 and 0.5
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
579 phase += diff * invmult;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
580 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
581
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
582 if (phase > t_sample(1.) || phase < t_sample(-0.)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
583 phase = phase - (long)(phase);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
584 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
585
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
586 prev = in1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
587
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
588 return phase;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
589 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
590
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
591 inline t_sample perform_cycle(t_sample in1, t_sample in2) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
592 // did multiplier change?
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
593 if (in2 != mult && !genlib_isnan(in2)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
594 mult = in2;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
595 invmult = safediv(1., mult);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
596 wantlock = 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
597 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
598 t_sample diff = in1 - prev;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
599
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
600 if (diff < t_sample(-0.5)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
601 if (wantlock) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
602 wantlock = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
603 phase = in1 * invmult;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
604 diff = t_sample(0);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
605 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
606 diff += t_sample(1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
607 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
608 } else if (diff > t_sample(0.5)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
609 if (wantlock) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
610 wantlock = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
611 phase = in1 * invmult;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
612 diff = t_sample(0);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
613 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
614 diff -= t_sample(1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
615 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
616 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
617
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
618 // diff is always between -0.5 and 0.5
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
619 phase += diff * invmult;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
620
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
621 if (phase > t_sample(1.) || phase < t_sample(-0.)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
622 phase = phase - (long)(phase);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
623 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
624
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
625 prev = in1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
626
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
627 return phase;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
628 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
629
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
630 inline t_sample perform_off(double in1, double in2) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
631 // did multiplier change?
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
632 if (in2 != mult && !genlib_isnan(in2)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
633 mult = in2;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
634 invmult = safediv(1., mult);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
635 wantlock = 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
636 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
637 double diff = in1 - prev;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
638
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
639 if (diff < t_sample(-0.5)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
640 diff += t_sample(1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
641 } else if (diff > t_sample(0.5)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
642 diff -= t_sample(1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
643 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
644
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
645 phase += diff * invmult;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
646
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
647 if (phase > t_sample(1.) || phase < t_sample(-0.)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
648 phase = phase - (long)(phase);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
649 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
650
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
651 prev = in1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
652
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
653 return phase;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
654 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
655 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
656
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
657 struct DCBlock {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
658 t_sample x1, y1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
659 DCBlock() { reset(); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
660 inline void reset() { x1=0; y1=0; }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
661
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
662 inline double operator()(t_sample in1) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
663 t_sample y = in1 - x1 + y1*t_sample(0.9997);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
664 x1 = in1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
665 y1 = y;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
666 return y;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
667 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
668 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
669
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
670 struct Noise {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
671 unsigned long last;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
672 static long uniqueTickCount(void) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
673 static long lasttime = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
674 long time = genlib_ticks();
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
675 return (time <= lasttime) ? (++lasttime) : (lasttime = time);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
676 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
677
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
678 Noise() { reset(); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
679 Noise(double seed) { reset(seed); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
680 void reset() { last = uniqueTickCount() * uniqueTickCount(); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
681 void reset(double seed) { last = seed; }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
682
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
683 inline t_sample operator()() {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
684 last = 1664525L * last + 1013904223L;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
685 unsigned long itemp = 0x3f800000 | (0x007fffff & last);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
686 unsigned long* itempptr = &itemp;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
687 return ((*(float *)itempptr) * 2.f) - 3.f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
688 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
689 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
690
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
691 struct Phasor {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
692 t_sample phase;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
693 Phasor() { reset(); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
694 void reset(t_sample v=0.) { phase=v; }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
695 inline double operator()(t_sample freq, t_sample invsamplerate) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
696 const t_sample pincr = freq * invsamplerate;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
697 //phase = genlib_wrapfew(phase + pincr, 0., 1.); // faster for low frequencies, but explodes with high frequencies
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
698 phase = wrap(phase + pincr, 0., 1.);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
699 return phase;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
700 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
701 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
702
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
703 struct PlusEquals {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
704 t_sample count;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
705 PlusEquals() { reset(); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
706 void reset(t_sample v=0.) { count=v; }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
707
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
708 // reset post-application mode:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
709 inline t_sample post(t_sample incr, t_sample reset, t_sample min, t_sample max) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
710 count = reset ? min : wrap(count+incr, min, max);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
711 return count;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
712 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
713 inline t_sample post(t_sample incr=1., t_sample reset=0., t_sample min=0.) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
714 count = reset ? min : count+incr;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
715 return count;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
716 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
717
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
718 // reset pre-application mode:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
719 inline t_sample pre(t_sample incr, t_sample reset, t_sample min, t_sample max) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
720 count = reset ? min+incr : wrap(count+incr, min, max);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
721 return count;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
722 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
723 inline t_sample pre(t_sample incr=1., t_sample reset=0., t_sample min=0.) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
724 count = reset ? min+incr : count+incr;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
725 return count;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
726 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
727 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
728
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
729 struct MulEquals {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
730 t_sample count;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
731 MulEquals() { reset(); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
732 void reset(t_sample v=0.) { count=v; }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
733
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
734 // reset post-application mode:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
735 inline t_sample post(t_sample incr, t_sample reset, t_sample min, t_sample max) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
736 count = reset ? min : wrap(fixdenorm(count*incr), min, max);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
737 return count;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
738 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
739 inline t_sample post(t_sample incr=1., t_sample reset=0., t_sample min=0.) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
740 count = reset ? min : fixdenorm(count*incr);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
741 return count;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
742 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
743
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
744 // reset pre-application mode:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
745 inline t_sample pre(t_sample incr, t_sample reset, t_sample min, t_sample max) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
746 count = reset ? min*incr : wrap(fixdenorm(count*incr), min, max);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
747 return count;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
748 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
749 inline t_sample pre(t_sample incr=1., t_sample reset=0., t_sample min=0.) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
750 count = reset ? min*incr : fixdenorm(count*incr);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
751 return count;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
752 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
753 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
754
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
755 struct Sah {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
756 t_sample prev, output;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
757 Sah() { reset(); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
758 void reset(t_sample o=0.) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
759 output = prev = o;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
760 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
761
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
762 inline t_sample operator()(t_sample in, t_sample trig, t_sample thresh) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
763 if (prev <= thresh && trig > thresh) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
764 output = in;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
765 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
766 prev = trig;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
767 return output;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
768 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
769 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
770
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
771 struct Train {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
772 t_sample phase, state;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
773 Train() { reset(); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
774 void reset(t_sample p=0) { phase = p; state = 0.; }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
775
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
776 inline t_sample operator()(t_sample pulseinterval, t_sample width, t_sample pulsephase) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
777 if (width <= t_sample(0.)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
778 state = t_sample(0.); // no pulse!
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
779 } else if (width >= 1.) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
780 state = t_sample(1.); // constant pulse!
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
781 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
782 const t_sample interval = maximum(pulseinterval, t_sample(1.)); // >= 1.
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
783 const t_sample p1 = clamp(pulsephase, t_sample(0.), t_sample(1.)); // [0..1]
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
784 const t_sample p2 = p1+width; // (p1..p1+1)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
785 const t_sample pincr = t_sample(1.)/interval; // (0..1]
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
786 phase += pincr; // +ve
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
787 if (state) { // on:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
788 if (phase > p2) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
789 state = t_sample(0.); // turn off
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
790 phase -= (int)(1.+phase-p2); // wrap phase back down
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
791 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
792 } else { // off:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
793 if (phase > p1) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
794 state = t_sample(1.); // turn on.
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
795 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
796 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
797 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
798 return state;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
799 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
800 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
801
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
802 struct Delay {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
803 t_sample * memory;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
804 long size, wrap, maxdelay;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
805 long reader, writer;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
806
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
807 t_genlib_data * dataRef;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
808
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
809 Delay() : memory(0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
810 size = wrap = maxdelay = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
811 reader = writer = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
812 dataRef = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
813 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
814 ~Delay() {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
815 if (dataRef != 0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
816 // store write position for persistence:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
817 genlib_data_setcursor(dataRef, writer);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
818 // decrement reference count:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
819 genlib_data_release(dataRef);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
820 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
821 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
822
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
823 inline void reset(const char * name, long d) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
824 // if needed, acquire the Data's global reference:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
825 if (dataRef == 0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
826
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
827 void * ref = genlib_obtain_reference_from_string(name);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
828 dataRef = genlib_obtain_data_from_reference(ref);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
829 if (dataRef == 0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
830 genlib_report_error("failed to acquire data");
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
831 return;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
832 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
833
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
834 // scale maxdelay to next highest power of 2:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
835 maxdelay = d;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
836 size = maxdelay < 2 ? 2 : maxdelay;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
837 size = next_power_of_two(size);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
838
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
839 // first reset should resize the memory:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
840 genlib_data_resize(dataRef, size, 1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
841
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
842 t_genlib_data_info info;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
843 if (genlib_data_getinfo(dataRef, &info) == GENLIB_ERR_NONE) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
844 if (info.dim != size) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
845 // at this point, could resolve by reducing to
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
846 // maxdelay = size = next_power_of_two(info.dim+1)/2;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
847 // but really, if this happens, it means more than one
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
848 // object is referring to the same t_gen_dsp_data.
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
849 // which is probably bad news.
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
850 genlib_report_error("delay memory size error");
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
851 memory = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
852 return;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
853 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
854 memory = info.data;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
855 writer = genlib_data_getcursor(dataRef);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
856 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
857 genlib_report_error("failed to acquire data info");
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
858 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
859
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
860 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
861 // subsequent reset should zero the memory & heads:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
862 set_zero64(memory, size);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
863 writer = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
864 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
865
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
866 reader = writer;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
867 wrap = size-1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
868 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
869
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
870 // called at bufferloop end, updates read pointer time
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
871 inline void step() {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
872 reader++;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
873 if (reader >= size) reader = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
874 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
875
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
876 inline void write(t_sample x) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
877 writer = reader; // update write ptr
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
878 memory[writer] = x;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
879 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
880
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
881 inline t_sample read_step(t_sample d) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
882 // extra half for nice rounding:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
883 // min 1 sample delay for read before write (r != w)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
884 const t_sample r = t_sample(size + reader) - clamp(d-t_sample(0.5), (reader != writer), maxdelay);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
885 long r1 = long(r);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
886 return memory[r1 & wrap];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
887 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
888
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
889 inline t_sample read_linear(t_sample d) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
890 // min 1 sample delay for read before write (r != w)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
891 t_sample c = clamp(d, (reader != writer), maxdelay);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
892 const t_sample r = t_sample(size + reader) - c;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
893 long r1 = long(r);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
894 long r2 = r1+1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
895 t_sample a = r - (t_sample)r1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
896 t_sample x = memory[r1 & wrap];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
897 t_sample y = memory[r2 & wrap];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
898 return linear_interp(a, x, y);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
899 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
900
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
901 inline t_sample read_cosine(t_sample d) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
902 // min 1 sample delay for read before write (r != w)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
903 const t_sample r = t_sample(size + reader) - clamp(d, (reader != writer), maxdelay);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
904 long r1 = long(r);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
905 long r2 = r1+1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
906 t_sample a = r - (t_sample)r1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
907 t_sample x = memory[r1 & wrap];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
908 t_sample y = memory[r2 & wrap];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
909 return cosine_interp(a, x, y);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
910 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
911
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
912 // cubic requires extra sample of compensation:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
913 inline t_sample read_cubic(t_sample d) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
914 // min 1 sample delay for read before write (r != w)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
915 // plus extra 1 sample compensation for 4-point interpolation
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
916 const t_sample r = t_sample(size + reader) - clamp(d, t_sample(1.)+t_sample(reader != writer), maxdelay);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
917 long r1 = long(r);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
918 long r2 = r1+1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
919 long r3 = r1+2;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
920 long r4 = r1+3;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
921 t_sample a = r - (t_sample)r1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
922 t_sample w = memory[r1 & wrap];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
923 t_sample x = memory[r2 & wrap];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
924 t_sample y = memory[r3 & wrap];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
925 t_sample z = memory[r4 & wrap];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
926 return cubic_interp(a, w, x, y, z);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
927 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
928
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
929 // spline requires extra sample of compensation:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
930 inline t_sample read_spline(t_sample d) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
931 // min 1 sample delay for read before write (r != w)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
932 // plus extra 1 sample compensation for 4-point interpolation
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
933 const t_sample r = t_sample(size + reader) - clamp(d, t_sample(1.)+t_sample(reader != writer), maxdelay);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
934 long r1 = long(r);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
935 long r2 = r1+1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
936 long r3 = r1+2;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
937 long r4 = r1+3;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
938 t_sample a = r - (t_sample)r1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
939 t_sample w = memory[r1 & wrap];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
940 t_sample x = memory[r2 & wrap];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
941 t_sample y = memory[r3 & wrap];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
942 t_sample z = memory[r4 & wrap];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
943 return spline_interp(a, w, x, y, z);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
944 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
945 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
946
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
947 template<typename T=t_sample>
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
948 struct DataInterface {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
949 long dim, channels;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
950 T * mData;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
951 void * mDataReference; // this was t_symbol *mName
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
952 int modified;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
953
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
954 DataInterface() : dim(0), channels(1), mData(0), modified(0) { mDataReference = 0; }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
955
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
956 // raw reading/writing/overdubbing (internal use only, no bounds checking)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
957 inline t_sample read(long index, long channel=0) const {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
958 return mData[channel+index*channels];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
959 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
960 inline void write(T value, long index, long channel=0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
961 mData[channel+index*channels] = value;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
962 modified = 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
963 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
964 // NO LONGER USED:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
965 inline void overdub(T value, long index, long channel=0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
966 mData[channel+index*channels] += value;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
967 modified = 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
968 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
969
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
970 // averaging overdub (used by splat)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
971 inline void blend(T value, long index, long channel, t_sample alpha) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
972 long offset = channel+index*channels;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
973 const T old = mData[offset];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
974 mData[offset] = old + alpha * (value - old);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
975 modified = 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
976 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
977
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
978 // NO LONGER USED:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
979 inline void read_ok(long index, long channel=0, bool ok=1) const {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
980 return ok ? mData[channel+index*channels] : T(0);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
981 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
982 inline void write_ok(T value, long index, long channel=0, bool ok=1) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
983 if (ok) mData[channel+index*channels] = value;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
984 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
985 inline void overdub_ok(T value, long index, long channel=0, bool ok=1) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
986 if (ok) mData[channel+index*channels] += value;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
987 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
988
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
989 // Bounds strategies:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
990 inline long index_clamp(long index) const { return clamp(index, 0, dim-1); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
991 inline long index_wrap(long index) const { return wrap(index, 0, dim); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
992 inline long index_fold(long index) const { return fold(index, 0, dim); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
993 inline bool index_oob(long index) const { return (index < 0 || index >= dim); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
994 inline bool index_inbounds(long index) const { return (index >=0 && index < dim); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
995
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
996 // channel bounds:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
997 inline long channel_clamp(long c) const { return clamp(c, 0, channels-1); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
998 inline long channel_wrap(long c) const { return wrap(c, 0, channels); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
999 inline long channel_fold(long c) const { return fold(c, 0, channels); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1000 inline bool channel_oob(long c) const { return (c < 0 || c >= channels); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1001 inline bool channel_inbounds(long c) const { return !channel_oob(c); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1002
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1003 // Indexing strategies:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1004 // [0..1] -> [0..(dim-1)]
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1005 inline t_sample phase2index(t_sample phase) const { return phase * t_sample(dim-1); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1006 // [0..1] -> [min..max]
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1007 inline t_sample subphase2index(t_sample phase, long min, long max) const {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1008 min = index_clamp(min);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1009 max = index_clamp(max);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1010 return t_sample(min) + phase * t_sample(max-min);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1011 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1012 // [-1..1] -> [0..(dim-1)]
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1013 inline t_sample signal2index(t_sample signal) const { return phase2index((signal+t_sample(1.)) * t_sample(0.5)); }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1014
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1015 inline T peek(t_sample index, long channel=0) const {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1016 const long i = (long)index;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1017 if (index_oob(i) || channel_oob(channel)) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1018 return 0.;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1019 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1020 return read(i, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1021 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1022 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1023
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1024 inline T index(double index, long channel=0) const {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1025 channel = channel_clamp(channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1026 // no-interp:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1027 long i = (long)index;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1028 // bound:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1029 i = index_clamp(i);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1030 return read(i, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1031 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1032
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1033 inline T cell(double index, long channel=0) const {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1034 channel = channel_clamp(channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1035 // no-interp:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1036 long i = (long)index;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1037 // bound:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1038 i = index_wrap(i);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1039 return read(i, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1040 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1041
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1042 inline T cycle(t_sample phase, long channel=0) const {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1043 channel = channel_clamp(channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1044 t_sample index = phase2index(phase);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1045 // interp:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1046 long i1 = (long)index;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1047 long i2 = i1+1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1048 const t_sample alpha = index - (t_sample)i1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1049 // bound:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1050 i1 = index_wrap(i1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1051 i2 = index_wrap(i2);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1052 // interp:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1053 T v1 = read(i1, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1054 T v2 = read(i2, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1055 return mix(v1, v2, alpha);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1056 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1057
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1058 inline T lookup(t_sample signal, long channel=0) const {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1059 channel = channel_clamp(channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1060 t_sample index = signal2index(signal);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1061 // interp:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1062 long i1 = (long)index;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1063 long i2 = i1+1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1064 t_sample alpha = index - (t_sample)i1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1065 // bound:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1066 i1 = index_clamp(i1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1067 i2 = index_clamp(i2);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1068 // interp:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1069 T v1 = read(i1, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1070 T v2 = read(i2, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1071 return mix(v1, v2, alpha);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1072 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1073 // NO LONGER USED:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1074 inline void poke(t_sample value, t_sample index, long channel=0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1075 const long i = (long)index;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1076 if (!(index_oob(i) || channel_oob(channel))) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1077 write(fixdenorm(value), i, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1078 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1079 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1080 // NO LONGER USED:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1081 inline void splat_adding(t_sample value, t_sample phase, long channel=0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1082 const t_sample valuef = fixdenorm(value);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1083 channel = channel_clamp(channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1084 t_sample index = phase2index(phase);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1085 // interp:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1086 long i1 = (long)index;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1087 long i2 = i1+1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1088 const t_sample alpha = index - (double)i1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1089 // bound:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1090 i1 = index_wrap(i1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1091 i2 = index_wrap(i2);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1092 // interp:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1093 overdub(valuef*(1.-alpha), i1, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1094 overdub(valuef*alpha, i2, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1095 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1096 // NO LONGER USED:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1097 inline void splat(t_sample value, t_sample phase, long channel=0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1098 const t_sample valuef = fixdenorm(value);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1099 channel = channel_clamp(channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1100 t_sample index = phase2index(phase);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1101 // interp:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1102 long i1 = (long)index;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1103 long i2 = i1+1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1104 const t_sample alpha = index - (t_sample)i1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1105 // bound:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1106 i1 = index_wrap(i1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1107 i2 = index_wrap(i2);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1108 // interp:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1109 const T v1 = read(i1, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1110 const T v2 = read(i2, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1111 write(v1 + (1.-alpha)*(valuef-v1), i1, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1112 write(v2 + (alpha)*(valuef-v2), i2, channel);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1113 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1114 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1115
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1116 // DATA_MAXIMUM_ELEMENTS * 8 bytes = 256 mb limit
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1117 #define DATA_MAXIMUM_ELEMENTS (33554432)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1118
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1119 struct Data : public DataInterface<t_sample> {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1120 t_genlib_data * dataRef; // a pointer to some external source of the data
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1121
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1122 Data() : DataInterface<t_sample>() {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1123 dataRef = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1124 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1125 ~Data() {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1126 //genlib_report_message("releasing data handle %d", dataRef);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1127 if (dataRef != 0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1128 genlib_data_release(dataRef);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1129 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1130 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1131 void reset(const char * name, long s, long c) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1132 // if needed, acquire the Data's global reference:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1133 if (dataRef == 0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1134 void * ref = genlib_obtain_reference_from_string(name);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1135 dataRef = genlib_obtain_data_from_reference(ref);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1136 if (dataRef == 0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1137 genlib_report_error("failed to acquire data");
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1138 return;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1139 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1140 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1141 genlib_data_resize(dataRef, s, c);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1142 getinfo();
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1143 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1144 bool setbuffer(void * bufferRef) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1145 //genlib_report_message("set buffer %p", bufferRef);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1146 if (dataRef == 0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1147 // error: no data, or obtain?
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1148 return false;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1149 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1150 genlib_data_setbuffer(dataRef, bufferRef);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1151 getinfo();
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1152 return true;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1153 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1154
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1155 void getinfo() {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1156 t_genlib_data_info info;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1157 if (genlib_data_getinfo(dataRef, &info) == GENLIB_ERR_NONE) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1158 mData = info.data;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1159 dim = info.dim;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1160 channels = info.channels;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1161 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1162 genlib_report_error("failed to acquire data info");
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1163 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1164 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1165 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1166
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1167 // Used by SineData
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1168 struct DataLocal : public DataInterface<t_sample> {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1169 DataLocal() : DataInterface<t_sample>() {}
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1170 ~DataLocal() {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1171 if (mData) sysmem_freeptr(mData);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1172 mData = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1173 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1174
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1175 void reset(long s, long c) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1176 mData=0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1177 resize(s, c);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1178 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1179
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1180 void resize(long s, long c) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1181 if (s * c > DATA_MAXIMUM_ELEMENTS) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1182 s = DATA_MAXIMUM_ELEMENTS/c;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1183 genlib_report_message("warning: resizing data to < 256MB");
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1184 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1185 if (mData) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1186 sysmem_resizeptr(mData, sizeof(t_sample) * s * c);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1187 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1188 mData = (t_sample *)sysmem_newptr(sizeof(t_sample) * s * c);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1189 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1190 if (!mData) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1191 genlib_report_error("out of memory");
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1192 resize(512, 1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1193 return;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1194 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1195 dim = s;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1196 channels = c;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1197 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1198 set_zero64(mData, dim * channels);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1199 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1200
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1201 // copy from a buffer~
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1202 // resizing is safe only during initialization!
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1203 bool setbuffer(void *dataReference) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1204 mDataReference = dataReference; // replaced mName
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1205 bool result = false;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1206 t_genlib_buffer * b;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1207 t_genlib_buffer_info info;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1208 if (mDataReference != 0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1209 b = (t_genlib_buffer *)genlib_obtain_buffer_from_reference(mDataReference);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1210 if (b) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1211 if (genlib_buffer_edit_begin(b)==GENLIB_ERR_NONE) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1212 if (genlib_buffer_getinfo(b, &info)==GENLIB_ERR_NONE) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1213 float * samples = info.b_samples;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1214 long frames = info.b_frames;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1215 long nchans = info.b_nchans;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1216 //long size = info.b_size;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1217 //long modtime = info.b_modtime; // cache & compare?
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1218
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1219 // resizing is safe only during initialization!
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1220 if (mData == 0) resize(frames, nchans);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1221
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1222 long frames_safe = frames < dim ? frames : dim;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1223 long channels_safe = nchans < channels ? nchans : channels;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1224 // copy:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1225 for (int f=0; f<frames_safe; f++) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1226 for (int c=0; c<channels_safe; c++) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1227 t_sample value = samples[c+f*nchans];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1228 write(value, f, c);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1229 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1230 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1231 result = true;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1232 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1233 genlib_report_message("couldn't get info for buffer\n");
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1234 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1235 genlib_buffer_edit_end(b, 1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1236 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1237 genlib_report_message("buffer locked\n");
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1238 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1239 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1240 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1241 genlib_report_message("buffer reference not valid");
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1242 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1243 return result;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1244 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1245 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1246
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1247 struct Buffer : public DataInterface<float> {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1248 t_genlib_buffer * mBuf;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1249 t_genlib_buffer_info mInfo;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1250 float mDummy; // safe access in case buffer is not valid
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1251
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1252 Buffer() : DataInterface<float>() {}
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1253
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1254 void reset(const char * name) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1255 dim = 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1256 channels = 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1257 mData = &mDummy;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1258 mDummy = 0.f;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1259 mBuf = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1260
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1261 // call into genlib:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1262 mDataReference = genlib_obtain_reference_from_string(name);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1263 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1264
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1265 void setbuffer(void * ref) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1266 mDataReference = ref;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1267 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1268
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1269 void begin() {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1270 t_genlib_buffer * b = genlib_obtain_buffer_from_reference(mDataReference);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1271 mBuf = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1272 if (b) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1273 if (genlib_buffer_perform_begin(b) == GENLIB_ERR_NONE) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1274 mBuf = b;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1275 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1276 //genlib_report_message ("not a buffer~ %s", mName->s_name);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1277 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1278 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1279 //genlib_report_message("no object %s\n", mName->s_name);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1280 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1281
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1282 if (mBuf && genlib_buffer_getinfo(mBuf, &mInfo)==GENLIB_ERR_NONE) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1283 // grab data:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1284 mBuf = b;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1285 mData = mInfo.b_samples;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1286 dim = mInfo.b_frames;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1287 channels = mInfo.b_nchans;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1288 } else {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1289 //genlib_report_message("couldn't get info");
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1290 mBuf = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1291 mData = &mDummy;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1292 dim = 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1293 channels = 1;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1294 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1295 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1296
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1297 void end() {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1298 if (mBuf) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1299 genlib_buffer_perform_end(mBuf);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1300 if (modified) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1301 genlib_buffer_dirty(mBuf);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1302 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1303 modified = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1304 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1305 mBuf = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1306 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1307 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1308
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1309 struct SineData : public DataLocal {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1310 SineData() : DataLocal() {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1311 const int costable_size = 1 << 14; // 14 bit index (noise floor at around -156 dB)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1312 mData=0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1313 resize(costable_size, 1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1314 for (int i=0; i<dim; i++) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1315 mData[i] = cos(i * GENLIB_PI * 2. / (double)(dim));
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1316 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1317 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1318
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1319 ~SineData() {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1320 if (mData) sysmem_freeptr(mData);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1321 mData = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1322 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1323 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1324
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1325 template<typename T>
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1326 inline int dim(const T& data) { return data.dim; }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1327
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1328 template<typename T>
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1329 inline int channels(const T& data) { return data.channels; }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1330
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1331 // used by cycle when no buffer/data is specified:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1332 struct SineCycle {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1333
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1334 uint32_t phasei, pincr;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1335 double f2i;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1336
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1337 void reset(t_sample samplerate, t_sample init = 0) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1338 phasei = init * t_sample(4294967296.0);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1339 pincr = 0;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1340 f2i = t_sample(4294967296.0) / samplerate;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1341 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1342
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1343 inline void freq(t_sample f) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1344 pincr = f * f2i;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1345 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1346
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1347 inline void phase(t_sample f) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1348 phasei = f * t_sample(4294967296.0);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1349 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1350
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1351 inline t_sample phase() const {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1352 return phasei * t_sample(0.232830643653869629e-9);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1353 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1354
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1355 template<typename T>
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1356 inline t_sample operator()(const DataInterface<T>& buf) {
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1357 T * data = buf.mData;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1358 // divide uint32_t range down to buffer size (32-bit to 14-bit)
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1359 uint32_t idx = phasei >> 18;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1360 // compute fractional portion and divide by 18-bit range
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1361 const t_sample frac = t_sample(phasei & 262143) * t_sample(3.81471181759574e-6);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1362 // index safely in 14-bit range:
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1363 const t_sample y0 = data[idx];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1364 const t_sample y1 = data[(idx+1) & 16383];
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1365 const t_sample y = linear_interp(frac, y0, y1);
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1366 phasei += pincr;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1367 return y;
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1368 }
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1369 };
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1370
24d60bdea349 ClonneChannel
prymula <prymula76@outlook.com>
parents:
diff changeset
1371 #endif