Chorus.cc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. Chorus.cc
  3. Copyright 2004-7 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. mono and mono-to-stereo chorus units.
  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 "Chorus.h"
  23. #include "Descriptor.h"
  24. template <sample_func_t F>
  25. void
  26. ChorusI::one_cycle (int frames)
  27. {
  28. sample_t * s = ports[0];
  29. double one_over_n = 1 / (double) frames;
  30. double ms = .001 * fs;
  31. double t = time;
  32. time = getport(1) * ms;
  33. double dt = (time - t) * one_over_n;
  34. double w = width;
  35. width = getport(2) * ms;
  36. /* clamp, or we need future samples from the delay line */
  37. if (width >= t - 3) width = t - 3;
  38. double dw = (width - w) * one_over_n;
  39. if (rate != *ports[3])
  40. lfo.set_f (max (rate = getport(3), .000001), fs, lfo.get_phase());
  41. double blend = getport(4);
  42. double ff = getport(5);
  43. double fb = getport(6);
  44. sample_t * d = ports[7];
  45. DSP::FPTruncateMode truncate;
  46. for (int i = 0; i < frames; ++i)
  47. {
  48. sample_t x = s[i];
  49. /* truncate the feedback tap to integer, better quality for less
  50. * cycles (just a bit of zipper when changing 't', but it does sound
  51. * interesting) */
  52. int ti;
  53. fistp (t, ti);
  54. x -= fb * delay[ti];
  55. delay.put (x + normal);
  56. # if 0
  57. /* allpass delay sounds a little cleaner for a chorus
  58. * but sucks big time when flanging. */
  59. x = blend * x + ff * tap.get (delay, t + w * lfo.get());
  60. # elif 0
  61. /* linear interpolation */
  62. x = blend * x + ff * delay.get_at (t + w * lfo.get());
  63. # else
  64. /* cubic interpolation */
  65. x = blend * x + ff * delay.get_cubic (t + w * lfo.get());
  66. # endif
  67. F (d, i, x, adding_gain);
  68. t += dt;
  69. w += dw;
  70. }
  71. }
  72. /* //////////////////////////////////////////////////////////////////////// */
  73. PortInfo
  74. ChorusI::port_info [] =
  75. {
  76. {
  77. "in",
  78. INPUT | AUDIO,
  79. {BOUNDED, -1, 1}
  80. }, {
  81. "t (ms)",
  82. INPUT | CONTROL,
  83. {BOUNDED | LOG | DEFAULT_LOW, 2.5, 40}
  84. }, {
  85. "width (ms)",
  86. INPUT | CONTROL,
  87. {BOUNDED | DEFAULT_1, .5, 10}
  88. }, {
  89. "rate (Hz)",
  90. INPUT | CONTROL,
  91. {BOUNDED | DEFAULT_LOW, 0, 5}
  92. }, {
  93. "blend",
  94. INPUT | CONTROL,
  95. {BOUNDED | DEFAULT_1, 0, 1}
  96. }, {
  97. "feedforward",
  98. INPUT | CONTROL,
  99. {BOUNDED | DEFAULT_LOW, 0, 1}
  100. }, {
  101. "feedback",
  102. INPUT | CONTROL,
  103. {BOUNDED | DEFAULT_0, 0, 1}
  104. }, {
  105. "out",
  106. OUTPUT | AUDIO,
  107. {0}
  108. }
  109. };
  110. template <> void
  111. Descriptor<ChorusI>::setup()
  112. {
  113. UniqueID = 1767;
  114. Label = "ChorusI";
  115. Properties = HARD_RT;
  116. Name = CAPS "ChorusI - Mono chorus/flanger";
  117. Maker = "Tim Goetze <tim@quitte.de>";
  118. Copyright = "GPL, 2004-7";
  119. /* fill port info and vtable */
  120. autogen();
  121. }
  122. /* //////////////////////////////////////////////////////////////////////// */
  123. template <sample_func_t F>
  124. void
  125. StereoChorusI::one_cycle (int frames)
  126. {
  127. sample_t * s = ports[0];
  128. double one_over_n = 1 / (double) frames;
  129. double ms = .001 * fs;
  130. double t = time;
  131. time = getport(1) * ms;
  132. double dt = (time - t) * one_over_n;
  133. double w = width;
  134. width = getport(2) * ms;
  135. /* clamp, or we need future samples from the delay line */
  136. if (width >= t - 1) width = t - 1;
  137. double dw = (width - w) * one_over_n;
  138. if (rate != *ports[3] && phase != *ports[4])
  139. {
  140. rate = getport(3);
  141. phase = getport(4);
  142. double phi = left.lfo.get_phase();
  143. left.lfo.set_f (max (rate, .000001), fs, phi);
  144. right.lfo.set_f (max (rate, .000001), fs, phi + phase * M_PI);
  145. }
  146. double blend = getport(5);
  147. double ff = getport(6);
  148. double fb = getport(7);
  149. sample_t * dl = ports[8];
  150. sample_t * dr = ports[9];
  151. /* to go sure (on i386) that the fistp instruction does the right thing
  152. * when looking up fractional sample indices */
  153. DSP::FPTruncateMode truncate;
  154. for (int i = 0; i < frames; ++i)
  155. {
  156. sample_t x = s[i];
  157. /* truncate the feedback tap to integer, better quality for less
  158. * cycles (just a bit of zipper when changing 't', but it does sound
  159. * interesting) */
  160. int ti;
  161. fistp (t, ti);
  162. x -= fb * delay[ti];
  163. delay.put (x + normal);
  164. sample_t l = blend * x + ff * delay.get_cubic (t + w * left.lfo.get());
  165. sample_t r = blend * x + ff * delay.get_cubic (t + w * right.lfo.get());
  166. F (dl, i, l, adding_gain);
  167. F (dr, i, r, adding_gain);
  168. t += dt;
  169. w += dw;
  170. }
  171. }
  172. /* //////////////////////////////////////////////////////////////////////// */
  173. PortInfo
  174. StereoChorusI::port_info [] =
  175. {
  176. {
  177. "in",
  178. INPUT | AUDIO,
  179. {BOUNDED, -1, 1}
  180. }, {
  181. "t (ms)",
  182. INPUT | CONTROL,
  183. {BOUNDED | DEFAULT_MIN, 2.5, 40}
  184. }, {
  185. "width (ms)",
  186. INPUT | CONTROL,
  187. {BOUNDED | DEFAULT_1, .5, 10}
  188. }, {
  189. "rate (Hz)",
  190. INPUT | CONTROL,
  191. {BOUNDED | DEFAULT_LOW, 0, 5}
  192. }, {
  193. "phase",
  194. INPUT | CONTROL,
  195. {BOUNDED | DEFAULT_MAX, 0, 1}
  196. }, {
  197. "blend",
  198. INPUT | CONTROL,
  199. {BOUNDED | DEFAULT_1, 0, 1}
  200. }, {
  201. "feedforward",
  202. INPUT | CONTROL,
  203. {BOUNDED | DEFAULT_LOW, 0, 1}
  204. }, {
  205. "feedback",
  206. INPUT | CONTROL,
  207. {BOUNDED | DEFAULT_0, 0, 1}
  208. }, {
  209. "out:l",
  210. OUTPUT | AUDIO,
  211. {0}
  212. }, {
  213. "out:r",
  214. OUTPUT | AUDIO,
  215. {0}
  216. }
  217. };
  218. template <> void
  219. Descriptor<StereoChorusI>::setup()
  220. {
  221. UniqueID = 1768;
  222. Label = "StereoChorusI";
  223. Properties = HARD_RT;
  224. Name = CAPS "StereoChorusI - Stereo chorus/flanger";
  225. Maker = "Tim Goetze <tim@quitte.de>";
  226. Copyright = "GPL, 2004-7";
  227. /* fill port info and vtable */
  228. autogen();
  229. }
  230. /* //////////////////////////////////////////////////////////////////////// */
  231. template <sample_func_t F>
  232. void
  233. ChorusII::one_cycle (int frames)
  234. {
  235. sample_t * s = ports[0];
  236. double one_over_n = 1 / (double) frames;
  237. double ms = .001 * fs;
  238. double t = time;
  239. time = getport(1) * ms;
  240. double dt = (time - t) * one_over_n;
  241. double w = width;
  242. width = getport(2) * ms;
  243. /* clamp, or we need future samples from the delay line */
  244. if (width >= t - 3) width = t - 3;
  245. double dw = (width - w) * one_over_n;
  246. if (rate != *ports[3])
  247. set_rate (*ports[3]);
  248. double blend = getport(4);
  249. double ff = getport(5);
  250. double fb = getport(6);
  251. sample_t * d = ports[7];
  252. DSP::FPTruncateMode truncate;
  253. for (int i = 0; i < frames; ++i)
  254. {
  255. sample_t x = s[i];
  256. x -= fb * delay.get_cubic (t);
  257. delay.put (filter.process (x + normal));
  258. double a = 0;
  259. for (int j = 0; j < Taps; ++j)
  260. a += taps[j].get (delay, t, w);
  261. x = blend * x + ff * a;
  262. F (d, i, x, adding_gain);
  263. t += dt;
  264. w += dw;
  265. }
  266. }
  267. /* //////////////////////////////////////////////////////////////////////// */
  268. PortInfo
  269. ChorusII::port_info [] =
  270. {
  271. {
  272. "in",
  273. INPUT | AUDIO,
  274. {BOUNDED, -1, 1}
  275. }, {
  276. "t (ms)",
  277. INPUT | CONTROL,
  278. {BOUNDED | LOG | DEFAULT_LOW, 2.5, 40}
  279. }, {
  280. "width (ms)",
  281. INPUT | CONTROL,
  282. {BOUNDED | DEFAULT_1, .5, 10}
  283. }, {
  284. "rate",
  285. INPUT | CONTROL,
  286. {BOUNDED | DEFAULT_LOW, 0, 1}
  287. }, {
  288. "blend",
  289. INPUT | CONTROL,
  290. {BOUNDED | DEFAULT_1, 0, 1}
  291. }, {
  292. "feedforward",
  293. INPUT | CONTROL,
  294. {BOUNDED | DEFAULT_LOW, 0, 1}
  295. }, {
  296. "feedback",
  297. INPUT | CONTROL,
  298. {BOUNDED | DEFAULT_0, 0, 1}
  299. }, {
  300. "out",
  301. OUTPUT | AUDIO,
  302. {0}
  303. }
  304. };
  305. template <> void
  306. Descriptor<ChorusII>::setup()
  307. {
  308. UniqueID = 2583;
  309. Label = "ChorusII";
  310. Properties = HARD_RT;
  311. Name = CAPS "ChorusII - Mono chorus/flanger modulated by a fractal";
  312. Maker = "Tim Goetze <tim@quitte.de>";
  313. Copyright = "GPL, 2004-7";
  314. /* fill port info and vtable */
  315. autogen();
  316. }
  317. /* //////////////////////////////////////////////////////////////////////// */
  318. template <sample_func_t F>
  319. void
  320. StereoChorusII::one_cycle (int frames)
  321. {
  322. sample_t * s = ports[0];
  323. double one_over_n = 1 / (double) frames;
  324. double ms = .001 * fs;
  325. double t = time;
  326. time = getport(1) * ms;
  327. double dt = (time - t) * one_over_n;
  328. double w = width;
  329. width = getport(2) * ms;
  330. /* clamp, or we need future samples from the delay line */
  331. if (width >= t - 1) width = t - 1;
  332. double dw = (width - w) * one_over_n;
  333. set_rate (*ports[3]);
  334. double blend = getport(4);
  335. double ff = getport(5);
  336. double fb = getport(6);
  337. sample_t * dl = ports[7];
  338. sample_t * dr = ports[8];
  339. /* to go sure (on i386) that the fistp instruction does the right thing
  340. * when looking up fractional sample indices */
  341. DSP::FPTruncateMode truncate;
  342. for (int i = 0; i < frames; ++i)
  343. {
  344. sample_t x = s[i];
  345. /* truncate the feedback tap to integer, better quality for less
  346. * cycles (just a bit of zipper when changing 't', but it does sound
  347. * interesting) */
  348. int ti;
  349. fistp (t, ti);
  350. x -= fb * delay[ti];
  351. delay.put (x + normal);
  352. double m;
  353. m = left.lfo_lp.process (left.fractal.get());
  354. sample_t l = blend * x + ff * delay.get_cubic (t + w * m);
  355. m = right.lfo_lp.process (right.fractal.get());
  356. sample_t r = blend * x + ff * delay.get_cubic (t + w * m);
  357. F (dl, i, l, adding_gain);
  358. F (dr, i, r, adding_gain);
  359. t += dt;
  360. w += dw;
  361. }
  362. }
  363. /* //////////////////////////////////////////////////////////////////////// */
  364. PortInfo
  365. StereoChorusII::port_info [] =
  366. {
  367. {
  368. "in",
  369. INPUT | AUDIO,
  370. {BOUNDED, -1, 1}
  371. }, {
  372. "t (ms)",
  373. INPUT | CONTROL,
  374. {BOUNDED | DEFAULT_LOW, 2.5, 40}
  375. }, {
  376. "width (ms)",
  377. INPUT | CONTROL,
  378. {BOUNDED | DEFAULT_LOW, .5, 10}
  379. }, {
  380. "rate",
  381. INPUT | CONTROL,
  382. {BOUNDED | DEFAULT_LOW, 0, 1}
  383. }, {
  384. "blend",
  385. INPUT | CONTROL,
  386. {BOUNDED | DEFAULT_1, 0, 1}
  387. }, {
  388. "feedforward",
  389. INPUT | CONTROL,
  390. {BOUNDED | DEFAULT_MID, 0, 1}
  391. }, {
  392. "feedback",
  393. INPUT | CONTROL,
  394. {BOUNDED | DEFAULT_0, 0, 1}
  395. }, {
  396. "out:l",
  397. OUTPUT | AUDIO,
  398. {0}
  399. }, {
  400. "out:r",
  401. OUTPUT | AUDIO,
  402. {0}
  403. }
  404. };
  405. template <> void
  406. Descriptor<StereoChorusII>::setup()
  407. {
  408. UniqueID = 2584;
  409. Label = "StereoChorusII";
  410. Properties = HARD_RT;
  411. Name = CAPS "StereoChorusII - Stereo chorus/flanger modulated by a fractal";
  412. Maker = "Tim Goetze <tim@quitte.de>";
  413. Copyright = "GPL, 2004-7";
  414. /* fill port info and vtable */
  415. autogen();
  416. }