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.

378 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_13;
  25. Delay m_delay_5;
  26. Delay m_delay_4;
  27. SineCycle m_cycle_14;
  28. SineData __sinedata;
  29. double m_morph_9;
  30. double m_time_10;
  31. double m_tone_12;
  32. double m_rate_11;
  33. double m_depth_8;
  34. double m_repeats_6;
  35. double m_smth_3;
  36. double m_mix_7;
  37. double m_smth_1;
  38. double samplerate;
  39. double m_y_2;
  40. int vectorsize;
  41. int __exception;
  42. // re-initialize all member variables;
  43. inline void reset(double __sr, int __vs) {
  44. __exception = 0;
  45. vectorsize = __vs;
  46. samplerate = __sr;
  47. m_smth_1 = 0;
  48. m_y_2 = 0;
  49. m_smth_3 = 0;
  50. m_delay_4.reset("m_delay_4", 44100);
  51. m_delay_5.reset("m_delay_5", 44100);
  52. m_repeats_6 = 75;
  53. m_mix_7 = 75;
  54. m_depth_8 = 1;
  55. m_morph_9 = 50;
  56. m_time_10 = 500;
  57. m_rate_11 = 2;
  58. m_tone_12 = 3000;
  59. m_dcblock_13.reset();
  60. m_cycle_14.reset(samplerate, 0);
  61. genlib_reset_complete(this);
  62. };
  63. // the signal processing routine;
  64. inline int perform(t_sample ** __ins, t_sample ** __outs, int __n) {
  65. vectorsize = __n;
  66. const t_sample * __in1 = __ins[0];
  67. t_sample * __out1 = __outs[0];
  68. if (__exception) {
  69. return __exception;
  70. } else if (( (__in1 == 0) || (__out1 == 0) )) {
  71. __exception = GENLIB_ERR_NULL_BUFFER;
  72. return __exception;
  73. };
  74. double mstosamps_19 = (m_time_10 * (samplerate * 0.001));
  75. double mul_7 = (m_mix_7 * 0.01);
  76. double expr_55 = safediv(((m_tone_12 * 2) * 3.1415926535898), 48000);
  77. double sin_14 = sin(expr_55);
  78. double clamp_15 = ((sin_14 <= 1e-05) ? 1e-05 : ((sin_14 >= 0.99999) ? 0.99999 : sin_14));
  79. int lt_22 = (m_morph_9 < 50);
  80. double mul_32 = (m_morph_9 * -0.8);
  81. double add_30 = (mul_32 + 40);
  82. double abs_31 = fabs(add_30);
  83. double mul_35 = (abs_31 * 0.01);
  84. double mul_11 = (m_repeats_6 * 0.01);
  85. double mul_21 = (m_morph_9 * -0.4);
  86. double add_20 = (mul_21 + 20);
  87. double mul_23 = (lt_22 * add_20);
  88. double mul_4 = (m_morph_9 * -0.8);
  89. double add_2 = (mul_4 + 40);
  90. double abs_3 = fabs(add_2);
  91. double rsub_1 = (80 - abs_3);
  92. double mul_5 = (rsub_1 * 0.01);
  93. double mul_34 = (m_morph_9 * 0.8);
  94. double sub_33 = (mul_34 - 40);
  95. double mul_36 = (sub_33 * 0.01);
  96. int gt_28 = (m_morph_9 > 50);
  97. double mul_27 = (gt_28 * m_morph_9);
  98. double sub_26 = (mul_27 - 50);
  99. double clamp_25 = ((sub_26 <= 0) ? 0 : ((sub_26 >= 50) ? 50 : sub_26));
  100. double mul_29 = (clamp_25 * 0.04);
  101. double add_24 = (mul_23 + mul_29);
  102. // the main sample loop;
  103. while ((__n--)) {
  104. const double in1 = (*(__in1++));
  105. double mix_61 = (mstosamps_19 + (0.999 * (m_smth_3 - mstosamps_19)));
  106. double mix_8 = mix_61;
  107. double tap_18 = m_delay_5.read_linear(mix_8);
  108. double mix_62 = (m_y_2 + (clamp_15 * (tap_18 - m_y_2)));
  109. double mix_12 = mix_62;
  110. double mul_41 = (mix_12 * mul_35);
  111. double mul_16 = (mix_12 * mul_11);
  112. double dcblock_9 = m_dcblock_13(mul_16);
  113. double clamp_10 = ((dcblock_9 <= -1) ? -1 : ((dcblock_9 >= 1) ? 1 : dcblock_9));
  114. m_cycle_14.freq(m_rate_11);
  115. double cycle_46 = m_cycle_14(__sinedata);
  116. double cycleindex_47 = m_cycle_14.phase();
  117. double add_45 = (cycle_46 + 1);
  118. double mul_44 = (add_45 * 0.5);
  119. double mul_48 = (m_depth_8 * mul_44);
  120. double add_37 = (mul_48 + add_24);
  121. double mstosamps_43 = (add_37 * (samplerate * 0.001));
  122. double mix_63 = (mstosamps_43 + (0.999 * (m_smth_1 - mstosamps_43)));
  123. double mix_42 = mix_63;
  124. double tap_50 = m_delay_4.read_linear(mix_42);
  125. double mul_38 = (tap_50 * mul_5);
  126. double mul_6 = ((mul_38 + mul_41) * mul_7);
  127. double out1 = (mul_6 + in1);
  128. double mul_39 = (tap_50 * mul_36);
  129. double add_40 = (mix_12 + mul_39);
  130. double smth1_next_58 = mix_8;
  131. double y0_next_59 = mix_12;
  132. double smth2_next_60 = mix_42;
  133. m_delay_5.write((clamp_10 + in1));
  134. m_delay_4.write(add_40);
  135. m_smth_3 = smth1_next_58;
  136. m_y_2 = y0_next_59;
  137. m_smth_1 = smth2_next_60;
  138. m_delay_4.step();
  139. m_delay_5.step();
  140. // assign results to output buffer;
  141. (*(__out1++)) = out1;
  142. };
  143. return __exception;
  144. };
  145. inline void set_repeats(double _value) {
  146. m_repeats_6 = (_value < 0 ? 0 : (_value > 110 ? 110 : _value));
  147. };
  148. inline void set_mix(double _value) {
  149. m_mix_7 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  150. };
  151. inline void set_depth(double _value) {
  152. m_depth_8 = (_value < 0 ? 0 : (_value > 3 ? 3 : _value));
  153. };
  154. inline void set_morph(double _value) {
  155. m_morph_9 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  156. };
  157. inline void set_time(double _value) {
  158. m_time_10 = (_value < 20 ? 20 : (_value > 1000 ? 1000 : _value));
  159. };
  160. inline void set_rate(double _value) {
  161. m_rate_11 = (_value < 0.1 ? 0.1 : (_value > 5 ? 5 : _value));
  162. };
  163. inline void set_tone(double _value) {
  164. m_tone_12 = (_value < 500 ? 500 : (_value > 6000 ? 6000 : _value));
  165. };
  166. } State;
  167. ///
  168. /// Configuration for the genlib API
  169. ///
  170. /// Number of signal inputs and outputs
  171. int gen_kernel_numins = 1;
  172. int gen_kernel_numouts = 1;
  173. int num_inputs() { return gen_kernel_numins; }
  174. int num_outputs() { return gen_kernel_numouts; }
  175. int num_params() { return 7; }
  176. /// Assistive lables for the signal inputs and outputs
  177. const char * gen_kernel_innames[] = { "in1" };
  178. const char * gen_kernel_outnames[] = { "out1" };
  179. /// Invoke the signal process of a State object
  180. int perform(CommonState *cself, t_sample **ins, long numins, t_sample **outs, long numouts, long n) {
  181. State * self = (State *)cself;
  182. return self->perform(ins, outs, n);
  183. }
  184. /// Reset all parameters and stateful operators of a State object
  185. void reset(CommonState *cself) {
  186. State * self = (State *)cself;
  187. self->reset(cself->sr, cself->vs);
  188. }
  189. /// Set a parameter of a State object
  190. void setparameter(CommonState *cself, long index, double value, void *ref) {
  191. State * self = (State *)cself;
  192. switch (index) {
  193. case 0: self->set_repeats(value); break;
  194. case 1: self->set_mix(value); break;
  195. case 2: self->set_depth(value); break;
  196. case 3: self->set_morph(value); break;
  197. case 4: self->set_time(value); break;
  198. case 5: self->set_rate(value); break;
  199. case 6: self->set_tone(value); break;
  200. default: break;
  201. }
  202. }
  203. /// Get the value of a parameter of a State object
  204. void getparameter(CommonState *cself, long index, double *value) {
  205. State *self = (State *)cself;
  206. switch (index) {
  207. case 0: *value = self->m_repeats_6; break;
  208. case 1: *value = self->m_mix_7; break;
  209. case 2: *value = self->m_depth_8; break;
  210. case 3: *value = self->m_morph_9; break;
  211. case 4: *value = self->m_time_10; break;
  212. case 5: *value = self->m_rate_11; break;
  213. case 6: *value = self->m_tone_12; break;
  214. default: break;
  215. }
  216. }
  217. /// Allocate and configure a new State object and it's internal CommonState:
  218. void * create(double sr, long vs) {
  219. State *self = new State;
  220. self->reset(sr, vs);
  221. ParamInfo *pi;
  222. self->__commonstate.inputnames = gen_kernel_innames;
  223. self->__commonstate.outputnames = gen_kernel_outnames;
  224. self->__commonstate.numins = gen_kernel_numins;
  225. self->__commonstate.numouts = gen_kernel_numouts;
  226. self->__commonstate.sr = sr;
  227. self->__commonstate.vs = vs;
  228. self->__commonstate.params = (ParamInfo *)genlib_sysmem_newptr(7 * sizeof(ParamInfo));
  229. self->__commonstate.numparams = 7;
  230. // initialize parameter 0 ("m_repeats_6")
  231. pi = self->__commonstate.params + 0;
  232. pi->name = "repeats";
  233. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  234. pi->defaultvalue = self->m_repeats_6;
  235. pi->defaultref = 0;
  236. pi->hasinputminmax = false;
  237. pi->inputmin = 0;
  238. pi->inputmax = 1;
  239. pi->hasminmax = true;
  240. pi->outputmin = 0;
  241. pi->outputmax = 110;
  242. pi->exp = 0;
  243. pi->units = ""; // no units defined
  244. // initialize parameter 1 ("m_mix_7")
  245. pi = self->__commonstate.params + 1;
  246. pi->name = "mix";
  247. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  248. pi->defaultvalue = self->m_mix_7;
  249. pi->defaultref = 0;
  250. pi->hasinputminmax = false;
  251. pi->inputmin = 0;
  252. pi->inputmax = 1;
  253. pi->hasminmax = true;
  254. pi->outputmin = 0;
  255. pi->outputmax = 100;
  256. pi->exp = 0;
  257. pi->units = ""; // no units defined
  258. // initialize parameter 2 ("m_depth_8")
  259. pi = self->__commonstate.params + 2;
  260. pi->name = "depth";
  261. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  262. pi->defaultvalue = self->m_depth_8;
  263. pi->defaultref = 0;
  264. pi->hasinputminmax = false;
  265. pi->inputmin = 0;
  266. pi->inputmax = 1;
  267. pi->hasminmax = true;
  268. pi->outputmin = 0;
  269. pi->outputmax = 3;
  270. pi->exp = 0;
  271. pi->units = ""; // no units defined
  272. // initialize parameter 3 ("m_morph_9")
  273. pi = self->__commonstate.params + 3;
  274. pi->name = "morph";
  275. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  276. pi->defaultvalue = self->m_morph_9;
  277. pi->defaultref = 0;
  278. pi->hasinputminmax = false;
  279. pi->inputmin = 0;
  280. pi->inputmax = 1;
  281. pi->hasminmax = true;
  282. pi->outputmin = 0;
  283. pi->outputmax = 100;
  284. pi->exp = 0;
  285. pi->units = ""; // no units defined
  286. // initialize parameter 4 ("m_time_10")
  287. pi = self->__commonstate.params + 4;
  288. pi->name = "time";
  289. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  290. pi->defaultvalue = self->m_time_10;
  291. pi->defaultref = 0;
  292. pi->hasinputminmax = false;
  293. pi->inputmin = 0;
  294. pi->inputmax = 1;
  295. pi->hasminmax = true;
  296. pi->outputmin = 20;
  297. pi->outputmax = 1000;
  298. pi->exp = 0;
  299. pi->units = ""; // no units defined
  300. // initialize parameter 5 ("m_rate_11")
  301. pi = self->__commonstate.params + 5;
  302. pi->name = "rate";
  303. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  304. pi->defaultvalue = self->m_rate_11;
  305. pi->defaultref = 0;
  306. pi->hasinputminmax = false;
  307. pi->inputmin = 0;
  308. pi->inputmax = 1;
  309. pi->hasminmax = true;
  310. pi->outputmin = 0.1;
  311. pi->outputmax = 5;
  312. pi->exp = 0;
  313. pi->units = ""; // no units defined
  314. // initialize parameter 6 ("m_tone_12")
  315. pi = self->__commonstate.params + 6;
  316. pi->name = "tone";
  317. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  318. pi->defaultvalue = self->m_tone_12;
  319. pi->defaultref = 0;
  320. pi->hasinputminmax = false;
  321. pi->inputmin = 0;
  322. pi->inputmax = 1;
  323. pi->hasminmax = true;
  324. pi->outputmin = 500;
  325. pi->outputmax = 6000;
  326. pi->exp = 0;
  327. pi->units = ""; // no units defined
  328. return self;
  329. }
  330. /// Release all resources and memory used by a State object:
  331. void destroy(CommonState *cself) {
  332. State * self = (State *)cself;
  333. genlib_sysmem_freeptr(cself->params);
  334. delete self;
  335. }
  336. } // gen_exported::