Amp.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. Amp.h
  3. Copyright 2002-9 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. Oversampled tube amplifier emulation.
  6. */
  7. /*
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. 02111-1307, USA or point your web browser to http://www.gnu.org.
  20. */
  21. #ifndef _AMP_H_
  22. #define _AMP_H_
  23. #include "dsp/util.h"
  24. #include "dsp/OnePole.h"
  25. #include "dsp/BiQuad.h"
  26. #include "dsp/TwelveAX7.h"
  27. #include "dsp/Roessler.h"
  28. #include "dsp/FIR.h"
  29. #include "dsp/sinc.h"
  30. #include "dsp/windows.h"
  31. #include "dsp/RBJ.h"
  32. #include "dsp/Eq.h"
  33. #include "dsp/ToneStack.h"
  34. class AmpStub
  35. : public Plugin
  36. {
  37. public:
  38. DSP::TwelveAX7_3 tube;
  39. sample_t drive, i_drive;
  40. struct {
  41. /* gain (remember current setting and fade to port setting in run) */
  42. double g;
  43. /* should also do this for temperature to remove another potential
  44. * source of zippering, but that would be overkill, at the cost of
  45. * at least one pow() per block. */
  46. } current;
  47. /* input is hipass-filtered first */
  48. DSP::OnePoleHP dc_blocker;
  49. enum {
  50. OVERSAMPLE = 8,
  51. FIR_SIZE = 64,
  52. };
  53. /* antialias filters */
  54. DSP::FIRUpsampler up;
  55. DSP::FIR down;
  56. AmpStub()
  57. : up (FIR_SIZE, OVERSAMPLE),
  58. down (FIR_SIZE, up.c)
  59. { }
  60. void init (bool adjust_downsampler = false);
  61. inline sample_t power_transfer (sample_t a)
  62. {
  63. return i_drive * (a - drive * fabs (a) * a);
  64. }
  65. };
  66. /* /////////////////////////////////////////////////////////////////////// */
  67. class PreampIII
  68. : public AmpStub
  69. {
  70. public:
  71. template <sample_func_t F, int OVERSAMPLE>
  72. void one_cycle (int frames);
  73. DSP::BiQuad filter;
  74. public:
  75. static PortInfo port_info[];
  76. sample_t adding_gain;
  77. void init();
  78. void activate()
  79. {
  80. current.g = 1;
  81. filter.reset();
  82. up.reset();
  83. down.reset();
  84. dc_blocker.reset();
  85. }
  86. void run (int n)
  87. {
  88. one_cycle<store_func, OVERSAMPLE> (n);
  89. }
  90. void run_adding (int n)
  91. {
  92. one_cycle<adding_func, OVERSAMPLE> (n);
  93. }
  94. };
  95. /* /////////////////////////////////////////////////////////////////////// */
  96. class AmpIII
  97. : public AmpStub
  98. {
  99. public:
  100. template <sample_func_t F, int OVERSAMPLE>
  101. void one_cycle (int frames);
  102. DSP::BiQuad filter;
  103. public:
  104. static PortInfo port_info[];
  105. sample_t adding_gain;
  106. void init();
  107. void activate()
  108. {
  109. current.g = 1;
  110. up.reset();
  111. down.reset();
  112. dc_blocker.reset();
  113. filter.reset();
  114. }
  115. void run (int n)
  116. {
  117. one_cycle<store_func, OVERSAMPLE> (n);
  118. }
  119. void run_adding (int n)
  120. {
  121. one_cycle<adding_func, OVERSAMPLE> (n);
  122. }
  123. };
  124. /* /////////////////////////////////////////////////////////////////////// */
  125. typedef struct
  126. {float center, Q, adjust;}
  127. PreampBand;
  128. class ToneControls
  129. {
  130. public:
  131. sample_t eq_gain[4];
  132. DSP::Eq<4> eq;
  133. static PreampBand bands[4];
  134. public:
  135. void init (double _fs);
  136. void activate (sample_t **);
  137. inline void
  138. start_cycle (sample_t ** ports, double one_over_n)
  139. {
  140. for (int i = 0; i < 4; ++i)
  141. {
  142. if (*ports[i] == eq_gain[i])
  143. {
  144. eq.gf[i] = 1;
  145. continue;
  146. }
  147. eq_gain[i] = *ports [i];
  148. double want = get_band_gain (i, eq_gain[i]);
  149. eq.gf[i] = pow (want / eq.gain[i], one_over_n);
  150. }
  151. }
  152. double get_band_gain (int i, double g);
  153. void set_band_gain (int i, float g);
  154. inline sample_t process (sample_t x)
  155. {
  156. return eq.process (x);
  157. }
  158. };
  159. /* /////////////////////////////////////////////////////////////////////// */
  160. class PreampIV
  161. : public PreampIII
  162. {
  163. public:
  164. ToneControls tone;
  165. template <sample_func_t F, int OVERSAMPLE>
  166. void one_cycle (int frames);
  167. public:
  168. static PortInfo port_info[];
  169. sample_t adding_gain;
  170. void init();
  171. void activate();
  172. void run (int n)
  173. {
  174. one_cycle<store_func, OVERSAMPLE> (n);
  175. }
  176. void run_adding (int n)
  177. {
  178. one_cycle<adding_func, OVERSAMPLE> (n);
  179. }
  180. };
  181. /* /////////////////////////////////////////////////////////////////////// */
  182. class AmpIV
  183. : public AmpStub
  184. {
  185. public:
  186. ToneControls tone;
  187. template <sample_func_t F, int OVERSAMPLE>
  188. void one_cycle (int frames);
  189. public:
  190. static PortInfo port_info[];
  191. sample_t adding_gain;
  192. void init();
  193. void activate()
  194. {
  195. current.g = 1;
  196. tone.activate (ports + 3);
  197. up.reset();
  198. down.reset();
  199. dc_blocker.reset();
  200. }
  201. void run (int n)
  202. {
  203. one_cycle<store_func, OVERSAMPLE> (n);
  204. }
  205. void run_adding (int n)
  206. {
  207. one_cycle<adding_func, OVERSAMPLE> (n);
  208. }
  209. };
  210. /* /////////////////////////////////////////////////////////////////////// */
  211. class AmpV
  212. : public AmpStub
  213. {
  214. public:
  215. template <sample_func_t F, int OVERSAMPLE>
  216. void one_cycle (int frames);
  217. DSP::BiQuad filter[3];
  218. sample_t cut, tone;
  219. /* supply voltage sag */
  220. sample_t supply;
  221. DSP::BiQuad power_cap[2];
  222. public:
  223. static PortInfo port_info[];
  224. sample_t adding_gain;
  225. void init();
  226. void activate()
  227. {
  228. current.g = 1;
  229. for (int i = 0; i < 2; ++i)
  230. filter[i].reset(),
  231. power_cap[i].reset();
  232. up.reset();
  233. down.reset();
  234. dc_blocker.reset();
  235. cut = 2;
  236. supply = 0.;
  237. tone = -1; /* causes initialisation of the filter at first cycle */
  238. }
  239. void run (int n)
  240. {
  241. one_cycle<store_func, OVERSAMPLE> (n);
  242. }
  243. void run_adding (int n)
  244. {
  245. one_cycle<adding_func, OVERSAMPLE> (n);
  246. }
  247. };
  248. /* /////////////////////////////////////////////////////////////////////// */
  249. class AmpVTS
  250. : public AmpStub
  251. {
  252. public:
  253. DSP::ToneStack tonestack;
  254. template <sample_func_t F, int OVERSAMPLE>
  255. void one_cycle (int frames);
  256. sample_t cut, tone;
  257. /* supply voltage sag */
  258. sample_t supply;
  259. DSP::BiQuad power_cap[2];
  260. public:
  261. static PortInfo port_info[];
  262. sample_t adding_gain;
  263. void init();
  264. void activate()
  265. {
  266. current.g = 1;
  267. for (int i = 0; i < 2; ++i)
  268. power_cap[i].reset();
  269. up.reset();
  270. down.reset();
  271. dc_blocker.reset();
  272. cut = 2;
  273. supply = 0.;
  274. }
  275. void run (int n)
  276. {
  277. one_cycle<store_func, OVERSAMPLE> (n);
  278. }
  279. void run_adding (int n)
  280. {
  281. one_cycle<adding_func, OVERSAMPLE> (n);
  282. }
  283. };
  284. #endif /* _AMP_H_ */