forked from dukesrg/logue-fx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvibrator.cpp
216 lines (194 loc) · 4.96 KB
/
vibrator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* File: vibrato.cpp
*
* Simple vibrato with pitch shifter for Modulation FX, Delay FX and Reverb FX
*
* 2019 (c) Oleg Burdaev
* mailto: [email protected]
*
*/
#include "buffer_ops.h"
#include "fxwrapper.h"
#include "osc_api.h"
#include "simplelfo.hpp"
#define MAX_RATE 30.f //maximum LFO rate
#define MAX_DEPTH 1.f //maximum LFO depth in semitones
#define MAX_SHIFT 12 //maximum frequency shift spread in semitones
#define BUF_SIZE (k_samplerate / 100)
#define F_FACTOR 1.0594631f //chromatic semitone frequency factor
static float s_depth;
#ifdef VIBRATORv2
static uint32_t s_lfo_index = 0;
#else
static float s_speed = 0.f;
#endif
static float s_read_pos;
static uint32_t s_write_pos;
static dsp::SimpleLFO s_lfo;
static __sdram f32pair_t s_loop[BUF_SIZE];
#ifdef FX_MODFX_SUB
static __sdram f32pair_t s_loop_sub[BUF_SIZE];
#endif
#ifdef VIBRATORv2
inline __attribute__((optimize("Ofast"),always_inline))
float sine_bi(void) {
return s_lfo.sine_bi();
}
inline __attribute__((optimize("Ofast"),always_inline))
float sine_uni(void) {
return s_lfo.sine_uni();
}
inline __attribute__((optimize("Ofast"),always_inline))
float triangle_bi(void) {
return s_lfo.triangle_bi();
}
inline __attribute__((optimize("Ofast"),always_inline))
float triangle_uni(void) {
return s_lfo.triangle_uni();
}
inline __attribute__((optimize("Ofast"),always_inline))
float saw_bi(void) {
return s_lfo.saw_bi();
}
inline __attribute__((optimize("Ofast"),always_inline))
float saw_uni(void) {
return s_lfo.saw_uni();
}
inline __attribute__((optimize("Ofast"),always_inline))
float square_bi(void) {
return s_lfo.square_bi();
}
inline __attribute__((optimize("Ofast"),always_inline))
float square_uni(void) {
return s_lfo.square_uni();
}
inline __attribute__((optimize("Ofast"),always_inline))
float snh_bi(void) {
static float snh, sq_old;
float sq_new = s_lfo.square_uni();
if (sq_new != sq_old) {
sq_old = sq_new;
snh = fx_white();
}
return snh;
}
inline __attribute__((optimize("Ofast"),always_inline))
float snh_uni(void) {
static float snh, sq_old;
float sq_new = s_lfo.square_uni();
if (sq_new != sq_old) {
sq_old = sq_new;
snh = si_fabsf(fx_white());
}
return snh;
}
static float (*s_lfo_ptr[])() = {
sine_uni,
triangle_uni,
saw_uni,
square_uni,
snh_uni,
sine_bi,
triangle_bi,
saw_bi,
square_bi,
snh_bi
};
#endif
FX_INIT
{
s_depth = 0.f;
s_read_pos = 0.f;
s_write_pos = 0;
s_lfo.reset();
s_lfo.setF0(0.f, k_samplerate_recipf);
buf_clr_f32((float*)s_loop, BUF_SIZE * sizeof(f32pair_t)/sizeof(float));
#ifdef FX_MODFX_SUB
buf_clr_f32((float*)s_loop_sub, BUF_SIZE * sizeof(f32pair_t)/sizeof(float));
#endif
}
FX_PROCESS
{
#ifdef FX_MODFX
f32pair_t * __restrict y = (f32pair_t*)yn;
#endif
#ifdef FX_MODFX_SUB
f32pair_t * __restrict y_sub = (f32pair_t*)sub_yn;
#endif
#ifdef VIBRATORv2
float (*func_lfo)() = s_lfo_ptr[s_lfo_index];
if (s_lfo_index < 5) {
#ifdef FX_MODFX_SUB
for (f32pair_t * __restrict x = (f32pair_t*)xn, * __restrict x_sub = (f32pair_t*)sub_xn; frames--; x++, x_sub++) {
#else
for (f32pair_t * __restrict x = (f32pair_t*)xn; frames--; x++) {
#endif
#ifdef FX_MODFX_SUB
*(y_sub++) = f32pair_mulscal(*x_sub, 1.f - (*func_lfo)() * s_depth);
#endif
#ifdef FX_MODFX
*(y++) = f32pair_mulscal(*x, 1.f - (*func_lfo)() * s_depth);
#else
*x = f32pair_mulscal(*x, 1.f - (*func_lfo)() * s_depth);
#endif
s_lfo.cycle();
}
} else {
#endif
#ifdef FX_MODFX_SUB
for (f32pair_t * __restrict x = (f32pair_t*)xn, * __restrict x_sub = (f32pair_t*)sub_xn; frames--; x++, x_sub++) {
#else
for (f32pair_t * __restrict x = (f32pair_t*)xn; frames--; x++) {
#endif
uint32_t pos = (uint32_t)s_read_pos;
#ifdef FX_MODFX_SUB
f32pair_t valp_sub = *x_sub;
*(y_sub++) = f32pair_linint(s_read_pos - pos, s_loop_sub[pos], s_loop_sub[pos < BUF_SIZE ? pos : 0]);
s_loop_sub[s_write_pos] = valp_sub;
#endif
f32pair_t valp = *x;
#ifdef FX_MODFX
*(y++) = f32pair_linint(s_read_pos - pos, s_loop[pos], s_loop[pos < BUF_SIZE ? pos : 0]);
#else
*x = f32pair_linint(s_read_pos - pos, s_loop[pos], s_loop[pos < BUF_SIZE ? pos : 0]);
#endif
s_loop[s_write_pos] = valp;
#ifdef VIBRATORv2
s_read_pos += fastpowf(F_FACTOR, (*func_lfo)() * s_depth);
#else
s_read_pos += fastpowf(F_FACTOR, s_speed + s_lfo.sine_bi() * s_depth);
#endif
if ((uint32_t)s_read_pos >= BUF_SIZE)
s_read_pos -= BUF_SIZE;
s_write_pos++;
if (s_write_pos >= BUF_SIZE)
s_write_pos = 0;
s_lfo.cycle();
}
#ifdef VIBRATORv2
}
#endif
}
FX_PARAM
{
const float valf = q31_to_f32(value);
switch (index) {
case FX_PARAM_TIME: //vibrato rate
s_lfo.setF0(valf * MAX_RATE, k_samplerate_recipf);
break;
case FX_PARAM_DEPTH: //vibrato depth
s_depth = valf * MAX_DEPTH;
break;
#ifndef FX_MODFX
case FX_PARAM_SHIFT_DEPTH:
#ifdef VIBRATORv2
s_lfo_index = clipmaxf(si_floorf(valf * 10), 10 - 1);
#else
s_speed = (valf - .5f) * MAX_SHIFT * 2; //pitch shift
#endif
break;
#endif
default:
break;
}
}