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.

effect_delay.cpp 3.8KB

10 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. 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. release(queue[tail]);
  69. queue[tail] = NULL;
  70. if (++tail >= DELAY_QUEUE_SIZE) tail = 0;
  71. } while (--count > 0);
  72. }
  73. tailindex = tail;
  74. // transmit the delayed outputs using queue data
  75. for (channel = 0; channel < 8; channel++) {
  76. if (!(activemask & (1<<channel))) continue;
  77. index = position[channel] / AUDIO_BLOCK_SAMPLES;
  78. offset = position[channel] % AUDIO_BLOCK_SAMPLES;
  79. if (head >= index) {
  80. index = head - index;
  81. } else {
  82. index = DELAY_QUEUE_SIZE + head - index;
  83. }
  84. if (offset == 0) {
  85. // delay falls on the block boundary
  86. if (queue[index]) {
  87. transmit(queue[index], channel);
  88. }
  89. } else {
  90. // delay requires grabbing data from 2 blocks
  91. output = allocate();
  92. if (!output) continue;
  93. dst = output->data;
  94. if (index > 0) {
  95. prev = index - 1;
  96. } else {
  97. prev = DELAY_QUEUE_SIZE-1;
  98. }
  99. if (queue[prev]) {
  100. end = queue[prev]->data + AUDIO_BLOCK_SAMPLES;
  101. src = end - offset;
  102. while (src < end) {
  103. *dst++ = *src++; // TODO: optimize
  104. }
  105. } else {
  106. end = dst + offset;
  107. while (dst < end) {
  108. *dst++ = 0;
  109. }
  110. }
  111. end = output->data + AUDIO_BLOCK_SAMPLES;
  112. if (queue[index]) {
  113. src = queue[index]->data;
  114. while (dst < end) {
  115. *dst++ = *src++; // TODO: optimize
  116. }
  117. } else {
  118. while (dst < end) {
  119. *dst++ = 0;
  120. }
  121. }
  122. transmit(output, channel);
  123. release(output);
  124. }
  125. }
  126. }