File: AutoFilter.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 (176 lines) | stat: -rw-r--r-- 3,935 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
/*
	AutoFilter.cc
	
	Copyright 2002-18 Tim Goetze <tim@quitte.de>
	
	http://quitte.de/dsp/

	AutoFilter, a Lorenz fractal modulating the cutoff frequency of a 
	state-variable (ladder) filter.

	AutoFilter, RMS envelope modulating a SVF bandpass filter.

*/
/*
	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 "AutoFilter.h"
#include "Descriptor.h"

#include "dsp/RBJ.h"
#include "dsp/polynomials.h"

inline sample_t sq (sample_t x) { return x * x; }

void
AutoFilter::init()
{
	blocksize = (uint) (fs/1200); /* samples to process with constant f and Q */
	f = .1;
	Q = .1;
	lorenz.init();

	/* for envelope RMS calculation */
	hp.set_f (250/fs);
	DSP::RBJ::LP (.001, .5, smoothenv);
}

void 
AutoFilter::activate()
{ 
	f=getport(2)/fs, Q=getport(3);

	svf1.reset();
	svf1.set_f_Q (f,Q);
	svf2.reset();
	svf2.set_f_Q (f,Q);

	rms.reset();
	hp.reset();
	smoothenv.reset();
}

void
AutoFilter::cycle (uint frames)
{
	div_t qr = div((int) frames, (int) blocksize);
	int blocks = qr.quot;
	if (qr.rem) ++blocks;
	double over_blocks = 1./blocks;

	svf1.set_out ((int) getport(0));
	svf2.set_out ((int) getport(0));

	int svf = 1 + (int) getport(1);

	/* f,Q sweep */
	double df = (getport(2)/fs - f) * over_blocks;
	double dQ = (getport(3) - Q) * over_blocks;

	float range = getport(4);
	float env = getport(5);

	lorenz.set_rate (2.268e-05*fs*.3*sq(getport(6)));
	float x = getport(7), z = 1-x;

	sample_t * s = ports[8];
	sample_t * d = ports[9];

	while (frames)
	{
		lorenz.step();

		double fmod = x*lorenz.get_x() + z*lorenz.get_z();
		double fenv = smoothenv.process (rms.get()+normal);
		fenv = 64*fenv*fenv;

		#if 0 
		static int _turn = 0;
		if (_turn++ % 100 == 0)
			fprintf (stderr, "%.4f %.4f\n", fmod, env);
		#endif
		fmod = fmod*(1-env) + fenv*env;
		fmod *= range;

		fmod = max (.001, f*(1 + fmod));
		uint n = min (frames, blocksize);
		
		/* envelope */
		for (uint i = 0; i < n; ++i)
		{
			sample_t x = hp.process (s[i]);
			rms.store (x*x);
		}

		/* filter it */
		if (svf == 1)
		{
			svf1.set_f_Q(fmod, Q);
			double g = 1.8;
			for (uint i = 0; i < n; ++i)
				d[i] = svf1.process<DSP::Polynomial::tanh>(s[i]+normal,g);
		}
		else if (svf == 2)
		{
			svf2.set_f_Q(fmod, Q);
			double g = .84*(1-Q) + .21;
			for (uint i = 0; i < n; ++i)
				d[i] = svf2.process<DSP::Polynomial::tanh>(s[i]+normal,g);
		}

		s += n;
		d += n;
		frames -= n;

		f += df;
		Q += dQ;
	}
}

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

PortInfo
AutoFilter::port_info [] =
{
	{ "mode", CTRL_IN, {DEFAULT_1 | INTEGER, 0, 1},
		"{0:'low pass',1:'band pass',}" },
	{ "filter", CTRL_IN | GROUP, {DEFAULT_1 | INTEGER, 0, 1}, 
		"{0:'breathy',1:'fat'}", }, 
	/* 2 */
	{ "f (Hz)", CTRL_IN | GROUP, {LOG | DEFAULT_HIGH, 20, 3800} }, 
	{ "Q", CTRL_IN, {DEFAULT_LOW, 0, 1} }, 
	/* 4 */
	{ "depth", CTRL_IN | GROUP, {DEFAULT_1, 0, 1} }, 
	{ "lfo/env", CTRL_IN, {DEFAULT_LOW, 0, 1} }, 
	{ "rate", CTRL_IN | GROUP, {DEFAULT_LOW, 0, 1} }, 
	{ "shape", CTRL_IN, {DEFAULT_1, 0, 1} }, 

	/* 8 */
	{ "in", AUDIO_IN}, 
	{	"out", AUDIO_OUT}
};

template <> void
Descriptor<AutoFilter>::setup()
{
	Label = "AutoFilter";
	Name = CAPS "AutoFilter - Self-modulating resonant filter";
	autogen();
}