// (Heavily) adapted from https://github.com/G6EJD/ESP32-8266-Audio-Spectrum-Display/blob/master/ESP32_Spectrum_Display_02.ino
// Adjusted to allow brightness changes on press+hold, Auto-cycle for 3 button presses within 2 seconds
// Edited to add Neomatrix support for easier compatibility with different layouts.
#include <FastLED_NeoMatrix.h>
#include <arduinoFFT.h>
#include <EasyButton.h>
#define SAMPLES 1024 // Must be a power of 2
#define SAMPLING_FREQ 40000 // Hz, must be 40000 or less due to ADC conversion time. Determines maximum frequency that can be analysed by the FFT Fmax=sampleF/2.
#define AMPLITUDE 300 // Depending on your audio source level, you may need to alter this value. Can be used as a 'sensitivity' control.
#define AUDIO_IN_PIN 35 // Signal in on this pin
#define LED_PIN 5 // LED strip data
#define BTN_PIN 4 // Connect a push button to this pin to change patterns
#define LONG_PRESS_MS 200 // Number of ms to count as a long press
#define COLOR_ORDER GRB // If colours look wrong, play with this
#define CHIPSET WS2812B // LED strip type
#define MAX_MILLIAMPS 2000 // Careful with the amount of power here if running off USB port
const int BRIGHTNESS_SETTINGS[3] = {5, 70, 200}; // 3 Integer array for 3 brightness settings (based on pressing+holding BTN_PIN)
#define LED_VOLTS 5 // Usually 5 or 12
#define NUM_BANDS 16 // To change this, you will need to change the bunch of if statements describing the mapping from bins to bands
#define NOISE 500 // Used as a crude noise filter, values below this are ignored
for (int i = 2; i < (SAMPLES/2); i++){ // Don't use sample 0 and only first SAMPLES/2 are usable. Each array element represents a frequency bin and its value the amplitude.
if (vReal[i] > NOISE) { // Add a crude noise filter
/*8 bands, 12kHz top band
if (i<=3 ) bandValues[0] += (int)vReal[i];
if (i>3 && i<=6 ) bandValues[1] += (int)vReal[i];
if (i>6 && i<=13 ) bandValues[2] += (int)vReal[i];
if (i>13 && i<=27 ) bandValues[3] += (int)vReal[i];
if (i>27 && i<=55 ) bandValues[4] += (int)vReal[i];
if (i>55 && i<=112) bandValues[5] += (int)vReal[i];
if (i>112 && i<=229) bandValues[6] += (int)vReal[i];
if (i>229 ) bandValues[7] += (int)vReal[i];*/
//16 bands, 12kHz top band
if (i<=2 ) bandValues[0] += (int)vReal[i];
if (i>2 && i<=3 ) bandValues[1] += (int)vReal[i];
if (i>3 && i<=5 ) bandValues[2] += (int)vReal[i];
if (i>5 && i<=7 ) bandValues[3] += (int)vReal[i];
if (i>7 && i<=9 ) bandValues[4] += (int)vReal[i];
if (i>9 && i<=13 ) bandValues[5] += (int)vReal[i];
if (i>13 && i<=18 ) bandValues[6] += (int)vReal[i];
if (i>18 && i<=25 ) bandValues[7] += (int)vReal[i];
if (i>25 && i<=36 ) bandValues[8] += (int)vReal[i];
if (i>36 && i<=50 ) bandValues[9] += (int)vReal[i];
if (i>50 && i<=69 ) bandValues[10] += (int)vReal[i];
if (i>69 && i<=97 ) bandValues[11] += (int)vReal[i];
if (i>97 && i<=135) bandValues[12] += (int)vReal[i];
if (i>135 && i<=189) bandValues[13] += (int)vReal[i];
if (i>189 && i<=264) bandValues[14] += (int)vReal[i];