Skip to content

Commit e1c2de4

Browse files
committed
Arduino UNO R4 LEDs
1 parent 05dd727 commit e1c2de4

File tree

8 files changed

+373
-509
lines changed

8 files changed

+373
-509
lines changed

examples/examples-audiokit/streams-audiokit-fft-led/streams-audiokit-fft-led.ino

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
#include "AudioTools.h"
1+
/**
2+
* @file streams-audiokit-fft-led.ino
3+
* @author Phil Schatzmann
4+
* @brief We peform FFT on the microphone input of the AudioKit and display the
5+
* result on a 32*8 LED matrix
6+
* @version 0.1
7+
* @date 2022-10-14
8+
*
9+
* @copyright Copyright (c) 2022
10+
*
11+
*/
212
#include "AudioLibs/AudioKit.h"
3-
#include "AudioLibs/AudioRealFFT.h" // or AudioKissFFT
13+
#include "AudioLibs/AudioRealFFT.h" // or AudioKissFFT
414
#include "AudioLibs/LEDOutput.h"
15+
#include "AudioTools.h"
516

617
#define PIN_LEDS 22
718
#define LED_X 32
819
#define LED_Y 8
920

1021
AudioKitStream kit; // Audio source
11-
AudioRealFFT fft; // or AudioKissFFT
22+
AudioRealFFT fft; // or AudioKissFFT
23+
FFTDisplay fft_dis(fft);
24+
LEDOutput led(fft_dis); // output to LED matrix
1225
StreamCopy copier(fft, kit); // copy mic to fft
13-
LEDOutput led(fft); // output to LED matrix
1426

1527
void setup() {
1628
Serial.begin(115200);
@@ -23,24 +35,27 @@ void setup() {
2335

2436
// Setup FFT output
2537
auto tcfg = fft.defaultConfig();
26-
tcfg.length = 1024;
38+
tcfg.length = 1024;
2739
tcfg.copyFrom(cfg);
2840
fft.begin(tcfg);
2941

42+
// Setup FFT Display
43+
fft_dis.fft_group_bin = 3;
44+
fft_dis.fft_start_bin = 0;
45+
fft_dis.fft_max_magnitude = 40000;
46+
fft_dis.begin();
47+
3048
// Setup LED matrix output
3149
auto lcfg = led.defaultConfig();
3250
lcfg.x = LED_X;
3351
lcfg.y = LED_Y;
34-
lcfg.fft_group_bin = 3;
35-
lcfg.fft_start_bin = 0;
36-
lcfg.fft_max_magnitude = 40000;
3752
led.begin(lcfg);
3853

3954
// add LEDs
4055
FastLED.addLeds<WS2812B, PIN_LEDS, GRB>(led.ledData(), led.ledCount());
4156
}
4257

43-
void loop() {
58+
void loop() {
4459
// update FFT
4560
copier.copy();
4661
// update LEDs

src/AudioLibs/FFTDisplay.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#pragma once
2+
#include "AudioLibs/AudioFFT.h"
3+
4+
namespace audio_tools {
5+
6+
class FFTDisplay;
7+
FFTDisplay *selfFFTDisplay = nullptr;
8+
// fft mutex
9+
static Mutex fft_mux;
10+
11+
/**
12+
* Display FFT result: we can define a start bin and group susequent bins for a
13+
* combined result.
14+
*/
15+
16+
class FFTDisplay {
17+
public:
18+
FFTDisplay(AudioFFTBase &fft) {
19+
p_fft = &fft;
20+
selfFFTDisplay = this;
21+
}
22+
23+
/// start bin which is displayed
24+
int fft_start_bin = 0;
25+
/// group result by adding subsequent bins
26+
int fft_group_bin = 1;
27+
/// Influences the senitivity
28+
int fft_max_magnitude = 700;
29+
30+
void begin() {
31+
// assign fft callback
32+
AudioFFTConfig &fft_cfg = p_fft->config();
33+
fft_cfg.callback = fftCallback;
34+
35+
// number of bins
36+
magnitudes.resize(p_fft->size());
37+
for (int j = 0; j < p_fft->size(); j++) {
38+
magnitudes[j] = 0;
39+
}
40+
}
41+
42+
/// Returns the magnitude for the indicated led x position. We might
43+
/// need to combine values from the magnitudes array if this is much bigger.
44+
float getMagnitude(int x) {
45+
// get magnitude from fft
46+
float total = 0;
47+
for (int j = 0; j < fft_group_bin; j++) {
48+
int idx = fft_start_bin + (x * fft_group_bin) + j;
49+
if (idx >= magnitudes.size()) {
50+
idx = magnitudes.size() - 1;
51+
}
52+
total += magnitudes[idx];
53+
}
54+
return total / fft_group_bin;
55+
}
56+
57+
int getMagnitudeScaled(int x, int max) {
58+
return mapFloat(getMagnitude(x), 0, fft_max_magnitude, 0.0f,
59+
static_cast<float>(max));
60+
}
61+
62+
/// callback method which provides updated data from fft
63+
static void fftCallback(AudioFFTBase &fft) {
64+
selfFFTDisplay->loadMangnitudes();
65+
};
66+
67+
protected:
68+
AudioFFTBase *p_fft = nullptr;
69+
Vector<float> magnitudes{0};
70+
71+
void loadMangnitudes() {
72+
// just save magnitudes to be displayed
73+
#ifdef USE_CONCURRENCY
74+
LockGuard guard(fft_mux);
75+
#endif
76+
for (int j = 0; j < p_fft->size(); j++) {
77+
float value = p_fft->magnitude(j);
78+
magnitudes[j] = value;
79+
}
80+
}
81+
};
82+
83+
} // namespace audio_tools

0 commit comments

Comments
 (0)