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.

366 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_morph_10;
  32. double m_depth_7;
  33. double m_repeats_5;
  34. double m_time_4;
  35. double m_tone_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_time_4 = 500;
  49. m_repeats_5 = 75;
  50. m_tone_6 = 2500;
  51. m_depth_7 = 1;
  52. m_rate_8 = 2;
  53. m_mix_9 = 100;
  54. m_morph_10 = 50;
  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 mstosamps_1384 = (m_time_4 * (samplerate * 0.001));
  71. double mul_1357 = (m_mix_9 * 0.01);
  72. double expr_1364 = safediv(((m_tone_6 * 2) * 3.1415926535898), 48000);
  73. double sin_1365 = sin(expr_1364);
  74. double clamp_1366 = ((sin_1365 <= 1e-05) ? 1e-05 : ((sin_1365 >= 0.99999) ? 0.99999 : sin_1365));
  75. double mul_1385 = (m_morph_10 * -1);
  76. double add_1386 = (mul_1385 + 150);
  77. double mul_1387 = (add_1386 * 0.01);
  78. double mul_1390 = (mul_1387 * mul_1357);
  79. double mul_1361 = (m_repeats_5 * 0.01);
  80. double mul_1352 = (m_morph_10 * -1.4);
  81. double add_1350 = (mul_1352 + 70);
  82. double abs_1351 = fabs(add_1350);
  83. double mul_1359 = (abs_1351 * 0.01);
  84. double mul_1354 = (m_morph_10 * 1.4);
  85. double sub_1353 = (mul_1354 - 70);
  86. double mul_1360 = (sub_1353 * 0.01);
  87. int lt_1343 = (m_morph_10 < 50);
  88. double mul_1388 = (m_morph_10 * -0.4);
  89. double add_1389 = (mul_1388 + 20);
  90. double mul_1344 = (lt_1343 * add_1389);
  91. int gt_1348 = (m_morph_10 > 50);
  92. double mul_1349 = (gt_1348 * m_morph_10);
  93. double mul_1346 = (mul_1349 * -0.6);
  94. double add_1347 = (mul_1346 + 130);
  95. double clamp_1345 = ((add_1347 <= 70) ? 70 : ((add_1347 >= 100) ? 100 : add_1347));
  96. double mul_1358 = (clamp_1345 * 0.01);
  97. // the main sample loop;
  98. while ((__n--)) {
  99. const double in1 = (*(__in1++));
  100. double tap_1383 = m_delay_3.read_cubic(mstosamps_1384);
  101. double mix_6935 = (m_y_1 + (clamp_1366 * (tap_1383 - m_y_1)));
  102. double mix_1363 = mix_6935;
  103. double mul_1380 = (mix_1363 * mul_1361);
  104. double dcblock_1355 = m_dcblock_11(mul_1380);
  105. double clamp_1356 = ((dcblock_1355 <= -1) ? -1 : ((dcblock_1355 >= 1) ? 1 : dcblock_1355));
  106. m_cycle_12.freq(m_rate_8);
  107. double cycle_1369 = m_cycle_12(__sinedata);
  108. double cycleindex_1370 = m_cycle_12.phase();
  109. double add_1368 = (cycle_1369 + 1);
  110. double mul_1367 = (add_1368 * 0.5);
  111. double mul_1372 = (m_depth_7 * mul_1367);
  112. double add_1371 = (mul_1344 + mul_1372);
  113. double mstosamps_1362 = (add_1371 * (samplerate * 0.001));
  114. double tap_1377 = m_delay_2.read_linear(mstosamps_1362);
  115. double mul_1375 = (tap_1377 * mul_1358);
  116. double mul_1373 = (tap_1377 * mul_1360);
  117. double add_1379 = (mix_1363 + mul_1373);
  118. double mul_1374 = (add_1379 * mul_1359);
  119. double add_1378 = (mul_1374 + mul_1375);
  120. double mul_1381 = (add_1378 * mul_1390);
  121. double out1 = (mul_1381 + in1);
  122. double y0_next_1391 = mix_1363;
  123. m_delay_3.write((clamp_1356 + in1));
  124. m_delay_2.write(add_1379);
  125. m_y_1 = y0_next_1391;
  126. m_delay_2.step();
  127. m_delay_3.step();
  128. // assign results to output buffer;
  129. (*(__out1++)) = out1;
  130. };
  131. return __exception;
  132. };
  133. inline void set_time(double _value) {
  134. m_time_4 = (_value < 20 ? 20 : (_value > 1000 ? 1000 : _value));
  135. };
  136. inline void set_repeats(double _value) {
  137. m_repeats_5 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  138. };
  139. inline void set_tone(double _value) {
  140. m_tone_6 = (_value < 500 ? 500 : (_value > 6000 ? 6000 : _value));
  141. };
  142. inline void set_depth(double _value) {
  143. m_depth_7 = (_value < 0 ? 0 : (_value > 5 ? 5 : _value));
  144. };
  145. inline void set_rate(double _value) {
  146. m_rate_8 = (_value < 0.1 ? 0.1 : (_value > 10 ? 10 : _value));
  147. };
  148. inline void set_mix(double _value) {
  149. m_mix_9 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  150. };
  151. inline void set_morph(double _value) {
  152. m_morph_10 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  153. };
  154. } State;
  155. ///
  156. /// Configuration for the genlib API
  157. ///
  158. /// Number of signal inputs and outputs
  159. int gen_kernel_numins = 1;
  160. int gen_kernel_numouts = 1;
  161. int num_inputs() { return gen_kernel_numins; }
  162. int num_outputs() { return gen_kernel_numouts; }
  163. int num_params() { return 7; }
  164. /// Assistive lables for the signal inputs and outputs
  165. const char * gen_kernel_innames[] = { "in1" };
  166. const char * gen_kernel_outnames[] = { "out1" };
  167. /// Invoke the signal process of a State object
  168. int perform(CommonState *cself, t_sample **ins, long numins, t_sample **outs, long numouts, long n) {
  169. State * self = (State *)cself;
  170. return self->perform(ins, outs, n);
  171. }
  172. /// Reset all parameters and stateful operators of a State object
  173. void reset(CommonState *cself) {
  174. State * self = (State *)cself;
  175. self->reset(cself->sr, cself->vs);
  176. }
  177. /// Set a parameter of a State object
  178. void setparameter(CommonState *cself, long index, double value, void *ref) {
  179. State * self = (State *)cself;
  180. switch (index) {
  181. case 0: self->set_time(value); break;
  182. case 1: self->set_repeats(value); break;
  183. case 2: self->set_tone(value); break;
  184. case 3: self->set_depth(value); break;
  185. case 4: self->set_rate(value); break;
  186. case 5: self->set_mix(value); break;
  187. case 6: self->set_morph(value); break;
  188. default: break;
  189. }
  190. }
  191. /// Get the value of a parameter of a State object
  192. void getparameter(CommonState *cself, long index, double *value) {
  193. State *self = (State *)cself;
  194. switch (index) {
  195. case 0: *value = self->m_time_4; break;
  196. case 1: *value = self->m_repeats_5; break;
  197. case 2: *value = self->m_tone_6; break;
  198. case 3: *value = self->m_depth_7; break;
  199. case 4: *value = self->m_rate_8; break;
  200. case 5: *value = self->m_mix_9; break;
  201. case 6: *value = self->m_morph_10; break;
  202. default: break;
  203. }
  204. }
  205. /// Allocate and configure a new State object and it's internal CommonState:
  206. void * create(double sr, long vs) {
  207. State *self = new State;
  208. self->reset(sr, vs);
  209. ParamInfo *pi;
  210. self->__commonstate.inputnames = gen_kernel_innames;
  211. self->__commonstate.outputnames = gen_kernel_outnames;
  212. self->__commonstate.numins = gen_kernel_numins;
  213. self->__commonstate.numouts = gen_kernel_numouts;
  214. self->__commonstate.sr = sr;
  215. self->__commonstate.vs = vs;
  216. self->__commonstate.params = (ParamInfo *)genlib_sysmem_newptr(7 * sizeof(ParamInfo));
  217. self->__commonstate.numparams = 7;
  218. // initialize parameter 0 ("m_time_4")
  219. pi = self->__commonstate.params + 0;
  220. pi->name = "time";
  221. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  222. pi->defaultvalue = self->m_time_4;
  223. pi->defaultref = 0;
  224. pi->hasinputminmax = false;
  225. pi->inputmin = 0;
  226. pi->inputmax = 1;
  227. pi->hasminmax = true;
  228. pi->outputmin = 20;
  229. pi->outputmax = 1000;
  230. pi->exp = 0;
  231. pi->units = ""; // no units defined
  232. // initialize parameter 1 ("m_repeats_5")
  233. pi = self->__commonstate.params + 1;
  234. pi->name = "repeats";
  235. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  236. pi->defaultvalue = self->m_repeats_5;
  237. pi->defaultref = 0;
  238. pi->hasinputminmax = false;
  239. pi->inputmin = 0;
  240. pi->inputmax = 1;
  241. pi->hasminmax = true;
  242. pi->outputmin = 0;
  243. pi->outputmax = 100;
  244. pi->exp = 0;
  245. pi->units = ""; // no units defined
  246. // initialize parameter 2 ("m_tone_6")
  247. pi = self->__commonstate.params + 2;
  248. pi->name = "tone";
  249. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  250. pi->defaultvalue = self->m_tone_6;
  251. pi->defaultref = 0;
  252. pi->hasinputminmax = false;
  253. pi->inputmin = 0;
  254. pi->inputmax = 1;
  255. pi->hasminmax = true;
  256. pi->outputmin = 500;
  257. pi->outputmax = 6000;
  258. pi->exp = 0;
  259. pi->units = ""; // no units defined
  260. // initialize parameter 3 ("m_depth_7")
  261. pi = self->__commonstate.params + 3;
  262. pi->name = "depth";
  263. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  264. pi->defaultvalue = self->m_depth_7;
  265. pi->defaultref = 0;
  266. pi->hasinputminmax = false;
  267. pi->inputmin = 0;
  268. pi->inputmax = 1;
  269. pi->hasminmax = true;
  270. pi->outputmin = 0;
  271. pi->outputmax = 5;
  272. pi->exp = 0;
  273. pi->units = ""; // no units defined
  274. // initialize parameter 4 ("m_rate_8")
  275. pi = self->__commonstate.params + 4;
  276. pi->name = "rate";
  277. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  278. pi->defaultvalue = self->m_rate_8;
  279. pi->defaultref = 0;
  280. pi->hasinputminmax = false;
  281. pi->inputmin = 0;
  282. pi->inputmax = 1;
  283. pi->hasminmax = true;
  284. pi->outputmin = 0.1;
  285. pi->outputmax = 10;
  286. pi->exp = 0;
  287. pi->units = ""; // no units defined
  288. // initialize parameter 5 ("m_mix_9")
  289. pi = self->__commonstate.params + 5;
  290. pi->name = "mix";
  291. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  292. pi->defaultvalue = self->m_mix_9;
  293. pi->defaultref = 0;
  294. pi->hasinputminmax = false;
  295. pi->inputmin = 0;
  296. pi->inputmax = 1;
  297. pi->hasminmax = true;
  298. pi->outputmin = 0;
  299. pi->outputmax = 100;
  300. pi->exp = 0;
  301. pi->units = ""; // no units defined
  302. // initialize parameter 6 ("m_morph_10")
  303. pi = self->__commonstate.params + 6;
  304. pi->name = "morph";
  305. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  306. pi->defaultvalue = self->m_morph_10;
  307. pi->defaultref = 0;
  308. pi->hasinputminmax = false;
  309. pi->inputmin = 0;
  310. pi->inputmax = 1;
  311. pi->hasminmax = true;
  312. pi->outputmin = 0;
  313. pi->outputmax = 100;
  314. pi->exp = 0;
  315. pi->units = ""; // no units defined
  316. return self;
  317. }
  318. /// Release all resources and memory used by a State object:
  319. void destroy(CommonState *cself) {
  320. State * self = (State *)cself;
  321. genlib_sysmem_freeptr(cself->params);
  322. delete self;
  323. }
  324. } // gen_exported::