選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

116 行
4.0KB

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