180 line
4.6KB

  1. /*
  2. VERSION 2 - use modified library which has been changed to handle
  3. one channel instead of two
  4. Proc = 21 (22), Mem = 4 (6)
  5. 140529
  6. 2a
  7. - default at startup is to have passthru ON and the button
  8. switches the flange effect in.
  9. From: http://www.cs.cf.ac.uk/Dave/CM0268/PDF/10_CM0268_Audio_FX.pdf
  10. about Comb filter effects
  11. Effect Delay range (ms) Modulation
  12. Resonator 0 - 20 None
  13. Flanger 0 - 15 Sinusoidal (approx 1Hz)
  14. Chorus 25 - 50 None
  15. Echo >50 None
  16. FMI:
  17. The audio board uses the following pins.
  18. 6 - MEMCS
  19. 7 - MOSI
  20. 9 - BCLK
  21. 10 - SDCS
  22. 11 - MCLK
  23. 12 - MISO
  24. 13 - RX
  25. 14 - SCLK
  26. 15 - VOL
  27. 18 - SDA
  28. 19 - SCL
  29. 22 - TX
  30. 23 - LRCLK
  31. AudioProcessorUsage()
  32. AudioProcessorUsageMax()
  33. AudioProcessorUsageMaxReset()
  34. AudioMemoryUsage()
  35. AudioMemoryUsageMax()
  36. AudioMemoryUsageMaxReset()
  37. The CPU usage is an integer from 0 to 100, and the memory is from 0 to however
  38. many blocks you provided with AudioMemory().
  39. */
  40. #include <Audio.h>
  41. #include <Wire.h>
  42. #include <SD.h>
  43. #include <SPI.h>
  44. #include <SerialFlash.h>
  45. #include <Bounce.h>
  46. // Number of samples in each delay line
  47. #define FLANGE_DELAY_LENGTH (6*AUDIO_BLOCK_SAMPLES)
  48. // Allocate the delay lines for left and right channels
  49. short l_delayline[FLANGE_DELAY_LENGTH];
  50. short r_delayline[FLANGE_DELAY_LENGTH];
  51. // Default is to just pass the audio through. Grounding this pin
  52. // applies the flange effect
  53. // Don't use any of the pins listed above
  54. #define PASSTHRU_PIN 1
  55. Bounce b_passthru = Bounce(PASSTHRU_PIN,15);
  56. //const int myInput = AUDIO_INPUT_MIC;
  57. const int myInput = AUDIO_INPUT_LINEIN;
  58. AudioInputI2S audioInput; // audio shield: mic or line-in
  59. AudioEffectFlange l_myEffect;
  60. AudioEffectFlange r_myEffect;
  61. AudioOutputI2S audioOutput; // audio shield: headphones & line-out
  62. // Create Audio connections between the components
  63. // Both channels of the audio input go to the flange effect
  64. AudioConnection c1(audioInput, 0, l_myEffect, 0);
  65. AudioConnection c2(audioInput, 1, r_myEffect, 0);
  66. // both channels from the flange effect go to the audio output
  67. AudioConnection c3(l_myEffect, 0, audioOutput, 0);
  68. AudioConnection c4(r_myEffect, 0, audioOutput, 1);
  69. AudioControlSGTL5000 audioShield;
  70. int s_idx = FLANGE_DELAY_LENGTH/4;
  71. int s_depth = FLANGE_DELAY_LENGTH/4;
  72. double s_freq = .5;
  73. void setup() {
  74. Serial.begin(9600);
  75. while (!Serial) ;
  76. delay(3000);
  77. pinMode(PASSTHRU_PIN,INPUT_PULLUP);
  78. // It doesn't work properly with any less than 8
  79. // but that was an earlier version. Processor and
  80. // memory usage are now (ver j)
  81. // Proc = 24 (24), Mem = 4 (4)
  82. AudioMemory(8);
  83. audioShield.enable();
  84. audioShield.inputSelect(myInput);
  85. audioShield.volume(0.5);
  86. // Warn that the passthru pin is grounded
  87. if(!digitalRead(PASSTHRU_PIN)) {
  88. Serial.print("PASSTHRU_PIN (");
  89. Serial.print(PASSTHRU_PIN);
  90. Serial.println(") is grounded");
  91. }
  92. // Set up the flange effect:
  93. // address of delayline
  94. // total number of samples in the delay line
  95. // Index (in samples) into the delay line for the added voice
  96. // Depth of the flange effect
  97. // frequency of the flange effect
  98. l_myEffect.begin(l_delayline,FLANGE_DELAY_LENGTH,s_idx,s_depth,s_freq);
  99. r_myEffect.begin(r_delayline,FLANGE_DELAY_LENGTH,s_idx,s_depth,s_freq);
  100. // Initially the effect is off. It is switched on when the
  101. // PASSTHRU button is pushed.
  102. l_myEffect.voices(FLANGE_DELAY_PASSTHRU,0,0);
  103. r_myEffect.voices(FLANGE_DELAY_PASSTHRU,0,0);
  104. Serial.println("setup done");
  105. AudioProcessorUsageMaxReset();
  106. AudioMemoryUsageMaxReset();
  107. }
  108. // audio volume
  109. int volume = 0;
  110. unsigned long last_time = millis();
  111. void loop()
  112. {
  113. // Volume control
  114. int n = analogRead(15);
  115. if (n != volume) {
  116. volume = n;
  117. audioShield.volume((float)n / 10.23);
  118. }
  119. if(0) {
  120. if(millis() - last_time >= 5000) {
  121. Serial.print("Proc = ");
  122. Serial.print(AudioProcessorUsage());
  123. Serial.print(" (");
  124. Serial.print(AudioProcessorUsageMax());
  125. Serial.print("), Mem = ");
  126. Serial.print(AudioMemoryUsage());
  127. Serial.print(" (");
  128. Serial.print(AudioMemoryUsageMax());
  129. Serial.println(")");
  130. last_time = millis();
  131. }
  132. }
  133. // update the button
  134. b_passthru.update();
  135. // If the passthru button is pushed
  136. // turn the flange effect on
  137. // filter index and then switch the effect to passthru
  138. if(b_passthru.fallingEdge()) {
  139. l_myEffect.voices(s_idx,s_depth,s_freq);
  140. r_myEffect.voices(s_idx,s_depth,s_freq);
  141. }
  142. // If passthru button is released restore passthru
  143. if(b_passthru.risingEdge()) {
  144. l_myEffect.voices(FLANGE_DELAY_PASSTHRU,0,0);
  145. r_myEffect.voices(FLANGE_DELAY_PASSTHRU,0,0);
  146. }
  147. }