-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFastLED.ino
168 lines (139 loc) · 4.87 KB
/
FastLED.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
FastLED.ino
By Shea Ivey
Reads I2S microphone data into samples[], processes them into frequency buckets and then outputs it to an LED strip for viewing.
Try wistling differrent tones to see which frequency buckets they fall into.
TIP: uncomment renderBeatRainbow() for a music beat visualization.
*/
#include <AudioInI2S.h>
#define SAMPLE_SIZE 1024 // Buffer size of read samples
#define SAMPLE_RATE 44100 // Audio Sample Rate
/* Required defines for audio analysis */
#define BAND_SIZE 8 // powers of 2 up to 64, defaults to 8
#include <AudioAnalysis.h>
AudioAnalysis audioInfo;
// ESP32 S2 Mini
// #define MIC_BCK_PIN 4 // Clock pin from the mic.
// #define MIC_WS_PIN 39 // WS pin from the mic.
// #define MIC_DATA_PIN 5 // SD pin data from the mic.
// #define MIC_CHANNEL_SELECT_PIN 40 // Left/Right pin to select the channel output from the mic.
// ESP32 TTGO T-Display
#define MIC_BCK_PIN 32 // Clock pin from the mic.
#define MIC_WS_PIN 25 // WS pin from the mic.
#define MIC_DATA_PIN 33 // SD pin data from the mic.
#define MIC_CHANNEL_SELECT_PIN 27 // Left/Right pin to select the channel output from the mic.
AudioInI2S mic(MIC_BCK_PIN, MIC_WS_PIN, MIC_DATA_PIN, MIC_CHANNEL_SELECT_PIN); // defaults to RIGHT channel.
int32_t samples[SAMPLE_SIZE]; // I2S sample data is stored here
#include "FastLED.h"
#define NUM_LEDS 144 // 1 meter strip
#define LED_PIN 13
#define MAX_BRIGHTNESS 80 // save your eyes
#define FRAME_RATE 30
CRGB leds[NUM_LEDS];
unsigned long nextFrame = 0;
unsigned long tick = 0;
void setup()
{
mic.begin(SAMPLE_SIZE, SAMPLE_RATE); // Starts the I2S DMA port.
// audio analysis setup
audioInfo.setNoiseFloor(10); // sets the noise floor
audioInfo.normalize(true, 0, 255); // normalize all values to range provided.
audioInfo.autoLevel(AudioAnalysis::ACCELERATE_FALLOFF, 1, 255, 1000); // set auto level falloff rate
audioInfo.bandPeakFalloff(AudioAnalysis::EXPONENTIAL_FALLOFF, 1); // set the band peak fall off rate
audioInfo.vuPeakFalloff(AudioAnalysis::ACCELERATE_FALLOFF, 1); // set the volume unit peak fall off rate
// FastLED setup
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(MAX_BRIGHTNESS);
FastLED.show();
}
void loop()
{
if (nextFrame > millis())
{
return;
}
// enforce a predictable frame rate
nextFrame = millis() + (1000 / FRAME_RATE);
tick++;
processSamples(); // does all the reading and frequency calculations
/* RENDER MODES */
renderBasicTest(); // bands and volume unit visualization
// renderBeatRainbow(); // uncommnet this line for a music beat visualization
}
void processSamples()
{
mic.read(samples); // Stores the current I2S port buffer into samples.
audioInfo.computeFFT(samples, SAMPLE_SIZE, SAMPLE_RATE);
audioInfo.computeFrequencies(BAND_SIZE);
}
void renderBasicTest()
{
float *bands = audioInfo.getBands();
float *peaks = audioInfo.getPeaks();
int vuMeter = audioInfo.getVolumeUnit();
int vuMeterPeak = audioInfo.getVolumeUnitPeak();
int vuMeterPeakMax = audioInfo.getVolumeUnitPeakMax();
leds[BAND_SIZE] = CRGB(0, 0, 0);
// equilizer first BAND_SIZE
for (int i = 0; i < BAND_SIZE; i++)
{
leds[i] = CHSV(i * (200 / BAND_SIZE), 255, (int)peaks[i]);
}
// volume unit meter rest of leds
uint8_t vuLed = (uint8_t)map(vuMeter, 0, vuMeterPeakMax, BAND_SIZE + 1, NUM_LEDS - 1);
uint8_t vuLedPeak = (uint8_t)map(vuMeterPeak, 0, vuMeterPeakMax, BAND_SIZE + 1, NUM_LEDS - 1);
for (int i = BAND_SIZE + 1; i < NUM_LEDS; i++)
{
leds[i] = CRGB(0, 0, 0);
if (i < vuLed)
{
leds[i] = i > (NUM_LEDS - ((NUM_LEDS - BAND_SIZE) * 0.2)) ? CRGB(50, 0, 0) : CRGB(0, 50, 0);
}
if (i == vuLedPeak)
{
leds[i] = CRGB(50, 50, 50);
}
}
FastLED.show();
}
void renderBeatRainbow()
{
float *bands = audioInfo.getBands();
float *peaks = audioInfo.getPeaks();
int peakBandIndex = audioInfo.getBandMaxIndex();
int peakBandValue = audioInfo.getBand(peakBandIndex);
static int beatCount = 0;
bool beatDetected = false;
bool clapsDetected = false;
// beat detection
if (peaks[0] == bands[0] && peaks[0] > 0) // new peak for bass must be a beat
{
beatCount++;
beatDetected = true;
}
if (peakBandIndex >= BAND_SIZE / 2 && peakBandValue > 0)
{
clapsDetected = true;
}
for (int i = 0; i < NUM_LEDS; i++)
{
leds[i] = blend(leds[i], CRGB(0, 0, 0), 100); // fade to black over time
// bass/beat = rainbow
if (beatDetected)
{
if (random(0, 10 - ((float)peaks[1] / (float)255 * 10.0)) == 0)
{
leds[i] = CHSV((beatCount * 10) % 255, 255, 255);
}
}
// claps/highs = white twinkles
if (clapsDetected)
{
if (random(0, 40 - ((float)peakBandIndex / (float)BAND_SIZE * 10.0)) == 0)
{
leds[i] = CRGB(255, 255, 255);
}
}
}
FastLED.show();
}