-
Notifications
You must be signed in to change notification settings - Fork 12
/
stuck.c
506 lines (478 loc) · 14.9 KB
/
stuck.c
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//Spencer Jackson
//stuck.c
#include<lv2.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include"rms_calc.h"
#include"stuck.h"
//#define CV_PORTS
#define AUTOCORR
//#define COMP
#ifdef AUTOCORR
#define START_SCORE 0
#else //least squared error
#define START_SCORE 200
#endif
enum states
{
INACTIVE = 0,
LOADING,
MATCHING,
LOADING_XFADE,
XFADE_ONLY,
PLAYING,
RELEASING,
QUICK_RELEASING,
DEBUGGING,
};
typedef struct _STUCK
{
uint16_t indx;//current write point in buffer
uint16_t indx2;//working/read point in buffer
uint16_t bufsize;//size of buffer
uint16_t wavesize;//size of waveform
uint16_t acorr_size;//size of autocorrelation
uint16_t xfade_size;
uint16_t wave_min;//int16_test allowed wavesize
uint16_t wave_max;//int32_test allowed wavesize
uint8_t state;
uint8_t stack;
uint8_t dbg;//used for whatever, delete it
double sample_freq;
float *buf;
float gain;
float env;//envelope gain to normalize compression to
float score;
float shortscore;
RMS_CALC rms_calc;
float *input_p;
float *output_p;
float *trigger_p;
float *stick_it_p;
float *drone_gain_p;
float *release_p;
float *dbg_p;
float *output2_p;
float *xf_func;
} STUCK;
void run_stuck(LV2_Handle handle, uint32_t nframes)
{
STUCK* plug = (STUCK*)handle;
uint32_t i,j,k,t,chunk=0;
double slope = 0;
double interp;
if(plug->stack)
for(i=0;i<nframes;i++)
plug->output_p[i] = 0;
else
memcpy(plug->output_p,plug->input_p,nframes*sizeof(float));
interp = nframes>64?nframes:64;
//Evaluate port values and see if it requires any state changes
if(plug->state == INACTIVE)
{
//decide if triggered
#ifdef CV_PORTS
if(*plug->stick_it_p >= 1 || plug->trigger_p[nframes-1] >= 1)
#else
if(*plug->stick_it_p >= 1)
#endif
{
plug->state = LOADING;
#ifdef COMP
plug->env = plug->rms_calc.rms;
}
else
{
rms_block_fill(&plug->rms_calc, plug->input_p,nframes);
#else
}
else{
#endif
return;
}
}
else if(plug->state < LOADING_XFADE)
{
//decide if need to abort
#ifdef CV_PORTS
if(*plug->stick_it_p < 1 && plug->trigger_p[nframes-1] < 1)
#else
if(*plug->stick_it_p < 1)
#endif
{
//reinit
plug->indx = 0;
plug->indx2 = plug->wave_min;
plug->state = INACTIVE;
plug->gain = 0;
plug->wavesize = plug->wave_max;
plug->score = START_SCORE;
plug->shortscore = .25*START_SCORE;
#ifdef COMP
rms_block_fill(&plug->rms_calc, plug->input_p,nframes);
#endif
return;
}
}
else if(plug->state < RELEASING)
{
//decide if released
#ifdef CV_PORTS
if(*plug->stick_it_p < 1 && plug->trigger_p[nframes-1] < 1)
#else
if(*plug->stick_it_p < 1)
#endif
{
plug->state = RELEASING;
}
}
else if(plug->state == RELEASING)
{
//decide if new trigger has been sent before release is complete
#ifdef CV_PORTS
if(*plug->stick_it_p >= 1 || plug->trigger_p[nframes-1] >= 1)
#else
if(*plug->stick_it_p >= 1)
#endif
{
plug->state = QUICK_RELEASING;
}
#ifdef COMP
else
{
rms_block_fill(&plug->rms_calc, plug->input_p,nframes);
}
#endif
}
//now run the state machine
for(i=0; i<nframes;)
{
chunk = nframes - i;
if(plug->state == LOADING)//load enough frames to start calculating the autocorrelation
{
//decide if reaching minimum length in this period
if(plug->indx+chunk >= plug->wave_min+plug->acorr_size)
{
chunk = plug->wave_min + plug->acorr_size - plug->indx;
plug->state = MATCHING;
}
//load buffer with compressed signal
for(j=0; j<chunk; j++)
{
#ifdef COMP
plug->buf[plug->indx++] = plug->input_p[i]*plug->env/rms_shift(&plug->rms_calc,plug->input_p[i]);
#else
plug->buf[plug->indx++] = plug->input_p[i];
#endif
i++;
}
}
else if(plug->state == MATCHING)//find autocorrelation
{
if(plug->indx2+chunk >= plug->wave_max)
{
chunk = plug->wave_max - plug->indx2;
plug->state = LOADING_XFADE;
}
// calculate autocorrelation of sample in buffer, save the minimum
float tmp,score;
for(j=0; j<chunk; j++)
{
#ifdef COMP
plug->buf[plug->indx++] = plug->input_p[i]*plug->env/rms_shift(&plug->rms_calc,plug->input_p[i]);
#else
plug->buf[plug->indx++] = plug->input_p[i];
#endif
i++;
score = 0;
t=0;
#ifdef AUTOCORR
for(k=plug->indx2; k<plug->indx2+(plug->acorr_size>>2); k++)
{
score += plug->buf[k]*plug->buf[t++];
}
if(score >= plug->shortscore)
{
//full calc
plug->shortscore = .9*score;
for( ; k<plug->indx2+plug->acorr_size; k++)
score += plug->buf[k]*plug->buf[t++];
}
//save place if score is higher than last highest
if(score>=plug->score)
#else
for(k=plug->indx2; k<plug->indx2+plug->acorr_size && score<=plug->score; k++)
{
tmp = plug->buf[k] - plug->buf[t++];//jsyk this isn't the strict definition of an autocorrelation, a variation on the principle
score += tmp*tmp;
}
//save place if score is lower than last minimum
if(score<=plug->score)
#endif
{
plug->wavesize = plug->indx2;
plug->score = score;
}
plug->indx2++;
}
if(plug->indx2>=plug->wave_max)
{
plug->indx2 = 0;//reset indx2
//xfade in beginning of sample, these will be halved in the next state
for(k=0; k<plug->xfade_size; k++)
plug->buf[k] *= k/plug->xfade_size;
}
}
else if(plug->state == LOADING_XFADE)//xfade end of buffer with start (loop it) over an entire wave and fade in drone
{
slope = (*plug->drone_gain_p-plug->gain)/interp;
//decide if xfade ends in this period
if(plug->indx2+chunk >= plug->wavesize)
{
//this means it has already played through the loop once and finished the xfade (layering really)
chunk = plug->wavesize - plug->indx2;
plug->state = PLAYING;
}
//decide if going to overflow
if(plug->indx+chunk >= plug->bufsize)
{
//this means we've already filled the buffer, but haven't finished the xfade
chunk = plug->bufsize - plug->indx;
plug->state = XFADE_ONLY;
}
//load buffer with xfade
for(j=0; j<chunk; j++)
{
//still loading end of buffer
#ifdef COMP
plug->buf[plug->indx++] = plug->input_p[i]*plug->env/rms_shift(&plug->rms_calc,plug->input_p[i]);
#else
plug->buf[plug->indx++] = plug->input_p[i];
#endif
//layer 2 full cycles on top of each other
plug->buf[plug->indx2] = .5*plug->buf[plug->indx2+plug->wavesize] + .5*plug->buf[plug->indx2];
//but now also playing back start of buffer
plug->output_p[i++] += plug->gain*plug->buf[plug->indx2++];
plug->gain += slope;
}
//xfade out end if we're there else we'll do it in the next state
if(plug->indx2>=plug->wavesize)//TODO: this must actually be ==
{
for(k=0; k<plug->xfade_size; k++)
plug->buf[k] += .5*(1-k/plug->xfade_size)*plug->buf[plug->indx2+plug->wavesize+k];
plug->indx2 = 0;
}
}
else if(plug->state == XFADE_ONLY)//xfade after buffer is full, in practice we never get here, but we might change our smoothing strategy again
{
slope = (*plug->drone_gain_p-plug->gain)/interp;
//decide if xfade ends in this period
if(plug->indx2+chunk >= plug->wavesize)
{
chunk = plug->wavesize - plug->indx2;
plug->state = PLAYING;
}
//xfade buffer
for(j=0; j<chunk; j++)
{
//continue layering the 2 cycles
plug->buf[plug->indx2] = .5*plug->buf[plug->indx2+plug->wavesize] + .5*plug->buf[plug->indx2];
plug->output_p[i++] += plug->gain*plug->buf[plug->indx2++];
plug->gain += slope;
}
//xfade out end
if(plug->indx2>=plug->wavesize)//TODO: this must acutally be ==
{
for(k=0; k<plug->xfade_size; k++)
plug->buf[k] += .5*(1-k/plug->xfade_size)*plug->buf[plug->indx2+plug->wavesize+k];
plug->indx2 = 0;
}
}
else if(plug->state == PLAYING)//just loop buffer and track gain changes
{
slope = (*plug->drone_gain_p-plug->gain)/interp;
for(j=0; j<chunk; j++)
{
plug->output_p[i++] += plug->gain*plug->buf[plug->indx2++];
plug->gain += slope;
plug->indx2 = plug->indx2<plug->wavesize?plug->indx2:0;
}
}
else if(plug->state == RELEASING)
{
slope = -*plug->drone_gain_p/(*plug->release_p*plug->sample_freq);
//decide if released in this period
if(plug->gain + chunk*slope < slope)
{
chunk = -plug->gain/slope;
plug->state = INACTIVE;
}
for(j=0; j<chunk; j++)
{
plug->output_p[i++] += plug->gain*plug->buf[plug->indx2++];
plug->gain += slope;
plug->indx2 = plug->indx2<plug->wavesize?plug->indx2:0;
}
if(plug->gain <= -slope)
{
plug->indx = 0;
plug->indx2 = plug->wave_min;
plug->state = INACTIVE;
plug->gain = 0;
plug->wavesize = plug->wave_max;
plug->score = START_SCORE;
plug->shortscore = .25*START_SCORE;
return;
}
}
else if(plug->state == QUICK_RELEASING)
{
slope = -*plug->drone_gain_p/(double)plug->wave_min;
//decide if released in this period
if(plug->gain + chunk*slope < slope)
{
chunk = -plug->gain/slope;
plug->state = LOADING;
}
for(j=0; j<chunk; j++)
{
plug->output_p[i++] += plug->gain*plug->buf[plug->indx2++];
plug->gain += slope;
plug->indx2 = plug->indx2<plug->wavesize?plug->indx2:0;
}
#ifdef COMP
rms_block_fill(&plug->rms_calc, plug->input_p,chunk);
#endif
if(plug->gain <= -slope)
{
plug->indx = 0;
plug->indx2 = plug->wave_min;
plug->state = LOADING;
plug->wavesize = plug->wave_max;
plug->score = START_SCORE;
plug->shortscore = .25*START_SCORE;
#ifdef COMP
plug->env = plug->rms_calc.rms;
#endif
}
}
}
return;
}
LV2_Handle init_stuck(const LV2_Descriptor *descriptor,double sample_freq, const char *bundle_path,const LV2_Feature * const* host_features)
{
STUCK* plug = malloc(sizeof(STUCK));
uint16_t tmp;
uint8_t i;
plug->sample_freq = sample_freq;
tmp = 0x8000;//15 bits
if(sample_freq<100000)//88.1 or 96.1kHz
tmp = tmp>>1;//14 bits
if(sample_freq<50000)//44.1 or 48kHz
tmp = tmp>>1;//13 bits //8192
plug->buf = (float*)malloc(tmp*sizeof(float));
plug->bufsize = tmp;
plug->acorr_size = tmp>>3;//1024 if you mess with this, keep in mind it may need change in the default score value
plug->xfade_size = tmp>>6;//128
plug->wave_max = (tmp - plug->xfade_size)>>1;//4064
plug->wave_min = tmp>>6;//128
plug->wavesize = plug->wave_max;
plug->indx = 0;
plug->indx2 = plug->wave_min;
plug->state = INACTIVE;
plug->gain = 0;
plug->score = START_SCORE;
plug->shortscore = .25*START_SCORE;
plug->env = 0;
plug->stack = 0;
plug->dbg = 0;
//half rasied cosine for equal power xfade
plug->xf_func = (float*)malloc(plug->xfade_size*sizeof(float));
for (i = 0; i < plug->xfade_size; i++) plug->xf_func[i] = 0.5 * (1 - cos((M_PI * i / plug->xfade_size)));
rms_init(&plug->rms_calc,tmp>>3);
return plug;
}
LV2_Handle init_stuckstacker(const LV2_Descriptor *descriptor,double sample_freq, const char *bundle_path,const LV2_Feature * const* host_features)
{
STUCK* plug = (STUCK*)init_stuck(descriptor, sample_freq, bundle_path, host_features);
plug->stack = 1;
return plug;
}
void connect_stuck_ports(LV2_Handle handle, uint32_t port, void *data)
{
STUCK* plug = (STUCK*)handle;
switch(port)
{
case IN:
plug->input_p = (float*)data;
break;
case OUT:
plug->output_p = (float*)data;
break;
case TRIGGER:
plug->trigger_p = (float*)data;
break;
case STICKIT:
plug->stick_it_p = (float*)data;
break;
case DRONEGAIN:
plug->drone_gain_p = (float*)data;
break;
case RELEASE:
plug->release_p = (float*)data;
break;
case DBG:
plug->dbg_p = (float*)data;
break;
case OUT2:
plug->output2_p = (float*)data;
break;
default:
puts("UNKNOWN PORT YO!!");
}
}
void cleanup_stuck(LV2_Handle handle)
{
STUCK* plug = (STUCK*)handle;
rms_deinit(&plug->rms_calc);
free(plug->buf);
free(plug->xf_func);
free(plug);
}
static const LV2_Descriptor stuck_descriptor=
{
STUCK_URI,
init_stuck,
connect_stuck_ports,
0,//activate
run_stuck,
0,//deactivate
cleanup_stuck,
0//extension
};
static const LV2_Descriptor stuckstacker_descriptor=
{
STUCKSTACKER_URI,
init_stuckstacker,
connect_stuck_ports,
0,//activate
run_stuck,
0,//deactivate
cleanup_stuck,
0//extension
};
LV2_SYMBOL_EXPORT
const LV2_Descriptor* lv2_descriptor(uint32_t index)
{
switch (index)
{
case 0:
return &stuck_descriptor;
case 1:
return &stuckstacker_descriptor;
default:
return 0;
}
}