Preamp.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Preamp.cc
  3. Copyright 2003-7 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. Loosely 12AX7-based tube preamp model with 8x oversampling.
  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. #include "basics.h"
  22. #include <stdio.h>
  23. #include "Amp.h"
  24. #include "Descriptor.h"
  25. void
  26. PreampIII::init()
  27. {
  28. this->AmpStub::init();
  29. DSP::RBJ::LoShelve (200 / fs, .2, -6, filter.a, filter.b);
  30. }
  31. template <sample_func_t F, int OVERSAMPLE>
  32. void
  33. PreampIII::one_cycle (int frames)
  34. {
  35. sample_t * s = ports[0];
  36. sample_t gain = getport(1);
  37. sample_t temp = getport(2) * tube.scale;
  38. sample_t * d = ports[3];
  39. *ports[4] = OVERSAMPLE;
  40. double g = current.g;
  41. current.g = max (gain < 1 ? gain : exp2 (gain - 1), .000001);
  42. /* correction for attenuated first transfer */
  43. current.g *= tube.scale / fabs (tube.transfer (temp));
  44. if (g == 0) g = current.g;
  45. /* recursive fade to prevent zipper noise from the 'gain' knob */
  46. double one_over_n = frames > 0 ? 1. / frames : 1;
  47. double gf = pow (current.g / g, one_over_n);
  48. for (int i = 0; i < frames; ++i)
  49. {
  50. register sample_t a = s[i] + normal;
  51. a = g * tube.transfer (a * temp);
  52. a = filter.process (a);
  53. a = down.process (tube.transfer_clip (up.upsample (a)));
  54. for (int o = 1; o < OVERSAMPLE; ++o)
  55. down.store (tube.transfer_clip (up.pad (o)));
  56. F (d, i, dc_blocker.process (a), adding_gain);
  57. g *= gf;
  58. }
  59. current.g = g;
  60. }
  61. /* //////////////////////////////////////////////////////////////////////// */
  62. PortInfo
  63. PreampIII::port_info [] =
  64. {
  65. {
  66. "in",
  67. INPUT | AUDIO,
  68. {BOUNDED, -1, 1}
  69. }, {
  70. "gain",
  71. INPUT | CONTROL,
  72. {BOUNDED | DEFAULT_1, 0, 10}
  73. }, {
  74. "temperature",
  75. INPUT | CONTROL,
  76. {BOUNDED | DEFAULT_LOW, 0.005, 1}
  77. }, {
  78. "out",
  79. OUTPUT | AUDIO,
  80. {0}
  81. }, {
  82. "latency",
  83. OUTPUT | CONTROL,
  84. {0}
  85. }
  86. };
  87. template <> void
  88. Descriptor<PreampIII>::setup()
  89. {
  90. UniqueID = 1776;
  91. Label = "PreampIII";
  92. Properties = HARD_RT;
  93. Name = CAPS "PreampIII - Tube preamp emulation";
  94. Maker = "Tim Goetze <tim@quitte.de>";
  95. Copyright = "GPL, 2002-7";
  96. /* fill port info and vtable */
  97. autogen();
  98. }
  99. /* //////////////////////////////////////////////////////////////////////// */
  100. void
  101. PreampIV::init()
  102. {
  103. this->AmpStub::init();
  104. tone.init (fs);
  105. }
  106. void
  107. PreampIV::activate()
  108. {
  109. this->PreampIII::activate();
  110. tone.activate (ports + 3);
  111. }
  112. template <sample_func_t F, int OVERSAMPLE>
  113. void
  114. PreampIV::one_cycle (int frames)
  115. {
  116. double one_over_n = frames > 0 ? 1. / frames : 1;
  117. sample_t * s = ports[0];
  118. sample_t gain = getport(1);
  119. sample_t temp = getport(2) * tube.scale;
  120. tone.start_cycle (ports + 3, one_over_n);
  121. sample_t * d = ports[7];
  122. *ports[8] = OVERSAMPLE;
  123. double g = current.g;
  124. current.g = max (gain < 1 ? gain : exp2 (gain - 1), .000001);
  125. current.g *= tube.scale / fabs (tube.transfer (temp));
  126. if (g == 0) g = current.g;
  127. /* recursive fade to prevent zipper noise from the 'gain' knob */
  128. double gf = pow (current.g / g, one_over_n);
  129. for (int i = 0; i < frames; ++i)
  130. {
  131. register sample_t a = tone.process (s[i] + normal);
  132. a = g * tube.transfer (a * temp);
  133. a = down.process (tube.transfer_clip (up.upsample (a)));
  134. for (int o = 1; o < OVERSAMPLE; ++o)
  135. down.store (tube.transfer_clip (up.pad (o)));
  136. F (d, i, dc_blocker.process (a), adding_gain);
  137. g *= gf;
  138. }
  139. current.g = g;
  140. }
  141. /* //////////////////////////////////////////////////////////////////////// */
  142. PortInfo
  143. PreampIV::port_info [] =
  144. {
  145. {
  146. "in",
  147. INPUT | AUDIO,
  148. {BOUNDED, -1, 1}
  149. }, {
  150. "gain",
  151. INPUT | CONTROL,
  152. {BOUNDED | DEFAULT_1, 0, 10}
  153. }, {
  154. "temperature",
  155. INPUT | CONTROL,
  156. {BOUNDED | DEFAULT_MID, 0.005, 1}
  157. }, {
  158. "bass",
  159. INPUT | CONTROL,
  160. {BOUNDED | DEFAULT_0, -20, 20}
  161. }, {
  162. "mid",
  163. INPUT | CONTROL,
  164. {BOUNDED | DEFAULT_0, -20, 20}
  165. }, {
  166. "treble",
  167. INPUT | CONTROL,
  168. {BOUNDED | DEFAULT_0, -20, 20}
  169. }, {
  170. "hi",
  171. INPUT | CONTROL,
  172. {BOUNDED | DEFAULT_0, -20, 20}
  173. }, {
  174. "out",
  175. OUTPUT | AUDIO,
  176. {0}
  177. }, {
  178. "latency",
  179. OUTPUT | CONTROL,
  180. {0}
  181. }
  182. };
  183. template <> void
  184. Descriptor<PreampIV>::setup()
  185. {
  186. UniqueID = 1777;
  187. Label = "PreampIV";
  188. Properties = HARD_RT;
  189. Name = CAPS "PreampIV - Tube preamp emulation + tone controls";
  190. Maker = "Tim Goetze <tim@quitte.de>";
  191. Copyright = "GPL, 2002-7";
  192. /* fill port info and vtable */
  193. autogen();
  194. }