Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1.  
  2.         private static bool initialized = false;
  3.  
  4.         static float param_cutoff, param_resonance;
  5.         static float[] filtCoefTab = new float[5];
  6.         static float lx1, lx2, ly1, ly2; // Left sample history
  7.  
  8.         private static void InitLPF()
  9.         {
  10.             param_cutoff = 22050;
  11.             param_resonance = 0;
  12.             InitFiltah();
  13.             lx1 = lx2 = ly1 = ly2 = 0.0f;
  14.         }
  15.  
  16.         private static void InitFiltah()
  17.         {
  18.             float alpha, omega, sn, cs;
  19.             float a0, a1, a2, b0, b1, b2;
  20.  
  21.             // These limits the cutoff frequency and resonance to
  22.             // reasoneable values.
  23.             if (param_cutoff < 20.0f) { param_cutoff = 20.0f; };
  24.             if (param_cutoff > 22000.0f) { param_cutoff = 22000.0f; };
  25.             if (param_resonance < 1.0f) { param_resonance = 1.0f; };
  26.             if (param_resonance > 127.0f) { param_resonance = 127.0f; };
  27.  
  28.             omega = (float)(2.0f * Math.PI * param_cutoff / 44100);
  29.             sn = (float)Math.Sin(omega);
  30.             cs = (float)Math.Cos(omega);
  31.             alpha = sn / param_resonance;
  32.             b0 = (1.0f - cs) / 2.0f;
  33.             b1 = 1.0f - cs;
  34.             b2 = (1.0f - cs) / 2.0f;
  35.             a0 = 1.0f + alpha;
  36.             a1 = -2.0f * cs;
  37.             a2 = 1.0f - alpha;
  38.             filtCoefTab[0] = b0 / a0;
  39.             filtCoefTab[1] = b1 / a0;
  40.             filtCoefTab[2] = b2 / a0;
  41.             filtCoefTab[3] = -a1 / a0;
  42.             filtCoefTab[4] = -a2 / a0;
  43.         }
  44.  
  45.         public static void EffectLpf(Int32[] input)
  46.         {
  47.             if (!initialized) InitLPF();
  48.  
  49.             int inM, outM, temp_y;
  50.             int i;
  51.  
  52.             for (i = 0; i < input.Length; i++)
  53.             {
  54.  
  55.                 inM = input[i];
  56.  
  57.                 outM = inM;
  58.  
  59.                 temp_y = (int)(filtCoefTab[0] * outM +
  60.                                 filtCoefTab[1] * lx1 +
  61.                                 filtCoefTab[2] * lx2 +
  62.                                 filtCoefTab[3] * ly1 +
  63.                                 filtCoefTab[4] * ly2);
  64.                 ly2 = ly1; ly1 = temp_y; lx2 = lx1; lx1 = outM; outM = temp_y;
  65.  
  66.                 i++;
  67.                 input[i] = outM;
  68.             }
  69.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement