Stanley Seow
Published © Apache-2.0

FFT Audio Visualizer with Teensy 3.2 + Audio Adapter

A 32-channel RGB audio spectrum analyzer / visualizer driven by teensy 3.2 with FFT using WS2812B 32x8 RGB LED

IntermediateFull instructions provided1,008
FFT Audio Visualizer with Teensy 3.2 + Audio Adapter

Things used in this project

Hardware components

Teensy USB 3.2 Development Board
Teensy USB 3.2 Development Board
×1
Teensy Audio Board
Teensy Audio Board
×1
WS2812B Digital RGB LED Flexi-Strip 144 LED - 1 Meter
Seeed Studio WS2812B Digital RGB LED Flexi-Strip 144 LED - 1 Meter
×1

Software apps and online services

Arduino IDE
Arduino IDE
Teensy Audio System Design Tool

Story

Read more

Code

Teensy FFT + WS2812B 32 channels

Arduino
#define MATRIX
//#undef MATRIX

#ifdef MATRIX
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#define PIN 3

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(32, 8, PIN,
  NEO_MATRIX_BOTTOM     + NEO_MATRIX_LEFT +
  NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
  NEO_GRB            + NEO_KHZ800);
 
#endif

#include <Audio.h>


///////////////////////////////////
// copy the Design Tool code here
///////////////////////////////////

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s2;           //xy=210,60
AudioPlayMemory          playMem1;       //xy=261,210
AudioSynthWaveform       waveform1;      //xy=284,269
AudioMixer4              mixer1;         //xy=450,196
AudioOutputI2S           i2s1;           //xy=669,145
AudioAnalyzeFFT1024      fft1024_1;      //xy=677,201
AudioConnection          patchCord1(i2s2, 0, mixer1, 0);
AudioConnection          patchCord2(i2s2, 1, mixer1, 1);
AudioConnection          patchCord3(playMem1, 0, mixer1, 2);
AudioConnection          patchCord4(waveform1, 0, mixer1, 3);
AudioConnection          patchCord5(mixer1, 0, i2s1, 0);
AudioConnection          patchCord6(mixer1, 0, i2s1, 1);
AudioConnection          patchCord7(mixer1, fft1024_1);
AudioControlSGTL5000     sgtl5000_1;     //xy=526,301
// GUItool: end automatically generated code


// Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_SCK_PIN   14

// Use these with the Teensy 3.5 & 3.6 SD card
//#define SDCARD_CS_PIN    BUILTIN_SDCARD
//#define SDCARD_MOSI_PIN  11  // not actually used
//#define SDCARD_SCK_PIN   13  // not actually used

// Use these for the SD+Wiz820 or other adaptors
//#define SDCARD_CS_PIN    4
//#define SDCARD_MOSI_PIN  11
//#define SDCARD_SCK_PIN   13
const int myInput = AUDIO_INPUT_LINEIN;


void setup() {

  Serial.begin(57600);

#ifdef MATRIX
  matrix.begin();
  matrix.setBrightness(40);
  //matrix.setTextWrap(false);
  //matrix.setTextColor(colors[0]);
  matrix.fillScreen(0);
  matrix.show();
#endif 
  
  AudioMemory(12);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  
//  SPI.setMOSI(SDCARD_MOSI_PIN);
//  SPI.setSCK(SDCARD_SCK_PIN);
//  if (!(SD.begin(SDCARD_CS_PIN))) {
//    while (1) {
//      Serial.println("Unable to access the SD card");
//      delay(500);
//    }
//  }

// Enable audio in
  sgtl5000_1.enable();
  sgtl5000_1.inputSelect(myInput);
  sgtl5000_1.volume(1);
  
  mixer1.gain(0, 1.0);
  mixer1.gain(1, 1.0);
  mixer1.gain(2, 0.0);
  mixer1.gain(3, 0.0);
  // Uncomment one these to try other window functions
  // fft1024_1.windowFunction(NULL);
  // fft1024_1.windowFunction(AudioWindowBartlett1024);
  // fft1024_1.windowFunction(AudioWindowFlattop1024);

  //playSdWav1.play("SDTEST2.WAV");

  delay(1000);
}

void loop() {
  
  if (fft1024_1.available()) {

    // Refer to FFT bin calculator at https://github.com/s-marley/ESP32_FFT_VU
    
    spectrum(0,fft1024_1.read(0,2));        
    spectrum(1,fft1024_1.read(2,2));        
    spectrum(2,fft1024_1.read(3,3));      
    spectrum(3,fft1024_1.read(3,4));      
    spectrum(4,fft1024_1.read(4,4));    
    spectrum(5,fft1024_1.read(4,5));    
    spectrum(6,fft1024_1.read(5,6));    
    spectrum(7,fft1024_1.read(6,7));   
    spectrum(8,fft1024_1.read(7,9));  
    spectrum(9,fft1024_1.read(9,10));  
    spectrum(10,fft1024_1.read(10,12));  
    spectrum(11,fft1024_1.read(12,15));  
    spectrum(12,fft1024_1.read(15,17)); 
    spectrum(13,fft1024_1.read(17,21)); 
    spectrum(14,fft1024_1.read(21,25)); 
    spectrum(15,fft1024_1.read(25,29)); 
    spectrum(16,fft1024_1.read(29,34)); 
    spectrum(17,fft1024_1.read(34,41)); 
    spectrum(18,fft1024_1.read(41,49)); 
    spectrum(19,fft1024_1.read(49,58)); 
    spectrum(20,fft1024_1.read(58,68));
    spectrum(21,fft1024_1.read(68,81));
    spectrum(22,fft1024_1.read(81,96));
    spectrum(23,fft1024_1.read(96,114)); 
    spectrum(24,fft1024_1.read(114,135));
    spectrum(25,fft1024_1.read(135,161));
    spectrum(26,fft1024_1.read(161,191)); 
    spectrum(27,fft1024_1.read(191,226));
    spectrum(28,fft1024_1.read(226,268)); 
    spectrum(29,fft1024_1.read(268,318));
    spectrum(30,fft1024_1.read(318,377));
    spectrum(31,fft1024_1.read(377,511));

#ifdef MATRIX
   matrix.show();
#endif    
  }

#ifdef MATRIX
    matrix.fillScreen(0);
    delay(10);
#endif    
}


void spectrum(int x, float n) {
  
  uint16_t colors[] = {
  matrix.Color(0 ,0, 0), 
  matrix.Color( 0, 128, 0), 
  matrix.Color( 0, 192, 0), 
  matrix.Color( 0, 255, 128), 
  matrix.Color( 0, 255, 255), 
  matrix.Color( 0, 0, 255), 
  matrix.Color( 128, 0, 255), 
  matrix.Color( 255, 0, 255), 
  matrix.Color( 255, 0, 0), 
  

  };

// x = band
// y = led height

    int y = LEDnumber(n);

#ifdef MATRIX

    switch ( y ) {

      case -1 :
        // All off
        matrix.drawPixel(x,y, colors[0] );
        break;

      case 1 :
        matrix.drawPixel(x,0, colors[1] );
        break;
      case 2 :
        matrix.drawPixel(x,0, colors[1] );
        matrix.drawPixel(x,1, colors[2] );
        break;  
      case 3 :
        matrix.drawPixel(x,0, colors[1] );
        matrix.drawPixel(x,1, colors[2] );
        matrix.drawPixel(x,2, colors[3] );
        break;  
      case 4 :
        matrix.drawPixel(x,0, colors[1] );
        matrix.drawPixel(x,1, colors[2] );
        matrix.drawPixel(x,2, colors[3] );
        matrix.drawPixel(x,3, colors[4] );
        break; 
      case 5 :
        matrix.drawPixel(x,0, colors[1] );
        matrix.drawPixel(x,1, colors[2] );
        matrix.drawPixel(x,2, colors[3] );
        matrix.drawPixel(x,3, colors[4] );
        matrix.drawPixel(x,4, colors[5] );
        break; 
      case 6 :
        matrix.drawPixel(x,0, colors[1] );
        matrix.drawPixel(x,1, colors[2] );
        matrix.drawPixel(x,2, colors[3] );
        matrix.drawPixel(x,3, colors[4] );
        matrix.drawPixel(x,4, colors[5] );
        matrix.drawPixel(x,5, colors[6] );
        break; 
       case 7 :
        matrix.drawPixel(x,0, colors[1] );
        matrix.drawPixel(x,1, colors[2] );
        matrix.drawPixel(x,2, colors[3] );
        matrix.drawPixel(x,3, colors[4] );
        matrix.drawPixel(x,4, colors[5] );
        matrix.drawPixel(x,5, colors[6] );
        matrix.drawPixel(x,6, colors[7] );
        break; 
      case 8 :
        matrix.drawPixel(x,0, colors[1] );
        matrix.drawPixel(x,1, colors[2] );
        matrix.drawPixel(x,2, colors[3] );
        matrix.drawPixel(x,3, colors[4] );
        matrix.drawPixel(x,4, colors[5] );
        matrix.drawPixel(x,5, colors[6] );
        matrix.drawPixel(x,6, colors[7] );    
        matrix.drawPixel(x,7, colors[8] );     
        break;                                                
      default:
        break;  
    }
    
#endif

}

int LEDnumber(float n) {
  int led = -1;  
  if (n > 0.250) {
    led = 8;
  } else if ( n > 0.220) {
    led = 7;
  } else if ( n > 0.180) {
    led = 6;
  } else if ( n > 0.100) {
    led = 5;  
  } else if ( n > 0.060) {
    led = 4;
  } else if ( n > 0.030) {
    led = 3;
  } else if ( n > 0.015) {
    led = 2;
  } else if ( n > 0.008) {
    led = 1;
  } else if ( n < 0.009) {
    led = -1;
  }

  return led;    
} 

Credits

Stanley Seow

Stanley Seow

2 projects • 0 followers
An curious maker & coder from Malaysia, doing fun projects related to Arduino, IoT, blockchain, PCB, LED, JS & Art Installations

Comments