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.

effect_chorus.ino 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. PROC/MEM 9/4
  3. 140219
  4. p
  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 CHORUS_DELAY_LENGTH (16*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 CHORUS_DELAYLINE (CHORUS_DELAY_LENGTH*2)
  43. // The delay line for left and right channels
  44. short delayline[CHORUS_DELAYLINE];
  45. // If this pin is grounded the chorus 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. AudioEffectChorus 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 chorus effect
  57. AudioConnection c1(audioInput, 0, myEffect, 0);
  58. AudioConnection c2(audioInput, 1, myEffect, 1);
  59. // both channels from the chorus effect go to the audio output
  60. AudioConnection c3(myEffect, 0, audioOutput, 0);
  61. AudioConnection c4(myEffect, 1, audioOutput, 1);
  62. AudioControlSGTL5000 audioShield;
  63. // number of "voices" in the chorus which INCLUDES the original voice
  64. int n_chorus = 2;
  65. // <<<<<<<<<<<<<<>>>>>>>>>>>>>>>>
  66. void setup() {
  67. Serial.begin(9600);
  68. while (!Serial) ;
  69. delay(3000);
  70. pinMode(PASSTHRU_PIN,INPUT_PULLUP);
  71. // Maximum memory usage was reported as 4
  72. // Proc = 9 (9), Mem = 4 (4)
  73. AudioMemory(4);
  74. audioShield.enable();
  75. audioShield.inputSelect(myInput);
  76. audioShield.volume(50);
  77. // Warn that the passthru pin is grounded
  78. if(!digitalRead(PASSTHRU_PIN)) {
  79. Serial.print("PASSTHRU_PIN (");
  80. Serial.print(PASSTHRU_PIN);
  81. Serial.println(") is grounded");
  82. }
  83. // Initialize the effect
  84. // - address of delayline
  85. // - total number of samples (left AND right) in the delay line
  86. // - number of voices in the chorus INCLUDING the original voice
  87. if(!myEffect.begin(delayline,CHORUS_DELAYLINE,n_chorus)) {
  88. Serial.println("AudioEffectChorus - begin failed");
  89. while(1);
  90. }
  91. // I want output on the line out too
  92. audioShield.unmuteLineout();
  93. // audioShield.muteHeadphone();
  94. Serial.println("setup done");
  95. AudioProcessorUsageMaxReset();
  96. AudioMemoryUsageMaxReset();
  97. }
  98. // audio volume
  99. int volume = 0;
  100. unsigned long last_time = millis();
  101. void loop()
  102. {
  103. // Volume control
  104. int n = analogRead(15);
  105. if (n != volume) {
  106. volume = n;
  107. audioShield.volume((float)n / 10.23);
  108. }
  109. if(0) {
  110. if(millis() - last_time >= 5000) {
  111. Serial.print("Proc = ");
  112. Serial.print(AudioProcessorUsage());
  113. Serial.print(" (");
  114. Serial.print(AudioProcessorUsageMax());
  115. Serial.print("), Mem = ");
  116. Serial.print(AudioMemoryUsage());
  117. Serial.print(" (");
  118. Serial.print(AudioMemoryUsageMax());
  119. Serial.println(")");
  120. last_time = millis();
  121. }
  122. }
  123. // update the button
  124. b_passthru.update();
  125. // If the passthru button is pushed, switch the effect to passthru
  126. if(b_passthru.fallingEdge()) {
  127. myEffect.modify(0);
  128. }
  129. // If passthru button is released, restore the previous chorus
  130. if(b_passthru.risingEdge()) {
  131. myEffect.modify(n_chorus);
  132. }
  133. }