您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

effect_chorus.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Pete (El Supremo)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include "effect_chorus.h"
  23. /******************************************************************/
  24. // A u d i o E f f e c t C h o r u s
  25. // Written by Pete (El Supremo) Jan 2014
  26. // 140219 - correct storage class (not static)
  27. boolean AudioEffectChorus::begin(short *delayline,int d_length,int n_chorus)
  28. {
  29. Serial.print("AudioEffectChorus.begin(Chorus delay line length = ");
  30. Serial.print(d_length);
  31. Serial.print(", n_chorus = ");
  32. Serial.print(n_chorus);
  33. Serial.println(")");
  34. l_delayline = NULL;
  35. r_delayline = NULL;
  36. delay_length = 0;
  37. l_circ_idx = 0;
  38. r_circ_idx = 0;
  39. if(delayline == NULL) {
  40. return(false);
  41. }
  42. if(d_length < 10) {
  43. return(false);
  44. }
  45. if(n_chorus < 1) {
  46. return(false);
  47. }
  48. l_delayline = delayline;
  49. r_delayline = delayline + d_length/2;
  50. delay_length = d_length/2;
  51. num_chorus = n_chorus;
  52. return(true);
  53. }
  54. void AudioEffectChorus::modify(int n_chorus)
  55. {
  56. num_chorus = n_chorus;
  57. }
  58. //int last_idx = 0;
  59. void AudioEffectChorus::update(void)
  60. {
  61. audio_block_t *block;
  62. short *bp;
  63. int sum;
  64. int c_idx;
  65. if(l_delayline == NULL)return;
  66. if(r_delayline == NULL)return;
  67. // do passthru
  68. // It stores the unmodified data in the delay line so that
  69. // it isn't as likely to click
  70. if(num_chorus < 1) {
  71. // Just passthrough
  72. block = receiveWritable(0);
  73. if(block) {
  74. bp = block->data;
  75. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  76. l_circ_idx++;
  77. if(l_circ_idx >= delay_length) {
  78. l_circ_idx = 0;
  79. }
  80. l_delayline[l_circ_idx] = *bp++;
  81. }
  82. transmit(block,0);
  83. release(block);
  84. }
  85. block = receiveWritable(1);
  86. if(block) {
  87. bp = block->data;
  88. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  89. r_circ_idx++;
  90. if(r_circ_idx >= delay_length) {
  91. r_circ_idx = 0;
  92. }
  93. r_delayline[r_circ_idx] = *bp++;
  94. }
  95. transmit(block,1);
  96. release(block);
  97. }
  98. return;
  99. }
  100. // L E F T C H A N N E L
  101. block = receiveWritable(0);
  102. if(block) {
  103. bp = block->data;
  104. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  105. l_circ_idx++;
  106. if(l_circ_idx >= delay_length) {
  107. l_circ_idx = 0;
  108. }
  109. l_delayline[l_circ_idx] = *bp;
  110. sum = 0;
  111. c_idx = l_circ_idx;
  112. for(int k = 0; k < num_chorus; k++) {
  113. sum += l_delayline[c_idx];
  114. if(num_chorus > 1)c_idx -= delay_length/(num_chorus - 1) - 1;
  115. if(c_idx < 0) {
  116. c_idx += delay_length;
  117. }
  118. }
  119. *bp++ = sum/num_chorus;
  120. }
  121. // send the effect output to the left channel
  122. transmit(block,0);
  123. release(block);
  124. }
  125. // R I G H T C H A N N E L
  126. block = receiveWritable(1);
  127. if(block) {
  128. bp = block->data;
  129. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  130. r_circ_idx++;
  131. if(r_circ_idx >= delay_length) {
  132. r_circ_idx = 0;
  133. }
  134. r_delayline[r_circ_idx] = *bp;
  135. sum = 0;
  136. c_idx = r_circ_idx;
  137. for(int k = 0; k < num_chorus; k++) {
  138. sum += r_delayline[c_idx];
  139. if(num_chorus > 1)c_idx -= delay_length/(num_chorus - 1) - 1;
  140. if(c_idx < 0) {
  141. c_idx += delay_length;
  142. }
  143. }
  144. *bp++ = sum/num_chorus;
  145. }
  146. // send the effect output to the left channel
  147. transmit(block,1);
  148. release(block);
  149. }
  150. }