No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

292 líneas
8.7KB

  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. // 140219 - correct the calculation of "frac"
  31. // circular addressing indices for left and right channels
  32. //short AudioEffectFlange::l_circ_idx;
  33. //short AudioEffectFlange::r_circ_idx;
  34. //short * AudioEffectFlange::l_delayline = NULL;
  35. //short * AudioEffectFlange::r_delayline = NULL;
  36. // User-supplied offset for the delayed sample
  37. // but start with passthru
  38. //int AudioEffectFlange::delay_offset_idx = FLANGE_DELAY_PASSTHRU;
  39. //int AudioEffectFlange::delay_length;
  40. //int AudioEffectFlange::delay_depth;
  41. //int AudioEffectFlange::delay_rate_incr;
  42. //unsigned int AudioEffectFlange::l_delay_rate_index;
  43. //unsigned int AudioEffectFlange::r_delay_rate_index;
  44. // fails if the user provides unreasonable values but will
  45. // coerce them and go ahead anyway. e.g. if the delay offset
  46. // is >= CHORUS_DELAY_LENGTH, the code will force it to
  47. // CHORUS_DELAY_LENGTH-1 and return false.
  48. // delay_rate is the rate (in Hz) of the sine wave modulation
  49. // delay_depth is the maximum variation around delay_offset
  50. // i.e. the total offset is delay_offset + delay_depth * sin(delay_rate)
  51. boolean AudioEffectFlange::begin(short *delayline,int d_length,int delay_offset,int d_depth,float delay_rate)
  52. {
  53. boolean all_ok = true;
  54. if(0) {
  55. Serial.print("AudioEffectFlange.begin(offset = ");
  56. Serial.print(delay_offset);
  57. Serial.print(", depth = ");
  58. Serial.print(d_depth);
  59. Serial.print(", rate = ");
  60. Serial.print(delay_rate,3);
  61. Serial.println(")");
  62. Serial.print(" FLANGE_DELAY_LENGTH = ");
  63. Serial.println(d_length);
  64. }
  65. delay_length = d_length/2;
  66. l_delayline = delayline;
  67. r_delayline = delayline + delay_length;
  68. delay_depth = d_depth;
  69. // initial index
  70. l_delay_rate_index = 0;
  71. r_delay_rate_index = 0;
  72. l_circ_idx = 0;
  73. r_circ_idx = 0;
  74. delay_rate_incr = delay_rate/44100.*2147483648.;
  75. //Serial.println(delay_rate_incr,HEX);
  76. delay_offset_idx = delay_offset;
  77. // Allow the passthru code to go through
  78. if(delay_offset_idx < -1) {
  79. delay_offset_idx = 0;
  80. all_ok = false;
  81. }
  82. if(delay_offset_idx >= delay_length) {
  83. delay_offset_idx = delay_length - 1;
  84. all_ok = false;
  85. }
  86. return(all_ok);
  87. }
  88. boolean AudioEffectFlange::modify(int delay_offset,int d_depth,float delay_rate)
  89. {
  90. boolean all_ok = true;
  91. delay_depth = d_depth;
  92. delay_rate_incr = delay_rate/44100.*2147483648.;
  93. delay_offset_idx = delay_offset;
  94. // Allow the passthru code to go through
  95. if(delay_offset_idx < -1) {
  96. delay_offset_idx = 0;
  97. all_ok = false;
  98. }
  99. if(delay_offset_idx >= delay_length) {
  100. delay_offset_idx = delay_length - 1;
  101. all_ok = false;
  102. }
  103. l_delay_rate_index = 0;
  104. r_delay_rate_index = 0;
  105. l_circ_idx = 0;
  106. r_circ_idx = 0;
  107. return(all_ok);
  108. }
  109. void AudioEffectFlange::update(void)
  110. {
  111. audio_block_t *block;
  112. int idx;
  113. short *bp;
  114. short frac;
  115. int idx1;
  116. if(l_delayline == NULL)return;
  117. if(r_delayline == NULL)return;
  118. // do passthru
  119. if(delay_offset_idx == FLANGE_DELAY_PASSTHRU) {
  120. // Just passthrough
  121. block = receiveWritable(0);
  122. if(block) {
  123. bp = block->data;
  124. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  125. l_circ_idx++;
  126. if(l_circ_idx >= delay_length) {
  127. l_circ_idx = 0;
  128. }
  129. l_delayline[l_circ_idx] = *bp++;
  130. }
  131. transmit(block,0);
  132. release(block);
  133. }
  134. block = receiveWritable(1);
  135. if(block) {
  136. bp = block->data;
  137. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  138. r_circ_idx++;
  139. if(r_circ_idx >= delay_length) {
  140. r_circ_idx = 0;
  141. }
  142. r_delayline[r_circ_idx] = *bp++;
  143. }
  144. transmit(block,1);
  145. release(block);
  146. }
  147. return;
  148. }
  149. // L E F T C H A N N E L
  150. block = receiveWritable(0);
  151. if(block) {
  152. bp = block->data;
  153. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  154. // increment the index into the circular delay line buffer
  155. l_circ_idx++;
  156. // wrap the index around if necessary
  157. if(l_circ_idx >= delay_length) {
  158. l_circ_idx = 0;
  159. }
  160. // store the current sample in the delay line
  161. l_delayline[l_circ_idx] = *bp;
  162. // The argument to the arm_sin_q15 function is NOT in radians. It is
  163. // actually, in effect, the fraction remaining after the division
  164. // of radians/(2*PI) which is then expressed as a positive Q15
  165. // fraction in the interval [0 , +1) - this is l_delay_rate_index.
  166. // l_delay_rate_index should probably be called l_delay_rate_phase
  167. // (sorry about that!)
  168. // It is a Q31 positive number of which the high order 16 bits are
  169. // used when calculating the sine. idx will have a value in the
  170. // interval [-1 , +1)
  171. frac = arm_sin_q15( (q15_t)((l_delay_rate_index >> 16) & 0x7fff));
  172. // multiply the sin by the delay depth
  173. idx = (frac * delay_depth) >> 15;
  174. //Serial.println(idx);
  175. // Calculate the offset into the buffer
  176. idx = l_circ_idx - (delay_offset_idx + idx);
  177. // and adjust idx to point into the circular buffer
  178. if(idx < 0) {
  179. idx += delay_length;
  180. }
  181. if(idx >= delay_length) {
  182. idx -= delay_length;
  183. }
  184. // Here we interpolate between two indices but if the sine was negative
  185. // then we interpolate between idx and idx-1, otherwise the
  186. // interpolation is between idx and idx+1
  187. if(frac < 0)
  188. idx1 = idx - 1;
  189. else
  190. idx1 = idx + 1;
  191. // adjust idx1 in the circular buffer
  192. if(idx1 < 0) {
  193. idx1 += delay_length;
  194. }
  195. if(idx1 >= delay_length) {
  196. idx1 -= delay_length;
  197. }
  198. // Do the interpolation
  199. frac = (l_delay_rate_index >> 1) &0x7fff;
  200. frac = (( (int)(l_delayline[idx1] - l_delayline[idx])*frac) >> 15);
  201. //frac = 0;
  202. *bp++ = (l_delayline[l_circ_idx]
  203. + l_delayline[idx] + frac
  204. // + l_delayline[(l_circ_idx + delay_length/2) % delay_length]
  205. )/2;
  206. l_delay_rate_index += delay_rate_incr;
  207. if(l_delay_rate_index & 0x80000000) {
  208. l_delay_rate_index &= 0x7fffffff;
  209. }
  210. }
  211. // send the effect output to the left channel
  212. transmit(block,0);
  213. release(block);
  214. }
  215. // R I G H T C H A N N E L
  216. block = receiveWritable(1);
  217. if(block) {
  218. bp = block->data;
  219. for(int i = 0;i < AUDIO_BLOCK_SAMPLES;i++) {
  220. r_circ_idx++;
  221. if(r_circ_idx >= delay_length) {
  222. r_circ_idx = 0;
  223. }
  224. r_delayline[r_circ_idx] = *bp;
  225. frac = arm_sin_q15( (q15_t)((r_delay_rate_index >> 16)&0x7fff));
  226. idx = (frac * delay_depth) >> 15;
  227. idx = r_circ_idx - (delay_offset_idx + idx);
  228. if(idx < 0) {
  229. idx += delay_length;
  230. }
  231. if(idx >= delay_length) {
  232. idx -= delay_length;
  233. }
  234. if(frac < 0)
  235. idx1 = idx - 1;
  236. else
  237. idx1 = idx + 1;
  238. if(idx1 < 0) {
  239. idx1 += delay_length;
  240. }
  241. if(idx1 >= delay_length) {
  242. idx1 -= delay_length;
  243. }
  244. frac = (r_delay_rate_index >> 1) &0x7fff;
  245. frac = (( (int)(r_delayline[idx1] - r_delayline[idx])*frac) >> 15);
  246. //frac = 0;
  247. *bp++ = (r_delayline[r_circ_idx]
  248. + r_delayline[idx] + frac
  249. )/2;
  250. r_delay_rate_index += delay_rate_incr;
  251. if(r_delay_rate_index & 0x80000000) {
  252. r_delay_rate_index &= 0x7fffffff;
  253. }
  254. }
  255. // send the effect output to the right channel
  256. transmit(block,1);
  257. release(block);
  258. }
  259. }