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.

174 line
3.7KB

  1. #include "effect_chorus.h"
  2. /******************************************************************/
  3. // A u d i o E f f e c t C h o r u s
  4. // Written by Pete (El Supremo) Jan 2014
  5. // circular addressing indices for left and right channels
  6. short AudioEffectChorus::l_circ_idx;
  7. short AudioEffectChorus::r_circ_idx;
  8. short * AudioEffectChorus::l_delayline = NULL;
  9. short * AudioEffectChorus::r_delayline = NULL;
  10. int AudioEffectChorus::delay_length;
  11. // An initial value of zero indicates passthru
  12. int AudioEffectChorus::num_chorus = 0;
  13. // All three must be valid.
  14. boolean AudioEffectChorus::begin(short *delayline,int d_length,int n_chorus)
  15. {
  16. Serial.print("AudioEffectChorus.begin(Chorus delay line length = ");
  17. Serial.print(d_length);
  18. Serial.print(", n_chorus = ");
  19. Serial.print(n_chorus);
  20. Serial.println(")");
  21. l_delayline = NULL;
  22. r_delayline = NULL;
  23. delay_length = 0;
  24. l_circ_idx = 0;
  25. r_circ_idx = 0;
  26. if(delayline == NULL) {
  27. return(false);
  28. }
  29. if(d_length < 10) {
  30. return(false);
  31. }
  32. if(n_chorus < 1) {
  33. return(false);
  34. }
  35. l_delayline = delayline;
  36. r_delayline = delayline + d_length/2;
  37. delay_length = d_length/2;
  38. num_chorus = n_chorus;
  39. return(true);
  40. }
  41. // This has the same effect as begin(NULL,0);
  42. void AudioEffectChorus::stop(void)
  43. {
  44. }
  45. void AudioEffectChorus::modify(int n_chorus)
  46. {
  47. num_chorus = n_chorus;
  48. }
  49. int iabs(int x)
  50. {
  51. if(x < 0)return(-x);
  52. return(x);
  53. }
  54. //static int d_count = 0;
  55. int last_idx = 0;
  56. void AudioEffectChorus::update(void)
  57. {
  58. audio_block_t *block;
  59. short *bp;
  60. int sum;
  61. int c_idx;
  62. if(l_delayline == NULL)return;
  63. if(r_delayline == NULL)return;
  64. // do passthru
  65. // It stores the unmodified data in the delay line so that
  66. // it isn't as likely to click
  67. if(num_chorus < 1) {
  68. // Just passthrough
  69. block = receiveWritable(0);
  70. if(block) {
  71. bp = block->data;
  72. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  73. l_circ_idx++;
  74. if(l_circ_idx >= delay_length) {
  75. l_circ_idx = 0;
  76. }
  77. l_delayline[l_circ_idx] = *bp++;
  78. }
  79. transmit(block,0);
  80. release(block);
  81. }
  82. block = receiveWritable(1);
  83. if(block) {
  84. bp = block->data;
  85. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  86. r_circ_idx++;
  87. if(r_circ_idx >= delay_length) {
  88. r_circ_idx = 0;
  89. }
  90. r_delayline[r_circ_idx] = *bp++;
  91. }
  92. transmit(block,1);
  93. release(block);
  94. }
  95. return;
  96. }
  97. // L E F T C H A N N E L
  98. block = receiveWritable(0);
  99. if(block) {
  100. bp = block->data;
  101. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  102. l_circ_idx++;
  103. if(l_circ_idx >= delay_length) {
  104. l_circ_idx = 0;
  105. }
  106. l_delayline[l_circ_idx] = *bp;
  107. sum = 0;
  108. c_idx = l_circ_idx;
  109. for(int k = 0; k < num_chorus; k++) {
  110. sum += l_delayline[c_idx];
  111. if(num_chorus > 1)c_idx -= delay_length/(num_chorus - 1) - 1;
  112. if(c_idx < 0) {
  113. c_idx += delay_length;
  114. }
  115. }
  116. *bp++ = sum/num_chorus;
  117. }
  118. // send the effect output to the left channel
  119. transmit(block,0);
  120. release(block);
  121. }
  122. // R I G H T C H A N N E L
  123. block = receiveWritable(1);
  124. if(block) {
  125. bp = block->data;
  126. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  127. r_circ_idx++;
  128. if(r_circ_idx >= delay_length) {
  129. r_circ_idx = 0;
  130. }
  131. r_delayline[r_circ_idx] = *bp;
  132. sum = 0;
  133. c_idx = r_circ_idx;
  134. for(int k = 0; k < num_chorus; k++) {
  135. sum += r_delayline[c_idx];
  136. if(num_chorus > 1)c_idx -= delay_length/(num_chorus - 1) - 1;
  137. if(c_idx < 0) {
  138. c_idx += delay_length;
  139. }
  140. }
  141. *bp++ = sum/num_chorus;
  142. }
  143. // send the effect output to the left channel
  144. transmit(block,1);
  145. release(block);
  146. }
  147. }