arduino-audio-tools
Loading...
Searching...
No Matches
LEDOutputUnoR4.h
Go to the documentation of this file.
1#pragma once
2#include "Arduino_LED_Matrix.h"
4#include "FFTDisplay.h"
5
6namespace audio_tools {
7class LEDOutputUnoR4;
8struct LEDOutputUnoR4Config;
9
10// default callback function which implements led update based on fft
11void fftLEDOutputUnoR4(LEDOutputUnoR4Config *cfg, LEDOutputUnoR4 *matrix);
12// led update for volume
13void volumeLEDOutputUnoR4(LEDOutputUnoR4Config *cfg, LEDOutputUnoR4 *matrix);
14
23 LEDOutputUnoR4 *matrix) = nullptr;
25 int update_frequency = 1; // update every call
27 int x = 12;
29 int y = 8;
31 bool y_mirror = true;
33 int max_magnitude = 700;
34};
35
44
45 public:
47 LEDOutputUnoR4() = default;
48
55
62
65
67 bool begin() { return begin(defaultConfig()); }
68
71 cfg = config;
72 frame.resize(cfg.x * cfg.y);
73 led_matrix.begin();
74 max_column = -1;
75 return true;
76 }
77
79 virtual void update() {
80 if (cfg.update_callback != nullptr && count++ % cfg.update_frequency == 0) {
81 // use custom update logic defined in config
82 cfg.update_callback(&cfg, this);
83 } else {
84 display();
85 }
86 }
87
89 bool &ledXY(uint8_t x, uint8_t y) {
90 if (cfg.y_mirror) y = cfg.y - y - 1;
91 return frame[x + (y * cfg.x)];
92 }
93
95 virtual float getMaxMagnitude() {
96 // get magnitude from
97 if (p_vol != nullptr) {
98 return p_vol->volume();
99 }
100 float max = 0;
101 if (p_fft != nullptr) {
102 for (int j = 0; j < cfg.x; j++) {
103 float value = p_fft->getMagnitude(j);
104 if (value > max) {
105 max = value;
106 }
107 }
108 }
109 return max;
110 }
111
113 void setColumnBar(int x, int currY) {
114 // update vertical bar
115 for (uint8_t y = 0; y < currY; y++) {
116 // update LED
117 ledXY(x, y) = true;
118 }
119 for (uint8_t y = currY; y < cfg.y; y++) {
120 ledXY(x, y) = false;
121 }
122 if (x > max_column) max_column = x;
123 }
124
126 void addColumnBar(int currY) {
127 max_column++;
128 if (max_column >= cfg.x) {
130 }
131 if (max_column > cfg.x - 1) {
132 max_column = cfg.x - 1;
133 }
135 }
136
139
141 void display() {
142 led_matrix.loadPixels((uint8_t *)frame.data(), cfg.x * cfg.y);
143 }
144
147 return *p_fft;
148 }
149
150 protected:
151 friend class AudioFFTBase;
153 FFTDisplay *p_fft = nullptr;
154 VolumeMeter *p_vol = nullptr;
158 int max_column = -1;
159
162 for (int x = 1; x < cfg.x; x++) {
163 for (int y = 0; y < cfg.y; y++) {
164 ledXY(x - 1, y) = ledXY(x, y);
165 }
166 }
167 for (int y = 0; y < cfg.y; y++) {
168 ledXY(cfg.x - 1, y) = false;
169 }
170 }
171};
172
175 // process horizontal
176 for (int x = 0; x < cfg->x; x++) {
177 // max y determined by magnitude
178 int currY = matrix->fftDisplay().getMagnitudeScaled(x, cfg->y);
179 LOGD("x: %d, y: %d", x, currY);
180 matrix->setColumnBar(x, currY);
181 }
182 matrix->display();
183}
184
187 float vol = matrix->getMaxMagnitude();
188 int currY = mapT<float>(vol, 0.0,
189 cfg->max_magnitude, 0.0f,
190 static_cast<float>(cfg->y));
191 matrix->addColumnBar(currY);
192 matrix->display();
193}
194
195} // namespace audio_tools
#define LOGD(...)
Definition AudioLoggerIDF.h:27
Executes FFT using audio data privded by write() and/or an inverse FFT where the samples are made ava...
Definition AudioFFT.h:195
Definition FFTDisplay.h:18
float getMagnitude(int x)
Definition FFTDisplay.h:46
LED output using the R4 LED matrix library.
Definition LEDOutputUnoR4.h:43
int max_column
Definition LEDOutputUnoR4.h:158
LEDOutputUnoR4Config cfg
Definition LEDOutputUnoR4.h:152
LEDOutputUnoR4Config & config()
Provides access to the actual config object. E.g. to change the update logic.
Definition LEDOutputUnoR4.h:138
void setColumnBar(int x, int currY)
Update the indicated column with the indicated bar.
Definition LEDOutputUnoR4.h:113
void display()
Update the led_matrix.
Definition LEDOutputUnoR4.h:141
LEDOutputUnoR4(VolumeMeter &vol)
Constructor for VolumeMeter scenario.
Definition LEDOutputUnoR4.h:58
bool begin(LEDOutputUnoR4Config config)
Setup Led matrix.
Definition LEDOutputUnoR4.h:70
void addColumnBar(int currY)
Update the last column with the indicated bar.
Definition LEDOutputUnoR4.h:126
FFTDisplay & fftDisplay()
Provides access to the FFTDisplay object.
Definition LEDOutputUnoR4.h:146
virtual float getMaxMagnitude()
Provodes the max magnitude for the VolumeMeter and FFT scenario.
Definition LEDOutputUnoR4.h:95
bool begin()
Starts the processing with the default configuration.
Definition LEDOutputUnoR4.h:67
void addEmptyColumn()
Adds an empty column to the end shifting the content to the left.
Definition LEDOutputUnoR4.h:161
LEDOutputUnoR4(FFTDisplay &fft)
Constructor for FFT scenario.
Definition LEDOutputUnoR4.h:51
FFTDisplay * p_fft
Definition LEDOutputUnoR4.h:153
VolumeMeter * p_vol
Definition LEDOutputUnoR4.h:154
ArduinoLEDMatrix led_matrix
Definition LEDOutputUnoR4.h:156
LEDOutputUnoR4Config defaultConfig()
Provides the default config object.
Definition LEDOutputUnoR4.h:64
LEDOutputUnoR4()=default
Default Constructor.
virtual void update()
Updates the display by calling the update callback method: call this method in your loop.
Definition LEDOutputUnoR4.h:79
bool & ledXY(uint8_t x, uint8_t y)
Determine the led with the help of the x and y pos.
Definition LEDOutputUnoR4.h:89
Vector< bool > frame
Definition LEDOutputUnoR4.h:157
uint64_t count
Definition LEDOutputUnoR4.h:155
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
bool resize(size_t newSize, T value)
Definition Vector.h:266
T * data()
Definition Vector.h:316
A simple class to determine the volume. You can use it as final output or as output or input in your ...
Definition AudioStreams.h:1831
float volume()
Definition AudioStreams.h:1879
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
void fftLEDOutputUnoR4(LEDOutputUnoR4Config *cfg, LEDOutputUnoR4 *matrix)
Default update implementation which provides the fft result as "barchart".
Definition LEDOutputUnoR4.h:174
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508
void volumeLEDOutputUnoR4(LEDOutputUnoR4Config *cfg, LEDOutputUnoR4 *matrix)
Default update implementation which provides the fft result as "barchart".
Definition LEDOutputUnoR4.h:186
Definition LEDOutputUnoR4.h:20
int y
Number of LEDs in a column.
Definition LEDOutputUnoR4.h:29
int max_magnitude
Influences the senitivity.
Definition LEDOutputUnoR4.h:33
int x
Number of LEDs in a rows.
Definition LEDOutputUnoR4.h:27
int update_frequency
Update the leds only ever nth call.
Definition LEDOutputUnoR4.h:25
void(* update_callback)(LEDOutputUnoR4Config *cfg, LEDOutputUnoR4 *matrix)
Custom callback logic to update the LEDs when update() is called.
Definition LEDOutputUnoR4.h:22
bool y_mirror
when true 0,0 is in the lower left corder
Definition LEDOutputUnoR4.h:31