Granular.ino 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Granular Effect Example - Pitch shift or freeze sound
  2. //
  3. // This example is meant to be used with 3 buttons (pin 0,
  4. // 1, 2) and 2 knobs (pins 16/A2, 17/A3), which are present
  5. // on the audio tutorial kit.
  6. // https://www.pjrc.com/store/audio_tutorial_kit.html
  7. //
  8. // Data files to put on your SD card can be downloaded here:
  9. // http://www.pjrc.com/teensy/td_libs_AudioDataFiles.html
  10. //
  11. // This example code is in the public domain.
  12. #include <Audio.h>
  13. #include <Wire.h>
  14. #include <SPI.h>
  15. #include <SD.h>
  16. #include <SerialFlash.h>
  17. #include <Bounce.h>
  18. AudioPlaySdWav playSdWav1; //xy=163,135
  19. AudioMixer4 mixer1; //xy=332,167
  20. AudioEffectGranular granular1; //xy=504,155
  21. AudioOutputI2S i2s1; //xy=664,185
  22. AudioConnection patchCord1(playSdWav1, 0, mixer1, 0);
  23. AudioConnection patchCord2(playSdWav1, 1, mixer1, 1);
  24. AudioConnection patchCord3(mixer1, granular1);
  25. AudioConnection patchCord4(granular1, 0, i2s1, 0);
  26. AudioConnection patchCord5(granular1, 0, i2s1, 1);
  27. AudioControlSGTL5000 sgtl5000_1; //xy=236,248
  28. Bounce button0 = Bounce(0, 15);
  29. Bounce button1 = Bounce(1, 15);
  30. Bounce button2 = Bounce(2, 15);
  31. #define GRANULAR_MEMORY_SIZE 12800 // enough for 290 ms at 44.1 kHz
  32. int16_t granularMemory[GRANULAR_MEMORY_SIZE];
  33. // Use these with the Teensy Audio Shield
  34. #define SDCARD_CS_PIN 10
  35. #define SDCARD_MOSI_PIN 7
  36. #define SDCARD_SCK_PIN 14
  37. // Use these with the Teensy 3.5 & 3.6 SD card
  38. //#define SDCARD_CS_PIN BUILTIN_SDCARD
  39. //#define SDCARD_MOSI_PIN 11 // not actually used
  40. //#define SDCARD_SCK_PIN 13 // not actually used
  41. // Use these for the SD+Wiz820 or other adaptors
  42. //#define SDCARD_CS_PIN 4
  43. //#define SDCARD_MOSI_PIN 11
  44. //#define SDCARD_SCK_PIN 13
  45. #define NUM_FILES 4
  46. const char *filenames[NUM_FILES]={"SDTEST1.WAV", "SDTEST2.WAV", "SDTEST3.WAV", "SDTEST4.WAV"};
  47. int nextfile=0;
  48. void setup() {
  49. Serial.begin(9600);
  50. AudioMemory(10);
  51. pinMode(0, INPUT_PULLUP);
  52. pinMode(1, INPUT_PULLUP);
  53. pinMode(2, INPUT_PULLUP);
  54. sgtl5000_1.enable();
  55. sgtl5000_1.volume(0.5);
  56. mixer1.gain(0, 0.5);
  57. mixer1.gain(1, 0.5);
  58. // the Granular effect requires memory to operate
  59. granular1.begin(granularMemory, GRANULAR_MEMORY_SIZE);
  60. SPI.setMOSI(SDCARD_MOSI_PIN);
  61. SPI.setSCK(SDCARD_SCK_PIN);
  62. if (!(SD.begin(SDCARD_CS_PIN))) {
  63. // stop here, but print a message repetitively
  64. while (1) {
  65. Serial.println("Unable to access the SD card");
  66. delay(500);
  67. }
  68. }
  69. }
  70. void loop() {
  71. if (playSdWav1.isPlaying() == false) {
  72. // start the next song playing
  73. playSdWav1.play(filenames[nextfile]);
  74. Serial.print("Playing: ");
  75. Serial.println(filenames[nextfile]);
  76. delay(5); // brief delay for the library read WAV info
  77. nextfile = nextfile + 1;
  78. if (nextfile >= NUM_FILES) {
  79. nextfile = 0;
  80. }
  81. }
  82. // read pushbuttons
  83. button0.update();
  84. button1.update();
  85. button2.update();
  86. // read knobs, scale to 0-1.0 numbers
  87. float knobA2 = (float)analogRead(A2) / 1023.0;
  88. float knobA3 = (float)analogRead(A3) / 1023.0;
  89. // Button 0 starts Freeze effect
  90. if (button0.fallingEdge()) {
  91. float msec = 100.0 + (knobA3 * 190.0);
  92. granular1.beginFreeze(msec);
  93. Serial.print("Begin granular freeze using ");
  94. Serial.print(msec);
  95. Serial.println(" grains");
  96. }
  97. if (button0.risingEdge()) {
  98. granular1.stop();
  99. }
  100. // Button 1 starts Pitch Shift effect
  101. if (button1.fallingEdge()) {
  102. float msec = 25.0 + (knobA3 * 75.0);
  103. granular1.beginPitchShift(msec);
  104. Serial.print("Begin granular pitch phift using ");
  105. Serial.print(msec);
  106. Serial.println(" grains");
  107. }
  108. if (button1.risingEdge()) {
  109. granular1.stop();
  110. }
  111. // Continuously adjust the speed, based on the A3 pot
  112. float ratio;
  113. ratio = powf(2.0, knobA2 * 2.0 - 1.0); // 0.5 to 2.0
  114. //ratio = powf(2.0, knobA2 * 6.0 - 3.0); // 0.125 to 8.0 -- uncomment for far too much range!
  115. granular1.setSpeed(ratio);
  116. }