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

effect_delay.h 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "AudioStream.h"
  29. #include "utility/dspinst.h"
  30. #define DELAY_QUEUE_SIZE 117
  31. class AudioEffectDelay : public AudioStream
  32. {
  33. public:
  34. AudioEffectDelay() : AudioStream(1, inputQueueArray) {
  35. activemask = 0;
  36. headindex = 0;
  37. tailindex = 0;
  38. maxblocks = 0;
  39. memset(queue, 0, sizeof(queue));
  40. }
  41. void delay(uint8_t channel, float milliseconds) {
  42. if (channel >= 8) return;
  43. if (milliseconds < 0.0) milliseconds = 0.0;
  44. uint32_t n = (milliseconds*(AUDIO_SAMPLE_RATE_EXACT/1000.0))+0.5;
  45. uint32_t nmax = AUDIO_BLOCK_SAMPLES * (DELAY_QUEUE_SIZE-1);
  46. if (n > nmax) n = nmax;
  47. uint32_t blks = (n + (AUDIO_BLOCK_SAMPLES-1)) / AUDIO_BLOCK_SAMPLES + 1;
  48. if (!(activemask & (1<<channel))) {
  49. // enabling a previously disabled channel
  50. position[channel] = n;
  51. if (blks > maxblocks) maxblocks = blks;
  52. activemask |= (1<<channel);
  53. } else {
  54. if (n > position[channel]) {
  55. // new delay is greater than previous setting
  56. if (blks > maxblocks) maxblocks = blks;
  57. position[channel] = n;
  58. } else {
  59. // new delay is less than previous setting
  60. position[channel] = n;
  61. recompute_maxblocks();
  62. }
  63. }
  64. }
  65. void disable(uint8_t channel) {
  66. if (channel >= 8) return;
  67. // diable this channel
  68. activemask &= ~(1<<channel);
  69. // recompute maxblocks for remaining enabled channels
  70. recompute_maxblocks();
  71. }
  72. virtual void update(void);
  73. private:
  74. void recompute_maxblocks(void) {
  75. uint32_t max=0;
  76. uint32_t channel = 0;
  77. do {
  78. if (activemask & (1<<channel)) {
  79. uint32_t n = position[channel];
  80. n = (n + (AUDIO_BLOCK_SAMPLES-1)) / AUDIO_BLOCK_SAMPLES + 1;
  81. if (n > max) max = n;
  82. }
  83. } while(++channel < 8);
  84. maxblocks = max;
  85. }
  86. uint8_t activemask; // which output channels are active
  87. uint8_t headindex; // head index (incoming) data in quueu
  88. uint8_t tailindex; // tail index (outgoing) data from queue
  89. uint8_t maxblocks; // number of blocks needed in queue
  90. uint16_t position[8]; // # of sample delay for each channel
  91. audio_block_t *queue[DELAY_QUEUE_SIZE];
  92. audio_block_t *inputQueueArray[1];
  93. };
  94. #endif