PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

220 lines
7.0KB

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