LMMS
Loading...
Searching...
No Matches
TwelveAX7.h
Go to the documentation of this file.
1/*
2 dsp/TwelveAX7.h
3
4 Copyright 2003-6 Tim Goetze <tim@quitte.de>
5
6 http://quitte.de/dsp/
7
8 collection of approximations of the 12AX7 voltage transfer function
9*/
10/*
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA or point your web browser to http://www.gnu.org.
25*/
26
27#ifndef _DSP_TWELVE_AX_7_H_
28#define _DSP_TWELVE_AX_7_H_
29
30namespace DSP {
31
32#include "r12ax7.h"
33
35
36/* this is the original tube model from caps < 0.1.9 or preamp.so, put
37 * back into use in 0.1.11; the replacement (below) is too strong in
38 * odd-order harmonics at the expense of even-order. it has to sound
39 * good: it took a good deal of fiddling to get the coefficients right.
40 */
42{
43 public:
45
46 struct {
48 } clip[2];
49
50 /* amplitude at which clipping starts */
52
53 public:
55 {
56 /* transfer polynomial parameters */
57 b = -0.79618574210627535;
58 c = -0.21108555430962023;
59 d = +0.38944033523200522;
60
61 set_clips();
62
63 scale = min (fabs (clip[0].threshold), fabs (clip[1].threshold));
64 }
65
67 {
68 return a * (b + a * (c + a * d));
69 }
70
72 {
73 if (a <= clip[0].threshold)
74 return clip[0].value;
75 if (a >= clip[1].threshold)
76 return clip[1].value;
77 return transfer (a);
78 }
79
80 inline double get_root (double sign)
81 {
82 /* only once, no need to optimize */
83 return
84 (-2*c + sign * sqrt ((2*c) * (2*c) - 4 * (3 * d * b))) / (6 * d);
85 }
86
87 inline void set_clips()
88 {
89 /* find 0 crossings in the derived, this is where we'll clip */
90 double x0 = get_root (-1);
91 double x1 = get_root (+1);
92
93 clip[0].value = transfer (x0);
94 clip[1].value = transfer (x1);
95
96 clip[0].threshold = x0;
97 clip[1].threshold = x1;
98 }
99};
100
101/* reworked model. higher order (than 3) polynomials make little sense;
102 * sonically the difference is minim, and the cycle count increases
103 * dramatically.
104 */
106{
107 public:
109
110 struct {
112 } clip[2];
113
115
116 public:
118 {
119 /* transfer polynomial parameters, made with gnuplot::fit() */
120 b = -1.08150605597883;
121 c = -0.262760944760536;
122 d = 0.445770802765903;
123
124 static double x[2] = {-.52, +.98};
125
126 for (int i = 0; i < 2; ++i)
127 clip[i].threshold = x[i],
128 clip[i].value = transfer (x[i]);
129
130 scale = min (fabs (clip[0].threshold), fabs (clip[1].threshold));
131 }
132
134 {
135 return a * (b + a * (c + a * d));
136 }
137
139 {
140 if (a <= clip[0].threshold)
141 return clip[0].value;
142 if (a >= clip[1].threshold)
143 return clip[1].value;
144 return transfer (a);
145 }
146};
147
148/* third model relies on linear interpolation based on the transfer function
149 * as calculated from a spice model.
150 */
152{
153 public:
155
156 struct {
158 } clip[2];
159
161
162 public:
164 {
165 static double x[2] =
166 {
167 (double) r12AX7::Zero /
168 ((double) r12AX7::Samples - (double) r12AX7::Zero),
169 1
170 };
171
172 for (int i = 0; i < 2; ++i)
173 clip[i].threshold = x[i],
174 clip[i].value = transfer (x[i]);
175
176 scale = min (fabs (clip[0].threshold), fabs (clip[1].threshold));
177 }
178
180 {
182 if (a <= 0)
183 return r12AX7::v2v[0];
184 if (a >= r12AX7::Samples - 1)
185 return r12AX7::v2v [r12AX7::Samples - 1];
186
187 /* linear interpolation from sampled function */
188 int i = lrintf (a);
189 a -= i;
190
191 return (r12AX7::v2v [i] * (1.f - a) + r12AX7::v2v [i + 1] * a);
192 }
193
195 {
196 return transfer (a);
197 }
198};
199
200/* experimental */
202{
203 public:
204 struct {
206 } clip[2];
207
208 /* amplitude at which clipping starts */
210
211 public:
213 {
214 static double x[2] = { -1, 1 };
215
216 for (int i = 0; i < 2; ++i)
217 clip[i].threshold = x[i],
218 clip[i].value = transfer (x[i]);
219
220 scale = min (fabs (clip[0].threshold), fabs (clip[1].threshold));
221 }
222
224 {
225 return 0.5469181606780 * (pow (1 - a, 1.5) - 1);
226 }
227
229 {
230 if (a <= clip[0].threshold)
231 return clip[0].value;
232 if (a >= clip[1].threshold)
233 return clip[1].value;
234 return transfer (a);
235 }
236};
237
238} /* namespace DSP */
239
240#endif /* _DSP_TWELVE_AX_7_H_ */
uint8_t a
Definition Spc_Cpu.h:141
LADSPA_Data sample_t
Definition basics.h:100
tube_sample transfer(tube_sample a)
Definition TwelveAX7.h:223
tube_sample transfer_clip(tube_sample a)
Definition TwelveAX7.h:228
struct DSP::NoTwelveAX7::@317055370262022370354213245210361261212045306166 clip[2]
tube_sample threshold
Definition TwelveAX7.h:205
tube_sample scale
Definition TwelveAX7.h:209
tube_sample value
Definition TwelveAX7.h:205
NoTwelveAX7()
Definition TwelveAX7.h:212
TwelveAX7_2()
Definition TwelveAX7.h:117
tube_sample c
Definition TwelveAX7.h:108
tube_sample transfer_clip(tube_sample a)
Definition TwelveAX7.h:138
tube_sample threshold
Definition TwelveAX7.h:111
tube_sample d
Definition TwelveAX7.h:108
struct DSP::TwelveAX7_2::@226344054146327112016300121334175045216143311032 clip[2]
tube_sample transfer(tube_sample a)
Definition TwelveAX7.h:133
tube_sample value
Definition TwelveAX7.h:111
tube_sample scale
Definition TwelveAX7.h:114
tube_sample b
Definition TwelveAX7.h:108
tube_sample scale
Definition TwelveAX7.h:160
tube_sample threshold
Definition TwelveAX7.h:157
tube_sample value
Definition TwelveAX7.h:157
tube_sample transfer(tube_sample a)
Definition TwelveAX7.h:179
tube_sample d
Definition TwelveAX7.h:154
tube_sample b
Definition TwelveAX7.h:154
tube_sample transfer_clip(tube_sample a)
Definition TwelveAX7.h:194
struct DSP::TwelveAX7_3::@032047042260325216176065067160043074156105274344 clip[2]
TwelveAX7_3()
Definition TwelveAX7.h:163
tube_sample c
Definition TwelveAX7.h:154
tube_sample c
Definition TwelveAX7.h:44
void set_clips()
Definition TwelveAX7.h:87
double get_root(double sign)
Definition TwelveAX7.h:80
tube_sample transfer(tube_sample a)
Definition TwelveAX7.h:66
TwelveAX7()
Definition TwelveAX7.h:54
tube_sample value
Definition TwelveAX7.h:47
tube_sample scale
Definition TwelveAX7.h:51
tube_sample threshold
Definition TwelveAX7.h:47
tube_sample transfer_clip(tube_sample a)
Definition TwelveAX7.h:71
tube_sample b
Definition TwelveAX7.h:44
struct DSP::TwelveAX7::@163360176143027134115340227210227057110366104040 clip[2]
tube_sample d
Definition TwelveAX7.h:44
register unsigned i
Definition inflate.c:1575
unsigned x[BMAX+1]
Definition inflate.c:1586
long int lrintf(float x)
@ Samples
Definition TwelveAX7.h:36
@ Zero
Definition TwelveAX7.h:36
static float v2v[]
Definition TwelveAX7.h:38
Definition BiQuad.h:31
sample_t tube_sample
Definition TwelveAX7.h:34
#define min(x, y)
Definition os.h:74