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