arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
VolumeControl.h
1#pragma once
2
3#include "AudioTools/CoreAudio/AudioTypes.h"
4
10namespace audio_tools {
11
20 public:
22 virtual float getVolumeFactor(float volume) = 0;
23
24 protected:
25
27 virtual float limit(float in){
28 float result = in;
29 if (result<0.0f) result = 0;
30 if (result>1.0f) result = 1;
31 return result;
32 }
33
34};
35
43 public:
45 setVolumeControl(vc);
46 }
47
49 if (vc!=nullptr) setVolumeControl(*vc);
50 }
51
52 void setVolumeControl(VolumeControl &vc){
53 p_vc = &vc;
54 }
55
57 virtual float getVolumeFactor(float volume) {
58 if (p_vc==nullptr) return 1.0f; // prevent NPE
59 if (fabs(volume-in)<0.01f){
60 return out;
61 }
62 in = volume;
63 out = p_vc->getVolumeFactor(volume);
64 return out;
65 }
66
67 protected:
68 VolumeControl *p_vc=nullptr;
69 float in=1.0, out=1.0;
70
71};
72
73
83 public:
84 LogarithmicVolumeControl(float ym=0.1){
85 this->ym = ym;
86 }
87
88 // provides a factor in the range of 0.0 to 1.0
89 virtual float getVolumeFactor(float input) {
90 float b = powf(((1/ym)-1), 2);
91 float a = 1.0f / (b - 1.0f);
92 float volumeFactor = powf(b,input) * a - a;
93 return limit(volumeFactor);
94 }
95
96 protected:
97 float ym;
98};
99
107 public:
108 // provides a factor in the range of 0.0 to 1.0
109 virtual float getVolumeFactor(float volume) {
110 float volumeFactor = pow(2.0, volume) - 1.0;
111 return limit(volumeFactor);
112 }
113};
114
125 public:
126 SimulatedAudioPot(float x=0.5, float y=0.1){
127 this->x = x;
128 this->y = y;
129 }
130 virtual float getVolumeFactor(float volume) {
131 float result = 0;
132 if (volume<=x){
133 result = mapT<float>(volume, 0.0, x, 0, y );
134 } else {
135 result = mapT<float>(volume, x, 1.0, y, 1.0);
136 }
137 return limit(result);
138 }
139 protected:
140 float x, y;
141};
142
151 public:
152 LinearVolumeControl(bool allowBoost=false){
153 is_limited = !allowBoost;
154 }
155 // provides a factor in the range of 0.0 to 1.0
156 virtual float getVolumeFactor(float volume) {
157 return is_limited ? limit(volume) : volume;
158 }
159 protected:
160 bool is_limited;
161};
162
163
171 public:
172 CallbackVolumeControl(float(*cb)(float in)){
173 callback = cb;
174 }
175 // provides a factor in the range of 0.0 to 1.0
176 virtual float getVolumeFactor(float volume) {
177 return limit(callback(volume));
178 }
179 protected:
180 float (*callback)(float in) = nullptr;
181
182};
183
184} // namespace
In order to optimize the processing time we cache the last input & factor and recalculate the new fac...
Definition VolumeControl.h:42
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:57
Provide the volume function as callback method: This is easy to use e.g together with a lamda functio...
Definition VolumeControl.h:170
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:176
Simple exponentional volume control using the formula pow(2.0, input) - 1.0;.
Definition VolumeControl.h:106
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:109
The simplest possible implementation of a VolumeControl: The input = output which describes a linear ...
Definition VolumeControl.h:150
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:156
Parametric Logarithmic volume control. Using the formula pow(b,input) * a - a, where b is b = pow(((1...
Definition VolumeControl.h:82
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:89
Simple simulated audio pot volume control inspired by https://eepower.com/resistor-guide/resistor-typ...
Definition VolumeControl.h:124
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:130
Abstract class for handling of the linear input volume to determine the multiplication factor which s...
Definition VolumeControl.h:19
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:27
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10