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.

402 lines
12KB

  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. Delay m_delay_2;
  25. Delay m_delay_3;
  26. SineCycle m_cycle_14;
  27. SineData __sinedata;
  28. double m_time_9;
  29. double m_rate_11;
  30. double m_bleed_12;
  31. double m_cutoff_10;
  32. double m_feedback_13;
  33. double m_mix_8;
  34. double m_depth_6;
  35. double m_feedforward_5;
  36. double m_repeats_7;
  37. double m_y_1;
  38. double samplerate;
  39. double m_delay_4;
  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_y_1 = 0;
  48. m_delay_2.reset("m_delay_2", 44100);
  49. m_delay_3.reset("m_delay_3", 44100);
  50. m_delay_4 = 0;
  51. m_feedforward_5 = 1;
  52. m_depth_6 = 0;
  53. m_repeats_7 = 0.75;
  54. m_mix_8 = 1;
  55. m_time_9 = 500;
  56. m_cutoff_10 = 4000;
  57. m_rate_11 = 2;
  58. m_bleed_12 = 0;
  59. m_feedback_13 = 0;
  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_580 = (m_time_9 * (samplerate * 0.001));
  75. double expr_582 = safediv(((m_cutoff_10 * 2) * 3.1415926535898), 44100);
  76. double sin_583 = sin(expr_582);
  77. double clamp_584 = ((sin_583 <= 1e-05) ? 1e-05 : ((sin_583 >= 0.99999) ? 0.99999 : sin_583));
  78. // the main sample loop;
  79. while ((__n--)) {
  80. const double in1 = (*(__in1++));
  81. double tap_579 = m_delay_3.read_linear(mstosamps_580);
  82. double mix_591 = (m_y_1 + (clamp_584 * (tap_579 - m_y_1)));
  83. double mix_581 = mix_591;
  84. double mul_576 = (mix_581 * m_repeats_7);
  85. m_cycle_14.freq(m_rate_11);
  86. double cycle_565 = m_cycle_14(__sinedata);
  87. double cycleindex_566 = m_cycle_14.phase();
  88. double add_564 = (cycle_565 + 1);
  89. double mul_563 = (add_564 * 0.5);
  90. double mul_568 = (m_depth_6 * mul_563);
  91. double add_567 = (m_delay_4 + mul_568);
  92. double mstosamps_585 = (add_567 * (samplerate * 0.001));
  93. double tap_573 = m_delay_2.read_linear(mstosamps_585);
  94. double mul_571 = (tap_573 * m_feedforward_5);
  95. double mul_569 = (tap_573 * m_feedback_13);
  96. double add_575 = (mix_581 + mul_569);
  97. double mul_570 = (add_575 * m_bleed_12);
  98. double add_574 = (mul_570 + mul_571);
  99. double mul_577 = (add_574 * m_mix_8);
  100. double out1 = (mul_577 + in1);
  101. double y0_next_586 = mix_581;
  102. m_delay_3.write((mul_576 + in1));
  103. m_delay_2.write(add_575);
  104. m_y_1 = y0_next_586;
  105. m_delay_2.step();
  106. m_delay_3.step();
  107. // assign results to output buffer;
  108. (*(__out1++)) = out1;
  109. };
  110. return __exception;
  111. };
  112. inline void set_delay(double _value) {
  113. m_delay_4 = (_value < 0 ? 0 : (_value > 30 ? 30 : _value));
  114. };
  115. inline void set_feedforward(double _value) {
  116. m_feedforward_5 = (_value < 0.7 ? 0.7 : (_value > 1 ? 1 : _value));
  117. };
  118. inline void set_depth(double _value) {
  119. m_depth_6 = (_value < 0 ? 0 : (_value > 30 ? 30 : _value));
  120. };
  121. inline void set_repeats(double _value) {
  122. m_repeats_7 = (_value < 0 ? 0 : (_value > 0.99 ? 0.99 : _value));
  123. };
  124. inline void set_mix(double _value) {
  125. m_mix_8 = (_value < 0 ? 0 : (_value > 1 ? 1 : _value));
  126. };
  127. inline void set_time(double _value) {
  128. m_time_9 = (_value < 10 ? 10 : (_value > 1000 ? 1000 : _value));
  129. };
  130. inline void set_cutoff(double _value) {
  131. m_cutoff_10 = (_value < 0 ? 0 : (_value > 6000 ? 6000 : _value));
  132. };
  133. inline void set_rate(double _value) {
  134. m_rate_11 = (_value < 0.1 ? 0.1 : (_value > 5 ? 5 : _value));
  135. };
  136. inline void set_bleed(double _value) {
  137. m_bleed_12 = (_value < 0 ? 0 : (_value > 0.7 ? 0.7 : _value));
  138. };
  139. inline void set_feedback(double _value) {
  140. m_feedback_13 = (_value < -0.7 ? -0.7 : (_value > 0.7 ? 0.7 : _value));
  141. };
  142. } State;
  143. ///
  144. /// Configuration for the genlib API
  145. ///
  146. /// Number of signal inputs and outputs
  147. int gen_kernel_numins = 1;
  148. int gen_kernel_numouts = 1;
  149. int num_inputs() { return gen_kernel_numins; }
  150. int num_outputs() { return gen_kernel_numouts; }
  151. int num_params() { return 10; }
  152. /// Assistive lables for the signal inputs and outputs
  153. const char * gen_kernel_innames[] = { "in1" };
  154. const char * gen_kernel_outnames[] = { "out1" };
  155. /// Invoke the signal process of a State object
  156. int perform(CommonState *cself, t_sample **ins, long numins, t_sample **outs, long numouts, long n) {
  157. State * self = (State *)cself;
  158. return self->perform(ins, outs, n);
  159. }
  160. /// Reset all parameters and stateful operators of a State object
  161. void reset(CommonState *cself) {
  162. State * self = (State *)cself;
  163. self->reset(cself->sr, cself->vs);
  164. }
  165. /// Set a parameter of a State object
  166. void setparameter(CommonState *cself, long index, double value, void *ref) {
  167. State * self = (State *)cself;
  168. switch (index) {
  169. case 0: self->set_delay(value); break;
  170. case 1: self->set_feedforward(value); break;
  171. case 2: self->set_depth(value); break;
  172. case 3: self->set_repeats(value); break;
  173. case 4: self->set_mix(value); break;
  174. case 5: self->set_time(value); break;
  175. case 6: self->set_cutoff(value); break;
  176. case 7: self->set_rate(value); break;
  177. case 8: self->set_bleed(value); break;
  178. case 9: self->set_feedback(value); break;
  179. default: break;
  180. }
  181. }
  182. /// Get the value of a parameter of a State object
  183. void getparameter(CommonState *cself, long index, double *value) {
  184. State *self = (State *)cself;
  185. switch (index) {
  186. case 0: *value = self->m_delay_4; break;
  187. case 1: *value = self->m_feedforward_5; break;
  188. case 2: *value = self->m_depth_6; break;
  189. case 3: *value = self->m_repeats_7; break;
  190. case 4: *value = self->m_mix_8; break;
  191. case 5: *value = self->m_time_9; break;
  192. case 6: *value = self->m_cutoff_10; break;
  193. case 7: *value = self->m_rate_11; break;
  194. case 8: *value = self->m_bleed_12; break;
  195. case 9: *value = self->m_feedback_13; break;
  196. default: break;
  197. }
  198. }
  199. /// Allocate and configure a new State object and it's internal CommonState:
  200. void * create(double sr, long vs) {
  201. State *self = new State;
  202. self->reset(sr, vs);
  203. ParamInfo *pi;
  204. self->__commonstate.inputnames = gen_kernel_innames;
  205. self->__commonstate.outputnames = gen_kernel_outnames;
  206. self->__commonstate.numins = gen_kernel_numins;
  207. self->__commonstate.numouts = gen_kernel_numouts;
  208. self->__commonstate.sr = sr;
  209. self->__commonstate.vs = vs;
  210. self->__commonstate.params = (ParamInfo *)genlib_sysmem_newptr(10 * sizeof(ParamInfo));
  211. self->__commonstate.numparams = 10;
  212. // initialize parameter 0 ("m_delay_4")
  213. pi = self->__commonstate.params + 0;
  214. pi->name = "delay";
  215. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  216. pi->defaultvalue = self->m_delay_4;
  217. pi->defaultref = 0;
  218. pi->hasinputminmax = false;
  219. pi->inputmin = 0;
  220. pi->inputmax = 1;
  221. pi->hasminmax = true;
  222. pi->outputmin = 0;
  223. pi->outputmax = 30;
  224. pi->exp = 0;
  225. pi->units = ""; // no units defined
  226. // initialize parameter 1 ("m_feedforward_5")
  227. pi = self->__commonstate.params + 1;
  228. pi->name = "feedforward";
  229. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  230. pi->defaultvalue = self->m_feedforward_5;
  231. pi->defaultref = 0;
  232. pi->hasinputminmax = false;
  233. pi->inputmin = 0;
  234. pi->inputmax = 1;
  235. pi->hasminmax = true;
  236. pi->outputmin = 0.7;
  237. pi->outputmax = 1;
  238. pi->exp = 0;
  239. pi->units = ""; // no units defined
  240. // initialize parameter 2 ("m_depth_6")
  241. pi = self->__commonstate.params + 2;
  242. pi->name = "depth";
  243. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  244. pi->defaultvalue = self->m_depth_6;
  245. pi->defaultref = 0;
  246. pi->hasinputminmax = false;
  247. pi->inputmin = 0;
  248. pi->inputmax = 1;
  249. pi->hasminmax = true;
  250. pi->outputmin = 0;
  251. pi->outputmax = 30;
  252. pi->exp = 0;
  253. pi->units = ""; // no units defined
  254. // initialize parameter 3 ("m_repeats_7")
  255. pi = self->__commonstate.params + 3;
  256. pi->name = "repeats";
  257. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  258. pi->defaultvalue = self->m_repeats_7;
  259. pi->defaultref = 0;
  260. pi->hasinputminmax = false;
  261. pi->inputmin = 0;
  262. pi->inputmax = 1;
  263. pi->hasminmax = true;
  264. pi->outputmin = 0;
  265. pi->outputmax = 0.99;
  266. pi->exp = 0;
  267. pi->units = ""; // no units defined
  268. // initialize parameter 4 ("m_mix_8")
  269. pi = self->__commonstate.params + 4;
  270. pi->name = "mix";
  271. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  272. pi->defaultvalue = self->m_mix_8;
  273. pi->defaultref = 0;
  274. pi->hasinputminmax = false;
  275. pi->inputmin = 0;
  276. pi->inputmax = 1;
  277. pi->hasminmax = true;
  278. pi->outputmin = 0;
  279. pi->outputmax = 1;
  280. pi->exp = 0;
  281. pi->units = ""; // no units defined
  282. // initialize parameter 5 ("m_time_9")
  283. pi = self->__commonstate.params + 5;
  284. pi->name = "time";
  285. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  286. pi->defaultvalue = self->m_time_9;
  287. pi->defaultref = 0;
  288. pi->hasinputminmax = false;
  289. pi->inputmin = 0;
  290. pi->inputmax = 1;
  291. pi->hasminmax = true;
  292. pi->outputmin = 10;
  293. pi->outputmax = 1000;
  294. pi->exp = 0;
  295. pi->units = ""; // no units defined
  296. // initialize parameter 6 ("m_cutoff_10")
  297. pi = self->__commonstate.params + 6;
  298. pi->name = "cutoff";
  299. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  300. pi->defaultvalue = self->m_cutoff_10;
  301. pi->defaultref = 0;
  302. pi->hasinputminmax = false;
  303. pi->inputmin = 0;
  304. pi->inputmax = 1;
  305. pi->hasminmax = true;
  306. pi->outputmin = 0;
  307. pi->outputmax = 6000;
  308. pi->exp = 0;
  309. pi->units = ""; // no units defined
  310. // initialize parameter 7 ("m_rate_11")
  311. pi = self->__commonstate.params + 7;
  312. pi->name = "rate";
  313. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  314. pi->defaultvalue = self->m_rate_11;
  315. pi->defaultref = 0;
  316. pi->hasinputminmax = false;
  317. pi->inputmin = 0;
  318. pi->inputmax = 1;
  319. pi->hasminmax = true;
  320. pi->outputmin = 0.1;
  321. pi->outputmax = 5;
  322. pi->exp = 0;
  323. pi->units = ""; // no units defined
  324. // initialize parameter 8 ("m_bleed_12")
  325. pi = self->__commonstate.params + 8;
  326. pi->name = "bleed";
  327. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  328. pi->defaultvalue = self->m_bleed_12;
  329. pi->defaultref = 0;
  330. pi->hasinputminmax = false;
  331. pi->inputmin = 0;
  332. pi->inputmax = 1;
  333. pi->hasminmax = true;
  334. pi->outputmin = 0;
  335. pi->outputmax = 0.7;
  336. pi->exp = 0;
  337. pi->units = ""; // no units defined
  338. // initialize parameter 9 ("m_feedback_13")
  339. pi = self->__commonstate.params + 9;
  340. pi->name = "feedback";
  341. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  342. pi->defaultvalue = self->m_feedback_13;
  343. pi->defaultref = 0;
  344. pi->hasinputminmax = false;
  345. pi->inputmin = 0;
  346. pi->inputmax = 1;
  347. pi->hasminmax = true;
  348. pi->outputmin = -0.7;
  349. pi->outputmax = 0.7;
  350. pi->exp = 0;
  351. pi->units = ""; // no units defined
  352. return self;
  353. }
  354. /// Release all resources and memory used by a State object:
  355. void destroy(CommonState *cself) {
  356. State * self = (State *)cself;
  357. genlib_sysmem_freeptr(cself->params);
  358. delete self;
  359. }
  360. } // gen_exported::