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.

89 lines
2.7KB

  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Pete (El Supremo)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include "filter_fir.h"
  23. void AudioFilterFIR::begin(short *cp,int n_coeffs)
  24. {
  25. // pointer to coefficients
  26. coeff_p = cp;
  27. // Initialize FIR instances for the left and right channels
  28. if(coeff_p && (coeff_p != FIR_PASSTHRU)) {
  29. arm_fir_init_q15(&l_fir_inst, n_coeffs, coeff_p, &l_StateQ15[0], AUDIO_BLOCK_SAMPLES);
  30. arm_fir_init_q15(&r_fir_inst, n_coeffs, coeff_p, &r_StateQ15[0], AUDIO_BLOCK_SAMPLES);
  31. }
  32. }
  33. // This has the same effect as begin(NULL,0);
  34. void AudioFilterFIR::stop(void)
  35. {
  36. coeff_p = NULL;
  37. }
  38. void AudioFilterFIR::update(void)
  39. {
  40. audio_block_t *block,*b_new;
  41. // If there's no coefficient table, give up.
  42. if(coeff_p == NULL)return;
  43. // do passthru
  44. if(coeff_p == FIR_PASSTHRU) {
  45. // Just passthrough
  46. block = receiveReadOnly(0);
  47. if(block) {
  48. transmit(block,0);
  49. release(block);
  50. }
  51. block = receiveReadOnly(1);
  52. if(block) {
  53. transmit(block,1);
  54. release(block);
  55. }
  56. return;
  57. }
  58. // Left Channel
  59. block = receiveReadOnly(0);
  60. // get a block for the FIR output
  61. b_new = allocate();
  62. if(block && b_new) {
  63. arm_fir_q15(&l_fir_inst, (q15_t *)block->data, (q15_t *)b_new->data, AUDIO_BLOCK_SAMPLES);
  64. // send the FIR output to the left channel
  65. transmit(b_new,0);
  66. }
  67. if(block)release(block);
  68. if(b_new)release(b_new);
  69. // Right Channel
  70. block = receiveReadOnly(1);
  71. b_new = allocate();
  72. if(block && b_new) {
  73. arm_fir_q15(&r_fir_inst, (q15_t *)block->data, (q15_t *)b_new->data, AUDIO_BLOCK_SAMPLES);
  74. transmit(b_new,1);
  75. }
  76. if(block)release(block);
  77. if(b_new)release(b_new);
  78. }