99 lines
2.8KB

  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 <Arduino.h>
  27. #include "effect_fade.h"
  28. #include "utility/dspinst.h"
  29. extern "C" {
  30. extern const int16_t fader_table[257];
  31. };
  32. void AudioEffectFade::update(void)
  33. {
  34. audio_block_t *block;
  35. uint32_t i, pos, inc, index, scale;
  36. int32_t val1, val2, val, sample;
  37. uint8_t dir;
  38. pos = position;
  39. if (pos == 0) {
  40. // output is silent
  41. block = receiveReadOnly();
  42. if (block) release(block);
  43. return;
  44. } else if (pos == 0xFFFFFFFF) {
  45. // output is 100%
  46. block = receiveReadOnly();
  47. if (!block) return;
  48. transmit(block);
  49. release(block);
  50. return;
  51. }
  52. block = receiveWritable();
  53. if (!block) return;
  54. inc = rate;
  55. dir = direction;
  56. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  57. index = pos >> 24;
  58. val1 = fader_table[index];
  59. val2 = fader_table[index+1];
  60. scale = (pos >> 8) & 0xFFFF;
  61. val2 *= scale;
  62. val1 *= 0x10000 - scale;
  63. val = (val1 + val2) >> 16;
  64. sample = block->data[i];
  65. sample = (sample * val) >> 15;
  66. block->data[i] = sample;
  67. if (dir > 0) {
  68. // output is increasing
  69. if (inc < 0xFFFFFFFF - pos) pos += inc;
  70. else pos = 0xFFFFFFFF;
  71. } else {
  72. // output is decreasing
  73. if (inc < pos) pos -= inc;
  74. else pos = 0;
  75. }
  76. }
  77. position = pos;
  78. transmit(block);
  79. release(block);
  80. }
  81. void AudioEffectFade::fadeBegin(uint32_t newrate, uint8_t dir)
  82. {
  83. __disable_irq();
  84. uint32_t pos = position;
  85. if (pos == 0) position = 1;
  86. else if (pos == 0xFFFFFFFF) position = 0xFFFFFFFE;
  87. rate = newrate;
  88. direction = dir;
  89. __enable_irq();
  90. }