-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin_WahWah.cpp
132 lines (120 loc) · 5.48 KB
/
Plugin_WahWah.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
#include "AudioPluginUtil.h"
namespace WahWah
{
enum Param
{
P_ATK,
P_REL,
P_BASE,
P_SENS,
P_RESO,
P_TYPE,
P_DEPTH,
P_SIDECHAIN,
P_NUM
};
struct EffectData
{
struct Data
{
float p[P_NUM];
struct Channel
{
StateVariableFilter filter1;
StateVariableFilter filter2;
float env;
} 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, "Attack Time", "s", 0.001f, 2.0f, 0.1f, 1.0f, 3.0f, P_ATK, "Attack time");
RegisterParameter(definition, "Release Time", "s", 0.001f, 2.0f, 0.5f, 1.0f, 3.0f, P_REL, "Release time");
RegisterParameter(definition, "Base Level", "%", 0.0f, 1.0f, 0.1f, 100.0f, 1.0f, P_BASE, "Base filter level");
RegisterParameter(definition, "Sensitivity", "%", -1.0f, 1.0f, 0.1f, 100.0f, 1.0f, P_SENS, "Filter sensitivity");
RegisterParameter(definition, "Resonance", "%", 0.0f, 1.0f, 0.1f, 100.0f, 1.0f, P_RESO, "Filter resonance");
RegisterParameter(definition, "Type", "", 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, P_TYPE, "Filter type (0 = lowpass, 1 = bandpass)");
RegisterParameter(definition, "Depth", "", 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, P_DEPTH, "Filter depth (0 = 12 dB, 1 = 24 dB)");
RegisterParameter(definition, "Sidechain Mix", "%", 0.0f, 1.0f, 0.0f, 100.0f, 1.0f, P_SIDECHAIN, "Sidechain mix (0 = use input, 1 = use sidechain)");
definition.flags |= UnityAudioEffectDefinitionFlags_IsSideChainTarget;
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 atksamples = data->p[P_ATK] * state->samplerate;
const float relsamples = data->p[P_REL] * state->samplerate;
const float atkconst = (atksamples <= 1.0f) ? 1.0f : (1.0f - powf(0.01f, 1.0f / atksamples));
const float relconst = (relsamples <= 1.0f) ? 1.0f : (1.0f - powf(0.01f, 1.0f / relsamples));
const float bw = powf(1.0f - 0.999f * data->p[P_RESO], 3.0f);
for (int i = 0; i < inchannels; i++)
{
EffectData::Data::Channel& ch = data->channels[i];
ch.filter1.bandwidth = bw;
ch.filter2.bandwidth = bw;
float* src = inbuffer + i;
float* dst = outbuffer + i;
float* sc = state->sidechainbuffer + i;
for (unsigned int n = 0; n < length; n++)
{
float s = *src;
float a = fabsf(s + (*sc - s) * data->p[P_SIDECHAIN]);
ch.env += (a - ch.env) * ((a > ch.env) ? atkconst : relconst);
ch.filter1.cutoff = FastClip(data->p[P_BASE] + ch.env * data->p[P_SENS], 0.0f, 1.4f);
ch.filter2.cutoff = ch.filter1.cutoff;
ch.filter2.ProcessLPF(ch.filter1.ProcessLPF(*src));
float lpf = ch.filter1.lpf + (ch.filter2.lpf - ch.filter1.lpf) * data->p[P_DEPTH];
float bpf = ch.filter1.bpf + (ch.filter2.bpf - ch.filter1.bpf) * data->p[P_DEPTH];
*dst = lpf + (bpf - lpf) * data->p[P_TYPE];
src += inchannels;
dst += outchannels;
sc += inchannels;
}
}
return UNITY_AUDIODSP_OK;
}
}