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.

76 lines
1.4KB

  1. #include "Audio.h"
  2. #include "arm_math.h"
  3. #include "utility/dspinst.h"
  4. extern "C" {
  5. extern const int16_t fader_table[256];
  6. };
  7. void AudioEffectFade::update(void)
  8. {
  9. audio_block_t *block;
  10. uint32_t i, pos, inc, index, scale;
  11. int32_t val1, val2, val, sample;
  12. uint8_t dir;
  13. pos = position;
  14. if (pos == 0) {
  15. // output is silent
  16. block = receiveReadOnly();
  17. if (block) release(block);
  18. return;
  19. } else if (pos == 0xFFFFFFFF) {
  20. // output is 100%
  21. block = receiveReadOnly();
  22. if (!block) return;
  23. transmit(block);
  24. release(block);
  25. return;
  26. }
  27. block = receiveWritable();
  28. if (!block) return;
  29. inc = rate;
  30. dir = direction;
  31. for (i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  32. index = pos >> 24;
  33. val1 = fader_table[index];
  34. val2 = fader_table[index+1];
  35. scale = (pos >> 8) & 0xFFFF;
  36. val2 *= scale;
  37. val1 *= 0x10000 - scale;
  38. val = (val1 + val2) >> 16;
  39. sample = block->data[i];
  40. sample = (sample * val) >> 15;
  41. block->data[i] = sample;
  42. if (dir > 0) {
  43. // output is increasing
  44. if (inc < 0xFFFFFFFF - pos) pos += inc;
  45. else pos = 0xFFFFFFFF;
  46. } else {
  47. // output is decreasing
  48. if (inc < pos) pos -= inc;
  49. else pos = 0;
  50. }
  51. }
  52. position = pos;
  53. transmit(block);
  54. release(block);
  55. }
  56. void AudioEffectFade::fadeBegin(uint32_t newrate, uint8_t dir)
  57. {
  58. __disable_irq();
  59. uint32_t pos = position;
  60. if (pos == 0) position = 1;
  61. else if (pos == 0xFFFFFFFF) position = 0xFFFFFFFE;
  62. rate = newrate;
  63. direction = dir;
  64. __enable_irq();
  65. }