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.

250 lines
6.3KB

  1. #include "Audio.h"
  2. #include "arm_math.h"
  3. #include "utility/dspinst.h"
  4. /******************************************************************/
  5. // A u d i o E f f e c t F l a n g e
  6. // Written by Pete (El Supremo) Jan 2014
  7. // 140207 - fix calculation of delay_rate_incr which is expressed as
  8. // a fraction of 2*PI
  9. // 140207 - cosmetic fix to begin()
  10. // circular addressing indices for left and right channels
  11. short AudioEffectFlange::l_circ_idx;
  12. short AudioEffectFlange::r_circ_idx;
  13. short * AudioEffectFlange::l_delayline = NULL;
  14. short * AudioEffectFlange::r_delayline = NULL;
  15. // User-supplied offset for the delayed sample
  16. // but start with passthru
  17. int AudioEffectFlange::delay_offset_idx = DELAY_PASSTHRU;
  18. int AudioEffectFlange::delay_length;
  19. int AudioEffectFlange::delay_depth;
  20. int AudioEffectFlange::delay_rate_incr;
  21. unsigned int AudioEffectFlange::l_delay_rate_index;
  22. unsigned int AudioEffectFlange::r_delay_rate_index;
  23. // fails if the user provides unreasonable values but will
  24. // coerce them and go ahead anyway. e.g. if the delay offset
  25. // is >= CHORUS_DELAY_LENGTH, the code will force it to
  26. // CHORUS_DELAY_LENGTH-1 and return false.
  27. // delay_rate is the rate (in Hz) of the sine wave modulation
  28. // delay_depth is the maximum variation around delay_offset
  29. // i.e. the total offset is delay_offset + delay_depth * sin(delay_rate)
  30. boolean AudioEffectFlange::begin(short *delayline,int d_length,int delay_offset,int d_depth,float delay_rate)
  31. {
  32. boolean all_ok = true;
  33. if(0) {
  34. Serial.print("AudioEffectFlange.begin(offset = ");
  35. Serial.print(delay_offset);
  36. Serial.print(", depth = ");
  37. Serial.print(d_depth);
  38. Serial.print(", rate = ");
  39. Serial.print(delay_rate,3);
  40. Serial.println(")");
  41. Serial.print(" FLANGE_DELAY_LENGTH = ");
  42. Serial.println(d_length);
  43. }
  44. delay_length = d_length/2;
  45. l_delayline = delayline;
  46. r_delayline = delayline + delay_length;
  47. delay_depth = d_depth;
  48. // initial index
  49. l_delay_rate_index = 0;
  50. r_delay_rate_index = 0;
  51. l_circ_idx = 0;
  52. r_circ_idx = 0;
  53. delay_rate_incr = delay_rate/44100.*2147483648.;
  54. //Serial.println(delay_rate_incr,HEX);
  55. delay_offset_idx = delay_offset;
  56. // Allow the passthru code to go through
  57. if(delay_offset_idx < -1) {
  58. delay_offset_idx = 0;
  59. all_ok = false;
  60. }
  61. if(delay_offset_idx >= delay_length) {
  62. delay_offset_idx = delay_length - 1;
  63. all_ok = false;
  64. }
  65. return(all_ok);
  66. }
  67. boolean AudioEffectFlange::modify(int delay_offset,int d_depth,float delay_rate)
  68. {
  69. boolean all_ok = true;
  70. delay_depth = d_depth;
  71. delay_rate_incr = delay_rate/44100.*2147483648.;
  72. delay_offset_idx = delay_offset;
  73. // Allow the passthru code to go through
  74. if(delay_offset_idx < -1) {
  75. delay_offset_idx = 0;
  76. all_ok = false;
  77. }
  78. if(delay_offset_idx >= delay_length) {
  79. delay_offset_idx = delay_length - 1;
  80. all_ok = false;
  81. }
  82. l_delay_rate_index = 0;
  83. r_delay_rate_index = 0;
  84. l_circ_idx = 0;
  85. r_circ_idx = 0;
  86. return(all_ok);
  87. }
  88. void AudioEffectFlange::update(void)
  89. {
  90. audio_block_t *block;
  91. int idx;
  92. short *bp;
  93. short frac;
  94. int idx1;
  95. if(l_delayline == NULL)return;
  96. if(r_delayline == NULL)return;
  97. // do passthru
  98. if(delay_offset_idx == DELAY_PASSTHRU) {
  99. // Just passthrough
  100. block = receiveWritable(0);
  101. if(block) {
  102. bp = block->data;
  103. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  104. l_circ_idx++;
  105. if(l_circ_idx >= delay_length) {
  106. l_circ_idx = 0;
  107. }
  108. l_delayline[l_circ_idx] = *bp++;
  109. }
  110. transmit(block,0);
  111. release(block);
  112. }
  113. block = receiveWritable(1);
  114. if(block) {
  115. bp = block->data;
  116. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  117. r_circ_idx++;
  118. if(r_circ_idx >= delay_length) {
  119. r_circ_idx = 0;
  120. }
  121. r_delayline[r_circ_idx] = *bp++;
  122. }
  123. transmit(block,1);
  124. release(block);
  125. }
  126. return;
  127. }
  128. // L E F T C H A N N E L
  129. block = receiveWritable(0);
  130. if(block) {
  131. bp = block->data;
  132. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  133. l_circ_idx++;
  134. if(l_circ_idx >= delay_length) {
  135. l_circ_idx = 0;
  136. }
  137. l_delayline[l_circ_idx] = *bp;
  138. idx = arm_sin_q15( (q15_t)((l_delay_rate_index >> 16) & 0x7fff));
  139. idx = (idx * delay_depth) >> 15;
  140. //Serial.println(idx);
  141. idx = l_circ_idx - (delay_offset_idx + idx);
  142. if(idx < 0) {
  143. idx += delay_length;
  144. }
  145. if(idx >= delay_length) {
  146. idx -= delay_length;
  147. }
  148. if(frac < 0)
  149. idx1 = idx - 1;
  150. else
  151. idx1 = idx + 1;
  152. if(idx1 < 0) {
  153. idx1 += delay_length;
  154. }
  155. if(idx1 >= delay_length) {
  156. idx1 -= delay_length;
  157. }
  158. frac = (l_delay_rate_index >> 1) &0x7fff;
  159. frac = (( (int)(l_delayline[idx1] - l_delayline[idx])*frac) >> 15);
  160. *bp++ = (l_delayline[l_circ_idx]
  161. + l_delayline[idx] + frac
  162. )/2;
  163. l_delay_rate_index += delay_rate_incr;
  164. if(l_delay_rate_index & 0x80000000) {
  165. l_delay_rate_index &= 0x7fffffff;
  166. }
  167. }
  168. // send the effect output to the left channel
  169. transmit(block,0);
  170. release(block);
  171. }
  172. // R I G H T C H A N N E L
  173. block = receiveWritable(1);
  174. if(block) {
  175. bp = block->data;
  176. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  177. r_circ_idx++;
  178. if(r_circ_idx >= delay_length) {
  179. r_circ_idx = 0;
  180. }
  181. r_delayline[r_circ_idx] = *bp;
  182. idx = arm_sin_q15( (q15_t)((r_delay_rate_index >> 16)&0x7fff));
  183. idx = (idx * delay_depth) >> 15;
  184. idx = r_circ_idx - (delay_offset_idx + idx);
  185. if(idx < 0) {
  186. idx += delay_length;
  187. }
  188. if(idx >= delay_length) {
  189. idx -= delay_length;
  190. }
  191. if(frac < 0)
  192. idx1 = idx - 1;
  193. else
  194. idx1 = idx + 1;
  195. if(idx1 < 0) {
  196. idx1 += delay_length;
  197. }
  198. if(idx1 >= delay_length) {
  199. idx1 -= delay_length;
  200. }
  201. frac = (r_delay_rate_index >> 1) &0x7fff;
  202. frac = (( (int)(r_delayline[idx1] - r_delayline[idx])*frac) >> 15);
  203. *bp++ = (r_delayline[r_circ_idx]
  204. + r_delayline[idx] + frac
  205. )/2;
  206. r_delay_rate_index += delay_rate_incr;
  207. if(r_delay_rate_index & 0x80000000) {
  208. r_delay_rate_index &= 0x7fffffff;
  209. }
  210. }
  211. // send the effect output to the right channel
  212. transmit(block,1);
  213. release(block);
  214. }
  215. }