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.

85 lines
3.1KB

  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 "Arduino.h"
  29. #include "AudioStream.h"
  30. class AudioFilterStateVariable: public AudioStream
  31. {
  32. public:
  33. AudioFilterStateVariable() : AudioStream(2, inputQueueArray) {
  34. frequency(1000);
  35. octaveControl(1.0); // default values
  36. resonance(0.707);
  37. state_inputprev = 0;
  38. state_lowpass = 0;
  39. state_bandpass = 0;
  40. }
  41. void frequency(float freq) {
  42. if (freq < 20.0) freq = 20.0;
  43. else if (freq > AUDIO_SAMPLE_RATE_EXACT/2.5) freq = AUDIO_SAMPLE_RATE_EXACT/2.5;
  44. setting_fcenter = (freq * (3.141592654/(AUDIO_SAMPLE_RATE_EXACT*2.0)))
  45. * 2147483647.0;
  46. // TODO: should we use an approximation when freq is not a const,
  47. // so the sinf() function isn't linked?
  48. setting_fmult = sinf(freq * (3.141592654/(AUDIO_SAMPLE_RATE_EXACT*2.0)))
  49. * 2147483647.0;
  50. }
  51. void resonance(float q) {
  52. if (q < 0.7) q = 0.7;
  53. else if (q > 5.0) q = 5.0;
  54. // TODO: allow lower Q when frequency is lower
  55. setting_damp = (1.0 / q) * 1073741824.0;
  56. }
  57. void octaveControl(float n) {
  58. // filter's corner frequency is Fcenter * 2^(control * N)
  59. // where "control" ranges from -1.0 to +1.0
  60. // and "N" allows the frequency to change from 0 to 7 octaves
  61. if (n < 0.0) n = 0.0;
  62. else if (n > 6.9999) n = 6.9999;
  63. setting_octavemult = n * 4096.0;
  64. }
  65. virtual void update(void);
  66. private:
  67. void update_fixed(const int16_t *in,
  68. int16_t *lp, int16_t *bp, int16_t *hp);
  69. void update_variable(const int16_t *in, const int16_t *ctl,
  70. int16_t *lp, int16_t *bp, int16_t *hp);
  71. int32_t setting_fcenter;
  72. int32_t setting_fmult;
  73. int32_t setting_octavemult;
  74. int32_t setting_damp;
  75. int32_t state_inputprev;
  76. int32_t state_lowpass;
  77. int32_t state_bandpass;
  78. audio_block_t *inputQueueArray[2];
  79. };
  80. #endif