DPF with Max Gen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
11KB

  1. #include "gen_exported.h"
  2. namespace gen_exported {
  3. /*******************************************************************************************************************
  4. Copyright (c) 2012 Cycling '74
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  6. and associated documentation files (the "Software"), to deal in the Software without restriction,
  7. including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all copies
  11. or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  14. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  15. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  16. OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. *******************************************************************************************************************/
  18. // global noise generator
  19. Noise noise;
  20. static const int GENLIB_LOOPCOUNT_BAIL = 100000;
  21. // The State struct contains all the state and procedures for the gendsp kernel
  22. typedef struct State {
  23. CommonState __commonstate;
  24. DCBlock m_dcblock_11;
  25. Delay m_delay_2;
  26. Delay m_delay_3;
  27. SineCycle m_cycle_12;
  28. SineData __sinedata;
  29. double m_mix_9;
  30. double m_rate_8;
  31. double m_cutoff_10;
  32. double m_morph_7;
  33. double m_time_5;
  34. double m_repeats_4;
  35. double m_depth_6;
  36. double samplerate;
  37. double m_y_1;
  38. int vectorsize;
  39. int __exception;
  40. // re-initialize all member variables;
  41. inline void reset(double __sr, int __vs) {
  42. __exception = 0;
  43. vectorsize = __vs;
  44. samplerate = __sr;
  45. m_y_1 = 0;
  46. m_delay_2.reset("m_delay_2", 48000);
  47. m_delay_3.reset("m_delay_3", 48000);
  48. m_repeats_4 = 75;
  49. m_time_5 = 500;
  50. m_depth_6 = 1;
  51. m_morph_7 = 50;
  52. m_rate_8 = 2;
  53. m_mix_9 = 100;
  54. m_cutoff_10 = 2500;
  55. m_dcblock_11.reset();
  56. m_cycle_12.reset(samplerate, 0);
  57. genlib_reset_complete(this);
  58. };
  59. // the signal processing routine;
  60. inline int perform(t_sample ** __ins, t_sample ** __outs, int __n) {
  61. vectorsize = __n;
  62. const t_sample * __in1 = __ins[0];
  63. t_sample * __out1 = __outs[0];
  64. if (__exception) {
  65. return __exception;
  66. } else if (( (__in1 == 0) || (__out1 == 0) )) {
  67. __exception = GENLIB_ERR_NULL_BUFFER;
  68. return __exception;
  69. };
  70. double mul_4216 = (m_mix_9 * 0.01);
  71. double mstosamps_4241 = (m_time_5 * (samplerate * 0.001));
  72. double expr_4223 = safediv(((m_cutoff_10 * 2) * 3.1415926535898), 48000);
  73. double sin_4224 = sin(expr_4223);
  74. double clamp_4225 = ((sin_4224 <= 1e-05) ? 1e-05 : ((sin_4224 >= 0.99999) ? 0.99999 : sin_4224));
  75. double mul_4220 = (m_repeats_4 * 0.01);
  76. double mul_4244 = (m_morph_7 * -1.4);
  77. double add_4246 = (mul_4244 + 70);
  78. double abs_4245 = fabs(add_4246);
  79. double mul_4218 = (abs_4245 * 0.01);
  80. double mul_4242 = (m_morph_7 * 1.4);
  81. double sub_4243 = (mul_4242 - 70);
  82. double mul_4219 = (sub_4243 * 0.01);
  83. int gt_4248 = (m_morph_7 > 50);
  84. double mul_4247 = (gt_4248 * m_morph_7);
  85. double mul_4249 = (mul_4247 * -0.6);
  86. double add_4250 = (mul_4249 + 130);
  87. double clamp_4251 = ((add_4250 <= 70) ? 70 : ((add_4250 >= 100) ? 100 : add_4250));
  88. double mul_4217 = (clamp_4251 * 0.01);
  89. int lt_4258 = (m_morph_7 < 50);
  90. double mul_4253 = (lt_4258 * m_morph_7);
  91. double mul_4252 = (mul_4253 * -1.2);
  92. double add_4257 = (mul_4252 + 30);
  93. double abs_4256 = fabs(add_4257);
  94. double mul_4255 = (abs_4256 * -1);
  95. double add_4254 = (mul_4255 + 30);
  96. // the main sample loop;
  97. while ((__n--)) {
  98. const double in1 = (*(__in1++));
  99. double tap_4240 = m_delay_3.read_cubic(mstosamps_4241);
  100. double mix_4285 = (m_y_1 + (clamp_4225 * (tap_4240 - m_y_1)));
  101. double mix_4222 = mix_4285;
  102. double mul_4237 = (mix_4222 * mul_4220);
  103. double dcblock_4214 = m_dcblock_11(mul_4237);
  104. double clamp_4215 = ((dcblock_4214 <= -1) ? -1 : ((dcblock_4214 >= 1) ? 1 : dcblock_4214));
  105. m_cycle_12.freq(m_rate_8);
  106. double cycle_4228 = m_cycle_12(__sinedata);
  107. double cycleindex_4229 = m_cycle_12.phase();
  108. double add_4227 = (cycle_4228 + 1);
  109. double mul_4226 = (add_4227 * 0.5);
  110. double mul_4230 = (m_depth_6 * mul_4226);
  111. double add_4259 = (add_4254 + mul_4230);
  112. double mstosamps_4221 = (add_4259 * (samplerate * 0.001));
  113. double tap_4235 = m_delay_2.read_linear(mstosamps_4221);
  114. double mul_4233 = (tap_4235 * mul_4217);
  115. double mul_4231 = (tap_4235 * mul_4219);
  116. double add_4236 = (mix_4222 + mul_4231);
  117. double mul_4232 = (add_4236 * mul_4218);
  118. double add_4213 = (mul_4232 + mul_4233);
  119. double mul_4238 = (add_4213 * mul_4216);
  120. double out1 = (mul_4238 + in1);
  121. double y0_next_4260 = mix_4222;
  122. m_delay_3.write((clamp_4215 + in1));
  123. m_delay_2.write(add_4236);
  124. m_y_1 = y0_next_4260;
  125. m_delay_2.step();
  126. m_delay_3.step();
  127. // assign results to output buffer;
  128. (*(__out1++)) = out1;
  129. };
  130. return __exception;
  131. };
  132. inline void set_repeats(double _value) {
  133. m_repeats_4 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  134. };
  135. inline void set_time(double _value) {
  136. m_time_5 = (_value < 20 ? 20 : (_value > 1000 ? 1000 : _value));
  137. };
  138. inline void set_depth(double _value) {
  139. m_depth_6 = (_value < 0 ? 0 : (_value > 5 ? 5 : _value));
  140. };
  141. inline void set_morph(double _value) {
  142. m_morph_7 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  143. };
  144. inline void set_rate(double _value) {
  145. m_rate_8 = (_value < 0.1 ? 0.1 : (_value > 10 ? 10 : _value));
  146. };
  147. inline void set_mix(double _value) {
  148. m_mix_9 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  149. };
  150. inline void set_cutoff(double _value) {
  151. m_cutoff_10 = (_value < 500 ? 500 : (_value > 6000 ? 6000 : _value));
  152. };
  153. } State;
  154. ///
  155. /// Configuration for the genlib API
  156. ///
  157. /// Number of signal inputs and outputs
  158. int gen_kernel_numins = 1;
  159. int gen_kernel_numouts = 1;
  160. int num_inputs() { return gen_kernel_numins; }
  161. int num_outputs() { return gen_kernel_numouts; }
  162. int num_params() { return 7; }
  163. /// Assistive lables for the signal inputs and outputs
  164. const char * gen_kernel_innames[] = { "in1" };
  165. const char * gen_kernel_outnames[] = { "out1" };
  166. /// Invoke the signal process of a State object
  167. int perform(CommonState *cself, t_sample **ins, long numins, t_sample **outs, long numouts, long n) {
  168. State * self = (State *)cself;
  169. return self->perform(ins, outs, n);
  170. }
  171. /// Reset all parameters and stateful operators of a State object
  172. void reset(CommonState *cself) {
  173. State * self = (State *)cself;
  174. self->reset(cself->sr, cself->vs);
  175. }
  176. /// Set a parameter of a State object
  177. void setparameter(CommonState *cself, long index, double value, void *ref) {
  178. State * self = (State *)cself;
  179. switch (index) {
  180. case 0: self->set_repeats(value); break;
  181. case 1: self->set_time(value); break;
  182. case 2: self->set_depth(value); break;
  183. case 3: self->set_morph(value); break;
  184. case 4: self->set_rate(value); break;
  185. case 5: self->set_mix(value); break;
  186. case 6: self->set_cutoff(value); break;
  187. default: break;
  188. }
  189. }
  190. /// Get the value of a parameter of a State object
  191. void getparameter(CommonState *cself, long index, double *value) {
  192. State *self = (State *)cself;
  193. switch (index) {
  194. case 0: *value = self->m_repeats_4; break;
  195. case 1: *value = self->m_time_5; break;
  196. case 2: *value = self->m_depth_6; break;
  197. case 3: *value = self->m_morph_7; break;
  198. case 4: *value = self->m_rate_8; break;
  199. case 5: *value = self->m_mix_9; break;
  200. case 6: *value = self->m_cutoff_10; break;
  201. default: break;
  202. }
  203. }
  204. /// Allocate and configure a new State object and it's internal CommonState:
  205. void * create(double sr, long vs) {
  206. State *self = new State;
  207. self->reset(sr, vs);
  208. ParamInfo *pi;
  209. self->__commonstate.inputnames = gen_kernel_innames;
  210. self->__commonstate.outputnames = gen_kernel_outnames;
  211. self->__commonstate.numins = gen_kernel_numins;
  212. self->__commonstate.numouts = gen_kernel_numouts;
  213. self->__commonstate.sr = sr;
  214. self->__commonstate.vs = vs;
  215. self->__commonstate.params = (ParamInfo *)genlib_sysmem_newptr(7 * sizeof(ParamInfo));
  216. self->__commonstate.numparams = 7;
  217. // initialize parameter 0 ("m_repeats_4")
  218. pi = self->__commonstate.params + 0;
  219. pi->name = "repeats";
  220. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  221. pi->defaultvalue = self->m_repeats_4;
  222. pi->defaultref = 0;
  223. pi->hasinputminmax = false;
  224. pi->inputmin = 0;
  225. pi->inputmax = 1;
  226. pi->hasminmax = true;
  227. pi->outputmin = 0;
  228. pi->outputmax = 100;
  229. pi->exp = 0;
  230. pi->units = ""; // no units defined
  231. // initialize parameter 1 ("m_time_5")
  232. pi = self->__commonstate.params + 1;
  233. pi->name = "time";
  234. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  235. pi->defaultvalue = self->m_time_5;
  236. pi->defaultref = 0;
  237. pi->hasinputminmax = false;
  238. pi->inputmin = 0;
  239. pi->inputmax = 1;
  240. pi->hasminmax = true;
  241. pi->outputmin = 20;
  242. pi->outputmax = 1000;
  243. pi->exp = 0;
  244. pi->units = ""; // no units defined
  245. // initialize parameter 2 ("m_depth_6")
  246. pi = self->__commonstate.params + 2;
  247. pi->name = "depth";
  248. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  249. pi->defaultvalue = self->m_depth_6;
  250. pi->defaultref = 0;
  251. pi->hasinputminmax = false;
  252. pi->inputmin = 0;
  253. pi->inputmax = 1;
  254. pi->hasminmax = true;
  255. pi->outputmin = 0;
  256. pi->outputmax = 5;
  257. pi->exp = 0;
  258. pi->units = ""; // no units defined
  259. // initialize parameter 3 ("m_morph_7")
  260. pi = self->__commonstate.params + 3;
  261. pi->name = "morph";
  262. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  263. pi->defaultvalue = self->m_morph_7;
  264. pi->defaultref = 0;
  265. pi->hasinputminmax = false;
  266. pi->inputmin = 0;
  267. pi->inputmax = 1;
  268. pi->hasminmax = true;
  269. pi->outputmin = 0;
  270. pi->outputmax = 100;
  271. pi->exp = 0;
  272. pi->units = ""; // no units defined
  273. // initialize parameter 4 ("m_rate_8")
  274. pi = self->__commonstate.params + 4;
  275. pi->name = "rate";
  276. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  277. pi->defaultvalue = self->m_rate_8;
  278. pi->defaultref = 0;
  279. pi->hasinputminmax = false;
  280. pi->inputmin = 0;
  281. pi->inputmax = 1;
  282. pi->hasminmax = true;
  283. pi->outputmin = 0.1;
  284. pi->outputmax = 10;
  285. pi->exp = 0;
  286. pi->units = ""; // no units defined
  287. // initialize parameter 5 ("m_mix_9")
  288. pi = self->__commonstate.params + 5;
  289. pi->name = "mix";
  290. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  291. pi->defaultvalue = self->m_mix_9;
  292. pi->defaultref = 0;
  293. pi->hasinputminmax = false;
  294. pi->inputmin = 0;
  295. pi->inputmax = 1;
  296. pi->hasminmax = true;
  297. pi->outputmin = 0;
  298. pi->outputmax = 100;
  299. pi->exp = 0;
  300. pi->units = ""; // no units defined
  301. // initialize parameter 6 ("m_cutoff_10")
  302. pi = self->__commonstate.params + 6;
  303. pi->name = "cutoff";
  304. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  305. pi->defaultvalue = self->m_cutoff_10;
  306. pi->defaultref = 0;
  307. pi->hasinputminmax = false;
  308. pi->inputmin = 0;
  309. pi->inputmax = 1;
  310. pi->hasminmax = true;
  311. pi->outputmin = 500;
  312. pi->outputmax = 6000;
  313. pi->exp = 0;
  314. pi->units = ""; // no units defined
  315. return self;
  316. }
  317. /// Release all resources and memory used by a State object:
  318. void destroy(CommonState *cself) {
  319. State * self = (State *)cself;
  320. genlib_sysmem_freeptr(cself->params);
  321. delete self;
  322. }
  323. } // gen_exported::