File: Noisegate.cc

package info (click to toggle)
caps 0.9.26-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 924 kB
  • sloc: cpp: 10,867; ansic: 1,324; makefile: 78; python: 38
file content (177 lines) | stat: -rw-r--r-- 3,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
/*
	Noisegate.cc
	
	Copyright 2011-16 Tim Goetze <tim@quitte.de>
	
	http://quitte.de/dsp/

	Noise reduction measures

*/
/*
	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 <stdio.h>

#include "Noisegate.h"
#include "Descriptor.h"

void
Noisegate::init()
{
	N = 3*960*fs/48000; /* 60 ms RMS accumulation when open */
	over_N = 1./N;
	hysteresis.threshold = (uint) (.180*fs); /* opening for at least 180 ms */
	gain.quiet = db2lin (-60);
	gain.lp.set_f (120*over_fs);
}

void
Noisegate::activate()
{
	rms.reset();
	remain = 0;
	hysteresis.age = 0;
	gain.current = gain.quiet;
	gain.delta = 0;
	gain.lp.reset();
	f_mains = -1; /* make sure filters are updated when processing */
}

void 
Noisegate::process (sample_t x)
{
	x += normal;
	normal = -normal;
	sample_t y = humfilter[0].process(x);
	y = humfilter[1].process(y);
	rms.store (x - .3*y);
}

void
Noisegate::cycle (uint frames)
{
	static int frame = 0;

	float open = db2lin(getport(0)-10);
	float attack = max(.005*N*getport(1), 2); /* in samples */
	float close = db2lin(getport(2));

	float f = getport(3);
	if (f != f_mains)
	{
		f_mains = f;
		if (!f) /* no mains hum filtering: set filters to identity */
		{
			humfilter[0].unity();
			humfilter[1].unity();
		}
		else
		{
			DSP::RBJ::BP (f_mains*over_fs, 5, humfilter[0]);
			DSP::RBJ::BP (f_mains*over_fs, 1, humfilter[1]);
		}
		humfilter[0].reset();
		humfilter[1].reset();
	}

	sample_t * s = ports[4];
	sample_t * d = ports[5]; 
	bool opennow = false;
	while (frames)
	{
		if (remain == 0)
		{
			remain = N;
			if (opennow)
			{
				remain = (int) attack;
				gain.delta = (1 - gain.current)/remain;
				hysteresis.age = 0;
				opennow = false;
			}
			else if (gain.delta > 0)
				gain.current = 1,
				gain.delta = 0;
			else if (gain.delta < 0) 
				gain.delta = 0;
			else if (gain.current > gain.quiet+.001 && rms.get() < close && hysteresis.age > hysteresis.threshold) 
			{
				//fprintf (stderr, "%.3f < %.3f\n", rms.get(), close);
				gain.delta = (gain.quiet - gain.current)*over_N;
			}
		}

		#define OUT (gain.current+0*gain.get())
		uint i = 0;
		uint n = min (frames, remain);
		//fprintf (stderr, "%d %.3f (%.3f)\n", n, gain.current, gain.delta);
		if (gain.delta > 0 || gain.current == 1) for (  ; i < n; ++i) /* opening or open */
		{
			sample_t a = s[i];
			process(a);
			d[i] = a*gain.get();
		}
		else for (  ; i < n; ++i) /* closed */
		{
			sample_t a = s[i];
			process(a);
			if (fabs(a) < open)
				d[i] = a*gain.get();
			else 
			{
				opennow = true;
				remain = i;
				break;
			}
		}
		
		hysteresis.age += i;
		s += i, d += i;
		frames -= i;
		remain -= i;
		frame += i;
	}
}

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

PortInfo
Noisegate::port_info [] = 
{
	{ "open (dB)", CTRL_IN, {DEFAULT_LOW, -55, 0} }, 
	{ "attack (ms)", CTRL_IN, {DEFAULT_0, 0, 5} }, 
	{ "close (dB)", CTRL_IN, {DEFAULT_LOW, -80, 0} }, 

	/* mains */
	{ "mains (Hz)", CTRL_IN|GROUP, {INTEGER|DEFAULT_MID, 0, 100},
		"{0:'off',50:'global',60:'imperial'}"}, 

	{ "in", INPUT | AUDIO },
	{	"out", OUTPUT | AUDIO }
};

template <> void
Descriptor<Noisegate>::setup()
{
	Label = "Noisegate";
	Name = CAPS "Noisegate - Attenuating hum and noise";
	autogen();
}