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.

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