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.

362 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_tone_9;
  30. double m_mix_8;
  31. double m_depth_10;
  32. double m_time_7;
  33. double m_repeats_5;
  34. double m_morph_4;
  35. double m_rate_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_morph_4 = 50;
  49. m_repeats_5 = 75;
  50. m_rate_6 = 2;
  51. m_time_7 = 500;
  52. m_mix_8 = 100;
  53. m_tone_9 = 2500;
  54. m_depth_10 = 1;
  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_46444 = (m_mix_8 * 0.01);
  71. double mstosamps_46471 = (m_time_7 * (samplerate * 0.001));
  72. double expr_46451 = safediv(((m_tone_9 * 2) * 3.1415926535898), 48000);
  73. double sin_46452 = sin(expr_46451);
  74. double clamp_46453 = ((sin_46452 <= 1e-05) ? 1e-05 : ((sin_46452 >= 0.99999) ? 0.99999 : sin_46452));
  75. double mul_46448 = (m_repeats_5 * 0.01);
  76. double mul_46439 = (m_morph_4 * -1.4);
  77. double add_46437 = (mul_46439 + 70);
  78. double abs_46438 = fabs(add_46437);
  79. double mul_46446 = (abs_46438 * 0.01);
  80. double mul_46441 = (m_morph_4 * 1.4);
  81. double sub_46440 = (mul_46441 - 70);
  82. double mul_46447 = (sub_46440 * 0.01);
  83. int lt_46430 = (m_morph_4 < 50);
  84. double mul_46429 = (m_morph_4 * -0.6);
  85. double add_46428 = (mul_46429 + 30);
  86. double mul_46431 = (lt_46430 * add_46428);
  87. int gt_46435 = (m_morph_4 > 50);
  88. double mul_46436 = (gt_46435 * m_morph_4);
  89. double mul_46433 = (mul_46436 * -0.6);
  90. double add_46434 = (mul_46433 + 130);
  91. double clamp_46432 = ((add_46434 <= 70) ? 70 : ((add_46434 >= 100) ? 100 : add_46434));
  92. double mul_46445 = (clamp_46432 * 0.01);
  93. // the main sample loop;
  94. while ((__n--)) {
  95. const double in1 = (*(__in1++));
  96. double tap_46470 = m_delay_3.read_cubic(mstosamps_46471);
  97. double mix_46495 = (m_y_1 + (clamp_46453 * (tap_46470 - m_y_1)));
  98. double mix_46450 = mix_46495;
  99. double mul_46467 = (mix_46450 * mul_46448);
  100. double dcblock_46442 = m_dcblock_11(mul_46467);
  101. double clamp_46443 = ((dcblock_46442 <= -1) ? -1 : ((dcblock_46442 >= 1) ? 1 : dcblock_46442));
  102. m_cycle_12.freq(m_rate_6);
  103. double cycle_46456 = m_cycle_12(__sinedata);
  104. double cycleindex_46457 = m_cycle_12.phase();
  105. double add_46455 = (cycle_46456 + 1);
  106. double mul_46454 = (add_46455 * 0.5);
  107. double mul_46459 = (m_depth_10 * mul_46454);
  108. double add_46458 = (mul_46431 + mul_46459);
  109. double mstosamps_46449 = (add_46458 * (samplerate * 0.001));
  110. double tap_46464 = m_delay_2.read_linear(mstosamps_46449);
  111. double mul_46462 = (tap_46464 * mul_46445);
  112. double mul_46460 = (tap_46464 * mul_46447);
  113. double add_46466 = (mix_46450 + mul_46460);
  114. double mul_46461 = (add_46466 * mul_46446);
  115. double add_46465 = (mul_46461 + mul_46462);
  116. double mul_46468 = (add_46465 * mul_46444);
  117. double out1 = (mul_46468 + in1);
  118. double y0_next_46472 = mix_46450;
  119. m_delay_3.write((clamp_46443 + in1));
  120. m_delay_2.write(add_46466);
  121. m_y_1 = y0_next_46472;
  122. m_delay_2.step();
  123. m_delay_3.step();
  124. // assign results to output buffer;
  125. (*(__out1++)) = out1;
  126. };
  127. return __exception;
  128. };
  129. inline void set_morph(double _value) {
  130. m_morph_4 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  131. };
  132. inline void set_repeats(double _value) {
  133. m_repeats_5 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  134. };
  135. inline void set_rate(double _value) {
  136. m_rate_6 = (_value < 0.1 ? 0.1 : (_value > 10 ? 10 : _value));
  137. };
  138. inline void set_time(double _value) {
  139. m_time_7 = (_value < 20 ? 20 : (_value > 1000 ? 1000 : _value));
  140. };
  141. inline void set_mix(double _value) {
  142. m_mix_8 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  143. };
  144. inline void set_tone(double _value) {
  145. m_tone_9 = (_value < 500 ? 500 : (_value > 6000 ? 6000 : _value));
  146. };
  147. inline void set_depth(double _value) {
  148. m_depth_10 = (_value < 0 ? 0 : (_value > 5 ? 5 : _value));
  149. };
  150. } State;
  151. ///
  152. /// Configuration for the genlib API
  153. ///
  154. /// Number of signal inputs and outputs
  155. int gen_kernel_numins = 1;
  156. int gen_kernel_numouts = 1;
  157. int num_inputs() { return gen_kernel_numins; }
  158. int num_outputs() { return gen_kernel_numouts; }
  159. int num_params() { return 7; }
  160. /// Assistive lables for the signal inputs and outputs
  161. const char * gen_kernel_innames[] = { "in1" };
  162. const char * gen_kernel_outnames[] = { "out1" };
  163. /// Invoke the signal process of a State object
  164. int perform(CommonState *cself, t_sample **ins, long numins, t_sample **outs, long numouts, long n) {
  165. State * self = (State *)cself;
  166. return self->perform(ins, outs, n);
  167. }
  168. /// Reset all parameters and stateful operators of a State object
  169. void reset(CommonState *cself) {
  170. State * self = (State *)cself;
  171. self->reset(cself->sr, cself->vs);
  172. }
  173. /// Set a parameter of a State object
  174. void setparameter(CommonState *cself, long index, double value, void *ref) {
  175. State * self = (State *)cself;
  176. switch (index) {
  177. case 0: self->set_morph(value); break;
  178. case 1: self->set_repeats(value); break;
  179. case 2: self->set_rate(value); break;
  180. case 3: self->set_time(value); break;
  181. case 4: self->set_mix(value); break;
  182. case 5: self->set_tone(value); break;
  183. case 6: self->set_depth(value); break;
  184. default: break;
  185. }
  186. }
  187. /// Get the value of a parameter of a State object
  188. void getparameter(CommonState *cself, long index, double *value) {
  189. State *self = (State *)cself;
  190. switch (index) {
  191. case 0: *value = self->m_morph_4; break;
  192. case 1: *value = self->m_repeats_5; break;
  193. case 2: *value = self->m_rate_6; break;
  194. case 3: *value = self->m_time_7; break;
  195. case 4: *value = self->m_mix_8; break;
  196. case 5: *value = self->m_tone_9; break;
  197. case 6: *value = self->m_depth_10; break;
  198. default: break;
  199. }
  200. }
  201. /// Allocate and configure a new State object and it's internal CommonState:
  202. void * create(double sr, long vs) {
  203. State *self = new State;
  204. self->reset(sr, vs);
  205. ParamInfo *pi;
  206. self->__commonstate.inputnames = gen_kernel_innames;
  207. self->__commonstate.outputnames = gen_kernel_outnames;
  208. self->__commonstate.numins = gen_kernel_numins;
  209. self->__commonstate.numouts = gen_kernel_numouts;
  210. self->__commonstate.sr = sr;
  211. self->__commonstate.vs = vs;
  212. self->__commonstate.params = (ParamInfo *)genlib_sysmem_newptr(7 * sizeof(ParamInfo));
  213. self->__commonstate.numparams = 7;
  214. // initialize parameter 0 ("m_morph_4")
  215. pi = self->__commonstate.params + 0;
  216. pi->name = "morph";
  217. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  218. pi->defaultvalue = self->m_morph_4;
  219. pi->defaultref = 0;
  220. pi->hasinputminmax = false;
  221. pi->inputmin = 0;
  222. pi->inputmax = 1;
  223. pi->hasminmax = true;
  224. pi->outputmin = 0;
  225. pi->outputmax = 100;
  226. pi->exp = 0;
  227. pi->units = ""; // no units defined
  228. // initialize parameter 1 ("m_repeats_5")
  229. pi = self->__commonstate.params + 1;
  230. pi->name = "repeats";
  231. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  232. pi->defaultvalue = self->m_repeats_5;
  233. pi->defaultref = 0;
  234. pi->hasinputminmax = false;
  235. pi->inputmin = 0;
  236. pi->inputmax = 1;
  237. pi->hasminmax = true;
  238. pi->outputmin = 0;
  239. pi->outputmax = 100;
  240. pi->exp = 0;
  241. pi->units = ""; // no units defined
  242. // initialize parameter 2 ("m_rate_6")
  243. pi = self->__commonstate.params + 2;
  244. pi->name = "rate";
  245. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  246. pi->defaultvalue = self->m_rate_6;
  247. pi->defaultref = 0;
  248. pi->hasinputminmax = false;
  249. pi->inputmin = 0;
  250. pi->inputmax = 1;
  251. pi->hasminmax = true;
  252. pi->outputmin = 0.1;
  253. pi->outputmax = 10;
  254. pi->exp = 0;
  255. pi->units = ""; // no units defined
  256. // initialize parameter 3 ("m_time_7")
  257. pi = self->__commonstate.params + 3;
  258. pi->name = "time";
  259. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  260. pi->defaultvalue = self->m_time_7;
  261. pi->defaultref = 0;
  262. pi->hasinputminmax = false;
  263. pi->inputmin = 0;
  264. pi->inputmax = 1;
  265. pi->hasminmax = true;
  266. pi->outputmin = 20;
  267. pi->outputmax = 1000;
  268. pi->exp = 0;
  269. pi->units = ""; // no units defined
  270. // initialize parameter 4 ("m_mix_8")
  271. pi = self->__commonstate.params + 4;
  272. pi->name = "mix";
  273. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  274. pi->defaultvalue = self->m_mix_8;
  275. pi->defaultref = 0;
  276. pi->hasinputminmax = false;
  277. pi->inputmin = 0;
  278. pi->inputmax = 1;
  279. pi->hasminmax = true;
  280. pi->outputmin = 0;
  281. pi->outputmax = 100;
  282. pi->exp = 0;
  283. pi->units = ""; // no units defined
  284. // initialize parameter 5 ("m_tone_9")
  285. pi = self->__commonstate.params + 5;
  286. pi->name = "tone";
  287. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  288. pi->defaultvalue = self->m_tone_9;
  289. pi->defaultref = 0;
  290. pi->hasinputminmax = false;
  291. pi->inputmin = 0;
  292. pi->inputmax = 1;
  293. pi->hasminmax = true;
  294. pi->outputmin = 500;
  295. pi->outputmax = 6000;
  296. pi->exp = 0;
  297. pi->units = ""; // no units defined
  298. // initialize parameter 6 ("m_depth_10")
  299. pi = self->__commonstate.params + 6;
  300. pi->name = "depth";
  301. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  302. pi->defaultvalue = self->m_depth_10;
  303. pi->defaultref = 0;
  304. pi->hasinputminmax = false;
  305. pi->inputmin = 0;
  306. pi->inputmax = 1;
  307. pi->hasminmax = true;
  308. pi->outputmin = 0;
  309. pi->outputmax = 5;
  310. pi->exp = 0;
  311. pi->units = ""; // no units defined
  312. return self;
  313. }
  314. /// Release all resources and memory used by a State object:
  315. void destroy(CommonState *cself) {
  316. State * self = (State *)cself;
  317. genlib_sysmem_freeptr(cself->params);
  318. delete self;
  319. }
  320. } // gen_exported::