-
Notifications
You must be signed in to change notification settings - Fork 7
/
windowingstub.cpp
276 lines (214 loc) · 7.72 KB
/
windowingstub.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
/* Windowingstub,
Featuring the Super Destroy FX Windowing System! */
#include "windowingstub.h"
#include <stdio.h>
#if defined(TARGET_API_VST) && TARGET_PLUGIN_HAS_GUI
#ifndef __DFX_WINDOWINGSTUBEDITOR_H
#include "windowingstubeditor.hpp"
#endif
#endif
/* this macro does boring entry point stuff for us */
DFX_ENTRY(Windowingstub);
DFX_CORE_ENTRY(WindowingstubDSP);
PLUGIN::PLUGIN(TARGET_API_BASE_INSTANCE_TYPE inInstance)
: DfxPlugin(inInstance, NUM_PARAMS, NUM_PRESETS) {
initparameter_indexed(P_BUFSIZE, "wsize", 9, 9, BUFFERSIZESSIZE, kDfxParamUnit_samples);
initparameter_indexed(P_SHAPE, "wshape", WINDOW_TRIANGLE, WINDOW_TRIANGLE, MAX_WINDOWSHAPES);
long i;
/* set up values for windowing */
char bufstr[64];
for (i=0; i < BUFFERSIZESSIZE; i++) {
if (buffersizes[i] > 1000)
sprintf(bufstr, "%ld,%03ld", buffersizes[i]/1000, buffersizes[i]%1000);
else
sprintf(bufstr, "%ld", buffersizes[i]);
setparametervaluestring(P_BUFSIZE, i, bufstr);
}
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 (i=NUM_WINDOWSHAPES; i < MAX_WINDOWSHAPES; i++)
setparametervaluestring(P_SHAPE, i, "???");
long delay_samples = buffersizes[getparameter_i(P_BUFSIZE)];
setlatency_samples(delay_samples);
settailsize_samples(delay_samples);
setpresetname(0, "Windowingstub 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
#if TARGET_PLUGIN_USES_DSPCORE
initCores<WindowingstubDSP>();
#endif
#ifdef TARGET_API_VST
/* if you have a GUI, need an Editor class... */
#if TARGET_PLUGIN_HAS_GUI
editor = new WindowingstubEditor(this);
#endif
#endif
}
PLUGIN::~PLUGIN() {
#ifdef TARGET_API_VST
/* VST doesn't have initialize and cleanup methods like Audio Unit does,
so we need to call this manually here */
do_cleanup();
#endif
}
PLUGINCORE::PLUGINCORE(DfxPlugin * inInstance)
: DfxPluginCore(inInstance) {
/* determine the size of the largest window size */
long maxframe = 0;
for (int i = 0; i < BUFFERSIZESSIZE; i++)
maxframe = (buffersizes[i] > maxframe) ? buffersizes[i] : maxframe;
/* add some leeway? */
in0 = (float*)malloc(maxframe * sizeof (float));
out0 = (float*)malloc(maxframe * 2 * sizeof (float));
/* prevmix is only a single third long */
prevmix = (float*)malloc((maxframe / 2) * sizeof (float));
}
PLUGINCORE::~PLUGINCORE() {
/* windowing buffers */
free (in0);
free (out0);
free (prevmix);
}
void PLUGINCORE::reset() {
framesize = buffersizes[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 */
for (int i = 0; i < third; i ++) {
prevmix[i] = 0.0f;
}
for (int j = 0; j < framesize; j ++) {
out0[j] = 0.0f;
}
/* start input at beginning. Output has a frame of silence. */
insize = 0;
outstart = 0;
outsize = framesize;
dfxplugin->setlatency_samples(framesize);
/* tail is the same as delay, of course */
dfxplugin->settailsize_samples(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 * in, float * out, long samples) {
memmove(out, in, samples * sizeof (float));
}
/* 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(const float *tin, float *tout, unsigned long samples, bool replacing) {
int z = 0;
for (unsigned long ii = 0; ii < samples; ii++) {
/* copy sample in */
in0[insize] = tin[ii];
insize ++;
if (insize == framesize) {
/* frame is full! */
/* in0 -> process -> out0(first free space) */
processw(in0, out0+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 */
memcpy(prevmix, out0 + outstart + outsize + third, third * sizeof (float));
/* copy 2nd third of input over in0 (need to re-use it for next frame),
now insize = third */
memcpy(in0, in0 + third, third * sizeof (float));
insize = third;
outsize += third;
}
/* send sample out */
#ifdef TARGET_API_VST
if (replacing)
#endif
tout[ii] = out0[outstart];
#ifdef TARGET_API_VST
else tout[ii] += out0[outstart];
#endif
outstart ++;
outsize --;
/* make sure there is always enough room for a frame in out buffer */
if (outstart == third) {
memmove(out0, out0 + outstart, outsize * sizeof (float));
outstart = 0;
}
}
}
void PLUGIN::makepresets() {
/* initialize presets here, see geometer for an example. */
}