File: Compress.cc

package info (click to toggle)
caps 0.9.26-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 924 kB
  • sloc: cpp: 10,867; ansic: 1,324; makefile: 78; python: 38
file content (189 lines) | stat: -rw-r--r-- 4,944 bytes parent folder | download
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
	Compress.cc
	
	Copyright 2011-18 Tim Goetze <tim@quitte.de>
	
	http://quitte.de/dsp/
	
*/
/*
	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 3
	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., 59 Temple Place - Suite 330, Boston, MA
	02111-1307, USA or point your web browser to http://www.gnu.org.
*/

#include "basics.h"

#include "Compress.h"
#include "Descriptor.h"

template <int Channels>
void
CompressStub<Channels>::activate()
{
	compress.peak.init(fs,4);
	compress.rms.init(fs,4);
	remain = 0;
}

template <int Channels>
void
CompressStub<Channels>::cycle(uint frames)
{
	int c = getport(0);
	if(c == 0) subcycle<DSP::CompressPeak> (frames, compress.peak);
	else subcycle<DSP::CompressRMS> (frames, compress.rms);
}

/* do-nothing 'saturator' as template for linear instead of saturating mode */
struct NoSat { sample_t process(sample_t x) { return x; } };

template <int Channels>
template <class Comp>
void
CompressStub<Channels>::subcycle(uint frames, Comp & comp)
{
	static NoSat none;

	int s = getport(1);
	if(s == 1) subsubcycle<Comp,CompSat2> 
			(frames, comp, saturate[0].two, saturate[1].two);
	else if(s == 2) subsubcycle<Comp,CompSat4> 
			(frames, comp, saturate[0].four, saturate[1].four);
	#if 0
	else if(s == 3) subsubcycle<Comp,CompSat8> 
			(frames, comp, saturate[0].eight, saturate[1].eight);
	#endif
	else subsubcycle<Comp, NoSat> 
			(frames, comp, none, none);
}

template <int Channels>
template <class Comp, class Sat>
void
CompressStub<Channels>::subsubcycle(uint frames, Comp & comp, Sat & satl, Sat & satr)
{
	comp.set_threshold(pow(getport(2), 1.6));
	sample_t strength = pow(getport(3), 1.4); /* more resolution in lower range */
	comp.set_attack(getport(4));
	comp.set_release(getport(5));
	sample_t gain_out = db2lin(getport(6));

	sample_t * sl = ports[Stereo ?  8 : 8]; /* ;) */
	sample_t * sr = ports[Stereo ?  9 : 8];

	sample_t * dl = ports[Stereo ? 10 : 9];
	sample_t * dr = ports[Stereo ? 11 : 9];

	sample_t state = 1;

	while(frames)
	{
		if(remain == 0)
		{
			remain = comp.blocksize;
			comp.start_block(strength);
			state = min(state,comp.gain.state);
		}

		uint n = min(frames, remain);
		
		for(uint i=0; i<n; ++i)
		{
			sample_t xl = sl[i], xr = sr[i];

			if(Stereo)
				comp.store(xl, xr);
			else
				comp.store(xl);

			sample_t gain = gain_out*comp.get();

			xl = satl.process(xl*gain);
			if(Stereo)
				xr = satr.process(xr*gain);

			dl[i] = xl;
			if(Stereo)
				dr[i] = xr;
		}

		sl += n;
		sr += n;
		dl += n;
		dr += n;
		frames -= n;
		remain -= n;
	}
	*ports[7] = lin2db(state);
}

/* //////////////////////////////////////////////////////////////////////// */

PortInfo
Compress::port_info [] =
{
	{ "measure", CTRL_IN, {INTEGER | DEFAULT_1, 0, 1}, "{0:'peak',1:'rms'}" }, 
	{ "mode", CTRL_IN | GROUP, {INTEGER | DEFAULT_1, 0, 2},
		"{0:'no limiting',1:'saturating 2x',2:'saturating 4x',3:'saturating 4x128'}" }, 
	/* 2 */
	{ "threshold", CTRL_IN | GROUP, {DEFAULT_MID, 0, 1} }, 
	{ "strength", CTRL_IN, {DEFAULT_LOW, 0, 1} }, 
	{ "attack", CTRL_IN | GROUP, {DEFAULT_HIGH, 0, 1} }, 
	{ "release", CTRL_IN, {DEFAULT_MID, 0, 1} }, 
	/* 6 */
	{ "gain (dB)", CTRL_IN | GROUP, {DEFAULT_MID, -12, 36} }, 
	{ "state (dB)", CONTROL|OUTPUT| GROUP, {DEFAULT_0,-144,0} }, 

	{ "in", INPUT | AUDIO, {BOUNDED, -1, 1} }, 
	{ "out", OUTPUT | AUDIO, {0} }
};

template <> void
Descriptor<Compress>::setup()
{
	Label = "Compress";
	Name = CAPS "Compress - Compressor and saturating limiter";
	autogen();
}

/* //////////////////////////////////////////////////////////////////////// */

PortInfo
CompressX2::port_info [] =
{
	{ "measure", CTRL_IN, {INTEGER | DEFAULT_1, 0, 1},
		"{0:'peak',1:'rms'}" }, 
	{ "mode", CTRL_IN | GROUP, {INTEGER | DEFAULT_1, 0, 2},
		"{0:'linear',1:'saturating 2x',2:'saturating 4x',3:'saturating 4x128'}" }, 
	{ "threshold", CTRL_IN | GROUP, {DEFAULT_MID, 0, 1} }, 
	{ "strength", CTRL_IN, {DEFAULT_LOW, 0, 1} }, 
	{ "attack", CTRL_IN | GROUP, {DEFAULT_HIGH, 0, 1} }, 
	{ "release", CTRL_IN, {DEFAULT_MID, 0, 1} }, 
	{ "gain (dB)", CTRL_IN | GROUP, {DEFAULT_MID, -12, 36} }, 
	{ "state (dB)", CONTROL|OUTPUT| GROUP, {DEFAULT_0,-144,0} }, 
	{ "in.l", INPUT | AUDIO },
	{ "in.r", INPUT | AUDIO },
	{ "out.l", OUTPUT | AUDIO },
	{ "out.r", OUTPUT | AUDIO },
};

template <> void
Descriptor<CompressX2>::setup()
{
	Label = "CompressX2";
	Name = CAPS "CompressX2 - Stereo compressor and saturating limiter";
	autogen();
}