Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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