This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
AudioVU.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Audio.h>
4 #include <Helpers/EMA.hpp>
5 #include <MIDI_Inputs/MCU/VU.hpp>
6 
8 
10 
17 class AudioVU : public IVU {
18  public:
32  template <class T>
33  AudioVU(T &level, float gain = 1.0, uint8_t max = 255)
34  : IVU(max), level{level}, gain(gain) {}
35 
51  template <class T>
53  uint8_t max = 255)
55 
61  uint8_t getValue() override {
62  uint16_t value = getFloatValue() * max;
63  return value;
64  }
65 
71  float getFloatValue() override {
72  if (!level.available())
73  return 0;
74  float peakLevel = level.read();
75  float value = ballistics(peakLevel) * gain;
76  if (value > 1.0f)
77  value = 1.0f;
78  else if (value < 0.0f)
79  value = 0.0f;
80  return value;
81  }
82 
84  bool getOverload() override { return false; } // TODO
91  void setGain(float gain) { this->gain = gain; }
92 
93  private:
95 
101  class Analyzer {
102  private:
103  enum { Peak, RMS } type;
104  void *analyzer;
105 
106  public:
107  Analyzer(AudioAnalyzePeak &analyzer) //
108  : type(Peak), analyzer(&analyzer) {}
109  Analyzer(AudioAnalyzeRMS &analyzer) //
110  : type(RMS), analyzer(&analyzer) {}
111 
112  float read() const {
113  switch (type) {
114  case Peak:
115  return static_cast<AudioAnalyzePeak *>(analyzer)->read();
116  case RMS:
117  return static_cast<AudioAnalyzeRMS *>(analyzer)->read();
118  default: ERROR(F("Error: Invalid type!"), 0x1518); return 0;
119  }
120  }
121 
122  bool available() const {
123  switch (type) {
124  case Peak:
125  return static_cast<AudioAnalyzePeak *>(analyzer)
126  ->available();
127  case RMS:
128  return static_cast<AudioAnalyzeRMS *>(analyzer)
129  ->available();
130  default: ERROR(F("Error: Invalid type!"), 0x1519); return false;
131  }
132  }
133  } level;
134 
135  float gain;
136 };
137 
AudioVU::AudioVU
AudioVU(T &level, MovingCoilBallistics ballistics, float gain=1.0, uint8_t max=255)
Create a new AudioVU object.
Definition: AudioVU.hpp:52
AudioVU::ballistics
MovingCoilBallistics ballistics
Definition: AudioVU.hpp:94
IVU::max
const uint8_t max
Definition: VU.hpp:33
IVU
An abstract interface for VU meters.
Definition: VU.hpp:20
AudioVU::Analyzer::analyzer
void * analyzer
Definition: AudioVU.hpp:104
ERROR
#define ERROR(x, e)
Definition: Error.hpp:27
AudioVU::AudioVU
AudioVU(T &level, float gain=1.0, uint8_t max=255)
Create a new AudioVU object.
Definition: AudioVU.hpp:33
MovingCoilBallistics::responsiveVU
static MovingCoilBallistics responsiveVU(float Tsfactor=2.0)
Definition: MovingCoilBallistics.hpp:40
AudioVU::getValue
uint8_t getValue() override
Get the value of the VU meter.
Definition: AudioVU.hpp:61
AudioVU::Analyzer::Analyzer
Analyzer(AudioAnalyzePeak &analyzer)
Definition: AudioVU.hpp:107
AudioVU::level
class AudioVU::Analyzer level
AudioVU::Analyzer::available
bool available() const
Definition: AudioVU.hpp:122
AudioVU
A VU meter that reads from an Audio stream using the Analyzer class.
Definition: AudioVU.hpp:17
AudioVU::Analyzer::read
float read() const
Definition: AudioVU.hpp:112
AudioVU::Analyzer::Analyzer
Analyzer(AudioAnalyzeRMS &analyzer)
Definition: AudioVU.hpp:109
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: NamespaceSettings.hpp:9
AudioVU::getFloatValue
float getFloatValue() override
Get the value of the VU meter.
Definition: AudioVU.hpp:71
VU.hpp
AudioVU::Analyzer::RMS
Definition: AudioVU.hpp:103
AudioVU::Analyzer::Peak
Definition: AudioVU.hpp:103
EMA.hpp
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: NamespaceSettings.hpp:10
AudioVU::Analyzer
The AudioAnalyzePeak and AudioAnalyzeRMS classes don't implement a common interface,...
Definition: AudioVU.hpp:101
MovingCoilBallistics
Definition: MovingCoilBallistics.hpp:7
AudioVU::getOverload
bool getOverload() override
Definition: AudioVU.hpp:84
MovingCoilBallistics.hpp
AudioVU::Analyzer::type
enum AudioVU::Analyzer::@0 type
AudioVU::setGain
void setGain(float gain)
Set the gain for the VU meter.
Definition: AudioVU.hpp:91
AudioVU::gain
float gain
Definition: AudioVU.hpp:135
MIDI_Notes::F
constexpr int8_t F
Definition: Notes.hpp:23