-
Notifications
You must be signed in to change notification settings - Fork 0
/
VoxRes.cpp
95 lines (84 loc) · 2.96 KB
/
VoxRes.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
#include "SC_PlugIn.h"
// InterfaceTable contains pointers to functions in the host
static InterfaceTable *ft;
struct VoxRes : public Unit
{
float* resampled;
float* local_buf;
int resampledFrames;
float* autocor_buffer;
float* lpc;
float* lpc_work;
float* res;
float* res_work;
int lpc_size;
int max_resonances;
SndBuf *m_buf;
float m_fbufnum;
int m_framepos;
int offset;
};
static void VoxRes_next_k(VoxRes *unit, int inNumSamples);
static void VoxRes_Ctor(VoxRes *unit);
extern "C"
{
float* vox_box_autocorrelate_mut_f32(const float*, size_t, size_t, float*);
void vox_box_resample_mut_f32(const float*, size_t, size_t, float*);
void vox_box_normalize_f32(void*, size_t);
void vox_box_lpc_mut_f32(const float*, size_t, size_t, float*, float*);
float* vox_box_resonances_mut_f32(float*, size_t, float, int*, float*, float*);
}
void VoxRes_Ctor(VoxRes *unit)
{
SETCALC(VoxRes_next_k);
unit->lpc_size = ZIN0(1);
unit->resampledFrames = ZIN0(2);
unit->m_buf = unit->mWorld->mSndBufs;
unit->m_fbufnum = -1.f;
unit->m_framepos = 0;
GET_BUF_SHARED;
unit->max_resonances = 4;
unit->offset = 0;
unit->local_buf = (float*)RTAlloc(unit->mWorld, sizeof(float) * bufFrames);
unit->autocor_buffer = (float*)RTAlloc(unit->mWorld, sizeof(float) * unit->resampledFrames);
unit->resampled = (float*)RTAlloc(unit->mWorld, sizeof(float) * unit->resampledFrames);
unit->lpc = (float*)RTAlloc(unit->mWorld, sizeof(float) * unit->lpc_size + 1);
unit->lpc_work = (float*)RTAlloc(unit->mWorld, sizeof(float) * unit->lpc_size * 4);
unit->res = (float*)RTAlloc(unit->mWorld, sizeof(float) * unit->lpc_size + 1);
unit->res_work = (float*)RTAlloc(unit->mWorld, sizeof(float) * unit->lpc_size * 16);
for (int i = 0; i < 4; i++) {
*OUT(i) = 0.f;
}
VoxRes_next_k(unit, 1);
}
void VoxRes_next_k(VoxRes *unit, int inNumSamples)
{
GET_BUF_SHARED;
for (int i = 0; i < bufFrames; i++) {
unit->local_buf[i] = bufData[(i + unit->offset) & (bufFrames - 1)];
}
vox_box_resample_mut_f32(unit->local_buf, (size_t)bufFrames, unit->resampledFrames, unit->resampled);
vox_box_autocorrelate_mut_f32(unit->resampled, (size_t)unit->resampledFrames, unit->lpc_size + 2, unit->autocor_buffer);
vox_box_normalize_f32(unit->autocor_buffer, unit->lpc_size + 2);
vox_box_lpc_mut_f32(unit->autocor_buffer, unit->lpc_size + 2, unit->lpc_size, unit->lpc, unit->lpc_work);
int resonance_count = 0;
vox_box_resonances_mut_f32(unit->lpc, unit->lpc_size + 1, FULLRATE * unit->resampledFrames / (float)bufFrames, &resonance_count, unit->res_work, unit->res);
for (int i = 0; (i < resonance_count) && (i < unit->max_resonances); i++) {
*OUT(i) = unit->res[i];
}
unit->offset += (int)BUFRATE;
}
void VoxRes_Dtor(VoxRes *unit)
{
RTFree(unit->mWorld, unit->local_buf);
RTFree(unit->mWorld, unit->autocor_buffer);
RTFree(unit->mWorld, unit->lpc);
RTFree(unit->mWorld, unit->lpc_work);
RTFree(unit->mWorld, unit->res);
RTFree(unit->mWorld, unit->res_work);
}
PluginLoad(VoxRes)
{
ft = inTable;
DefineSimpleUnit(VoxRes);
}