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.

69 lines
2.5KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef filter_variable_h_
  27. #define filter_variable_h_
  28. #include "AudioStream.h"
  29. class AudioFilterStateVariable: public AudioStream
  30. {
  31. public:
  32. AudioFilterStateVariable() : AudioStream(2, inputQueueArray) {
  33. frequency(1000);
  34. resonance(0.707);
  35. state_inputprev = 0;
  36. state_lowpass = 0;
  37. state_bandpass = 0;
  38. }
  39. void frequency(float freq) {
  40. if (freq < 20.0) freq = 20.0;
  41. else if (freq > AUDIO_SAMPLE_RATE_EXACT/2.5) freq = AUDIO_SAMPLE_RATE_EXACT/2.5;
  42. setting_fmult = sinf(freq * (3.141592654/(AUDIO_SAMPLE_RATE_EXACT*2.0)))
  43. * 2147483647.0;
  44. }
  45. void resonance(float q) {
  46. if (q < 0.7) q = 0.7;
  47. else if (q > 5.0) q = 5.0;
  48. // TODO: allow lower Q when frequency is lower
  49. setting_damp = (1.0 / q) * 1073741824.0;
  50. }
  51. virtual void update(void);
  52. private:
  53. void update_fixed(const int16_t *in,
  54. int16_t *lp, int16_t *bp, int16_t *hp);
  55. void update_variable(const int16_t *in, const int16_t *ctl,
  56. int16_t *lp, int16_t *bp, int16_t *hp);
  57. int32_t setting_fmult;
  58. int32_t setting_damp;
  59. int32_t state_inputprev;
  60. int32_t state_lowpass;
  61. int32_t state_bandpass;
  62. audio_block_t *inputQueueArray[2];
  63. };
  64. #endif