Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

effect_delay.cpp 3.9KB

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