Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

effect_flange.cpp 7.0KB

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