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 (fabs(volume-in)<0.01f){
58  return out;
59  }
60  in = volume;
61  out = p_vc->getVolumeFactor(volume);
62  return out;
63  }
64 
65  protected:
66  VolumeControl *p_vc=nullptr;
67  float in=1.0, out=1.0;
68 
69 };
70 
71 
81  public:
82  LogarithmicVolumeControl(float ym=0.1){
83  this->ym = ym;
84  }
85 
86  // provides a factor in the range of 0.0 to 1.0
87  virtual float getVolumeFactor(float input) {
88  float b = powf(((1/ym)-1), 2);
89  float a = 1.0f / (b - 1.0f);
90  float volumeFactor = powf(b,input) * a - a;
91  return limit(volumeFactor);
92  }
93 
94  protected:
95  float ym;
96 };
97 
105  public:
106  // provides a factor in the range of 0.0 to 1.0
107  virtual float getVolumeFactor(float volume) {
108  float volumeFactor = pow(2.0, volume) - 1.0;
109  return limit(volumeFactor);
110  }
111 };
112 
123  public:
124  SimulatedAudioPot(float x=0.5, float y=0.1){
125  this->x = x;
126  this->y = y;
127  }
128  virtual float getVolumeFactor(float volume) {
129  float result = 0;
130  if (volume<=x){
131  result = mapT<float>(volume, 0.0, x, 0, y );
132  } else {
133  result = mapT<float>(volume, x, 1.0, y, 1.0);
134  }
135  return limit(result);
136  }
137  protected:
138  float x, y;
139 };
140 
149  public:
150  LinearVolumeControl(bool allowBoost=false){
151  is_limited = !allowBoost;
152  }
153  // provides a factor in the range of 0.0 to 1.0
154  virtual float getVolumeFactor(float volume) {
155  return is_limited ? limit(volume) : volume;
156  }
157  protected:
158  bool is_limited;
159 };
160 
161 
169  public:
170  CallbackVolumeControl(float(*cb)(float in)){
171  callback = cb;
172  }
173  // provides a factor in the range of 0.0 to 1.0
174  virtual float getVolumeFactor(float volume) {
175  return limit(callback(volume));
176  }
177  protected:
178  float (*callback)(float in) = nullptr;
179 
180 };
181 
182 } // 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:168
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:174
Simple exponentional volume control using the formula pow(2.0, input) - 1.0;.
Definition: VolumeControl.h:104
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:107
The simplest possible implementation of a VolumeControl: The input = output which describes a linear ...
Definition: VolumeControl.h:148
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:154
Parametric Logarithmic volume control. Using the formula pow(b,input) * a - a, where b is b = pow(((1...
Definition: VolumeControl.h:80
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:87
Simple simulated audio pot volume control inspired by https://eepower.com/resistor-guide/resistor-typ...
Definition: VolumeControl.h:122
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:128
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:868