#include <Arduino.h>
#include <driver/i2s.h>
#include <arduinoFFT.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
// ============================================
// CONFIGURACIÓN DE PINES Y HARDWARE
// ============================================
// — Pantalla OLED —
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// — Micrófono INMP441 (I2S) —
#define I2S_WS 25 // Word Select (LRCLK)
#define I2S_SD 32 // Data Out (DOUT)
#define I2S_SCK 26 // Bit Clock (BCLK)
#define I2S_PORT I2S_NUM_0
// ============================================
// CONFIGURACIÓN DE MUESTREO Y FFT
// ============================================
#define SAMPLE_RATE 16000 // Frecuencia de muestreo (Hz)
#define SAMPLES 512 // Número de muestras (potencia de 2)
// Arrays para FFT
double vReal[SAMPLES];
double vImag[SAMPLES];
// Objeto FFT (versión 2.x de la librería)
ArduinoFFT<double> FFT = ArduinoFFT<double>(vReal, vImag, SAMPLES, SAMPLE_RATE);
// Buffer para muestras del micrófono
int32_t sampleBuffer[SAMPLES];
// ============================================
// CONFIGURACIÓN DE BANDAS DE FRECUENCIA
// ============================================
#define NUM_BANDS 12 // Reducido a 12 para barras más gruesas
// Bandas de frecuencia (logarítmicas de 20Hz a 8kHz)
const int bandFreqs[NUM_BANDS + 1] = {
20, 60, 120, 200, 350, 550,
850, 1300, 2000, 3000, 4500, 7000, 8000
};
// Arrays para visualización
float bandValues[NUM_BANDS] = {0};
float peakValues[NUM_BANDS] = {0};
float bandMaxValues[NUM_BANDS] = {0}; // Para normalización dinámica
// ============================================
// CONFIGURACIÓN I2S
// ============================================
void i2s_install() {
const i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = SAMPLE_RATE,
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = (i2s_comm_format_t)I2S_COMM_FORMAT_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 4,
.dma_buf_len = 1024,
.use_apll = false
};
i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
}
void i2s_setpins() {
const i2s_pin_config_t pin_config = {
.bck_io_num = I2S_SCK,
.ws_io_num = I2S_WS,
.data_out_num = -1,
.data_in_num = I2S_SD
};
i2s_set_pin(I2S_PORT, &pin_config);
}
// ============================================
// CONFIGURACIÓN OLED
// ============================================
void setupOLED() {
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F(«Error: No se encuentra pantalla OLED»));
for(;;);
}
// Configuración inicial
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.cp437(true); // Usar caracteres extendidos
// Mostrar mensaje de inicio
display.setCursor(0, 0);
display.println(«Espectro Audio»);
display.println(«INMP441 + FFT»);
display.println(«Iniciando…»);
display.display();
delay(2000);
}
// ============================================
// SETUP
// ============================================
void setup() {
Serial.begin(115200);
Serial.println(«Iniciando Analizador de Espectro…»);
// Inicializar I2S
i2s_install();
i2s_setpins();
i2s_start(I2S_PORT);
Serial.println(«✅ I2S inicializado»);
// Inicializar OLED
setupOLED();
Serial.println(«✅ OLED inicializado»);
// Dibujar marco inicial
display.clearDisplay();
display.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SSD1306_WHITE);
display.display();
delay(500);
}
// ============================================
// FUNCIÓN PARA NORMALIZAR BANDAS
// ============================================
void normalizeBands() {
// Encontrar el valor máximo actual
float maxVal = 0.1; // Valor pequeño para evitar división por cero
for (int b = 0; b < NUM_BANDS; b++) {
if (bandValues[b] > maxVal) {
maxVal = bandValues[b];
}
}
// Actualizar máximos históricos con caída gradual
for (int b = 0; b < NUM_BANDS; b++) {
if (bandValues[b] > bandMaxValues[b]) {
bandMaxValues[b] = bandValues[b];
} else {
bandMaxValues[b] *= 0.999; // Caída muy lenta
if (bandMaxValues[b] < 0.1) bandMaxValues[b] = 0.1;
}
}
// Normalizar usando el máximo actual (respuesta rápida)
float globalMax = maxVal;
if (globalMax < 0.1) globalMax = 0.1;
for (int b = 0; b < NUM_BANDS; b++) {
bandValues[b] = (bandValues[b] / globalMax) * 50; // Normalizar a 0-50
if (bandValues[b] > 50) bandValues[b] = 50;
}
}
// ============================================
// LOOP PRINCIPAL
// ============================================
void loop() {
size_t bytesRead;
// — 1. LEER MICRÓFONO —
esp_err_t result = i2s_read(I2S_PORT, &sampleBuffer, sizeof(sampleBuffer), &bytesRead, portMAX_DELAY);
if (result != ESP_OK) {
Serial.println(«Error leyendo I2S»);
delay(100);
return;
}
int samplesRead = bytesRead / sizeof(int32_t);
// — 2. PREPARAR DATOS PARA FFT —
// Eliminar DC offset y escalar
int32_t sum = 0;
for (int i = 0; i < samplesRead; i++) {
sum += sampleBuffer[i];
}
int32_t avg = sum / samplesRead;
for (int i = 0; i < samplesRead; i++) {
// Restar DC offset y escalar apropiadamente
vReal[i] = (double)((sampleBuffer[i] – avg) >> 12);
vImag[i] = 0.0;
}
// — 3. CALCULAR FFT —
FFT.windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.complexToMagnitude(vReal, vImag, SAMPLES);
// — 4. LIMPIAR BANDAS —
for (int i = 0; i < NUM_BANDS; i++) {
bandValues[i] = 0;
}
// — 5. AGRUPAR EN BANDAS —
int counts[NUM_BANDS] = {0}; // Para promediar
for (int i = 2; i < (SAMPLES/2); i++) {
float freq = (i * 1.0 * SAMPLE_RATE) / SAMPLES;
// Ignorar frecuencias muy bajas (ruido)
if (freq < 30) continue;
for (int b = 0; b < NUM_BANDS; b++) {
if (freq >= bandFreqs[b] && freq < bandFreqs[b+1]) {
// Usar magnitud al cuadrado para mejor respuesta
bandValues[b] += (vReal[i] * vReal[i]);
counts[b]++;
break;
}
}
}
// Promediar las bandas
for (int b = 0; b < NUM_BANDS; b++) {
if (counts[b] > 0) {
bandValues[b] = sqrt(bandValues[b] / counts[b]); // RMS
}
}
// — 6. APLICAR SUAVIZADO —
static float smoothBands[NUM_BANDS] = {0};
float smoothingFactor = 0.4; // 0.3-0.7, más alto = más suave
for (int b = 0; b < NUM_BANDS; b++) {
smoothBands[b] = smoothBands[b] * smoothingFactor +
bandValues[b] * (1 – smoothingFactor);
bandValues[b] = smoothBands[b];
}
// — 7. NORMALIZAR —
normalizeBands();
// — 8. ACTUALIZAR PICOS —
for (int b = 0; b < NUM_BANDS; b++) {
if (bandValues[b] > peakValues[b]) {
peakValues[b] = bandValues[b];
} else {
peakValues[b] -= 0.8; // Caída del pico
if (peakValues[b] < 0) peakValues[b] = 0;
}
}
// — 9. DIBUJAR EN OLED —
display.clearDisplay();
// Dibujar líneas guía horizontales (opcional)
for (int y = 0; y < SCREEN_HEIGHT; y += 16) {
for (int x = 0; x < SCREEN_WIDTH; x += 4) {
display.drawPixel(x, y, SSD1306_WHITE);
}
}
int barWidth = SCREEN_WIDTH / NUM_BANDS;
int maxBarHeight = SCREEN_HEIGHT – 8; // Dejar margen superior
for (int b = 0; b < NUM_BANDS; b++) {
int barHeight = (int)bandValues[b];
if (barHeight > maxBarHeight) barHeight = maxBarHeight;
int x = b * barWidth + 2; // +2 para margen izquierdo
int y = SCREEN_HEIGHT – barHeight – 4; // -4 para margen inferior
// Dibujar barra con degradado (más intensidad = más blanco)
for (int h = 0; h < barHeight; h++) {
int intensity = map(h, 0, barHeight, 0, 255);
if (intensity > 240) {
display.drawFastHLine(x, y + h, barWidth – 4, SSD1306_WHITE);
}
}
// Dibujar contorno de la barra
display.drawRect(x, y, barWidth – 4, barHeight, SSD1306_WHITE);
// Dibujar pico (punto blanco)
int peakY = SCREEN_HEIGHT – (int)peakValues[b] – 4;
if (peakY > y && peakY < SCREEN_HEIGHT – 4) {
display.fillRect(x + (barWidth/2) – 2, peakY – 1, 4, 3, SSD1306_WHITE);
}
}
// Dibujar texto informativo (opcional)
display.setCursor(0, 0);
display.print(«FFT»);
display.setCursor(SCREEN_WIDTH – 30, 0);
display.print(«dB»);
display.display();
// Pequeña pausa para estabilidad
delay(30);
}