arduino-audio-tools
LEDOutputUnoR4.h
1 #pragma once
2 #include "Arduino_LED_Matrix.h"
3 #include "AudioLibs/AudioFFT.h"
4 #include "FFTDisplay.h"
5 
6 namespace audio_tools {
7 class LEDOutputUnoR4;
8 struct LEDOutputUnoR4Config;
9 
10 // default callback function which implements led update based on fft
11 void fftLEDOutputUnoR4(LEDOutputUnoR4Config *cfg, LEDOutputUnoR4 *matrix);
12 // led update for volume
13 void 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 
42 
43  public:
45  LEDOutputUnoR4() = default;
46 
50  p_fft = &fft;
52  }
53 
57  p_vol = &vol;
59  }
60 
63 
65  bool begin() { return begin(defaultConfig()); }
66 
69  cfg = config;
70  frame.resize(cfg.x * cfg.y);
71  led_matrix.begin();
72  max_column = -1;
73  return true;
74  }
75 
77  virtual void update() {
78  if (cfg.update_callback != nullptr && count++ % cfg.update_frequency == 0) {
79  // use custom update logic defined in config
80  cfg.update_callback(&cfg, this);
81  } else {
82  display();
83  }
84  }
85 
87  bool &ledXY(uint8_t x, uint8_t y) {
88  if (cfg.y_mirror) y = cfg.y - y - 1;
89  return frame[x + (y * cfg.x)];
90  }
91 
93  virtual float getMaxMagnitude() {
94  // get magnitude from
95  if (p_vol != nullptr) {
96  return p_vol->volume();
97  }
98  float max = 0;
99  if (p_fft != nullptr) {
100  for (int j = 0; j < cfg.x; j++) {
101  float value = p_fft->getMagnitude(j);
102  if (value > max) {
103  max = value;
104  }
105  }
106  }
107  return max;
108  }
109 
111  void setColumnBar(int x, int currY) {
112  // update vertical bar
113  for (uint8_t y = 0; y < currY; y++) {
114  // update LED
115  ledXY(x, y) = true;
116  }
117  for (uint8_t y = currY; y < cfg.y; y++) {
118  ledXY(x, y) = false;
119  }
120  if (x > max_column) max_column = x;
121  }
122 
124  void addColumnBar(int currY) {
125  max_column++;
126  if (max_column >= cfg.x) {
127  addEmptyColumn();
128  }
129  if (max_column > cfg.x - 1) {
130  max_column = cfg.x - 1;
131  }
132  setColumnBar(max_column, currY);
133  }
134 
136  LEDOutputUnoR4Config &config() { return cfg; }
137 
139  void display() {
140  led_matrix.loadPixels((uint8_t *)frame.data(), cfg.x * cfg.y);
141  }
142 
145  return *p_fft;
146  }
147 
148  protected:
149  friend class AudioFFTBase;
151  FFTDisplay *p_fft = nullptr;
152  VolumeOutput *p_vol = nullptr;
153  uint64_t count = 0;
154  ArduinoLEDMatrix led_matrix;
155  Vector<bool> frame{0};
156  int max_column = -1;
157 
159  void addEmptyColumn() {
160  for (int x = 1; x < cfg.x; x++) {
161  for (int y = 0; y < cfg.y; y++) {
162  ledXY(x - 1, y) = ledXY(x, y);
163  }
164  }
165  for (int y = 0; y < cfg.y; y++) {
166  ledXY(cfg.x - 1, y) = false;
167  }
168  }
169 };
170 
173  // process horizontal
174  for (int x = 0; x < cfg->x; x++) {
175  // max y determined by magnitude
176  int currY = matrix->fftDisplay().getMagnitudeScaled(x, cfg->y);
177  LOGD("x: %d, y: %d", x, currY);
178  matrix->setColumnBar(x, currY);
179  }
180  matrix->display();
181 }
182 
185  float vol = matrix->getMaxMagnitude();
186  int currY = mapFloat(vol, 0.0,
187  cfg->max_magnitude, 0.0f,
188  static_cast<float>(cfg->y));
189  matrix->addColumnBar(currY);
190  matrix->display();
191 }
192 
193 } // namespace audio_tools
Executes FFT using audio data. The Driver which is passed in the constructor selects a specifc FFT im...
Definition: AudioFFT.h:82
Definition: FFTDisplay.h:17
float getMagnitude(int x)
Definition: FFTDisplay.h:45
LED output using the R4 LED matrix library.
Definition: LEDOutputUnoR4.h:41
FFTDisplay & fftDisplay()
Provides access to the FFTDisplay object.
Definition: LEDOutputUnoR4.h:144
void setColumnBar(int x, int currY)
Update the indicated column with the indicated bar.
Definition: LEDOutputUnoR4.h:111
void display()
Update the led_matrix.
Definition: LEDOutputUnoR4.h:139
LEDOutputUnoR4(VolumeOutput &vol)
Constructor for VolumeOutput scenario.
Definition: LEDOutputUnoR4.h:56
bool begin(LEDOutputUnoR4Config config)
Setup Led matrix.
Definition: LEDOutputUnoR4.h:68
void addColumnBar(int currY)
Update the last column with the indicated bar.
Definition: LEDOutputUnoR4.h:124
virtual float getMaxMagnitude()
Provodes the max magnitude for the VolumeOutput and FFT scenario.
Definition: LEDOutputUnoR4.h:93
bool begin()
Starts the processing with the default configuration.
Definition: LEDOutputUnoR4.h:65
void addEmptyColumn()
Adds an empty column to the end shifting the content to the left.
Definition: LEDOutputUnoR4.h:159
LEDOutputUnoR4(FFTDisplay &fft)
Constructor for FFT scenario.
Definition: LEDOutputUnoR4.h:49
LEDOutputUnoR4Config defaultConfig()
Provides the default config object.
Definition: LEDOutputUnoR4.h:62
LEDOutputUnoR4()=default
Default Constructor.
LEDOutputUnoR4Config & config()
Provides access to the actual config object. E.g. to change the update logic.
Definition: LEDOutputUnoR4.h:136
bool & ledXY(uint8_t x, uint8_t y)
Determine the led with the help of the x and y pos.
Definition: LEDOutputUnoR4.h:87
virtual void update()
Updates the display by calling the update callback method: call this method in your loop.
Definition: LEDOutputUnoR4.h:77
A simple class to determine the volume.
Definition: AudioOutput.h:515
float volume()
Definition: AudioOutput.h:546
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AnalogAudio.h:10
float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)
Similar to Arduino map function but using floats.
Definition: AudioTypes.h:483
void fftLEDOutputUnoR4(LEDOutputUnoR4Config *cfg, LEDOutputUnoR4 *matrix)
Default update implementation which provides the fft result as "barchart".
Definition: LEDOutputUnoR4.h:172
void volumeLEDOutputUnoR4(LEDOutputUnoR4Config *cfg, LEDOutputUnoR4 *matrix)
Default update implementation which provides the fft result as "barchart".
Definition: LEDOutputUnoR4.h:184
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