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.

143 satır
4.2KB

  1. #include <LiquidCrystal.h>
  2. #include <Audio.h>
  3. #include <Wire.h>
  4. #include <SPI.h>
  5. #include <SD.h>
  6. // GUItool: begin automatically generated code
  7. AudioInputI2S i2s1; //xy=139,91
  8. AudioMixer4 mixer1; //xy=312,134
  9. AudioOutputI2S i2s2; //xy=392,32
  10. AudioAnalyzeFFT1024 fft1024; //xy=467,147
  11. AudioConnection patchCord1(i2s1, 0, mixer1, 0);
  12. AudioConnection patchCord2(i2s1, 0, i2s2, 0);
  13. AudioConnection patchCord3(i2s1, 1, mixer1, 1);
  14. AudioConnection patchCord4(i2s1, 1, i2s2, 1);
  15. AudioConnection patchCord5(mixer1, fft1024);
  16. AudioControlSGTL5000 audioShield; //xy=366,225
  17. // GUItool: end automatically generated code
  18. const int myInput = AUDIO_INPUT_LINEIN;
  19. //const int myInput = AUDIO_INPUT_MIC;
  20. // The scale sets how much sound is needed in each frequency range to
  21. // show all 8 bars. Higher numbers are more sensitive.
  22. float scale = 60.0;
  23. // An array to hold the 16 frequency bands
  24. float level[16];
  25. // This array holds the on-screen levels. When the signal drops quickly,
  26. // these are used to lower the on-screen level 1 bar per update, which
  27. // looks more pleasing to corresponds to human sound perception.
  28. int shown[16];
  29. // Use the LiquidCrystal library to display the spectrum
  30. //
  31. LiquidCrystal lcd(0, 1, 2, 3, 4, 5);
  32. byte bar1[8] = {0,0,0,0,0,0,0,255};
  33. byte bar2[8] = {0,0,0,0,0,0,255,255}; // 8 bar graph
  34. byte bar3[8] = {0,0,0,0,0,255,255,255}; // custom
  35. byte bar4[8] = {0,0,0,0,255,255,255,255}; // characters
  36. byte bar5[8] = {0,0,0,255,255,255,255,255};
  37. byte bar6[8] = {0,0,255,255,255,255,255,255};
  38. byte bar7[8] = {0,255,255,255,255,255,255,255};
  39. byte bar8[8] = {255,255,255,255,255,255,255,255};
  40. void setup() {
  41. // Audio requires memory to work.
  42. AudioMemory(12);
  43. // Enable the audio shield and set the output volume.
  44. audioShield.enable();
  45. audioShield.inputSelect(myInput);
  46. audioShield.volume(0.5);
  47. // turn on the LCD and define the custom characters
  48. lcd.begin(16, 2);
  49. lcd.print("Audio Spectrum");
  50. lcd.createChar(0, bar1);
  51. lcd.createChar(1, bar2);
  52. lcd.createChar(2, bar3);
  53. lcd.createChar(3, bar4);
  54. lcd.createChar(4, bar5);
  55. lcd.createChar(5, bar6);
  56. lcd.createChar(6, bar7);
  57. lcd.createChar(7, bar8);
  58. // configure the mixer to equally add left & right
  59. mixer1.gain(0, 0.5);
  60. mixer1.gain(1, 0.5);
  61. // pin 21 will select rapid vs animated display
  62. pinMode(21, INPUT_PULLUP);
  63. }
  64. void loop() {
  65. if (fft1024.available()) {
  66. // read the 512 FFT frequencies into 16 levels
  67. // music is heard in octaves, but the FFT data
  68. // is linear, so for the higher octaves, read
  69. // many FFT bins together.
  70. level[0] = fft1024.read(0);
  71. level[1] = fft1024.read(1);
  72. level[2] = fft1024.read(2, 3);
  73. level[3] = fft1024.read(4, 6);
  74. level[4] = fft1024.read(7, 10);
  75. level[5] = fft1024.read(11, 15);
  76. level[6] = fft1024.read(16, 22);
  77. level[7] = fft1024.read(23, 32);
  78. level[8] = fft1024.read(33, 46);
  79. level[9] = fft1024.read(47, 66);
  80. level[10] = fft1024.read(67, 93);
  81. level[11] = fft1024.read(94, 131);
  82. level[12] = fft1024.read(132, 184);
  83. level[13] = fft1024.read(185, 257);
  84. level[14] = fft1024.read(258, 359);
  85. level[15] = fft1024.read(360, 511);
  86. // if you have the volume pot soldered to your audio shield
  87. // uncomment this line to make it adjust the full scale signal
  88. //scale = 8.0 + analogRead(A1) / 5.0;
  89. // begin drawing at the first character on the 2nd row
  90. lcd.setCursor(0, 1);
  91. for (int i=0; i<16; i++) {
  92. Serial.print(level[i]);
  93. // TODO: conversion from FFT data to display bars should be
  94. // exponentially scaled. But how keep it a simple example?
  95. int val = level[i] * scale;
  96. if (val > 8) val = 8;
  97. if (val >= shown[i]) {
  98. shown[i] = val;
  99. } else {
  100. if (shown[i] > 0) shown[i] = shown[i] - 1;
  101. val = shown[i];
  102. }
  103. //Serial.print(shown[i]);
  104. Serial.print(" ");
  105. // print each custom digit
  106. if (shown[i] == 0) {
  107. lcd.write(' ');
  108. } else {
  109. lcd.write(shown[i] - 1);
  110. }
  111. }
  112. Serial.print(" cpu:");
  113. Serial.println(AudioProcessorUsageMax());
  114. }
  115. }