1 |
persson |
832 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* Copyright (C) 2006 Andreas Persson * |
4 |
|
|
* * |
5 |
|
|
* This program is free software; you can redistribute it and/or modify * |
6 |
|
|
* it under the terms of the GNU General Public License as published by * |
7 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
8 |
|
|
* (at your option) any later version. * |
9 |
|
|
* * |
10 |
|
|
* This program is distributed in the hope that it will be useful, * |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
|
|
* GNU General Public License for more details. * |
14 |
|
|
* * |
15 |
|
|
* You should have received a copy of the GNU General Public License * |
16 |
|
|
* along with this program; if not, write to the Free Software * |
17 |
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * |
18 |
|
|
* MA 02110-1301 USA * |
19 |
|
|
***************************************************************************/ |
20 |
|
|
|
21 |
|
|
#include <cmath> |
22 |
|
|
#include "SmoothVolume.h" |
23 |
|
|
|
24 |
|
|
#include <cstdio> |
25 |
|
|
|
26 |
|
|
namespace LinuxSampler { namespace gig { |
27 |
|
|
|
28 |
|
|
void SmoothVolume::trigger(float volume, float sampleRate) { |
29 |
|
|
float d = 1 / sampleRate; |
30 |
|
|
a1 = exp(-44 * d); |
31 |
|
|
b0 = 1 - a1; |
32 |
|
|
decay = exp(-11 * d); |
33 |
|
|
coeff = 0.33f * d; |
34 |
|
|
this->volume = volume; |
35 |
|
|
moving = false; |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
float SmoothVolume::process() { |
39 |
|
|
if (goal < volume) { |
40 |
|
|
// decreasing |
41 |
|
|
float newVolume = volume > 0.059f ? volume * decay : volume - coeff; |
42 |
|
|
if (newVolume > goal) volume = newVolume; |
43 |
|
|
else { |
44 |
|
|
volume = goal; |
45 |
|
|
moving = false; |
46 |
|
|
} |
47 |
|
|
} else { |
48 |
|
|
// increasing |
49 |
|
|
if (goal - volume > 0.013f) |
50 |
|
|
volume = b0 * goal + a1 * volume; // one-pole LP filter |
51 |
|
|
else { |
52 |
|
|
float newVolume = volume + coeff; |
53 |
|
|
if (newVolume < goal) volume = newVolume; |
54 |
|
|
else { |
55 |
|
|
volume = goal; |
56 |
|
|
moving = false; |
57 |
|
|
} |
58 |
|
|
} |
59 |
|
|
} |
60 |
|
|
return volume; |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
}}; |