No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

effect_delay.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include <Arduino.h>
  27. #include "effect_delay.h"
  28. void AudioEffectDelay::update(void)
  29. {
  30. audio_block_t *output;
  31. uint32_t head, tail, count, channel, index, prev, offset;
  32. const int16_t *src, *end;
  33. int16_t *dst;
  34. // grab incoming data and put it into the queue
  35. head = headindex;
  36. tail = tailindex;
  37. if (++head >= DELAY_QUEUE_SIZE) head = 0;
  38. if (head == tail) {
  39. if (queue[tail] != NULL) release(queue[tail]);
  40. if (++tail >= DELAY_QUEUE_SIZE) tail = 0;
  41. }
  42. queue[head] = receiveReadOnly();
  43. headindex = head;
  44. // testing only.... don't allow null pointers into the queue
  45. // instead, fill the empty times with blocks of zeros
  46. //if (queue[head] == NULL) {
  47. // queue[head] = allocate();
  48. // if (queue[head]) {
  49. // dst = queue[head]->data;
  50. // end = dst + AUDIO_BLOCK_SAMPLES;
  51. // do {
  52. // *dst++ = 0;
  53. // } while (dst < end);
  54. // } else {
  55. // digitalWriteFast(2, HIGH);
  56. // delayMicroseconds(5);
  57. // digitalWriteFast(2, LOW);
  58. // }
  59. //}
  60. // discard unneeded blocks from the queue
  61. if (head >= tail) {
  62. count = head - tail;
  63. } else {
  64. count = DELAY_QUEUE_SIZE + head - tail;
  65. }
  66. if (count > maxblocks) {
  67. count -= maxblocks;
  68. do {
  69. if (queue[tail] != NULL) {
  70. release(queue[tail]);
  71. queue[tail] = NULL;
  72. }
  73. if (++tail >= DELAY_QUEUE_SIZE) tail = 0;
  74. } while (--count > 0);
  75. }
  76. tailindex = tail;
  77. // transmit the delayed outputs using queue data
  78. for (channel = 0; channel < 8; channel++) {
  79. if (!(activemask & (1<<channel))) continue;
  80. index = position[channel] / AUDIO_BLOCK_SAMPLES;
  81. offset = position[channel] % AUDIO_BLOCK_SAMPLES;
  82. if (head >= index) {
  83. index = head - index;
  84. } else {
  85. index = DELAY_QUEUE_SIZE + head - index;
  86. }
  87. if (offset == 0) {
  88. // delay falls on the block boundary
  89. if (queue[index]) {
  90. transmit(queue[index], channel);
  91. }
  92. } else {
  93. // delay requires grabbing data from 2 blocks
  94. output = allocate();
  95. if (!output) continue;
  96. dst = output->data;
  97. if (index > 0) {
  98. prev = index - 1;
  99. } else {
  100. prev = DELAY_QUEUE_SIZE-1;
  101. }
  102. if (queue[prev]) {
  103. end = queue[prev]->data + AUDIO_BLOCK_SAMPLES;
  104. src = end - offset;
  105. while (src < end) {
  106. *dst++ = *src++; // TODO: optimize
  107. }
  108. } else {
  109. end = dst + offset;
  110. while (dst < end) {
  111. *dst++ = 0;
  112. }
  113. }
  114. end = output->data + AUDIO_BLOCK_SAMPLES;
  115. if (queue[index]) {
  116. src = queue[index]->data;
  117. while (dst < end) {
  118. *dst++ = *src++; // TODO: optimize
  119. }
  120. } else {
  121. while (dst < end) {
  122. *dst++ = 0;
  123. }
  124. }
  125. transmit(output, channel);
  126. release(output);
  127. }
  128. }
  129. }