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.

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