FastLED 3.9.15
Loading...
Searching...
No Matches
PJRCSpectrumAnalyzer.h
Go to the documentation of this file.
1
2
6
7// LED Audio Spectrum Analyzer Display
8//
9// Creates an impressive LED light show to music input
10// using Teensy 3.1 with the OctoWS2811 adaptor board
11// http://www.pjrc.com/store/teensy31.html
12// http://www.pjrc.com/store/octo28_adaptor.html
13//
14// Line Level Audio Input connects to analog pin A3
15// Recommended input circuit:
16// http://www.pjrc.com/teensy/gui/?info=AudioInputAnalog
17//
18// This example code is in the public domain.
19
20#define USE_OCTOWS2811
21#include <OctoWS2811.h>
22#include <FastLED.h>
23#include <Audio.h>
24#include <Wire.h>
25#include <SD.h>
26#include <SPI.h>
27
28// The display size and color to use
29const unsigned int matrix_width = 60;
30const unsigned int matrix_height = 32;
31const unsigned int myColor = 0x400020;
32
33// These parameters adjust the vertical thresholds
34const float maxLevel = 0.5; // 1.0 = max, lower is more "sensitive"
35const float dynamicRange = 40.0; // total range to display, in decibels
36const float linearBlend = 0.3; // useful range is 0 to 0.7
37
39
40// Audio library objects
41AudioInputAnalog adc1(A3); //xy=99,55
42AudioAnalyzeFFT1024 fft; //xy=265,75
43AudioConnection patchCord1(adc1, fft);
44
45
46// This array holds the volume level (0 to 1.0) for each
47// vertical pixel to turn on. Computed in setup() using
48// the 3 parameters above.
50
51// This array specifies how many of the FFT frequency bin
52// to use for each horizontal pixel. Because humans hear
53// in octaves and FFT bins are linear, the low frequencies
54// use a small number of bins, higher frequencies use more.
56 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
57 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
58 3, 3, 3, 3, 4, 4, 4, 4, 4, 5,
59 5, 5, 6, 6, 6, 7, 7, 7, 8, 8,
60 9, 9, 10, 10, 11, 12, 12, 13, 14, 15,
61 15, 16, 17, 18, 19, 20, 22, 23, 24, 25
62};
63
64
65
66// Run setup once
67void setup() {
68 // the audio library needs to be given memory to start working
69 AudioMemory(12);
70
71 // compute the vertical thresholds before starting
73
74 // turn on the display
76}
77
78// A simple xy() function to turn display matrix coordinates
79// into the index numbers OctoWS2811 requires. If your LEDs
80// are arranged differently, edit this code...
81unsigned int xy(unsigned int x, unsigned int y) {
82 if ((y & 1) == 0) {
83 // even numbered rows (0, 2, 4...) are left to right
84 return y * matrix_width + x;
85 } else {
86 // odd numbered rows (1, 3, 5...) are right to left
87 return y * matrix_width + matrix_width - 1 - x;
88 }
89}
90
91// Run repetitively
92void loop() {
93 unsigned int x, y, freqBin;
94 float level;
95
96 if (fft.available()) {
97 // freqBin counts which FFT frequency data has been used,
98 // starting at low frequency
99 freqBin = 0;
100
101 for (x=0; x < matrix_width; x++) {
102 // get the volume for each horizontal pixel position
103 level = fft.read(freqBin, freqBin + frequencyBinsHorizontal[x] - 1);
104
105 // uncomment to see the spectrum in Arduino's Serial Monitor
106 // Serial.print(level);
107 // Serial.print(" ");
108
109 for (y=0; y < matrix_height; y++) {
110 // for each vertical pixel, check if above the threshold
111 // and turn the LED on or off
112 if (level >= thresholdVertical[y]) {
113 leds[xy(x,y)] = CRGB(myColor);
114 } else {
115 leds[xy(x,y)] = CRGB::Black;
116 }
117 }
118 // increment the frequency bin count, so we display
119 // low to higher frequency from left to right
120 freqBin = freqBin + frequencyBinsHorizontal[x];
121 }
122 // after all pixels set, show them all at the same instant
123 FastLED.show();
124 // Serial.println();
125 }
126}
127
128
129// Run once from setup, the compute the vertical levels
131 unsigned int y;
132 float n, logLevel, linearLevel;
133
134 for (y=0; y < matrix_height; y++) {
135 n = (float)y / (float)(matrix_height - 1);
136 logLevel = pow10f(n * -1.0 * (dynamicRange / 20.0));
137 linearLevel = 1.0 - n;
138 linearLevel = linearLevel * linearBlend;
139 logLevel = logLevel * (1.0 - linearBlend);
140 thresholdVertical[y] = (logLevel + linearLevel) * maxLevel;
141 }
142}
CRGB leds[NUM_LEDS]
int y
Definition simple.h:93
int x
Definition simple.h:92
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:74
@ OCTOWS2811
Definition FastLED.h:139
central include file for FastLED, defines the CFastLED class/object
float thresholdVertical[matrix_height]
AudioConnection patchCord1(adc1, fft)
const unsigned int matrix_height
unsigned int xy(unsigned int x, unsigned int y)
const float linearBlend
void setup()
const unsigned int myColor
const float dynamicRange
AudioAnalyzeFFT1024 fft
const unsigned int matrix_width
int frequencyBinsHorizontal[matrix_width]
const float maxLevel
AudioInputAnalog adc1(A3)
void computeVerticalLevels()
void loop()
@ Black
<div style='background:#000000;width:4em;height:4em;'></div>
Definition crgb.h:567
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86