-
Notifications
You must be signed in to change notification settings - Fork 7
/
slowft.cpp
334 lines (253 loc) · 9.79 KB
/
slowft.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*------------------------------------------------------------------------
Copyright (C) 2005-2023 Tom Murphy 7
This file is part of Slowft.
Slowft is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Slowft is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Slowft. If not, see <http:https://www.gnu.org/licenses/>.
To contact the author, use the contact form at http:https://destroyfx.org
Slowft, featuring the Super Destroy FX Windowing System!
------------------------------------------------------------------------*/
#include "slowft.h"
#include <algorithm>
#include <array>
#include <cstdio>
#include "dfxmath.h"
/* this macro does boring entry point stuff for us */
DFX_ENTRY(Slowft);
DFX_CORE_ENTRY(SlowftDSP);
PLUGIN::PLUGIN(TARGET_API_BASE_INSTANCE_TYPE inInstance)
: DfxPlugin(inInstance, NUM_PARAMS, NUM_PRESETS) {
initparameter_indexed(P_BUFSIZE, {"wsize"}, 9, 9, std::ssize(buffersizes), kDfxParamUnit_samples);
initparameter_indexed(P_SHAPE, {"wshape"}, WINDOW_TRIANGLE, WINDOW_TRIANGLE, MAX_WINDOWSHAPES);
/* set up values for windowing */
std::array<char, 64> bufstr {};
for (size_t i = 0; i < buffersizes.size(); i++) {
if (buffersizes[i] > 1000)
std::snprintf(bufstr.data(), bufstr.size(), "%ld,%03ld", buffersizes[i]/1000, buffersizes[i]%1000);
else
std::snprintf(bufstr.data(), bufstr.size(), "%ld", buffersizes[i]);
setparametervaluestring(P_BUFSIZE, static_cast<long>(i), bufstr.data());
}
setparametervaluestring(P_SHAPE, WINDOW_TRIANGLE, "linear");
setparametervaluestring(P_SHAPE, WINDOW_ARROW, "arrow");
setparametervaluestring(P_SHAPE, WINDOW_WEDGE, "wedge");
setparametervaluestring(P_SHAPE, WINDOW_COS, "best");
for (int i = NUM_WINDOWSHAPES; i < MAX_WINDOWSHAPES; i++)
setparametervaluestring(P_SHAPE, i, "???");
auto const delay_samples = dfx::math::ToUnsigned(buffersizes.at(getparameter_i(P_BUFSIZE)));
setlatency_samples(delay_samples);
settailsize_samples(delay_samples);
setpresetname(0, "Slowft Default"); /* default preset name */
makepresets();
/* allow MIDI keys to be used to control parameters */
dfxsettings->setAllowPitchbendEvents(true);
dfxsettings->setAllowNoteEvents(true);
#if !TARGET_PLUGIN_USES_DSPCORE
addchannelconfig(1, 1); /* mono */
#endif
}
PLUGINCORE::PLUGINCORE(DfxPlugin& inInstance)
: DfxPluginCore(inInstance) {
/* determine the size of the largest window size */
constexpr auto maxframe = *std::ranges::max_element(buffersizes);
/* add some leeway? */
in0.assign(maxframe, 0.f);
out0.assign(maxframe * 2, 0.f);
/* prevmix is only a single third long */
prevmix.assign(maxframe / 2, 0.f);
}
void PLUGINCORE::reset() {
framesize = buffersizes.at(getparameter_i(P_BUFSIZE));
third = framesize / 2;
bufsize = third * 3;
/* set up buffers. Prevmix and first frame of output are always
filled with zeros. XXX memset */
std::fill_n(prevmix.begin(), third, 0.f);
std::fill_n(out0.begin(), framesize, 0.f);
/* start input at beginning. Output has a frame of silence. */
insize = 0;
outstart = 0;
outsize = framesize;
dfxplugin->setlatency_samples(dfx::math::ToUnsigned(framesize));
/* tail is the same as delay, of course */
dfxplugin->settailsize_samples(dfx::math::ToUnsigned(framesize));
}
void PLUGINCORE::processparameters() {
shape = getparameter_i(P_SHAPE);
#ifdef TARGET_API_VST
/* this tells the host to call a suspend()-resume() pair,
which updates initialDelay value */
if (getparameterchanged(P_BUFSIZE))
dfxplugin->setlatencychanged(true);
#endif
}
/* this processes an individual window. Basically, this is where you
write your DSP, and it will be always called with the same sample
size (as long as the block size parameter stays the same) and
automatically overlapped. */
void PLUGINCORE::processw(float const * in, float * out, long samples) {
/* compute the 'slow fourier transform' */
/* XXX get sample rate from parameter somewhere. */
float rate = 44100.0f;
/* freq given in hz */
{
float freq = BASE_FREQ;
for(size_t key = 0; key < NUM_KEYS; key ++) {
/* compute dot product */
sines[key] = 0.0f;
cosines[key] = 0.0f;
/* PERF this is probably really inefficient. pre-computing
tables of sines first, at least, would probably help. */
for(int s = 0; s < samples; s ++) {
// float frac = ((float)s / (float)samples);
/* PERF This could be strength-reduced */
float seconds = ((float)s / (float)rate);
/* argument to sin, cosine. */
float arg = freq * seconds * SLOWFT_2PI;
sines[key] += sin(arg) * in[s];
cosines[key] += cos(arg) * in[s];
}
/* XXX this normalization is wrong: it should be the
maximum possible score, which is the area under
the curve of abs(sin(..)) within the region. */
sines[key] /= (float)samples;
cosines[key] /= (float)samples;
/* go to next key */
freq *= HALFSTEP_RATIO;
}
}
/* XXX do ops... */
size_t maxkey = 0;
{
float maxval = 0.0;
for(size_t k = 12; k < NUM_KEYS; k ++) {
float tval = abs(sines[k]) + abs(cosines[k]);
if (tval > maxval) {
maxkey = k;
maxval = tval;
}
}
}
/* now generate output! */
/* Start silent */
{
for(int s = 0; s < samples; s++) {
out[s] = 0.0f;
}
}
{
float freq = BASE_FREQ;
/* now add back in sines and cosines */
for(size_t key = 0; key < NUM_KEYS; key ++) {
if (key == maxkey)
for(int s = 0; s < samples; s ++) {
float seconds = ((float)s / (float)rate);
/* argument to sin, cosine. */
float arg = freq * seconds * SLOWFT_2PI;
out[s] += (sines[key] * sin(arg)) + (cosines[key] * cos(arg));
}
freq *= HALFSTEP_RATIO;
}
}
}
/* this fake process function reads samples one at a time
from the true input. It simultaneously copies samples from
the beginning of the output buffer to the true output.
We maintain that out0 always has at least 'third' samples
in it; this is enough to pick up for the delay of input
processing and to make sure we always have enough samples
to fill the true output buffer.
If the input frame is full:
- calls wprocess on this full input frame
- applies the windowing envelope to the tail of out0 (output frame)
- mixes in prevmix with the first half of the output frame
- increases outsize so that the first half of the output frame is
now available output
- copies the second half of the output to be prevmix for next frame.
- copies the second half of the input buffer to the first,
resets the size (thus we process each third-size chunk twice)
If we have read more than 'third' samples out of the out0 buffer:
- Slide contents to beginning of buffer
- Reset outstart
*/
/* to improve:
- use memcpy and arithmetic instead of
sample-by-sample copy
- can we use tail of out0 as prevmix, instead of copying?
- can we use circular buffers instead of memmoving a lot?
(probably not)
*/
void PLUGINCORE::process(std::span<float const> tin, std::span<float> tout) {
int z = 0;
for (size_t ii = 0; ii < tout.size(); ii++) {
/* copy sample in */
in0[insize] = tin[ii];
insize ++;
if (insize == framesize) {
/* frame is full! */
/* in0 -> process -> out0(first free space) */
processw(in0.data(), out0.data()+outstart+outsize, framesize);
float oneDivThird = 1.0f / (float)third;
/* apply envelope */
switch(shape) {
case WINDOW_TRIANGLE:
for(z = 0; z < third; z++) {
out0[z+outstart+outsize] *= ((float)z * oneDivThird);
out0[z+outstart+outsize+third] *= (1.0f - ((float)z * oneDivThird));
}
break;
case WINDOW_ARROW:
for(z = 0; z < third; z++) {
float p = (float)z * oneDivThird;
p *= p;
out0[z+outstart+outsize] *= p;
out0[z+outstart+outsize+third] *= (1.0f - p);
}
break;
case WINDOW_WEDGE:
for(z = 0; z < third; z++) {
float p = sqrtf((float)z * oneDivThird);
out0[z+outstart+outsize] *= p;
out0[z+outstart+outsize+third] *= (1.0f - p);
}
break;
case WINDOW_COS:
for(z = 0; z < third; z ++) {
float p = 0.5f * (-cosf(PI * ((float)z * oneDivThird)) + 1.0f);
out0[z+outstart+outsize] *= p;
out0[z+outstart+outsize+third] *= (1.0f - p);
}
break;
}
/* mix in prevmix */
for(int u = 0; u < third; u ++)
out0[u+outstart+outsize] += prevmix[u];
/* prevmix becomes out1 */
std::copy_n(std::next(out0.cbegin(), outstart + outsize + third), third, prevmix.begin());
/* copy 2nd third of input over in0 (need to re-use it for next frame),
now insize = third */
std::copy_n(std::next(in0.cbegin(), third), third, in0.begin());
insize = third;
outsize += third;
}
/* send sample out */
tout[ii] = out0[outstart];
outstart ++;
outsize --;
/* make sure there is always enough room for a frame in out buffer */
if (outstart == third) {
memmove(out0.data(), out0.data() + outstart, outsize * sizeof (float));
outstart = 0;
}
}
}
void PLUGIN::makepresets() {
/* initialize presets here, see geometer for an example. */
}