summaryrefslogtreecommitdiffstats
path: root/arts/modules/synth/synth_moog_vcf_impl.cc
blob: 77bf5394ff8a07ceaddae6a4f6e9435eab8db8e4 (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
/*

	Copyright (C) 2000 Jeff Tranter
			           [email protected]

			  (C) 1999 Stefan Westerfeld
                       [email protected]

    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"

using namespace Arts;

/*
Try this. It's a really nice 4pole. Very Moog like.

in[x] and out[x] are member variables, init to 0.0
the controls:

fc = cutoff, nearly linear [0,1] -> [0, fs/2]
res = resonance [0, 4] -> [no resonance, self-oscillation]
*/

class Synth_MOOG_VCF_impl : virtual public Synth_MOOG_VCF_skel,
							virtual public StdSynthModule
{
protected:
	float _frequency, _resonance;
	double freqcorrect;
	double in1, in2, in3, in4;
	double out1, out2, out3, out4;

public:
	float frequency() { return _frequency; }
	void frequency(float newFrequency) { _frequency = newFrequency; }

	float resonance() { return _resonance; }
	void resonance(float newResonance) { _resonance = newResonance; }

	void streamInit()
	{
		in1 = in2 = in3 = in4 = out1 = out2 = out3 = out4 = 0.0;
	}
	
	void calculateBlock(unsigned long samples)
	{
		freqcorrect = 1.16 / (double)(samplingRate / 2);

		for	(unsigned int i=0; i < samples; i++)
		{
			double input = invalue[i];
			double fc = _frequency;
			double res = _resonance;
			double f = fc * freqcorrect;
			double fb = res * (1.0 - 0.15 * f * f);

			input -= out4 * fb;
			input *= 0.35013 * (f * f) * (f * f);
			
			out1 = input + 0.3 * in1 + (1 - f) * out1; // Pole 1
			in1  = input;
			out2 = out1 + 0.3 * in2 + (1 - f) * out2;  // Pole 2
			in2  = out1;
			out3 = out2 + 0.3 * in3 + (1 - f) * out3;  // Pole 3
			in3  = out2;
			out4 = out3 + 0.3 * in4 + (1 - f) * out4;  // Pole 4
			in4  = out3;
			
			outvalue[i] = out4;
		}
	}

};

REGISTER_IMPLEMENTATION(Synth_MOOG_VCF_impl);