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.

268 lines
7.4KB

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