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.

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