VCO.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. VCO.cc
  3. Copyright 2004-7 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. an oversampled triangle/saw/square oscillator, and a combination of two
  6. such oscillators with hard sync.
  7. TODO: optimize for phase clamping like this:
  8. phi -= floor (phi);
  9. */
  10. /*
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  22. 02111-1307, USA or point your web browser to http://www.gnu.org.
  23. */
  24. #include "basics.h"
  25. #include "VCO.h"
  26. #include "Descriptor.h"
  27. void
  28. VCOs::init()
  29. {
  30. /* going a fair bit lower than nominal with fc because the filter
  31. * rolloff is not as steep as we might like it to be. */
  32. double f = .5 * M_PI / OVERSAMPLE;
  33. /* construct the downsampler filter kernel */
  34. DSP::sinc (f, down.c, FIR_SIZE);
  35. DSP::kaiser<DSP::apply_window> (down.c, FIR_SIZE, 6.4);
  36. /* normalize downsampler filter gain */
  37. double s = 0;
  38. for (int i = 0; i < down.n; ++i)
  39. s += down.c[i];
  40. /* scale downsampler kernel */
  41. s = 1 / s;
  42. for (int i = 0; i < down.n; ++i)
  43. down.c[i] *= s;
  44. }
  45. template <sample_func_t F>
  46. void
  47. VCOs::one_cycle (int frames)
  48. {
  49. vco.set_f (getport(0), OVERSAMPLE * fs);
  50. vco.set_saw_square (getport(1), getport(2));
  51. double g = (gain == *ports[3]) ?
  52. 1 : pow (getport(3) / gain, 1. / (double) frames);
  53. sample_t * d = ports[4];
  54. for (int i = 0; i < frames; ++i)
  55. {
  56. F (d, i, gain * down.process (vco.get()), adding_gain);
  57. for (int o = 1; o < OVERSAMPLE; ++o)
  58. down.store (vco.get());
  59. gain *= g;
  60. }
  61. gain = getport(3);
  62. }
  63. /* //////////////////////////////////////////////////////////////////////// */
  64. PortInfo
  65. VCOs::port_info [] =
  66. {
  67. {
  68. "f",
  69. INPUT | CONTROL,
  70. {BOUNDED | LOG | DEFAULT_100, 1, 5751}
  71. }, {
  72. "tri .. saw",
  73. INPUT | CONTROL,
  74. {BOUNDED | DEFAULT_0, 0, 1}
  75. }, {
  76. "~ .. square",
  77. INPUT | CONTROL,
  78. {BOUNDED | DEFAULT_0, 0, 1}
  79. }, {
  80. "volume",
  81. INPUT | CONTROL,
  82. {BOUNDED | DEFAULT_MID, MIN_GAIN, 1}
  83. }, {
  84. "out",
  85. OUTPUT | AUDIO,
  86. {0}
  87. }
  88. };
  89. template <> void
  90. Descriptor<VCOs>::setup()
  91. {
  92. UniqueID = 1783;
  93. Label = "VCOs";
  94. Properties = HARD_RT;
  95. Name = CAPS "VCOs - Virtual 'analogue' oscillator";
  96. Maker = "Tim Goetze <tim@quitte.de>";
  97. Copyright = "GPL, 2004-7";
  98. /* fill port info and vtable */
  99. autogen();
  100. }
  101. /* //////////////////////////////////////////////////////////////////////// */
  102. void
  103. VCOd::init()
  104. {
  105. /* going a fair bit lower than nominal with fc because the filter
  106. * rolloff is not as steep as we might like it to be. */
  107. double f = .5 * M_PI / OVERSAMPLE;
  108. /* construct the downsampler filter kernel */
  109. DSP::sinc (f, down.c, FIR_SIZE);
  110. DSP::kaiser<DSP::apply_window> (down.c, FIR_SIZE, 6.4);
  111. /* normalize downsampler filter gain */
  112. double s = 0;
  113. for (int i = 0; i < down.n; ++i)
  114. s += down.c[i];
  115. /* scale downsampler kernel */
  116. s = 1 / s;
  117. for (int i = 0; i < down.n; ++i)
  118. down.c[i] *= s;
  119. }
  120. template <sample_func_t F>
  121. void
  122. VCOd::one_cycle (int frames)
  123. {
  124. vco.set_f (getport(0), OVERSAMPLE * fs, getport(5));
  125. vco.vco[0].set_saw_square (getport(1), getport(2));
  126. vco.vco[1].set_saw_square (getport(3), getport(4));
  127. vco.set_sync (getport(6));
  128. vco.set_blend (getport(7));
  129. double g = (gain == *ports[8]) ?
  130. 1 : pow (getport(8) / gain, 1. / (double) frames);
  131. sample_t * d = ports[9];
  132. for (int i = 0; i < frames; ++i)
  133. {
  134. F (d, i, gain * down.process (vco.get()), adding_gain);
  135. for (int o = 1; o < OVERSAMPLE; ++o)
  136. down.store (vco.get());
  137. gain *= g;
  138. }
  139. gain = getport(8);
  140. }
  141. /* //////////////////////////////////////////////////////////////////////// */
  142. PortInfo
  143. VCOd::port_info [] =
  144. {
  145. {
  146. "f",
  147. INPUT | CONTROL,
  148. {BOUNDED | LOG | DEFAULT_100, 1, 5751}
  149. }, {
  150. "1: tri .. saw",
  151. INPUT | CONTROL,
  152. {BOUNDED | DEFAULT_0, 0, 1}
  153. }, {
  154. "1: ~ .. square",
  155. INPUT | CONTROL,
  156. {BOUNDED | DEFAULT_0, 0, 1}
  157. }, {
  158. "2: tri .. saw",
  159. INPUT | CONTROL,
  160. {BOUNDED | DEFAULT_0, 0, 1}
  161. }, {
  162. "2: ~ .. square",
  163. INPUT | CONTROL,
  164. {BOUNDED | DEFAULT_0, 0, 1}
  165. }, {
  166. "2: tune",
  167. INPUT | CONTROL,
  168. {BOUNDED | DEFAULT_0, -12, 12}
  169. }, {
  170. "sync",
  171. INPUT | CONTROL,
  172. {BOUNDED | DEFAULT_0, 0, 1}
  173. }, {
  174. "blend",
  175. INPUT | CONTROL,
  176. {BOUNDED | DEFAULT_HIGH, -1, 1}
  177. }, {
  178. "volume",
  179. INPUT | CONTROL,
  180. {BOUNDED | DEFAULT_MID, MIN_GAIN, 1}
  181. }, {
  182. "out",
  183. OUTPUT | AUDIO,
  184. {0}
  185. }
  186. };
  187. template <> void
  188. Descriptor<VCOd>::setup()
  189. {
  190. UniqueID = 1784;
  191. Label = "VCOd";
  192. Properties = HARD_RT;
  193. Name = CAPS "VCOd - Double VCO with detune and hard sync options";
  194. Maker = "Tim Goetze <tim@quitte.de>";
  195. Copyright = "GPL, 2004-7";
  196. /* fill port info and vtable */
  197. autogen();
  198. }