-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin_TubeResonator.cpp
179 lines (162 loc) · 6.48 KB
/
Plugin_TubeResonator.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
#include "AudioPluginUtil.h"
namespace TubeResonator
{
const int MAXSECTIONS = 10;
enum Param
{
P_NUMSECTIONS,
P_FB,
P_NL,
P_MIKEPOS,
P_L1,
P_A1,
P_NUM = P_L1 + MAXSECTIONS * 2
};
struct Delay
{
enum { MAXLEN = 1 << 8, MASK = MAXLEN - 1 };
float delay;
float input;
float output;
int writepos;
float data[MAXLEN + 1];
inline void Process()
{
data[writepos] = input;
float f = writepos + MASK - delay;
int r = FastFloor(f);
f -= r;
r &= MASK;
writepos = (writepos + 1) & MASK;
float s1 = data[r & MASK];
float s2 = data[(r + 1) & MASK];
output = s1 + (s2 - s1) * f;
}
};
struct Section
{
float coeff;
Delay upper;
Delay lower;
};
struct EffectData
{
struct Data
{
float p[P_NUM];
struct Channel
{
Section section[MAXSECTIONS];
} channels[8];
};
union
{
Data data;
unsigned char pad[(sizeof(Data) + 15) & ~15]; // This entire structure must be a multiple of 16 bytes (and and instance 16 byte aligned) for PS3 SPU DMA requirements
};
};
int InternalRegisterEffectDefinition(UnityAudioEffectDefinition& definition)
{
int numparams = P_NUM;
definition.paramdefs = new UnityAudioParameterDefinition[numparams];
RegisterParameter(definition, "Num sections", "", 1.0f, MAXSECTIONS, 3.0f, 1.0f, 1.0f, P_NUMSECTIONS, "Number of sections");
RegisterParameter(definition, "Feedback", "%", 0.0f, 1.0f, 0.5f, 100.0f, 1.0f, P_FB, "Feedback");
RegisterParameter(definition, "Nonlinearity", "%", 0.0f, 1.0f, 0.0f, 100.0f, 1.0f, P_NL, "Amount of nonlinearity at reflection");
RegisterParameter(definition, "Mike position", "%", 0.0f, 1.0f, 0.0f, 100.0f, 1.0f, P_MIKEPOS, "Microphone position");
for (int n = 0; n < MAXSECTIONS; n++)
{
RegisterParameter(definition, tmpstr(0, "Length %d", n + 1), "cm", 0.01f, (float)Delay::MASK * (34000.0f / 48000.0f), 7.0f, 1.0f, 3.0f, P_L1 + n * 2, tmpstr(1, "Section %d length", n + 1));
RegisterParameter(definition, tmpstr(0, "Radius %d", n + 1), "cm", 0.01f, 100.0f, 3.0f, 1.0f, 3.0f, P_A1 + n * 2, tmpstr(1, "Section %d radius", n + 1));
}
return numparams;
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK CreateCallback(UnityAudioEffectState* state)
{
EffectData* effectdata = new EffectData;
memset(effectdata, 0, sizeof(EffectData));
state->effectdata = effectdata;
InitParametersFromDefinitions(InternalRegisterEffectDefinition, effectdata->data.p);
return UNITY_AUDIODSP_OK;
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK ReleaseCallback(UnityAudioEffectState* state)
{
EffectData::Data* data = &state->GetEffectData<EffectData>()->data;
delete data;
return UNITY_AUDIODSP_OK;
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK SetFloatParameterCallback(UnityAudioEffectState* state, int index, float value)
{
EffectData::Data* data = &state->GetEffectData<EffectData>()->data;
if (index >= P_NUM)
return UNITY_AUDIODSP_ERR_UNSUPPORTED;
data->p[index] = value;
return UNITY_AUDIODSP_OK;
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK GetFloatParameterCallback(UnityAudioEffectState* state, int index, float* value, char *valuestr)
{
EffectData::Data* data = &state->GetEffectData<EffectData>()->data;
if (index >= P_NUM)
return UNITY_AUDIODSP_ERR_UNSUPPORTED;
if (value != NULL)
*value = data->p[index];
if (valuestr != NULL)
valuestr[0] = 0;
return UNITY_AUDIODSP_OK;
}
int UNITY_AUDIODSP_CALLBACK GetFloatBufferCallback(UnityAudioEffectState* state, const char* name, float* buffer, int numsamples)
{
return UNITY_AUDIODSP_OK;
}
UNITY_AUDIODSP_RESULT UNITY_AUDIODSP_CALLBACK ProcessCallback(UnityAudioEffectState* state, float* inbuffer, float* outbuffer, unsigned int length, int inchannels, int outchannels)
{
EffectData::Data* data = &state->GetEffectData<EffectData>()->data;
const float lenscale = state->samplerate / 34000.0f;
const float fb = -data->p[P_FB];
const float nl = data->p[P_NL] * 3.0f;
const int numsections = (int)data->p[P_NUMSECTIONS];
const int mikepos = (int)(data->p[P_MIKEPOS] * (numsections - 1));
for (int i = 0; i < inchannels; i++)
{
EffectData::Data::Channel& ch = data->channels[i];
for (int k = 0; k < numsections; k++)
{
if (k < numsections - 1)
{
float A1 = data->p[P_A1 + k * 2];
float A2 = data->p[P_A1 + k * 2 + 2];
ch.section[k].coeff = (A2 - A1) / (A1 + A2);
}
const float len = data->p[P_L1 + k * 2] * lenscale - 1.0f;
ch.section[k].upper.delay = len;
ch.section[k].lower.delay = len;
}
float* src = inbuffer + i;
float* dst = outbuffer + i;
for (unsigned int n = 0; n < length; n++)
{
float refl = ch.section[numsections - 1].upper.output;
float r = FastMin(refl * refl, 1.0f);
refl += (r * refl - refl) * nl;
ch.section[0].upper.input = ch.section[0].lower.output + *src;
ch.section[numsections - 1].lower.input = refl * fb;
for (int k = 0; k < numsections - 1; k++)
{
float c = ch.section[k].coeff;
float u = ch.section[k].upper.output;
float l = ch.section[k + 1].lower.output;
ch.section[k + 1].upper.input = u * (1.0f + c) - c * l;
ch.section[k].lower.input = l * (1.0f - c) + c * u;
}
for (int k = 0; k < numsections; k++)
{
ch.section[k].upper.Process();
ch.section[k].lower.Process();
}
*dst = ch.section[mikepos].upper.output;
src += inchannels;
dst += outchannels;
}
}
return UNITY_AUDIODSP_OK;
}
}