Advertisement
Guest User

audio fft neomatrix

a guest
Dec 6th, 2019
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.82 KB | None | 0 0
  1. #include <Adafruit_GFX.h>
  2. #include <Adafruit_NeoMatrix.h>
  3. #include <Adafruit_NeoPixel.h>
  4.  
  5. #define PIN 3
  6.  
  7. Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(32, 8, PIN,
  8.   NEO_MATRIX_BOTTOM     + NEO_MATRIX_LEFT +
  9.   NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
  10.   NEO_GRB            + NEO_KHZ800);
  11.  
  12. const uint16_t colors[] = {
  13.   matrix.Color(128, 0, 0),
  14.   matrix.Color(255, 0, 0),
  15.   matrix.Color(255, 128, 0),
  16.   matrix.Color(255, 255, 0),
  17.   matrix.Color(0, 255, 0),
  18.   matrix.Color(0, 255, 128),
  19.   matrix.Color(0, 128, 255),
  20.   matrix.Color(0, 0, 255),
  21.   };
  22.  
  23. #include <Audio.h>
  24. #include <Bounce.h>
  25. Bounce button0 = Bounce(0, 15);
  26. Bounce button1 = Bounce(1, 15);  // 15 = 15 ms debounce time
  27. Bounce button2 = Bounce(2, 15);
  28.  
  29.  
  30.  
  31. ///////////////////////////////////
  32. // copy the Design Tool code here
  33. ///////////////////////////////////
  34.  
  35. #include <Audio.h>
  36. #include <Wire.h>
  37. #include <SPI.h>
  38. #include <SD.h>
  39. #include <SerialFlash.h>
  40.  
  41. // GUItool: begin automatically generated code
  42. AudioInputI2S            i2s2;           //xy=210,60
  43. AudioPlayMemory          playMem1;       //xy=261,210
  44. AudioSynthWaveform       waveform1;      //xy=284,269
  45. AudioMixer4              mixer1;         //xy=450,196
  46. AudioOutputI2S           i2s1;           //xy=669,145
  47. AudioAnalyzeFFT1024      fft1024_1;      //xy=677,201
  48. AudioConnection          patchCord1(i2s2, 0, mixer1, 0);
  49. AudioConnection          patchCord2(i2s2, 1, mixer1, 1);
  50. AudioConnection          patchCord3(playMem1, 0, mixer1, 2);
  51. AudioConnection          patchCord4(waveform1, 0, mixer1, 3);
  52. AudioConnection          patchCord5(mixer1, 0, i2s1, 0);
  53. AudioConnection          patchCord6(mixer1, 0, i2s1, 1);
  54. AudioConnection          patchCord7(mixer1, fft1024_1);
  55. AudioControlSGTL5000     sgtl5000_1;     //xy=526,301
  56. // GUItool: end automatically generated code
  57.  
  58.  
  59. // Use these with the Teensy Audio Shield
  60. #define SDCARD_CS_PIN    10
  61. #define SDCARD_MOSI_PIN  7
  62. #define SDCARD_SCK_PIN   14
  63.  
  64. // Use these with the Teensy 3.5 & 3.6 SD card
  65. //#define SDCARD_CS_PIN    BUILTIN_SDCARD
  66. //#define SDCARD_MOSI_PIN  11  // not actually used
  67. //#define SDCARD_SCK_PIN   13  // not actually used
  68.  
  69. // Use these for the SD+Wiz820 or other adaptors
  70. //#define SDCARD_CS_PIN    4
  71. //#define SDCARD_MOSI_PIN  11
  72. //#define SDCARD_SCK_PIN   13
  73. const int myInput = AUDIO_INPUT_LINEIN;
  74.  
  75.  
  76. void setup() {
  77.  
  78.   Serial.begin(57600);
  79.  
  80.   matrix.begin();
  81.   matrix.setTextWrap(false);
  82.   matrix.setBrightness(40);
  83.   matrix.setTextColor(colors[0]);
  84.   matrix.fillScreen(0);
  85.   matrix.show();
  86.  
  87.   AudioMemory(12);
  88.   sgtl5000_1.enable();
  89.   sgtl5000_1.volume(0.5);
  90. //  SPI.setMOSI(SDCARD_MOSI_PIN);
  91. //  SPI.setSCK(SDCARD_SCK_PIN);
  92. //  if (!(SD.begin(SDCARD_CS_PIN))) {
  93. //    while (1) {
  94. //      Serial.println("Unable to access the SD card");
  95. //      delay(500);
  96. //    }
  97. //  }
  98.  
  99. // Enable audio in
  100.   sgtl5000_1.enable();
  101.   sgtl5000_1.inputSelect(myInput);
  102.   sgtl5000_1.volume(1);
  103.  
  104.   mixer1.gain(0, 1.0);
  105.   mixer1.gain(1, 1.0);
  106.   mixer1.gain(2, 0.0);
  107.   mixer1.gain(3, 0.0);
  108.   // Uncomment one these to try other window functions
  109.   // fft1024_1.windowFunction(NULL);
  110.   // fft1024_1.windowFunction(AudioWindowBartlett1024);
  111.   // fft1024_1.windowFunction(AudioWindowFlattop1024);
  112.   delay(2000);
  113.   //playSdWav1.play("SDTEST2.WAV");
  114.  
  115. }
  116.  
  117. void loop() {
  118.  
  119.   if (fft1024_1.available()) {
  120.  
  121.     spectrum(0,fft1024_1.read(0,1));        
  122.     spectrum(1,fft1024_1.read(1,15));        
  123.     spectrum(2,fft1024_1.read(16,31));      
  124.     spectrum(3,fft1024_1.read(32,47));      
  125.     spectrum(4,fft1024_1.read(48,63));    
  126.     spectrum(5,fft1024_1.read(64,79));    
  127.     spectrum(6,fft1024_1.read(80,95));    
  128.     spectrum(7,fft1024_1.read(96,111));  
  129.     spectrum(8,fft1024_1.read(112,127));  
  130.     spectrum(9,fft1024_1.read(128,143));  
  131.     spectrum(10,fft1024_1.read(144,159));  
  132.     spectrum(11,fft1024_1.read(160,175));  
  133.     spectrum(12,fft1024_1.read(176,191));
  134.     spectrum(13,fft1024_1.read(192,207));
  135.     spectrum(14,fft1024_1.read(208,223));
  136.     spectrum(15,fft1024_1.read(224,239));
  137.     spectrum(16,fft1024_1.read(240,255));
  138.     spectrum(17,fft1024_1.read(256,271));
  139.     spectrum(18,fft1024_1.read(272,287));
  140.     spectrum(19,fft1024_1.read(288,303));
  141.     spectrum(20,fft1024_1.read(304,319));
  142.     spectrum(21,fft1024_1.read(320,335));
  143.     spectrum(22,fft1024_1.read(336,351));
  144.     spectrum(23,fft1024_1.read(352,367));
  145.     spectrum(24,fft1024_1.read(368,383));
  146.     spectrum(25,fft1024_1.read(384,399));
  147.     spectrum(26,fft1024_1.read(400,415));
  148.     spectrum(27,fft1024_1.read(416,432));
  149.     spectrum(28,fft1024_1.read(433,447));
  150.     spectrum(29,fft1024_1.read(448,463));
  151.     spectrum(30,fft1024_1.read(464,479));
  152.     spectrum(31,fft1024_1.read(480,4511));
  153.    
  154.     matrix.show();
  155.   }
  156.  
  157.     matrix.fillScreen(0);
  158.     delay(10);
  159.     matrix.show();
  160.    
  161. }
  162.  
  163.  
  164. void spectrum(int x, float n) {
  165.  
  166. // x = band
  167. // y = led height
  168.  
  169.     int y = LEDnumber(n);
  170.  
  171.     matrix.drawPixel(x,y,colors[y]);
  172.  
  173. //    Serial.print(band);
  174. //    Serial.print(" ");
  175. //    Serial.print(LEDnumber(n));
  176. //    Serial.println();
  177. }
  178.  
  179. int LEDnumber(float n) {
  180.   int led = 0;  
  181.   if (n > 0.250) {
  182.     led = 8;
  183.   } else if ( n > 0.220) {
  184.     led = 7;
  185.   } else if ( n > 0.180) {
  186.     led = 6;
  187.   } else if ( n > 0.100) {
  188.     led = 5;  
  189.   } else if ( n > 0.060) {
  190.     led = 4;
  191.   } else if ( n > 0.030) {
  192.     led = 3;
  193.   } else if ( n > 0.010) {
  194.     led = 2;
  195.   } else if ( n > 0.008) {
  196.     led = 1;
  197.   } else if ( n < 0.009) {
  198.     led = 0;
  199.   }
  200.  
  201.   return led;    
  202. }
  203.  
  204. //void printNumber(float n) {
  205. //    
  206. //  if (n > 0.25) {
  207. //    Serial.print("##### ");
  208. //  } else if (n > 0.18) {
  209. //    Serial.print(".###. ");
  210. //  } else if (n > 0.06) {
  211. //    Serial.print("..#.. ");
  212. //  } else if (n > 0.005) {
  213. //    Serial.print("..*.. ");
  214. //  } else if ( n < 0.005) {
  215. //    Serial.print("..... ");
  216. //  }
  217. //  
  218. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement