Chorus.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. Chorus.h
  3. Copyright 2004-5 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. mono and stereo chorus/flanger units, traditional designs and some
  6. differentiated a bit further.
  7. */
  8. /*
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. 02111-1307, USA or point your web browser to http://www.gnu.org.
  21. */
  22. #ifndef _CHORUS_H_
  23. #define _CHORUS_H_
  24. #include "dsp/Sine.h"
  25. #include "dsp/Roessler.h"
  26. #include "dsp/Lorenz.h"
  27. #include "dsp/Delay.h"
  28. #include "dsp/OnePole.h"
  29. #include "dsp/BiQuad.h"
  30. #include "dsp/RBJ.h"
  31. class ChorusStub
  32. : public Plugin
  33. {
  34. public:
  35. sample_t time, width, rate;
  36. };
  37. class ChorusI
  38. : public ChorusStub
  39. {
  40. public:
  41. DSP::Sine lfo;
  42. DSP::Delay delay;
  43. DSP::DelayTapA tap;
  44. template <sample_func_t>
  45. void one_cycle (int frames);
  46. public:
  47. static PortInfo port_info [];
  48. void init()
  49. {
  50. rate = .15;
  51. delay.init ((int) (.040 * fs));
  52. }
  53. void activate()
  54. {
  55. time = 0;
  56. width = 0;
  57. rate = *ports[3];
  58. delay.reset();
  59. tap.reset();
  60. lfo.set_f (rate, fs, 0);
  61. }
  62. void run (int n)
  63. {
  64. one_cycle<store_func> (n);
  65. }
  66. void run_adding (int n)
  67. {
  68. one_cycle<adding_func> (n);
  69. }
  70. };
  71. class StereoChorusI
  72. : public ChorusStub
  73. {
  74. public:
  75. sample_t rate;
  76. sample_t phase;
  77. DSP::Delay delay;
  78. struct {
  79. DSP::Sine lfo;
  80. DSP::DelayTapA tap;
  81. } left, right;
  82. template <sample_func_t>
  83. void one_cycle (int frames);
  84. public:
  85. static PortInfo port_info [];
  86. void init()
  87. {
  88. rate = .15;
  89. phase = .5; /* pi */
  90. delay.init ((int) (.040 * fs));
  91. }
  92. void activate()
  93. {
  94. time = 0;
  95. width = 0;
  96. delay.reset();
  97. left.tap.reset();
  98. right.tap.reset();
  99. left.lfo.set_f (rate, fs, 0);
  100. right.lfo.set_f (rate, fs, phase * M_PI);
  101. }
  102. void run (int n)
  103. {
  104. one_cycle<store_func> (n);
  105. }
  106. void run_adding (int n)
  107. {
  108. one_cycle<adding_func> (n);
  109. }
  110. };
  111. /* //////////////////////////////////////////////////////////////////////// */
  112. #define FRACTAL_RATE 0.02
  113. /* fractally modulated Chorus units */
  114. class FracTap
  115. {
  116. public:
  117. DSP::Lorenz f1;
  118. DSP::Roessler f2;
  119. DSP::OnePoleLP lp;
  120. void init (double fs)
  121. {
  122. lp.set_f (30. / fs);
  123. f1.init (.001, frandom());
  124. f2.init (.001, frandom());
  125. }
  126. void set_rate (sample_t r)
  127. {
  128. f1.set_rate (r * FRACTAL_RATE);
  129. f2.set_rate (3.3 * r * FRACTAL_RATE);
  130. }
  131. /* t = time, w = width, should inline nicely */
  132. sample_t get (DSP::Delay & d, double t, double w)
  133. {
  134. double m = lp.process (f1.get() + .3 * f2.get());
  135. return d.get_cubic (t + w * m);
  136. }
  137. };
  138. class ChorusII
  139. : public ChorusStub
  140. {
  141. public:
  142. enum {
  143. Taps = 1
  144. };
  145. FracTap taps[Taps];
  146. DSP::BiQuad filter;
  147. DSP::Delay delay;
  148. template <sample_func_t>
  149. void one_cycle (int frames);
  150. void set_rate (sample_t r)
  151. {
  152. rate = r;
  153. for (int i = 0; i < Taps; ++i)
  154. {
  155. taps[i].set_rate (rate * (i * FRACTAL_RATE) / Taps);
  156. // fprintf (stderr, "[%d] %.3f\n", i, (rate * (i * FRACTAL_RATE) / Taps));
  157. }
  158. }
  159. public:
  160. static PortInfo port_info [];
  161. void init()
  162. {
  163. delay.init ((int) (.040 * fs));
  164. for (int i = 0; i < Taps; ++i)
  165. taps[i].init (fs);
  166. DSP::RBJ::HiShelve (1000. / fs, 1., 6, filter.a, filter.b);
  167. }
  168. void activate()
  169. {
  170. time = 0;
  171. width = 0;
  172. set_rate (*ports[3]);
  173. delay.reset();
  174. filter.reset();
  175. }
  176. void run (int n)
  177. {
  178. one_cycle<store_func> (n);
  179. }
  180. void run_adding (int n)
  181. {
  182. one_cycle<adding_func> (n);
  183. }
  184. };
  185. class StereoChorusII
  186. : public ChorusStub
  187. {
  188. public:
  189. sample_t rate;
  190. sample_t phase;
  191. DSP::Delay delay;
  192. struct {
  193. DSP::Roessler fractal;
  194. DSP::OnePoleLP lfo_lp;
  195. DSP::DelayTapA tap;
  196. } left, right;
  197. template <sample_func_t>
  198. void one_cycle (int frames);
  199. void set_rate (sample_t r)
  200. {
  201. rate = r;
  202. left.fractal.set_rate (rate * FRACTAL_RATE);
  203. right.fractal.set_rate (rate * FRACTAL_RATE);
  204. left.lfo_lp.set_f (3. / fs);
  205. right.lfo_lp.set_f (3. / fs);
  206. }
  207. public:
  208. static PortInfo port_info [];
  209. sample_t adding_gain;
  210. void init()
  211. {
  212. phase = .5; /* pi */
  213. delay.init ((int) (.040 * fs));
  214. left.fractal.init (.001, frandom());
  215. right.fractal.init (.001, frandom());
  216. }
  217. void activate()
  218. {
  219. time = 0;
  220. width = 0;
  221. delay.reset();
  222. left.tap.reset();
  223. right.tap.reset();
  224. set_rate (*ports[3]);
  225. }
  226. void run (int n)
  227. {
  228. one_cycle<store_func> (n);
  229. }
  230. void run_adding (int n)
  231. {
  232. one_cycle<adding_func> (n);
  233. }
  234. };
  235. #endif /* _CHORUS_H_ */