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.

119 satır
4.2KB

  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. #ifndef effect_delay_h_
  27. #define effect_delay_h_
  28. #include "Arduino.h"
  29. #include "AudioStream.h"
  30. #include "utility/dspinst.h"
  31. #if defined(__IMXRT1062__)
  32. // 4.00 second maximum on Teensy 4.0
  33. #define DELAY_QUEUE_SIZE (176512 / AUDIO_BLOCK_SAMPLES)
  34. #elif defined(__MK66FX1M0__)
  35. // 2.41 second maximum on Teensy 3.6
  36. #define DELAY_QUEUE_SIZE (106496 / AUDIO_BLOCK_SAMPLES)
  37. #elif defined(__MK64FX512__)
  38. // 1.67 second maximum on Teensy 3.5
  39. #define DELAY_QUEUE_SIZE (73728 / AUDIO_BLOCK_SAMPLES)
  40. #elif defined(__MK20DX256__)
  41. // 0.45 second maximum on Teensy 3.1 & 3.2
  42. #define DELAY_QUEUE_SIZE (19826 / AUDIO_BLOCK_SAMPLES)
  43. #else
  44. // 0.14 second maximum on Teensy 3.0
  45. #define DELAY_QUEUE_SIZE (6144 / AUDIO_BLOCK_SAMPLES)
  46. #endif
  47. class AudioEffectDelay : public AudioStream
  48. {
  49. public:
  50. AudioEffectDelay() : AudioStream(1, inputQueueArray) {
  51. activemask = 0;
  52. headindex = 0;
  53. tailindex = 0;
  54. maxblocks = 0;
  55. memset(queue, 0, sizeof(queue));
  56. }
  57. void delay(uint8_t channel, float milliseconds) {
  58. if (channel >= 8) return;
  59. if (milliseconds < 0.0) milliseconds = 0.0;
  60. uint32_t n = (milliseconds*(AUDIO_SAMPLE_RATE_EXACT/1000.0))+0.5;
  61. uint32_t nmax = AUDIO_BLOCK_SAMPLES * (DELAY_QUEUE_SIZE-1);
  62. if (n > nmax) n = nmax;
  63. uint32_t blks = (n + (AUDIO_BLOCK_SAMPLES-1)) / AUDIO_BLOCK_SAMPLES + 1;
  64. if (!(activemask & (1<<channel))) {
  65. // enabling a previously disabled channel
  66. position[channel] = n;
  67. if (blks > maxblocks) maxblocks = blks;
  68. activemask |= (1<<channel);
  69. } else {
  70. if (n > position[channel]) {
  71. // new delay is greater than previous setting
  72. if (blks > maxblocks) maxblocks = blks;
  73. position[channel] = n;
  74. } else {
  75. // new delay is less than previous setting
  76. position[channel] = n;
  77. recompute_maxblocks();
  78. }
  79. }
  80. }
  81. void disable(uint8_t channel) {
  82. if (channel >= 8) return;
  83. // diable this channel
  84. activemask &= ~(1<<channel);
  85. // recompute maxblocks for remaining enabled channels
  86. recompute_maxblocks();
  87. }
  88. virtual void update(void);
  89. private:
  90. void recompute_maxblocks(void) {
  91. uint32_t max=0;
  92. uint32_t channel = 0;
  93. do {
  94. if (activemask & (1<<channel)) {
  95. uint32_t n = position[channel];
  96. n = (n + (AUDIO_BLOCK_SAMPLES-1)) / AUDIO_BLOCK_SAMPLES + 1;
  97. if (n > max) max = n;
  98. }
  99. } while(++channel < 8);
  100. maxblocks = max;
  101. }
  102. uint8_t activemask; // which output channels are active
  103. uint16_t headindex; // head index (incoming) data in quueu
  104. uint16_t tailindex; // tail index (outgoing) data from queue
  105. uint16_t maxblocks; // number of blocks needed in queue
  106. #if DELAY_QUEUE_SIZE * AUDIO_BLOCK_SAMPLES < 65535
  107. uint16_t position[8]; // # of sample delay for each channel
  108. #else
  109. uint32_t position[8]; // # of sample delay for each channel
  110. #endif
  111. audio_block_t *queue[DELAY_QUEUE_SIZE];
  112. audio_block_t *inputQueueArray[1];
  113. };
  114. #endif