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.

227 lines
5.3KB

  1. /*
  2. Change the chorus code to produce a flange effect
  3. 140219
  4. e
  5. FMI:
  6. The audio board uses the following pins.
  7. 6 - MEMCS
  8. 7 - MOSI
  9. 9 - BCLK
  10. 10 - SDCS
  11. 11 - MCLK
  12. 12 - MISO
  13. 13 - RX
  14. 14 - SCLK
  15. 15 - VOL
  16. 18 - SDA
  17. 19 - SCL
  18. 22 - TX
  19. 23 - LRCLK
  20. AudioProcessorUsage()
  21. AudioProcessorUsageMax()
  22. AudioProcessorUsageMaxReset()
  23. AudioMemoryUsage()
  24. AudioMemoryUsageMax()
  25. AudioMemoryUsageMaxReset()
  26. The CPU usage is an integer from 0 to 100, and the memory is from 0 to however
  27. many blocks you provided with AudioMemory().
  28. */
  29. #include <arm_math.h>
  30. #include <Audio.h>
  31. #include <Wire.h>
  32. //#include <WM8731.h>
  33. #include <SD.h>
  34. #include <SPI.h>
  35. #include <Bounce.h>
  36. // Number of samples in ONE channel
  37. #define FLANGE_DELAY_LENGTH (6*AUDIO_BLOCK_SAMPLES)
  38. // Allocate the delay line for left and right channels
  39. // The delayline will hold left and right samples so it
  40. // should be declared to be twice as long as the desired
  41. // number of samples in one channel
  42. #define FLANGE_DELAYLINE (FLANGE_DELAY_LENGTH*2)
  43. // The delay line for left and right channels
  44. short delayline[FLANGE_DELAYLINE];
  45. // If this pin is grounded the effect is turned off,
  46. // which makes it just pass through the audio
  47. // Don't use any of the pins listed above
  48. #define PASSTHRU_PIN 1
  49. Bounce b_passthru = Bounce(PASSTHRU_PIN,15);
  50. //const int myInput = AUDIO_INPUT_MIC;
  51. const int myInput = AUDIO_INPUT_LINEIN;
  52. AudioInputI2S audioInput; // audio shield: mic or line-in
  53. AudioEffectFlange myEffect;
  54. AudioOutputI2S audioOutput; // audio shield: headphones & line-out
  55. // Create Audio connections between the components
  56. // Both channels of the audio input go to the flange effect
  57. AudioConnection c1(audioInput, 0, myEffect, 0);
  58. AudioConnection c2(audioInput, 1, myEffect, 1);
  59. // both channels from the flange effect go to the audio output
  60. AudioConnection c3(myEffect, 0, audioOutput, 0);
  61. AudioConnection c4(myEffect, 1, audioOutput, 1);
  62. AudioControlSGTL5000 audioShield;
  63. /*
  64. int s_idx = FLANGE_DELAY_LENGTH/2;
  65. int s_depth = FLANGE_DELAY_LENGTH/16;
  66. double s_freq = 1;
  67. // <<<<<<<<<<<<<<>>>>>>>>>>>>>>>>
  68. // 12
  69. int s_idx = FLANGE_DELAY_LENGTH/2;
  70. int s_depth = FLANGE_DELAY_LENGTH/8;
  71. double s_freq = .125;
  72. // with .125 the ticking is about 1Hz with music
  73. // but with the noise sample it is a bit slower than that
  74. // <<<<<<<<<<<<<<>>>>>>>>>>>>>>>>
  75. */
  76. /*
  77. // <<<<<<<<<<<<<<>>>>>>>>>>>>>>>>
  78. // 12
  79. int s_idx = FLANGE_DELAY_LENGTH/2;
  80. int s_depth = FLANGE_DELAY_LENGTH/12;
  81. double s_freq = .125;
  82. // with .125 the ticking is about 1Hz with music
  83. // but with the noise sample it is a bit slower than that
  84. // <<<<<<<<<<<<<<>>>>>>>>>>>>>>>>
  85. */
  86. /*
  87. //12
  88. int s_idx = 15*FLANGE_DELAY_LENGTH/16;
  89. int s_depth = 15*FLANGE_DELAY_LENGTH/16;
  90. double s_freq = 0;
  91. */
  92. /*
  93. //12
  94. int s_idx = 2*FLANGE_DELAY_LENGTH/4;
  95. int s_depth = FLANGE_DELAY_LENGTH/8;
  96. double s_freq = .0625;
  97. */
  98. /*
  99. //12 - good with Eric Clapton Unplugged
  100. int s_idx = 3*FLANGE_DELAY_LENGTH/4;
  101. int s_depth = FLANGE_DELAY_LENGTH/8;
  102. double s_freq = .0625;
  103. */
  104. /*
  105. // Real flange effect! delay line is 2*
  106. int s_idx = 2*FLANGE_DELAY_LENGTH/4;
  107. int s_depth = FLANGE_DELAY_LENGTH/4;
  108. double s_freq = 2;
  109. */
  110. /* 2 -
  111. int s_idx = 2*FLANGE_DELAY_LENGTH/4;
  112. int s_depth = FLANGE_DELAY_LENGTH/8;
  113. double s_freq = 4;
  114. */
  115. /*
  116. // 4
  117. int s_idx = FLANGE_DELAY_LENGTH/4;
  118. int s_depth = FLANGE_DELAY_LENGTH/4;
  119. double s_freq = .25;
  120. */
  121. // 4
  122. int s_idx = FLANGE_DELAY_LENGTH/4;
  123. int s_depth = FLANGE_DELAY_LENGTH/4;
  124. double s_freq = .5;
  125. void setup() {
  126. Serial.begin(9600);
  127. while (!Serial) ;
  128. delay(3000);
  129. pinMode(PASSTHRU_PIN,INPUT_PULLUP);
  130. // It doesn't work properly with any less than 8
  131. // but that was an earlier version. Processor and
  132. // memory usage are now (ver j)
  133. // Proc = 24 (24), Mem = 4 (4)
  134. AudioMemory(8);
  135. audioShield.enable();
  136. audioShield.inputSelect(myInput);
  137. audioShield.volume(0.5);
  138. // Warn that the passthru pin is grounded
  139. if(!digitalRead(PASSTHRU_PIN)) {
  140. Serial.print("PASSTHRU_PIN (");
  141. Serial.print(PASSTHRU_PIN);
  142. Serial.println(") is grounded");
  143. }
  144. // Set up the flange effect
  145. // - address of delayline
  146. // - total number of samples (left AND right) in the delay line
  147. // - Index (in samples) into the delay line for the added voice
  148. // - Depth of the flange effect
  149. // - frequency of the flange effect
  150. myEffect.begin(delayline,FLANGE_DELAYLINE,s_idx,s_depth,s_freq);
  151. // I want output on the line out too
  152. audioShield.unmuteLineout();
  153. Serial.println("setup done");
  154. AudioProcessorUsageMaxReset();
  155. AudioMemoryUsageMaxReset();
  156. }
  157. // audio volume
  158. int volume = 0;
  159. unsigned long last_time = millis();
  160. void loop()
  161. {
  162. // Volume control
  163. int n = analogRead(15);
  164. if (n != volume) {
  165. volume = n;
  166. audioShield.volume((float)n / 10.23);
  167. }
  168. if(0) {
  169. if(millis() - last_time >= 5000) {
  170. Serial.print("Proc = ");
  171. Serial.print(AudioProcessorUsage());
  172. Serial.print(" (");
  173. Serial.print(AudioProcessorUsageMax());
  174. Serial.print("), Mem = ");
  175. Serial.print(AudioMemoryUsage());
  176. Serial.print(" (");
  177. Serial.print(AudioMemoryUsageMax());
  178. Serial.println(")");
  179. last_time = millis();
  180. }
  181. }
  182. // update the button
  183. b_passthru.update();
  184. // If the passthru button is pushed, save the current
  185. // filter index and then switch the effect to passthru
  186. if(b_passthru.fallingEdge()) {
  187. myEffect.modify(DELAY_PASSTHRU,0,0);
  188. }
  189. // If passthru button is released, restore the effect
  190. if(b_passthru.risingEdge()) {
  191. myEffect.modify(s_idx,s_depth,s_freq);
  192. }
  193. }