summaryrefslogtreecommitdiff
path: root/plugins/ZamTube/wdfcircuits.h
blob: a7b793369c88033f337e8883091dcd930d2eb964 (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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#ifndef WDFCIRCUITS_H
#define WDFCIRCUITS_H
#include "glue.h"
#include "triode.h"

class TubeStageCircuit {
	/*Tube Preamp*/
public:
	Triode t;
	bool on;

	TubeStageCircuit() {
		reset_tubes();
	}

	void warmup_tubes(void) {
		int i;
		on = false;
		for (i = 0; i < 5000; i++) {
			advanc(0.0);
		}
		on = true;
	}

	void reset_tubes(void) {
		Cia = 0.0;
		Cka = 0.0;
		Coa = 0.0;
		Vg = 0.0;
		Vk = 0.0;
		P3_3Gamma1 = 1.;
		P0_3Gamma1 = 1.;
		S1_3Gamma1 = 1.;
		S3_3Gamma1 = 1.;
		P1_3Gamma1 = 1.;
		St1_3Gamma1 = 1.;
		St2_3Gamma1 = 1.;
		Pt_3Gamma1 = 1.;
		ViE = 0.;
		Rda = 0.;

		S0_3Gamma1 = 1.;
		S2_3Gamma1 = 1.;
		P2_3Gamma1 = 1.;
		E500E = 0.;
	}

	void updateRValues(Real C_Ci, Real C_Ck, Real C_Co, Real E_E500, Real R_E500, Real R_Rg, Real R_Ri, Real R_Rk, Real R_Vi, Real R_Ro, Real sampleRate) {

		Real ViR = R_Vi;
		Real CiR = 1.0 / (2.0*C_Ci*sampleRate);
		Real RiR = R_Ri;
		Real RgR = R_Rg;
		Real RoR = R_Ro;
		Real RkR = R_Rk;
		Real RdR = 2.7e+3;
		Real CkR = 1.0 / (2.0*C_Ck*sampleRate);
		Real E500R = R_E500;
		E500E = E_E500;
		Real CoR = 1.0 / (2.0*C_Co*sampleRate);
		Real S0_3R = (CiR + ViR);
		S0_3Gamma1 = CiR/(CiR + ViR);
		Assert(S0_3Gamma1 >= 0.0 && S0_3Gamma1 <= 1.0);
		Real P0_1R = S0_3R;
		Real P0_2R = RiR;
		Real P0_3R = 1.0 /(1.0 / P0_1R + 1.0 / P0_2R);
		P0_3Gamma1 = 1.0 / P0_1R/(1.0 / P0_1R + 1.0 / P0_2R);
		Assert(P0_3Gamma1 >= 0.0 && P0_3Gamma1 <= 1.0);
		Real S1_3R = (RgR + P0_3R);
		S1_3Gamma1 = RgR/(RgR + P0_3R);
		Assert(S1_3Gamma1 >= 0.0 && S1_3Gamma1 <= 1.0);
		Real P1_1R = CkR;
		Real P1_2R = RkR;
		Real P1_3R = 1.0 /(1.0 / P1_1R + 1.0 / P1_2R);
		P1_3Gamma1 = 1.0 / P1_1R/(1.0 / P1_1R + 1.0 / P1_2R);
		Assert(P1_3Gamma1 >= 0.0 && P1_3Gamma1 <= 1.0);
		Real S3_3R = (RoR + CoR);
		S3_3Gamma1 = RoR/(RoR + CoR);
		Assert(S3_3Gamma1 >= 0.0 && S3_3Gamma1 <= 1.0);
		Real P2_1R = S3_3R;
		Real P2_2R = E500R;
		Real P2_3R = 1.0 /(1.0 / P2_1R + 1.0 / P2_2R);
		P2_3Gamma1 = 1.0 / P2_1R/(1.0 / P2_1R + 1.0 / P2_2R);
		Assert(P2_3Gamma1 >= 0.0 && P2_3Gamma1 <= 1.0);
		Real St1_3R = (RdR + S1_3R);
		St1_3Gamma1 = RdR/(RdR + S1_3R);
		Assert(St1_3Gamma1 >= 0.0 && St1_3Gamma1 <= 1.0);
		Real Pt_1R = St1_3R;
		Real Pt_2R = P1_3R;
		Real Pt_3R = 1.0 /(1.0 / Pt_1R + 1.0 / Pt_2R);
		Pt_3Gamma1 = 1.0 / Pt_1R/(1.0 / Pt_1R + 1.0 / Pt_2R);
		Assert(Pt_3Gamma1 >= 0.0 && Pt_3Gamma1 <= 1.0);
		St2_3Gamma1 = Pt_3R/(Pt_3R + P2_3R);
		Assert(St2_3Gamma1 >= 0.0 && St2_3Gamma1 <= 1.0);
	}

	Real advanc(Real ViE) {
		//Get Bs
		//St2_3GetB
		//Pt_3GetB
		//St1_3GetB
		//RdGetB
		//St1_1SetA
		//S1_3GetB
		//RgGetB
		//S1_1SetA
		//P0_3GetB
		//S0_3GetB
		Real Cib = Cia;
		//S0_1SetA
		//ViGetB
		//S0_2SetA
		Real S0_3b3 = -(Cib + ViE);
		//P0_1SetA
		//RiGetB
		//P0_2SetA
		Real P0_3b3 = -P0_3Gamma1*(-S0_3b3);
		//S1_2SetA
		Real S1_3b3 = -(P0_3b3);
		//St1_2SetA
		Real St1_3b3 = -(S1_3b3);
		//Pt_1SetA
		//P1_3GetB
		Real Ckb = Cka;
		//P1_1SetA
		//RkGetB
		//P1_2SetA
		Real P1_3b3 = -P1_3Gamma1*(-Ckb);
		//Pt_2SetA
		Real Pt_3b3 = P1_3b3 - Pt_3Gamma1*(P1_3b3 - St1_3b3);
		//St2_1SetA
		//P2_3GetB
		//S3_3GetB
		//RoGetB
		//S3_1SetA
		Real Cob = Coa;
		//S3_2SetA
		Real S3_3b3 = -(Cob);
		//P2_1SetA
		//E500GetB
		//P2_2SetA
		Real P2_3b3 = E500E - P2_3Gamma1*(E500E - S3_3b3);
		//St2_2SetA
		Real St2_3b3 = -(Pt_3b3 + P2_3b3);
		//Pt_3GetB
		//St1_3GetB
		//RdGetB
		//St1_1SetA
		//S1_3GetB
		//RgGetB
		//S1_1SetA
		//P0_3GetB
		//S0_3GetB
		//S0_1SetA
		//ViGetB
		//S0_2SetA
		//P0_1SetA
		//RiGetB
		//P0_2SetA
		//S1_2SetA
		//St1_2SetA
		//Call tube model
		Vg = -(Rda);
		Vk = Pt_3b3;
		Real b = t.compute(St2_3b3, St2_3Gamma1, Vg, Vk);
		//Set As
		//St2_3SetA
		Real St2_3b1 = Pt_3b3 - St2_3Gamma1*(Pt_3b3 + P2_3b3 + b);
		//Pt_3SetA
		Real Pt_3b1 = St2_3b1 + P1_3b3 - St1_3b3 - Pt_3Gamma1*(P1_3b3 - St1_3b3);
		//St1_3SetA
		Real St1_3b1 = -St1_3Gamma1*(S1_3b3 + Pt_3b1);
		//RdSetA
		Rda = St1_3b1;
		Real St1_3b2 = Pt_3b1 - St1_3Gamma1*(S1_3b3 + Pt_3b1);
		//S1_3SetA
		//RgSetA
		Real S1_3b2 = St1_3b2 - S1_3Gamma1*(P0_3b3 + St1_3b2);
		//P0_3SetA
		Real P0_3b1 = S1_3b2  - S0_3b3 - P0_3Gamma1*(-S0_3b3);
		//S0_3SetA
		Real S0_3b1 = Cib - S0_3Gamma1*(Cib + ViE + P0_3b1);
		Cia = S0_3b1;
		//RiSetA
		Real Pt_3b2 = St2_3b1 - Pt_3Gamma1*(P1_3b3 - St1_3b3);
		//P1_3SetA
		Real P1_3b1 = Pt_3b2  - Ckb - P1_3Gamma1*(-Ckb);
		Cka = P1_3b1;
		//RkSetA
		Real St2_3b2 = Pt_3b3 + b - St2_3Gamma1*(Pt_3b3 + P2_3b3 + b);
		//P2_3SetA
		Real P2_3b1 = St2_3b2 + E500E - S3_3b3 - P2_3Gamma1*(E500E - S3_3b3);
		//S3_3SetA
		Real S3_3b1 = -S3_3Gamma1*(Cob + P2_3b1);
		//RoSetA
		Real Roa = S3_3b1;
		Real S3_3b2 = P2_3b1 - S3_3Gamma1*(Cob + P2_3b1);
		Coa = S3_3b2;
		//printf("Vk=%f Vg=%f Vp=%f in=%f out=%f\n", Vk,Vg,St2_3b3, ViE,Roa);
		return -(Roa);
	}

private:
	//State variables
	Real Coa;
	Real Cia;
	Real Cka;
	Real Rda;
	Real Vk;
	Real Vg;

	//R values
	Real P3_3Gamma1;
	Real P0_3Gamma1;
	Real S1_3Gamma1;
	Real S3_3Gamma1;
	Real P1_3Gamma1;
	Real ViE;
	Real S0_3Gamma1;
	Real S2_3Gamma1;
	Real P2_3Gamma1;
	Real E500E;
	Real St1_3Gamma1;
	Real St2_3Gamma1;
	Real Pt_3Gamma1;
};

#endif