No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filter.c 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <math.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "filter.h"
  5. void filter_init(struct Filter *filter, float cutoff, float resonance) {
  6. memset(filter, 0, sizeof(struct Filter));
  7. filter->cutoff = cutoff;
  8. filter->resonance = resonance;
  9. }
  10. void filter_set_cutoff(struct Filter *filter, float cutoff) {
  11. filter->cutoff = cutoff;
  12. if(filter->cutoff > 0.9) filter->cutoff = 0.9;
  13. else if(filter->cutoff < 0.0) filter->cutoff = 0.0;
  14. }
  15. void filter_set_resonance(struct Filter *filter, float resonance) {
  16. filter->resonance = resonance;
  17. }
  18. // http://www.musicdsp.org/showone.php?id=25
  19. // float filter_sample(struct Filter *filter, float input) {
  20. // float q = 1.0f - filter->cutoff;
  21. // float p = filter->cutoff + 0.8f * filter->cutoff * q;
  22. // float f = p + p - 1.0f;
  23. // q = filter->resonance * (1.0f + 0.5f * q * (1.0f - q + 5.6f * q * q));
  24. // #define OVERSAMPLE 2
  25. // float output = 0;
  26. // for(int i = 0; i < OVERSAMPLE; i++) {
  27. // input -= q * filter->b4; //feedback
  28. // float t1 = filter->b1; filter->b1 = (input + filter->b0) * p - filter->b1 * f;
  29. // float t2 = filter->b2; filter->b2 = (filter->b1 + t1) * p - filter->b2 * f;
  30. // t1 = filter->b3; filter->b3 = (filter->b2 + t2) * p - filter->b3 * f;
  31. // filter->b4 = (filter->b3 + t1) * p - filter->b4 * f;
  32. // filter->b4 = filter->b4 - filter->b4 * filter->b4 * filter->b4 * 0.166667f; //clipping
  33. // filter->b0 = input;
  34. // output += filter->b4;
  35. // }
  36. // return output / (float)OVERSAMPLE;
  37. // }
  38. // mad's filter code
  39. float filter_sample(struct Filter *filter, float input) {
  40. if(++filter->last_calc >= 16) {
  41. filter->last_calc = 0;
  42. filter->cutoff_calc = pow(0.5, 8.5 - filter->cutoff * 8);
  43. filter->resonance_calc = pow(0.5, (1.0 - filter->resonance) * 4.5);
  44. }
  45. float cutoff = filter->cutoff_calc;
  46. float resonance = filter->resonance_calc;
  47. #define OVERSAMPLE 2
  48. float output = 0;
  49. for(int i = 0; i < OVERSAMPLE; i++) {
  50. float in = filter->resonance_input;
  51. if(in > 1) in = 1;
  52. if(in < -1) in = -1;
  53. in = 1.5*in - 0.5*in*in*in;
  54. in = input - in;
  55. filter->stage_1 += (in - filter->stage_1) * cutoff;
  56. filter->stage_2 += (filter->stage_1 - filter->stage_2) * cutoff;
  57. filter->stage_3 += (filter->stage_2 - filter->stage_3) * cutoff;
  58. filter->stage_4 += (filter->stage_3 - filter->stage_4) * cutoff;
  59. output += filter->stage_4;
  60. filter->resonance_input = output * resonance;
  61. }
  62. return output / OVERSAMPLE;
  63. }