- Home /
Chain multiple effects in OnAudioFilterRead
Hello, I'm trying to chain multiple effects in OnAudioFilterRead and i'm having hard time for that, quite hard considering i'm not familiar with dsp processing. Here is part of my code that i'm struggling with.
void OnAudioFilterRead(float[] data, int channels)
{
if(effects.Count>0)
{
for(int j=0; j<effects.Count; j++)
{
switch(effects[j].effectType)
{
case DSPEffectType.LowPassFilter:
case DSPEffectType.HighPassFilter:
for (int i = 0; i < data.Length; i = i + channels)
{
for(int c = 0; c < channels; c++)
{
if(c==0)
{
buffc0[j] = data[i+c];
data[i+c] = (filters[effects[j].id].b0/filters[effects[j].id].a0)*buffc0[j] + (filters[effects[j].id].b1/filters[effects[j].id].a0)*x1c0[j] + (filters[effects[j].id].b2/filters[effects[j].id].a0)*x2c0[j] + (-filters[effects[j].id].a1/filters[effects[j].id].a0)*y1c0[j] + (-filters[effects[j].id].a2/filters[effects[j].id].a0)*y2c0[j];
x2c0[j] = x1c0[j];
x1c0[j] = buffc0[j];
y2c0[j] = y1c0[j];
y1c0[j] = data[i+c];
}
else
{
buffc1[j] = data[i+c];
data[i+c] = (filters[effects[j].id].b0/filters[effects[j].id].a0)*buffc1[j] + (filters[effects[j].id].b1/filters[effects[j].id].a0)*x1c1[j] + (filters[effects[j].id].b2/filters[effects[j].id].a0)*x2c1[j] + (-filters[effects[j].id].a1/filters[effects[j].id].a0)*y1c1[j] + (-filters[effects[j].id].a2/filters[effects[j].id].a0)*y2c1[j];
x2c1[j] = x1c1[j];
x1c1[j] = buffc1[j];
y2c1[j] = y1c1[j];
y1c1[j] = data[i+c];
}
}
}
break;
}
}
}
First i did this code but not using lists for buffc0, x1c0 etc... but i had some crackling like i did something wrong. Then i did those arrays, it sounds better but as soon as i have 3 effects it's not working at all (sound struggling to play). Even 2 of them may have some issues sometimes with maybe performances. Can someone please help me to understand what i'm doing wrong?
Comment