arduino-audio-tools
Loading...
Searching...
No Matches
src
AudioTools
CoreAudio
VolumeControl.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
AudioTools/CoreAudio/AudioTypes.h
"
4
10
namespace
audio_tools
{
11
19
class
VolumeControl
{
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
42
class
CachedVolumeControl
:
public
VolumeControl
{
43
public
:
44
CachedVolumeControl
(
VolumeControl
&vc){
45
setVolumeControl
(vc);
46
}
47
48
CachedVolumeControl
(
VolumeControl
*vc){
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
82
class
LogarithmicVolumeControl
:
public
VolumeControl
{
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
106
class
ExponentialVolumeControl
:
public
VolumeControl
{
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
124
class
SimulatedAudioPot
:
public
VolumeControl
{
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
150
class
LinearVolumeControl
:
public
VolumeControl
{
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
170
class
CallbackVolumeControl
:
public
VolumeControl
{
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
AudioTypes.h
audio_tools::CachedVolumeControl
In order to optimize the processing time we cache the last input & factor and recalculate the new fac...
Definition
VolumeControl.h:42
audio_tools::CachedVolumeControl::CachedVolumeControl
CachedVolumeControl(VolumeControl &vc)
Definition
VolumeControl.h:44
audio_tools::CachedVolumeControl::getVolumeFactor
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
audio_tools::CachedVolumeControl::p_vc
VolumeControl * p_vc
Definition
VolumeControl.h:68
audio_tools::CachedVolumeControl::CachedVolumeControl
CachedVolumeControl(VolumeControl *vc)
Definition
VolumeControl.h:48
audio_tools::CachedVolumeControl::in
float in
Definition
VolumeControl.h:69
audio_tools::CachedVolumeControl::setVolumeControl
void setVolumeControl(VolumeControl &vc)
Definition
VolumeControl.h:52
audio_tools::CachedVolumeControl::out
float out
Definition
VolumeControl.h:69
audio_tools::CallbackVolumeControl
Provide the volume function as callback method: This is easy to use e.g together with a lamda functio...
Definition
VolumeControl.h:170
audio_tools::CallbackVolumeControl::getVolumeFactor
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
audio_tools::CallbackVolumeControl::CallbackVolumeControl
CallbackVolumeControl(float(*cb)(float in))
Definition
VolumeControl.h:172
audio_tools::CallbackVolumeControl::callback
float(* callback)(float in)
Definition
VolumeControl.h:180
audio_tools::ExponentialVolumeControl
Simple exponentional volume control using the formula pow(2.0, input) - 1.0;.
Definition
VolumeControl.h:106
audio_tools::ExponentialVolumeControl::getVolumeFactor
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
audio_tools::LinearVolumeControl
The simplest possible implementation of a VolumeControl: The input = output which describes a linear ...
Definition
VolumeControl.h:150
audio_tools::LinearVolumeControl::getVolumeFactor
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
audio_tools::LinearVolumeControl::LinearVolumeControl
LinearVolumeControl(bool allowBoost=false)
Definition
VolumeControl.h:152
audio_tools::LinearVolumeControl::is_limited
bool is_limited
Definition
VolumeControl.h:160
audio_tools::LogarithmicVolumeControl
Parametric Logarithmic volume control. Using the formula pow(b,input) * a - a, where b is b = pow(((1...
Definition
VolumeControl.h:82
audio_tools::LogarithmicVolumeControl::ym
float ym
Definition
VolumeControl.h:97
audio_tools::LogarithmicVolumeControl::getVolumeFactor
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
audio_tools::LogarithmicVolumeControl::LogarithmicVolumeControl
LogarithmicVolumeControl(float ym=0.1)
Definition
VolumeControl.h:84
audio_tools::SimulatedAudioPot
Simple simulated audio pot volume control inspired by https://eepower.com/resistor-guide/resistor-typ...
Definition
VolumeControl.h:124
audio_tools::SimulatedAudioPot::getVolumeFactor
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
audio_tools::SimulatedAudioPot::SimulatedAudioPot
SimulatedAudioPot(float x=0.5, float y=0.1)
Definition
VolumeControl.h:126
audio_tools::SimulatedAudioPot::y
float y
Definition
VolumeControl.h:140
audio_tools::SimulatedAudioPot::x
float x
Definition
VolumeControl.h:140
audio_tools::VolumeControl
Abstract class for handling of the linear input volume to determine the multiplication factor which s...
Definition
VolumeControl.h:19
audio_tools::VolumeControl::getVolumeFactor
virtual float getVolumeFactor(float volume)=0
determines a multiplication factor (0.0 to 1.0) from an input value (0.0 to 1.0).
audio_tools::VolumeControl::limit
virtual float limit(float in)
limits the output to the range of 0 to 1.0
Definition
VolumeControl.h:27
audio_tools
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition
AudioCodecsBase.h:10
audio_tools::writeData
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition
AudioTypes.h:512
Generated by
1.9.8