[go: up one dir, main page]

File: Compress.cc

package info (click to toggle)
caps 0.3.0-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 888 kB
  • ctags: 1,273
  • sloc: cpp: 9,837; ansic: 584; makefile: 62
file content (159 lines) | stat: -rw-r--r-- 3,818 bytes parent folder | download | duplicates (2)
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
/*
	Compress.cc
	
	Copyright 2004-5 Tim Goetze <tim@quitte.de>
	
	http://quitte.de/dsp/

	mono compressor suitable for solo instruments. adaptation of Steve
	Harris' 'sc1' unit, with minor tweaks: table lookup for attack and
	release time to frames mapping replaced with exp. port order changed.
*/
/*
	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., 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"

void
Compress::init (double _fs)
{
	fs = _fs;
}

template <sample_func_t F>
void
Compress::one_cycle (int frames)
{
	d_sample * s = ports[0];

	d_sample range = DSP::db2lin (*ports[1]); 
	d_sample ratio = (*ports[2] - 1) / *ports[2];

	/* sc1 has lookup tables here, and they're only 40 % used (400 ms/1 s). 
	 * thus, sc1's attack/release controls are a bit coarse due to truncation 
	 * error. calling exp() once per cycle doesn't seem too expensive. 
	 *
	 * besides, i have a suspicion that the attack and release parameters
	 * don't work like they should. if they, as the code suggests, control
	 * an exponential envelope fade, pow (.5, n_frames) or something like
	 * that seems more appropriate. so ...
	 *
	 * TODO: check whether these parameters work like they should, try pow()
	 */
	double ga = exp (-1 / (fs * *ports[3])); 
	double gr = exp (-1 / (fs * *ports[4]));

	d_sample threshold = *ports[5];
	d_sample knee = *ports[6];

	d_sample * d = ports[7];

	d_sample knee0 = DSP::db2lin (threshold - knee);
	d_sample knee1 = DSP::db2lin (threshold + knee);

	d_sample ef_a = ga * .25;
	d_sample ef_ai = 1 - ef_a;
	
	for (int i = 0; i < frames; ++i)
	{
		sum += s[i] * s[i];

		if (amp > env)
			env = env * ga + amp * (1 - ga);
		else
			env = env * gr + amp * (1 - gr);

		if ((count++ & 3) == 3)
		{
			amp = rms.process (sum * .25);
			sum = 0;
			
			if (env < knee0)
				gain_t = 1;
			else if (env < knee1)
			{
				float x = -(threshold - knee - DSP::lin2db (env)) / knee;
				gain_t = DSP::db2lin (-knee * ratio * x * x * 0.25f);
			}
			else
				gain_t = DSP::db2lin ((threshold - DSP::lin2db (env)) * ratio);
		}

	  gain = gain * ef_a + gain_t * ef_ai;

		F (d, i, s[i] * gain * range, adding_gain);
	}
}

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

PortInfo
Compress::port_info [] =
{
	{
		"in",
		INPUT | AUDIO,
		{BOUNDED, -1, 1}
	}, {
		"gain (dB)",
		INPUT | CONTROL,
		{BOUNDED | DEFAULT_LOW, 0, 24}
	}, {
		"ratio (1:n)",
		INPUT | CONTROL,
		{BOUNDED | DEFAULT_MIN, 1, 10}
	}, {
		"attack (s)",
		INPUT | CONTROL,
		{BOUNDED | DEFAULT_MIN, .001, 1}
	}, {
		"release (s)",
		INPUT | CONTROL,
		{BOUNDED | DEFAULT_MID, .001, 1}
	}, {
		"threshold (dB)",
		INPUT | CONTROL,
		{BOUNDED | DEFAULT_0, -30, 400}
	}, {
		"knee radius (dB)",
		INPUT | CONTROL,
		{BOUNDED | DEFAULT_LOW, 1, 10}
	}, {
		"out",
		OUTPUT | AUDIO,
		{0}
	}
};

template <> void
Descriptor<Compress>::setup()
{
	UniqueID = 1772;
	Label = "Compress";
	Properties = HARD_RT;

	Name = "CAPS: Compress - Mono compressor";
	Maker = "Tim Goetze <tim@quitte.de>, Steve Harris <steve@plugin.org.uk>";
	Copyright = "GPL, 2004-5";

	/* fill port info and vtable */
	autogen();
}