Control Surface  1.1.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 <MIDI_Inputs/MCU/VU.hpp>
5 
7 
9 
10 /**
11  * @brief A VU meter that reads from an Audio stream using the
12  * Analyzer class.
13  *
14  * @ingroup Audio
15  */
16 class AudioVU : public IVU {
17  public:
18  /**
19  * @brief Create a new AudioVU object.
20  *
21  * @param level
22  * The Teensy Audio peak or RMS analyzer object.
23  * Note that it is kept by reference, so it must outlive the
24  * AudioVU instance.
25  * @param gain
26  * A multiplier to calibrate the VU meter.
27  * @param max
28  * The max output, or the length of the output scale:
29  * `getValue` will output a number in [0, max].
30  */
31  template <class T>
32  AudioVU(T &level, float gain = 1.0, uint8_t max = 255)
33  : IVU(max), level{level}, gain(gain) {}
34 
35  /**
36  * @brief Create a new AudioVU object.
37  *
38  * @param level
39  * The Teensy Audio peak or RMS analyzer object.
40  * Note that it is kept by reference, so it must outlive the
41  * AudioVU instance.
42  * @param ballistics
43  * The dynamic model to use for the VU meter.
44  * @param gain
45  * A multiplier to calibrate the VU meter.
46  * @param max
47  * The max output, or the length of the output scale:
48  * `getValue` will output a number in [0, max].
49  */
50  template <class T>
52  uint8_t max = 255)
54 
55  /**
56  * @brief Get the value of the VU meter.
57  *
58  * @return A value in [0, max]
59  */
60  uint8_t getValue() override {
61  uint16_t value = getFloatValue() * max;
62  return value;
63  }
64 
65  /**
66  * @brief Get the value of the VU meter.
67  *
68  * @return A value in [0.0, 1.0]
69  */
70  float getFloatValue() override {
71  if (!level.available())
72  return 0;
73  float peakLevel = level.read();
74  float value = ballistics(peakLevel) * gain;
75  if (value > 1.0f)
76  value = 1.0f;
77  else if (value < 0.0f)
78  value = 0.0f;
79  return value;
80  }
81 
82  /** @note This function will always return false for an AudioVU. */
83  bool getOverload() override { return false; } // TODO
84  /**
85  * @brief Set the gain for the VU meter.
86  *
87  * @param gain
88  * The new gain.
89  */
90  void setGain(float gain) { this->gain = gain; }
91 
92  private:
94 
95  /**
96  * @brief The `AudioAnalyzePeak` and `AudioAnalyzeRMS` classes don't
97  * implement a common interface, so we have to use our own RTTI
98  * wrapper to allow both classes to be used.
99  */
100  class Analyzer {
101  private:
102  enum { Peak, RMS } type;
103  void *analyzer;
104 
105  public:
106  Analyzer(AudioAnalyzePeak &analyzer) //
107  : type(Peak), analyzer(&analyzer) {}
108  Analyzer(AudioAnalyzeRMS &analyzer) //
109  : type(RMS), analyzer(&analyzer) {}
110 
111  float read() const {
112  switch (type) {
113  case Peak:
114  return static_cast<AudioAnalyzePeak *>(analyzer)->read();
115  case RMS:
116  return static_cast<AudioAnalyzeRMS *>(analyzer)->read();
117  default: ERROR(F("Error: Invalid type!"), 0x1518); return 0;
118  }
119  }
120 
121  bool available() const {
122  switch (type) {
123  case Peak:
124  return static_cast<AudioAnalyzePeak *>(analyzer)
125  ->available();
126  case RMS:
127  return static_cast<AudioAnalyzeRMS *>(analyzer)
128  ->available();
129  default: ERROR(F("Error: Invalid type!"), 0x1519); return false;
130  }
131  }
132  } level;
133 
134  float gain;
135 };
136 
AudioVU::Analyzer::RMS
Definition: AudioVU.hpp:102
AudioVU::AudioVU
AudioVU(T &level, MovingCoilBallistics ballistics, float gain=1.0, uint8_t max=255)
Create a new AudioVU object.
Definition: AudioVU.hpp:51
AudioVU::ballistics
MovingCoilBallistics ballistics
Definition: AudioVU.hpp:93
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:103
AudioVU::AudioVU
AudioVU(T &level, float gain=1.0, uint8_t max=255)
Create a new AudioVU object.
Definition: AudioVU.hpp:32
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:60
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
AudioVU::Analyzer::Analyzer
Analyzer(AudioAnalyzePeak &analyzer)
Definition: AudioVU.hpp:106
AudioVU::level
class AudioVU::Analyzer level
AudioVU::Analyzer::available
bool available() const
Definition: AudioVU.hpp:121
AudioVU
A VU meter that reads from an Audio stream using the Analyzer class.
Definition: AudioVU.hpp:16
AudioVU::Analyzer::read
float read() const
Definition: AudioVU.hpp:111
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
AudioVU::Analyzer::Analyzer
Analyzer(AudioAnalyzeRMS &analyzer)
Definition: AudioVU.hpp:108
ERROR
#define ERROR(msg, errc)
Print the error message and error code, and stop the execution if FATAL_ERRORS are enabled.
Definition: Error.hpp:42
AudioVU::getFloatValue
float getFloatValue() override
Get the value of the VU meter.
Definition: AudioVU.hpp:70
VU.hpp
AudioVU::Analyzer::Peak
Definition: AudioVU.hpp:102
AudioVU::Analyzer
The AudioAnalyzePeak and AudioAnalyzeRMS classes don't implement a common interface,...
Definition: AudioVU.hpp:100
AudioVU::Analyzer::type
enum AudioVU::Analyzer::@3 type
MovingCoilBallistics
Definition: MovingCoilBallistics.hpp:7
AudioVU::getOverload
bool getOverload() override
Definition: AudioVU.hpp:83
MovingCoilBallistics.hpp
AudioVU::setGain
void setGain(float gain)
Set the gain for the VU meter.
Definition: AudioVU.hpp:90
AudioVU::gain
float gain
Definition: AudioVU.hpp:134
MIDI_Notes::F
constexpr int8_t F
Definition: Notes.hpp:23