Control Surface  1.1.0
MIDI Control Surface library for Arduino
VolumeControl.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
7 #include <Arduino.h>
8 #include <Audio.h>
9 #include <Def/Def.hpp>
10 
12 
13 /**
14  * @brief A class for controlling the volume of AudioMixer4 objects using a
15  * potentiometer.
16  *
17  * @tparam N
18  * The number of mixers.
19  *
20  * @ingroup Audio
21  */
22 template <uint8_t N>
23 class VolumeControl : public AH::Updatable<Potentiometer> {
24  public:
25  /**
26  * @brief Create a new VolumeControl object.
27  *
28  * @param mixers
29  * An array of pointers to audio mixers.
30  * Only the pointers are saved, so the mixers should outlive this
31  * object.
32  * @param analogPin
33  * The analog pin with the potentiometer connected.
34  * @param maxGain
35  * The maximum gain for the mixers.
36  */
38  float maxGain = 1.0)
39  : mixers(mixers), filteredAnalog(analogPin), maxGain(maxGain) {}
40 
41  /**
42  * @brief Read the potentiometer value, and adjust the gain of the mixers.
43  */
44  void update() override {
45  if (filteredAnalog.update()) {
46  float gain = filteredAnalog.getFloatValue() * maxGain;
47  for (AudioMixer4 *mixer : mixers)
48  for (uint8_t ch = 0; ch < 4; ch++)
49  mixer->gain(ch, gain);
50  }
51  }
52 
53  /**
54  * @brief Initialize.
55  */
56  void begin() override {}
57 
58  /**
59  * @brief Specify a mapping function that is applied to the raw
60  * analog value before setting the volume.
61  *
62  * @param fn
63  * A function pointer to the mapping function. This function
64  * should take the filtered analog value of @f$ 16 -
65  * \mathrm{ANALOG\_FILTER\_SHIFT\_FACTOR} @f$ bits as a parameter,
66  * and should return a value in the same range.
67  *
68  * @see FilteredAnalog::map
69  */
71 
72  /// Invert the analog value.
74 
75  private:
78  const float maxGain;
79 };
80 
AH::Updatable
A super class for object that have to be updated regularly.
Definition: Updatable.hpp:25
AH::FilteredAnalog::map
void map(MappingFunction fn)
Specify a mapping function that is applied to the raw analog value before filtering.
Definition: FilteredAnalog.hpp:85
Updatable.hpp
AH::FilteredAnalog::invert
void invert()
Invert the analog value.
Definition: FilteredAnalog.hpp:94
VolumeControl::mixers
Array< AudioMixer4 *, N > mixers
Definition: VolumeControl.hpp:76
VolumeControl::map
void map(MappingFunction fn)
Specify a mapping function that is applied to the raw analog value before setting the volume.
Definition: VolumeControl.hpp:70
Def.hpp
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
AH::FilteredAnalog
A class that reads and filters an analog input.
Definition: FilteredAnalog.hpp:54
VolumeControl::begin
void begin() override
Initialize.
Definition: VolumeControl.hpp:56
VolumeControl::VolumeControl
VolumeControl(const Array< AudioMixer4 *, N > &mixers, pin_t analogPin, float maxGain=1.0)
Create a new VolumeControl object.
Definition: VolumeControl.hpp:37
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
AH::FilteredAnalog::update
bool update()
Read the analog input value, apply the mapping function, and update the average.
Definition: FilteredAnalog.hpp:108
AH::Array
An array wrapper for easy copying, comparing, and iterating.
Definition: Array.hpp:36
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
VolumeControl::update
void update() override
Read the potentiometer value, and adjust the gain of the mixers.
Definition: VolumeControl.hpp:44
AH::FilteredAnalog::getFloatValue
float getFloatValue() const
Get the filtered value of the analog input with the mapping function applied as a floating point numb...
Definition: FilteredAnalog.hpp:133
VolumeControl
A class for controlling the volume of AudioMixer4 objects using a potentiometer.
Definition: VolumeControl.hpp:23
FilteredAnalog.hpp
VolumeControl::filteredAnalog
AH::FilteredAnalog filteredAnalog
Definition: VolumeControl.hpp:77
MappingFunction
analog_t(*)(analog_t) MappingFunction
Definition: Def.hpp:19
VolumeControl::maxGain
const float maxGain
Definition: VolumeControl.hpp:78
VolumeControl::invert
void invert()
Invert the analog value.
Definition: VolumeControl.hpp:73