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.

179 satır
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 <Bounce.h>
  45. // Number of samples in each delay line
  46. #define FLANGE_DELAY_LENGTH (6*AUDIO_BLOCK_SAMPLES)
  47. // Allocate the delay lines for left and right channels
  48. short l_delayline[FLANGE_DELAY_LENGTH];
  49. short r_delayline[FLANGE_DELAY_LENGTH];
  50. // Default is to just pass the audio through. Grounding this pin
  51. // applies the flange effect
  52. // Don't use any of the pins listed above
  53. #define PASSTHRU_PIN 1
  54. Bounce b_passthru = Bounce(PASSTHRU_PIN,15);
  55. //const int myInput = AUDIO_INPUT_MIC;
  56. const int myInput = AUDIO_INPUT_LINEIN;
  57. AudioInputI2S audioInput; // audio shield: mic or line-in
  58. AudioEffectFlange l_myEffect;
  59. AudioEffectFlange r_myEffect;
  60. AudioOutputI2S audioOutput; // audio shield: headphones & line-out
  61. // Create Audio connections between the components
  62. // Both channels of the audio input go to the flange effect
  63. AudioConnection c1(audioInput, 0, l_myEffect, 0);
  64. AudioConnection c2(audioInput, 1, r_myEffect, 0);
  65. // both channels from the flange effect go to the audio output
  66. AudioConnection c3(l_myEffect, 0, audioOutput, 0);
  67. AudioConnection c4(r_myEffect, 0, audioOutput, 1);
  68. AudioControlSGTL5000 audioShield;
  69. int s_idx = FLANGE_DELAY_LENGTH/4;
  70. int s_depth = FLANGE_DELAY_LENGTH/4;
  71. double s_freq = .5;
  72. void setup() {
  73. Serial.begin(9600);
  74. while (!Serial) ;
  75. delay(3000);
  76. pinMode(PASSTHRU_PIN,INPUT_PULLUP);
  77. // It doesn't work properly with any less than 8
  78. // but that was an earlier version. Processor and
  79. // memory usage are now (ver j)
  80. // Proc = 24 (24), Mem = 4 (4)
  81. AudioMemory(8);
  82. audioShield.enable();
  83. audioShield.inputSelect(myInput);
  84. audioShield.volume(0.5);
  85. // Warn that the passthru pin is grounded
  86. if(!digitalRead(PASSTHRU_PIN)) {
  87. Serial.print("PASSTHRU_PIN (");
  88. Serial.print(PASSTHRU_PIN);
  89. Serial.println(") is grounded");
  90. }
  91. // Set up the flange effect:
  92. // address of delayline
  93. // total number of samples in the delay line
  94. // Index (in samples) into the delay line for the added voice
  95. // Depth of the flange effect
  96. // frequency of the flange effect
  97. l_myEffect.begin(l_delayline,FLANGE_DELAY_LENGTH,s_idx,s_depth,s_freq);
  98. r_myEffect.begin(r_delayline,FLANGE_DELAY_LENGTH,s_idx,s_depth,s_freq);
  99. // Initially the effect is off. It is switched on when the
  100. // PASSTHRU button is pushed.
  101. l_myEffect.voices(FLANGE_DELAY_PASSTHRU,0,0);
  102. r_myEffect.voices(FLANGE_DELAY_PASSTHRU,0,0);
  103. Serial.println("setup done");
  104. AudioProcessorUsageMaxReset();
  105. AudioMemoryUsageMaxReset();
  106. }
  107. // audio volume
  108. int volume = 0;
  109. unsigned long last_time = millis();
  110. void loop()
  111. {
  112. // Volume control
  113. int n = analogRead(15);
  114. if (n != volume) {
  115. volume = n;
  116. audioShield.volume((float)n / 10.23);
  117. }
  118. if(0) {
  119. if(millis() - last_time >= 5000) {
  120. Serial.print("Proc = ");
  121. Serial.print(AudioProcessorUsage());
  122. Serial.print(" (");
  123. Serial.print(AudioProcessorUsageMax());
  124. Serial.print("), Mem = ");
  125. Serial.print(AudioMemoryUsage());
  126. Serial.print(" (");
  127. Serial.print(AudioMemoryUsageMax());
  128. Serial.println(")");
  129. last_time = millis();
  130. }
  131. }
  132. // update the button
  133. b_passthru.update();
  134. // If the passthru button is pushed
  135. // turn the flange effect on
  136. // filter index and then switch the effect to passthru
  137. if(b_passthru.fallingEdge()) {
  138. l_myEffect.voices(s_idx,s_depth,s_freq);
  139. r_myEffect.voices(s_idx,s_depth,s_freq);
  140. }
  141. // If passthru button is released restore passthru
  142. if(b_passthru.risingEdge()) {
  143. l_myEffect.voices(FLANGE_DELAY_PASSTHRU,0,0);
  144. r_myEffect.voices(FLANGE_DELAY_PASSTHRU,0,0);
  145. }
  146. }