Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

221 linhas
7.0KB

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