dcblock.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * DCblock
  3. *
  4. * This code has been extracted from the Csound opcode "dcblock".
  5. * It has been modified to work as a Soundpipe module.
  6. *
  7. * Original Author(s): Perry R. Cook
  8. * Year: 1995
  9. * Location: Opcodes/biquad.c
  10. *
  11. */
  12. #include <math.h>
  13. #include <stdlib.h>
  14. #include "base.h"
  15. #include "dcblock.h"
  16. int sp_dcblock_create(sp_dcblock **p)
  17. {
  18. *p = malloc(sizeof(sp_dcblock));
  19. return SP_OK;
  20. }
  21. int sp_dcblock_destroy(sp_dcblock **p)
  22. {
  23. free(*p);
  24. return SP_OK;
  25. }
  26. int sp_dcblock_init(sp_data *sp, sp_dcblock *p, int oversampling )
  27. {
  28. p->outputs = 0.0;
  29. p->inputs = 0.0;
  30. p->gain = pow( 0.99, 1.0f / oversampling );
  31. if (p->gain == 0.0 || p->gain>=1.0 || p->gain<=-1.0)
  32. p->gain = 0.99;
  33. return SP_OK;
  34. }
  35. int sp_dcblock_compute(sp_data *sp, sp_dcblock *p, SPFLOAT *in, SPFLOAT *out)
  36. {
  37. SPFLOAT gain = p->gain;
  38. SPFLOAT outputs = p->outputs;
  39. SPFLOAT inputs = p->inputs;
  40. SPFLOAT sample = *in;
  41. outputs = sample - inputs + (gain * outputs);
  42. inputs = sample;
  43. *out = outputs;
  44. p->outputs = outputs;
  45. p->inputs = inputs;
  46. return SP_OK;
  47. }