Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

105 lines
3.3KB

  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. #include "filter_biquad.h"
  27. #include "utility/dspinst.h"
  28. void AudioFilterBiquad::update(void)
  29. {
  30. audio_block_t *block;
  31. int32_t a0, a1, a2, b1, b2, sum;
  32. uint32_t in2, out2, aprev, bprev, flag;
  33. uint32_t *data, *end;
  34. int32_t *state;
  35. block = receiveWritable();
  36. data = (uint32_t *)(block->data);
  37. if (!block) return;
  38. end = data + AUDIO_BLOCK_SAMPLES/2;
  39. state = (int32_t *)definition;
  40. do {
  41. a0 = *state++;
  42. a1 = *state++;
  43. a2 = *state++;
  44. b1 = *state++;
  45. b2 = *state++;
  46. aprev = *state++;
  47. bprev = *state++;
  48. sum = *state & 0x3FFF;
  49. do {
  50. in2 = *data;
  51. sum = signed_multiply_accumulate_32x16b(sum, a0, in2);
  52. sum = signed_multiply_accumulate_32x16t(sum, a1, aprev);
  53. sum = signed_multiply_accumulate_32x16b(sum, a2, aprev);
  54. sum = signed_multiply_accumulate_32x16t(sum, b1, bprev);
  55. sum = signed_multiply_accumulate_32x16b(sum, b2, bprev);
  56. out2 = (uint32_t)sum >> 14;
  57. sum &= 0x3FFF;
  58. sum = signed_multiply_accumulate_32x16t(sum, a0, in2);
  59. sum = signed_multiply_accumulate_32x16b(sum, a1, in2);
  60. sum = signed_multiply_accumulate_32x16t(sum, a2, aprev);
  61. sum = signed_multiply_accumulate_32x16b(sum, b1, out2);
  62. sum = signed_multiply_accumulate_32x16t(sum, b2, bprev);
  63. aprev = in2;
  64. bprev = pack_16x16(sum >> 14, out2);
  65. sum &= 0x3FFF;
  66. aprev = in2;
  67. *data++ = bprev;
  68. } while (data < end);
  69. flag = *state & 0x80000000;
  70. *state++ = sum | flag;
  71. *(state-2) = bprev;
  72. *(state-3) = aprev;
  73. data = (uint32_t *)(block->data); // needs to be reset, may as well be done here
  74. } while (flag);
  75. transmit(block);
  76. release(block);
  77. }
  78. void AudioFilterBiquad::updateCoefs(uint8_t set,int *source, bool doReset)
  79. {
  80. int32_t *dest=(int32_t *)definition;
  81. while(set)
  82. {
  83. *dest+=7;
  84. if(!(*dest&0x80000000)) return;
  85. *dest++;
  86. set--;
  87. }
  88. __disable_irq();
  89. for(uint8_t index=0;index<5;index++)
  90. {
  91. *dest++=*source++;
  92. }
  93. if(doReset)
  94. {
  95. *dest++=0;
  96. *dest++=0;
  97. *dest++=0;
  98. }
  99. __enable_irq();
  100. }