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.

203 lines
6.5KB

  1. #include <Audio.h>
  2. #include <Wire.h>
  3. #include <SPI.h>
  4. #include <SD.h>
  5. #include <SerialFlash.h>
  6. #include <Bounce.h>
  7. // Bitcrusher example by Jonathan Payne (Pensive) jon@jonnypayne.com
  8. // No rights reserved. Do what you like with it.
  9. // Create the Audio components.
  10. AudioPlaySdWav playWav1; //this will play a looping demo file
  11. AudioOutputI2S headphones; // and it will come out of the headphone socket.
  12. //Audio Effect BitCrusher defs here
  13. AudioEffectBitcrusher left_BitCrusher;
  14. AudioEffectBitcrusher right_BitCrusher;
  15. // Create Audio connections between the components
  16. AudioConnection patchCord1(playWav1, 0, left_BitCrusher, 0);
  17. AudioConnection patchCord2(playWav1, 1, right_BitCrusher, 0);
  18. AudioConnection patchCord3(left_BitCrusher, 0, headphones, 0);
  19. AudioConnection patchCord4(right_BitCrusher, 0, headphones, 1);
  20. // Create an object to control the audio shield.
  21. //
  22. AudioControlSGTL5000 audioShield;
  23. // Bounce objects to read six pushbuttons (pins 0-5)
  24. //
  25. Bounce button0 = Bounce(0, 5); // cycles the bitcrusher through all bitdepths
  26. Bounce button1 = Bounce(1, 5); //cycles the bitcrusher through some key samplerates
  27. Bounce button2 = Bounce(2, 5); // turns on the compressor (or "Automatic Volume Leveling")
  28. unsigned long SerialMillisecondCounter;
  29. //BitCrusher
  30. int current_CrushBits = 16; //this defaults to passthrough.
  31. int current_SampleRate = 44100; // this defaults to passthrough.
  32. //Compressor
  33. boolean compressorOn = false; // default this to off.
  34. void setup() {
  35. // Configure the pushbutton pins for pullups.
  36. // Each button should connect from the pin to GND.
  37. pinMode(0, INPUT_PULLUP);
  38. pinMode(1, INPUT_PULLUP);
  39. pinMode(2, INPUT_PULLUP);
  40. Serial.begin(38400); // open the serial
  41. // Audio connections require memory to work. For more
  42. // detailed information, see the MemoryAndCpuUsage example
  43. AudioMemory(40); //this is WAY more tha nwe need
  44. // turn on the output
  45. audioShield.enable();
  46. audioShield.volume(0.7);
  47. // by default the Teensy 3.1 DAC uses 3.3Vp-p output
  48. // if your 3.3V power has noise, switching to the
  49. // internal 1.2V reference can give you a clean signal
  50. //dac.analogReference(INTERNAL);
  51. // reduce the gain on mixer channels, so more than 1
  52. // sound can play simultaneously without clipping
  53. //SDCard Initialise
  54. SPI.setMOSI(7);
  55. SPI.setSCK(14);
  56. if (!(SD.begin(10))) {
  57. // stop here, but print a message repetitively
  58. while (1) {
  59. Serial.println("Unable to access the SD card");
  60. delay(500);
  61. }
  62. }
  63. // Bitcrusher
  64. left_BitCrusher.bits(current_CrushBits); //set the crusher to defaults. This will passthrough clean at 16,44100
  65. left_BitCrusher.sampleRate(current_SampleRate); //set the crusher to defaults. This will passthrough clean at 16,44100
  66. right_BitCrusher.bits(current_CrushBits); //set the crusher to defaults. This will passthrough clean at 16,44100
  67. right_BitCrusher.sampleRate(current_SampleRate); //set the crusher to defaults. This will passthrough clean at 16,44100
  68. //Bitcrusher
  69. /* Valid values for dap_avc parameters
  70. maxGain; Maximum gain that can be applied
  71. 0 - 0 dB
  72. 1 - 6.0 dB
  73. 2 - 12 dB
  74. lbiResponse; Integrator Response
  75. 0 - 0 mS
  76. 1 - 25 mS
  77. 2 - 50 mS
  78. 3 - 100 mS
  79. hardLimit
  80. 0 - Hard limit disabled. AVC Compressor/Expander enabled.
  81. 1 - Hard limit enabled. The signal is limited to the programmed threshold (signal saturates at the threshold)
  82. threshold
  83. floating point in range 0 to -96 dB
  84. attack
  85. floating point figure is dB/s rate at which gain is increased
  86. decay
  87. floating point figure is dB/s rate at which gain is reduced
  88. */
  89. // Initialise the AutoVolumeLeveller
  90. audioShield.autoVolumeControl(1, 1, 0, -6, 40, 20); // **BUG** with a max gain of 0, turning the AVC off leaves a hung AVC problem where the attack seems to hang in a loop. with it set 1 or 2, this does not occur.
  91. audioShield.autoVolumeDisable();
  92. audioShield.audioPostProcessorEnable();
  93. SerialMillisecondCounter = millis();
  94. }
  95. int val; //temporary variable for memory usage reporting.
  96. void loop() {
  97. if (millis() - SerialMillisecondCounter >= 5000) {
  98. Serial.print("Proc = ");
  99. Serial.print(AudioProcessorUsage());
  100. Serial.print(" (");
  101. Serial.print(AudioProcessorUsageMax());
  102. Serial.print("), Mem = ");
  103. Serial.print(AudioMemoryUsage());
  104. Serial.print(" (");
  105. Serial.print(AudioMemoryUsageMax());
  106. Serial.println(")");
  107. SerialMillisecondCounter = millis();
  108. AudioProcessorUsageMaxReset();
  109. AudioMemoryUsageMaxReset();
  110. }
  111. // Update all the button objects
  112. button0.update();
  113. button1.update();
  114. button2.update();
  115. // Start test sound if it is not playing. This will loop infinitely.
  116. if (! (playWav1.isPlaying())){
  117. playWav1.play("SDTEST1.WAV"); // http://www.pjrc.com/teensy/td_libs_AudioDataFiles.html
  118. }
  119. if (button0.fallingEdge()) {
  120. //Bitcrusher BitDepth
  121. if (current_CrushBits >= 2) { //eachtime you press it, deduct 1 bit from the settings.
  122. current_CrushBits--;
  123. } else {
  124. current_CrushBits = 16; // if you get down to 1 go back to the top.
  125. }
  126. left_BitCrusher.bits(current_CrushBits);
  127. left_BitCrusher.sampleRate(current_SampleRate);
  128. right_BitCrusher.bits(current_CrushBits);
  129. right_BitCrusher.sampleRate(current_SampleRate);
  130. Serial.print("Bitcrusher set to ");
  131. Serial.print(current_CrushBits);
  132. Serial.print(" Bit, Samplerate at ");
  133. Serial.print(current_SampleRate);
  134. Serial.println("Hz");
  135. }
  136. if (button1.fallingEdge()) {
  137. //Bitcrusher SampleRate // the lowest sensible setting is 345. There is a 128 sample buffer, and this will copy sample 1, to each of the other 127 samples.
  138. if (current_SampleRate >= 690) { // 345 * 2, so we can do one more divide
  139. current_SampleRate = current_SampleRate / 2; // half the sample rate each time
  140. } else {
  141. current_SampleRate=44100; // if you get down to the minimum then go back to the top and start over.
  142. }
  143. left_BitCrusher.bits(current_CrushBits);
  144. left_BitCrusher.sampleRate(current_SampleRate);
  145. right_BitCrusher.bits(current_CrushBits);
  146. right_BitCrusher.sampleRate(current_SampleRate);
  147. Serial.print("Bitcrusher set to ");
  148. Serial.print(current_CrushBits);
  149. Serial.print(" Bit, Samplerate at ");
  150. Serial.print(current_SampleRate);
  151. Serial.println("Hz");
  152. }
  153. if (button2.fallingEdge()) {
  154. if (compressorOn) {
  155. //turn off compressor
  156. audioShield.autoVolumeDisable();
  157. compressorOn = false;
  158. } else {
  159. //turn on compressor
  160. audioShield.autoVolumeEnable();
  161. compressorOn = true;
  162. }
  163. Serial.print("Compressor: ");
  164. Serial.println(compressorOn);
  165. }
  166. }