arduino-audio-tools
VolumeControl.h
1 #pragma once
2 
8 namespace audio_tools {
9 
18  public:
20  virtual float getVolumeFactor(float volume) = 0;
21 
22  protected:
23 
25  virtual float limit(float in){
26  float result = in;
27  if (result<0.0f) result = 0;
28  if (result>1.0f) result = 1;
29  return result;
30  }
31 
32 };
33 
41  public:
43  setVolumeControl(vc);
44  }
45 
47  if (vc!=nullptr) setVolumeControl(*vc);
48  }
49 
50  void setVolumeControl(VolumeControl &vc){
51  p_vc = &vc;
52  }
53 
55  virtual float getVolumeFactor(float volume) {
56  if (p_vc==nullptr) return 1.0f; // prevent NPE
57  if (abs(volume-in)<0.01f){
58  return out;
59  } else {
60  in = volume;
61  out = p_vc->getVolumeFactor(volume);
62  return out;
63  }
64  }
65 
66  protected:
67  VolumeControl *p_vc=nullptr;
68  float in=1.0, out=1.0;
69 
70 };
71 
72 
82  public:
83  LogarithmicVolumeControl(float ym=0.1){
84  this->ym = ym;
85  }
86 
87  // provides a factor in the range of 0.0 to 1.0
88  virtual float getVolumeFactor(float input) {
89  float b = pow(((1/ym)-1), 2);
90  float a = 1.0f / (b - 1.0f);
91  float volumeFactor = pow(b,input) * a - a;
92  return limit(volumeFactor);
93  }
94 
95  protected:
96  float ym;
97 };
98 
106  public:
107  // provides a factor in the range of 0.0 to 1.0
108  virtual float getVolumeFactor(float volume) {
109  float volumeFactor = pow(2.0, volume) - 1.0;
110  return limit(volumeFactor);
111  }
112 };
113 
124  public:
125  SimulatedAudioPot(float x=0.5, float y=0.1){
126  this->x = x;
127  this->y = y;
128  }
129  virtual float getVolumeFactor(float volume) {
130  float result = 0;
131  if (volume<=x){
132  result = mapT<float>(volume, 0.0, x, 0, y );
133  } else {
134  result = mapT<float>(volume, x, 1.0, y, 1.0);
135  }
136  return limit(result);
137  }
138  protected:
139  float x, y;
140 };
141 
150  public:
151  LinearVolumeControl(bool allowBoost=false){
152  is_limited = !allowBoost;
153  }
154  // provides a factor in the range of 0.0 to 1.0
155  virtual float getVolumeFactor(float volume) {
156  return is_limited ? limit(volume) : volume;
157  }
158  protected:
159  bool is_limited;
160 };
161 
162 
170  public:
171  CallbackVolumeControl(float(*cb)(float in)){
172  callback = cb;
173  }
174  // provides a factor in the range of 0.0 to 1.0
175  virtual float getVolumeFactor(float volume) {
176  return limit(callback(volume));
177  }
178  protected:
179  float (*callback)(float in) = nullptr;
180 
181 };
182 
183 } // namespace
In order to optimize the processing time we cache the last input & factor and recalculate the new fac...
Definition: VolumeControl.h:40
virtual float getVolumeFactor(float volume)
determines a multiplication factor (0.0 to 1.0) from an input value (0.0 to 1.0).
Definition: VolumeControl.h:55
Provide the volume function as callback method: This is easy to use e.g together with a lamda functio...
Definition: VolumeControl.h:169
virtual float getVolumeFactor(float volume)
determines a multiplication factor (0.0 to 1.0) from an input value (0.0 to 1.0).
Definition: VolumeControl.h:175
Simple exponentional volume control using the formula pow(2.0, input) - 1.0;.
Definition: VolumeControl.h:105
virtual float getVolumeFactor(float volume)
determines a multiplication factor (0.0 to 1.0) from an input value (0.0 to 1.0).
Definition: VolumeControl.h:108
The simplest possible implementation of a VolumeControl: The input = output which describes a linear ...
Definition: VolumeControl.h:149
virtual float getVolumeFactor(float volume)
determines a multiplication factor (0.0 to 1.0) from an input value (0.0 to 1.0).
Definition: VolumeControl.h:155
Parametric Logarithmic volume control. Using the formula pow(b,input) * a - a, where b is b = pow(((1...
Definition: VolumeControl.h:81
virtual float getVolumeFactor(float input)
determines a multiplication factor (0.0 to 1.0) from an input value (0.0 to 1.0).
Definition: VolumeControl.h:88
Simple simulated audio pot volume control inspired by https://eepower.com/resistor-guide/resistor-typ...
Definition: VolumeControl.h:123
virtual float getVolumeFactor(float volume)
determines a multiplication factor (0.0 to 1.0) from an input value (0.0 to 1.0).
Definition: VolumeControl.h:129
Abstract class for handling of the linear input volume to determine the multiplication factor which s...
Definition: VolumeControl.h:17
virtual float getVolumeFactor(float volume)=0
determines a multiplication factor (0.0 to 1.0) from an input value (0.0 to 1.0).
virtual float limit(float in)
limits the output to the range of 0 to 1.0
Definition: VolumeControl.h:25
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823