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.

155 lines
4.2KB

  1. // MemoryAndCpuUsage
  2. //
  3. // This example demonstrates how to monitor CPU and memory
  4. // usage by the audio library. You can see the total memory
  5. // used at any moment, and the maximum (worst case) used.
  6. //
  7. // The total CPU usage, and CPU usage for each object can
  8. // be monitored. Reset functions clear the maximums.
  9. //
  10. // Use the Arduino Serial Monitor to view the usage info
  11. // and control ('F', 'S', and 'R' keys) this program.
  12. //
  13. // This example code is in the public domain.
  14. #include <Audio.h>
  15. #include <Wire.h>
  16. #include <SPI.h>
  17. #include <SD.h>
  18. #include <SerialFlash.h>
  19. // GUItool: begin automatically generated code
  20. AudioSynthWaveformSine sine1; //xy=125,221
  21. AudioSynthNoisePink pink1; //xy=133,121
  22. AudioEffectEnvelope envelope1; //xy=298,133
  23. AudioEffectEnvelope envelope2; //xy=302,197
  24. AudioAnalyzeFFT256 fft256_1; //xy=304,272
  25. AudioMixer4 mixer1; //xy=486,163
  26. AudioOutputI2S i2s1; //xy=640,161
  27. AudioConnection patchCord1(sine1, envelope2);
  28. AudioConnection patchCord2(sine1, fft256_1);
  29. AudioConnection patchCord3(pink1, envelope1);
  30. AudioConnection patchCord4(envelope1, 0, mixer1, 0);
  31. AudioConnection patchCord5(envelope2, 0, mixer1, 1);
  32. AudioConnection patchCord6(mixer1, 0, i2s1, 0);
  33. AudioConnection patchCord7(mixer1, 0, i2s1, 1);
  34. AudioControlSGTL5000 sgtl5000_1; //xy=517,297
  35. // GUItool: end automatically generated code
  36. void setup() {
  37. // give the audio library some memory. We'll be able
  38. // to see how much it actually uses, which can be used
  39. // to reduce this to the minimum necessary.
  40. AudioMemory(20);
  41. // enable the audio shield
  42. sgtl5000_1.enable();
  43. sgtl5000_1.volume(0.6);
  44. // create a simple percussive sound using pink noise
  45. // and an envelope to shape it.
  46. pink1.amplitude(0.5);
  47. envelope1.attack(1.5);
  48. envelope1.hold(5);
  49. envelope1.decay(20);
  50. envelope1.sustain(0);
  51. // create a simple bass note using a sine wave
  52. sine1.frequency(120);
  53. sine1.amplitude(0.6);
  54. envelope2.attack(6.5);
  55. envelope2.hold(25);
  56. envelope2.decay(70);
  57. envelope2.sustain(0);
  58. // add both the note together, so we can hear them
  59. mixer1.gain(0, 0.5);
  60. mixer1.gain(1, 0.5);
  61. }
  62. int count = 0;
  63. int speed = 60;
  64. void loop() {
  65. // a simple sequencer, count goes from 0 to 15
  66. count = count + 1;
  67. if (count >= 16) count = 0;
  68. // play percussive sounds every 4th time
  69. if (count == 0) envelope1.noteOn();
  70. if (count == 4) envelope1.noteOn();
  71. if (count == 8) envelope1.noteOn();
  72. if (count == 12) envelope1.noteOn();
  73. // play the bass tone every 8th time
  74. if (count == 4) {
  75. sine1.amplitude(0.6);
  76. sine1.frequency(100);
  77. envelope2.noteOn();
  78. }
  79. if (count == 12) {
  80. sine1.amplitude(0.3);
  81. sine1.frequency(120);
  82. envelope2.noteOn();
  83. }
  84. // turn off the sine wave, which saves
  85. // CPU power (especially since the sine goes
  86. // to a CPU-hungry FFT analysis)
  87. if (count == 6) {
  88. sine1.amplitude(0);
  89. }
  90. // check for incoming characters from the serial monitor
  91. if (Serial.available()) {
  92. char c = Serial.read();
  93. if ((c == 'r' || c == 'R')) {
  94. pink1.processorUsageMaxReset();
  95. fft256_1.processorUsageMaxReset();
  96. AudioProcessorUsageMaxReset();
  97. AudioMemoryUsageMaxReset();
  98. Serial.println("Reset all max numbers");
  99. }
  100. if ((c == 'f' || c == 'F') && speed > 16) {
  101. speed = speed - 2;
  102. }
  103. if ((c == 's' || c == 'S') && speed < 250) {
  104. speed = speed + 2;
  105. }
  106. }
  107. // print a summary of the current & maximum usage
  108. Serial.print("CPU: ");
  109. Serial.print("pink=");
  110. Serial.print(pink1.processorUsage());
  111. Serial.print(",");
  112. Serial.print(pink1.processorUsageMax());
  113. Serial.print(" ");
  114. Serial.print("fft=");
  115. Serial.print(fft256_1.processorUsage());
  116. Serial.print(",");
  117. Serial.print(fft256_1.processorUsageMax());
  118. Serial.print(" ");
  119. Serial.print("all=");
  120. Serial.print(AudioProcessorUsage());
  121. Serial.print(",");
  122. Serial.print(AudioProcessorUsageMax());
  123. Serial.print(" ");
  124. Serial.print("Memory: ");
  125. Serial.print(AudioMemoryUsage());
  126. Serial.print(",");
  127. Serial.print(AudioMemoryUsageMax());
  128. Serial.print(" ");
  129. Serial.print("Send: (R)eset, (S)lower, (F)aster");
  130. Serial.println();
  131. // very simple timing :-)
  132. delay(speed);
  133. }