summaryrefslogtreecommitdiffstats
path: root/arts/modules/synth/synth_compressor_impl.cpp
blob: b27b2be1d9c4726e301e433eef80026c8f437da3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*

   Copyright (C) 2001 Matthias Kretz <kretz@kde.org>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

 */

#include "artsmodulessynth.h"
#include "stdsynthmodule.h"
#include "debug.h"

#include <math.h>
#include <string.h>

#ifndef LN2
# define LN2 0.69314718
#endif

#ifndef MAX
# define MAX(a,b) (((a) > (b) ? (a) : (b)))
#endif

using namespace std;
namespace Arts {

class Synth_COMPRESSOR_impl : virtual public Synth_COMPRESSOR_skel,
							  virtual public StdSynthModule
{
protected:
	float _attack, _release, _threshold, _ratiominus1, _output;
	float _attackfactor, _releasefactor;
	float _volume;
	float _compfactor;
	bool _autooutput;

public:
	float attack() { return _attack; }
	float release() { return _release; }
	float threshold() { return _threshold; }
	float ratio() { return _ratiominus1 + 1.0; }
	float output() { return _output; }

	Synth_COMPRESSOR_impl()
		: _threshold( 1 )
		, _ratiominus1( -0.2 )
		, _output( 0 )
		, _autooutput( true )
	{
		newCompFactor();
		attack( 10 );
		release( 10 );
	}

	void newCompFactor()
	{
		_compfactor = _output / pow( _threshold, _ratiominus1 );
	}

	void streamInit()
	{
		_volume = 0;
	}

	void calculateBlock(unsigned long samples)
	{
		for( unsigned long i = 0; i < samples; i++ ) {
			float delta = fabs( invalue[i] ) - _volume;
			if( delta > 0.0 )
				_volume += _attackfactor * delta;
			else
				_volume += _releasefactor * delta;

			if( _volume > _threshold )
				// compress
				// this is what it does:
				// UtodB(x) = 20 * log( x )
				// dBtoU(x) = pow( 10, x / 20 )
				// outvalue[i] = dBtoU( ( UtodB( volume ) - UtodB( threshold ) ) * ratio + UtodB( threshold ) ) / volume * output * invalue[ i ];
				// showing that it's equal to the formula below
				// is left as an exercise to the reader.
				outvalue[i] = pow( _volume, _ratiominus1 ) * _compfactor * invalue[ i ];
			else
				outvalue[i] = invalue[i] * _output;
		}
	}

	void attack( float newAttack )
	{ // in ms
		_attack = newAttack;
		// _attackfactor has to be <= 1, that's why we need the MAX here
		_attackfactor = LN2 / MAX( _attack / 1000 * samplingRateFloat, LN2 );
		attack_changed( newAttack );
	}

	void release( float newRelease )
	{ // in ms
		_release = newRelease;
		// _releasefactor has to be <= 1, that's why we need the MAX here
		_releasefactor = LN2 / MAX( _release / 1000 * samplingRateFloat, LN2 );
		release_changed( newRelease );
	}

	void threshold( float newThreshold )
	{ // in V not in dB
		_threshold = newThreshold;
		newCompFactor();
		threshold_changed( newThreshold );
	}

	void ratio( float newRatio )
	{
		_ratiominus1 = newRatio - 1;
		newCompFactor();
		ratio_changed( newRatio );
	}

	void output( float newOutput )
	{ // in V not in dB
		_output = newOutput;
		newCompFactor();
		output_changed( newOutput );
	}
};

REGISTER_IMPLEMENTATION(Synth_COMPRESSOR_impl);
}