SweepVF.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. SweepVF.cc
  3. Copyright 2002-7 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. SweepVFI, a lorenz fractal modulating the cutoff frequency of a
  6. state-variable (ladder) filter.
  7. SweepVFII, the same with Q being modulated by a second fractal.
  8. */
  9. /*
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  21. 02111-1307, USA or point your web browser to http://www.gnu.org.
  22. */
  23. #include "basics.h"
  24. #include <algorithm>
  25. #include "SweepVF.h"
  26. #include "Descriptor.h"
  27. #include "dsp/RBJ.h"
  28. void
  29. SweepVFI::init()
  30. {
  31. f = .1;
  32. Q = .1;
  33. lorenz.init();
  34. }
  35. void
  36. SweepVFI::activate()
  37. {
  38. svf.reset();
  39. svf.set_f_Q (f = getport(1) / fs, Q = getport(2));
  40. }
  41. template <sample_func_t F>
  42. void
  43. SweepVFI::one_cycle (int frames)
  44. {
  45. sample_t * s = ports[0];
  46. int blocks = frames / BLOCK_SIZE;
  47. if (frames & (BLOCK_SIZE - 1))
  48. ++blocks;
  49. double one_over_blocks = 1 / (double) blocks;
  50. /* cheesy linear interpolation for f, works well though. */
  51. double df = (getport(1) / fs - f) * one_over_blocks;
  52. double dQ = (getport(2) - Q) * one_over_blocks;
  53. svf.set_out ((int) getport(3));
  54. lorenz.set_rate (getport(7));
  55. sample_t * d = ports[8];
  56. while (frames)
  57. {
  58. lorenz.step();
  59. double modulation =
  60. getport(4) * lorenz.get_x() +
  61. getport(5) * lorenz.get_y() +
  62. getport(6) * lorenz.get_z();
  63. double scale = getport(4) + getport(5) + getport(6);
  64. modulation *= scale * f;
  65. svf.set_f_Q (max (.001, f + modulation), Q);
  66. int n = std::min<int> (frames, BLOCK_SIZE);
  67. for (int i = 0; i < n; ++i)
  68. F (d, i, svf.process (s[i] + normal), adding_gain);
  69. s += n;
  70. d += n;
  71. frames -= n;
  72. f += df;
  73. Q += dQ;
  74. }
  75. f = getport(1) / fs;
  76. Q = getport(2);
  77. }
  78. /* //////////////////////////////////////////////////////////////////////// */
  79. PortInfo
  80. SweepVFI::port_info [] =
  81. {
  82. {
  83. "in",
  84. INPUT | AUDIO,
  85. {0, 0, 0}
  86. }, {
  87. "f",
  88. INPUT | CONTROL,
  89. {BOUNDED | LOG | DEFAULT_LOW, 83, 3383}
  90. }, {
  91. "Q",
  92. INPUT | CONTROL,
  93. {BOUNDED | DEFAULT_MID, 0.001, .999}
  94. }, {
  95. "mode",
  96. INPUT | CONTROL,
  97. {BOUNDED | DEFAULT_1 | INTEGER, 0, 1} /* only lo and band make sense */
  98. }, {
  99. "depth:x",
  100. INPUT | CONTROL,
  101. {BOUNDED | DEFAULT_LOW, 0, 1}
  102. }, {
  103. "depth:y",
  104. INPUT | CONTROL,
  105. {BOUNDED | DEFAULT_MID, 0, 1}
  106. }, {
  107. "depth:z",
  108. INPUT | CONTROL,
  109. {BOUNDED | DEFAULT_MAX, 0, 1}
  110. }, {
  111. "h",
  112. INPUT | CONTROL,
  113. {BOUNDED | DEFAULT_LOW, 0.001, 1} /* .039 */
  114. }, {
  115. "out",
  116. OUTPUT | AUDIO,
  117. {0}
  118. }
  119. };
  120. template <> void
  121. Descriptor<SweepVFI>::setup()
  122. {
  123. UniqueID = 1782;
  124. Label = "SweepVFI";
  125. Properties = HARD_RT;
  126. Name = CAPS "SweepVFI - Resonant filter swept by a Lorenz fractal";
  127. Maker = "Tim Goetze <tim@quitte.de>";
  128. Copyright = "GPL, 2004-7";
  129. /* fill port info and vtable */
  130. autogen();
  131. }
  132. /* //////////////////////////////////////////////////////////////////////// */
  133. void
  134. SweepVFII::init()
  135. {
  136. f = .1;
  137. Q = .1;
  138. lorenz1.init();
  139. lorenz2.init();
  140. }
  141. void
  142. SweepVFII::activate()
  143. {
  144. svf.reset();
  145. svf.set_f_Q (f = getport(1) / fs, Q = getport(2));
  146. }
  147. template <sample_func_t F>
  148. void
  149. SweepVFII::one_cycle (int frames)
  150. {
  151. sample_t * s = ports[0];
  152. int blocks = frames / BLOCK_SIZE;
  153. if (frames & (BLOCK_SIZE - 1))
  154. ++blocks;
  155. double one_over_blocks = 1 / (double) blocks;
  156. /* cheesy linear interpolation for f, works well though. */
  157. double df = (getport(1) / fs - f) * one_over_blocks;
  158. double dQ = (getport(2) - Q) * one_over_blocks;
  159. svf.set_out ((int) getport(3));
  160. lorenz1.set_rate (getport(7));
  161. lorenz2.set_rate (getport(11));
  162. sample_t * d = ports[12];
  163. while (frames)
  164. {
  165. /* f modulation */
  166. lorenz1.step();
  167. double modulation1 =
  168. getport(4) * lorenz1.get_x() +
  169. getport(5) * lorenz1.get_y() +
  170. getport(6) * lorenz1.get_z();
  171. double scale1 = getport(4) + getport(5) + getport(6);
  172. modulation1 *= scale1 * f;
  173. /* Q modulation */
  174. lorenz2.step();
  175. double modulation2 =
  176. getport(8) * lorenz2.get_x() +
  177. getport(9) * lorenz2.get_y() +
  178. getport(10) * lorenz2.get_z();
  179. double scale2 = getport(8) + getport(9) + getport(10);
  180. /* enforce Q limit */
  181. double q = Q + (modulation2 * scale2 * Q);
  182. q = min (0.96, max (q, 0));
  183. svf.set_f_Q (max (.001, f + modulation1), q);
  184. int n = std::min<int> (frames, BLOCK_SIZE);
  185. for (int i = 0; i < n; ++i)
  186. F (d, i, svf.process (s[i] + normal), adding_gain);
  187. s += n;
  188. d += n;
  189. frames -= n;
  190. f += df;
  191. Q += dQ;
  192. }
  193. f = getport(1) / fs;
  194. Q = getport(2);
  195. }
  196. /* //////////////////////////////////////////////////////////////////////// */
  197. PortInfo
  198. SweepVFII::port_info [] =
  199. {
  200. {
  201. "in",
  202. INPUT | AUDIO,
  203. {0, 0, 0}
  204. }, {
  205. "f",
  206. INPUT | CONTROL,
  207. {BOUNDED | LOG | DEFAULT_LOW, 83, 3383}
  208. }, {
  209. "Q",
  210. INPUT | CONTROL,
  211. {BOUNDED | DEFAULT_MID, 0.001, .999}
  212. }, {
  213. "mode",
  214. INPUT | CONTROL,
  215. {BOUNDED | DEFAULT_1 | INTEGER, 0, 1} /* only lo and band make sense */
  216. }, {
  217. "f:depth:x",
  218. INPUT | CONTROL,
  219. {BOUNDED | DEFAULT_LOW, 0, 1}
  220. }, {
  221. "f:depth:y",
  222. INPUT | CONTROL,
  223. {BOUNDED | DEFAULT_MID, 0, 1}
  224. }, {
  225. "f:depth:z",
  226. INPUT | CONTROL,
  227. {BOUNDED | DEFAULT_MAX, 0, 1}
  228. }, {
  229. "f:h",
  230. INPUT | CONTROL,
  231. {BOUNDED | DEFAULT_LOW, 0.001, 1} /* .039 */
  232. }, {
  233. "Q:depth:x",
  234. INPUT | CONTROL,
  235. {BOUNDED | DEFAULT_LOW, 0, 1}
  236. }, {
  237. "Q:depth:y",
  238. INPUT | CONTROL,
  239. {BOUNDED | DEFAULT_MID, 0, 1}
  240. }, {
  241. "Q:depth:z",
  242. INPUT | CONTROL,
  243. {BOUNDED | DEFAULT_MAX, 0, 1}
  244. }, {
  245. "Q:h",
  246. INPUT | CONTROL,
  247. {BOUNDED | DEFAULT_LOW, 0.001, 1} /* .039 */
  248. }, {
  249. "out",
  250. OUTPUT | AUDIO,
  251. {0}
  252. }
  253. };
  254. template <> void
  255. Descriptor<SweepVFII>::setup()
  256. {
  257. UniqueID = 2582;
  258. Label = "SweepVFII";
  259. Properties = HARD_RT;
  260. Name = CAPS "SweepVFII - Resonant filter, f and Q swept by a Lorenz fractal";
  261. Maker = "Tim Goetze <tim@quitte.de>";
  262. Copyright = "GPL, 2004-7";
  263. /* fill port info and vtable */
  264. autogen();
  265. }
  266. /* //////////////////////////////////////////////////////////////////////// */
  267. void
  268. AutoWah::init()
  269. {
  270. f = 800 / fs;
  271. Q = .5;
  272. }
  273. void
  274. AutoWah::activate()
  275. {
  276. svf.reset();
  277. svf.set_f_Q (f = getport(1) / fs, Q = getport(2));
  278. svf.set_out (DSP::SVF<1>::Band);
  279. /* hi-passing input for envelope RMS calculation */
  280. hp.set_f (250. / fs);
  281. /* smoothing the envelope at 20 Hz */
  282. DSP::RBJ::LP (20. * BLOCK_SIZE / fs, .6, filter.a, filter.b);
  283. rms.reset();
  284. hp.reset();
  285. filter.reset();
  286. }
  287. template <sample_func_t F>
  288. void
  289. AutoWah::one_cycle (int frames)
  290. {
  291. sample_t * s = ports[0];
  292. int blocks = frames / BLOCK_SIZE;
  293. if (frames & (BLOCK_SIZE - 1))
  294. ++blocks;
  295. double one_over_blocks = 1 / (double) blocks;
  296. /* cheesy linear interpolation for f, works well though. */
  297. double df = (getport(1) / fs - f) * one_over_blocks;
  298. double dQ = (getport(2) - Q) * one_over_blocks;
  299. double scale = getport(3);
  300. sample_t * d = ports[4];
  301. while (frames)
  302. {
  303. double m = rms.rms();
  304. m = filter.process (m + normal);
  305. /* Leaving debug output in your code is cheesy! */
  306. /*
  307. static int _turn = 0;
  308. if (_turn++ % 100 == 0)
  309. fprintf (stderr, "%.4f\n", m);
  310. */
  311. m *= scale * .08;
  312. svf.set_f_Q (max (.001, f + m), Q);
  313. int n = std::min<int> (frames, BLOCK_SIZE);
  314. for (int i = 0; i < n; ++i)
  315. {
  316. sample_t x = s[i] + normal;
  317. /* A stacked SVF in bandpass mode is rather quiet, which is
  318. * compensated here */
  319. F (d, i, 2 * svf.process (x), adding_gain);
  320. /* for envelope calculation, prefer high f content */
  321. x = hp.process (x);
  322. rms.store (x * x);
  323. }
  324. s += n;
  325. d += n;
  326. frames -= n;
  327. f += df;
  328. Q += dQ;
  329. normal = -normal;
  330. }
  331. f = getport(1) / fs;
  332. Q = getport(2);
  333. }
  334. /* //////////////////////////////////////////////////////////////////////// */
  335. PortInfo
  336. AutoWah::port_info [] =
  337. {
  338. {
  339. "in",
  340. INPUT | AUDIO,
  341. {0, 0, 0}
  342. }, {
  343. "f",
  344. INPUT | CONTROL,
  345. {BOUNDED | LOG | DEFAULT_LOW, 43, 933}
  346. }, {
  347. "Q",
  348. INPUT | CONTROL,
  349. {BOUNDED | DEFAULT_LOW, 0.001, .999}
  350. }, {
  351. "depth",
  352. INPUT | CONTROL,
  353. {BOUNDED | DEFAULT_MID, 0, 1}
  354. }, {
  355. "out",
  356. OUTPUT | AUDIO,
  357. {0}
  358. }
  359. };
  360. template <> void
  361. Descriptor<AutoWah>::setup()
  362. {
  363. UniqueID = 2593;
  364. Label = "AutoWah";
  365. Properties = HARD_RT;
  366. Name = CAPS "AutoWah - Resonant envelope-following filter";
  367. Maker = "Tim Goetze <tim@quitte.de>";
  368. Copyright = "GPL, 2004-7";
  369. /* fill port info and vtable */
  370. autogen();
  371. }