Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static bool initialized = false;
- static float param_cutoff, param_resonance;
- static float[] filtCoefTab = new float[5];
- static float lx1, lx2, ly1, ly2; // Left sample history
- private static void InitLPF()
- {
- param_cutoff = 22050;
- param_resonance = 0;
- InitFiltah();
- lx1 = lx2 = ly1 = ly2 = 0.0f;
- }
- private static void InitFiltah()
- {
- float alpha, omega, sn, cs;
- float a0, a1, a2, b0, b1, b2;
- // These limits the cutoff frequency and resonance to
- // reasoneable values.
- if (param_cutoff < 20.0f) { param_cutoff = 20.0f; };
- if (param_cutoff > 22000.0f) { param_cutoff = 22000.0f; };
- if (param_resonance < 1.0f) { param_resonance = 1.0f; };
- if (param_resonance > 127.0f) { param_resonance = 127.0f; };
- omega = (float)(2.0f * Math.PI * param_cutoff / 44100);
- sn = (float)Math.Sin(omega);
- cs = (float)Math.Cos(omega);
- alpha = sn / param_resonance;
- b0 = (1.0f - cs) / 2.0f;
- b1 = 1.0f - cs;
- b2 = (1.0f - cs) / 2.0f;
- a0 = 1.0f + alpha;
- a1 = -2.0f * cs;
- a2 = 1.0f - alpha;
- filtCoefTab[0] = b0 / a0;
- filtCoefTab[1] = b1 / a0;
- filtCoefTab[2] = b2 / a0;
- filtCoefTab[3] = -a1 / a0;
- filtCoefTab[4] = -a2 / a0;
- }
- public static void EffectLpf(Int32[] input)
- {
- if (!initialized) InitLPF();
- int inM, outM, temp_y;
- int i;
- for (i = 0; i < input.Length; i++)
- {
- inM = input[i];
- outM = inM;
- temp_y = (int)(filtCoefTab[0] * outM +
- filtCoefTab[1] * lx1 +
- filtCoefTab[2] * lx2 +
- filtCoefTab[3] * ly1 +
- filtCoefTab[4] * ly2);
- ly2 = ly1; ly1 = temp_y; lx2 = lx1; lx1 = outM; outM = temp_y;
- i++;
- input[i] = outM;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement