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.

407 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_feedforward_9;
  29. double m_mix_11;
  30. double m_depth_12;
  31. double m_time_10;
  32. double m_feedback_13;
  33. double m_repeats_8;
  34. double m_bleed_6;
  35. double m_delay_5;
  36. double m_cutoff_7;
  37. double m_y_1;
  38. double samplerate;
  39. double m_rate_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_rate_4 = 2;
  51. m_delay_5 = 0;
  52. m_bleed_6 = 0;
  53. m_cutoff_7 = 3000;
  54. m_repeats_8 = 75;
  55. m_feedforward_9 = 100;
  56. m_time_10 = 500;
  57. m_mix_11 = 100;
  58. m_depth_12 = 1;
  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 mul_164 = (m_mix_11 * 0.01);
  75. double mstosamps_191 = (m_time_10 * (samplerate * 0.001));
  76. double mul_166 = (m_bleed_6 * 0.01);
  77. double mul_165 = (m_feedforward_9 * 0.01);
  78. double mul_168 = (m_repeats_8 * 0.01);
  79. double expr_192 = safediv(((m_cutoff_7 * 2) * 3.1415926535898), 44100);
  80. double sin_172 = sin(expr_192);
  81. double clamp_173 = ((sin_172 <= 1e-05) ? 1e-05 : ((sin_172 >= 0.99999) ? 0.99999 : sin_172));
  82. double mul_167 = (m_feedback_13 * 0.01);
  83. // the main sample loop;
  84. while ((__n--)) {
  85. const double in1 = (*(__in1++));
  86. double tap_190 = m_delay_3.read_linear(mstosamps_191);
  87. double mix_196 = (m_y_1 + (clamp_173 * (tap_190 - m_y_1)));
  88. double mix_170 = mix_196;
  89. double mul_187 = (mix_170 * mul_168);
  90. m_cycle_14.freq(m_rate_4);
  91. double cycle_176 = m_cycle_14(__sinedata);
  92. double cycleindex_177 = m_cycle_14.phase();
  93. double add_175 = (cycle_176 + 1);
  94. double mul_174 = (add_175 * 0.5);
  95. double mul_179 = (m_depth_12 * mul_174);
  96. double add_178 = (m_delay_5 + mul_179);
  97. double mstosamps_169 = (add_178 * (samplerate * 0.001));
  98. double tap_184 = m_delay_2.read_linear(mstosamps_169);
  99. double mul_182 = (tap_184 * mul_165);
  100. double mul_180 = (tap_184 * mul_167);
  101. double add_186 = (mix_170 + mul_180);
  102. double mul_181 = (add_186 * mul_166);
  103. double add_185 = (mul_181 + mul_182);
  104. double mul_188 = (add_185 * mul_164);
  105. double out1 = (mul_188 + in1);
  106. double y0_next_193 = mix_170;
  107. m_delay_3.write((mul_187 + in1));
  108. m_delay_2.write(add_186);
  109. m_y_1 = y0_next_193;
  110. m_delay_2.step();
  111. m_delay_3.step();
  112. // assign results to output buffer;
  113. (*(__out1++)) = out1;
  114. };
  115. return __exception;
  116. };
  117. inline void set_rate(double _value) {
  118. m_rate_4 = (_value < 0.1 ? 0.1 : (_value > 10 ? 10 : _value));
  119. };
  120. inline void set_delay(double _value) {
  121. m_delay_5 = (_value < 0 ? 0 : (_value > 30 ? 30 : _value));
  122. };
  123. inline void set_bleed(double _value) {
  124. m_bleed_6 = (_value < 0 ? 0 : (_value > 70 ? 70 : _value));
  125. };
  126. inline void set_cutoff(double _value) {
  127. m_cutoff_7 = (_value < 0 ? 0 : (_value > 6000 ? 6000 : _value));
  128. };
  129. inline void set_repeats(double _value) {
  130. m_repeats_8 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  131. };
  132. inline void set_feedforward(double _value) {
  133. m_feedforward_9 = (_value < 70 ? 70 : (_value > 100 ? 100 : _value));
  134. };
  135. inline void set_time(double _value) {
  136. m_time_10 = (_value < 10 ? 10 : (_value > 1000 ? 1000 : _value));
  137. };
  138. inline void set_mix(double _value) {
  139. m_mix_11 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  140. };
  141. inline void set_depth(double _value) {
  142. m_depth_12 = (_value < 0 ? 0 : (_value > 5 ? 5 : _value));
  143. };
  144. inline void set_feedback(double _value) {
  145. m_feedback_13 = (_value < -70 ? -70 : (_value > 70 ? 70 : _value));
  146. };
  147. } State;
  148. ///
  149. /// Configuration for the genlib API
  150. ///
  151. /// Number of signal inputs and outputs
  152. int gen_kernel_numins = 1;
  153. int gen_kernel_numouts = 1;
  154. int num_inputs() { return gen_kernel_numins; }
  155. int num_outputs() { return gen_kernel_numouts; }
  156. int num_params() { return 10; }
  157. /// Assistive lables for the signal inputs and outputs
  158. const char * gen_kernel_innames[] = { "in1" };
  159. const char * gen_kernel_outnames[] = { "out1" };
  160. /// Invoke the signal process of a State object
  161. int perform(CommonState *cself, t_sample **ins, long numins, t_sample **outs, long numouts, long n) {
  162. State * self = (State *)cself;
  163. return self->perform(ins, outs, n);
  164. }
  165. /// Reset all parameters and stateful operators of a State object
  166. void reset(CommonState *cself) {
  167. State * self = (State *)cself;
  168. self->reset(cself->sr, cself->vs);
  169. }
  170. /// Set a parameter of a State object
  171. void setparameter(CommonState *cself, long index, double value, void *ref) {
  172. State * self = (State *)cself;
  173. switch (index) {
  174. case 0: self->set_rate(value); break;
  175. case 1: self->set_delay(value); break;
  176. case 2: self->set_bleed(value); break;
  177. case 3: self->set_cutoff(value); break;
  178. case 4: self->set_repeats(value); break;
  179. case 5: self->set_feedforward(value); break;
  180. case 6: self->set_time(value); break;
  181. case 7: self->set_mix(value); break;
  182. case 8: self->set_depth(value); break;
  183. case 9: self->set_feedback(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_rate_4; break;
  192. case 1: *value = self->m_delay_5; break;
  193. case 2: *value = self->m_bleed_6; break;
  194. case 3: *value = self->m_cutoff_7; break;
  195. case 4: *value = self->m_repeats_8; break;
  196. case 5: *value = self->m_feedforward_9; break;
  197. case 6: *value = self->m_time_10; break;
  198. case 7: *value = self->m_mix_11; break;
  199. case 8: *value = self->m_depth_12; break;
  200. case 9: *value = self->m_feedback_13; 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(10 * sizeof(ParamInfo));
  216. self->__commonstate.numparams = 10;
  217. // initialize parameter 0 ("m_rate_4")
  218. pi = self->__commonstate.params + 0;
  219. pi->name = "rate";
  220. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  221. pi->defaultvalue = self->m_rate_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.1;
  228. pi->outputmax = 10;
  229. pi->exp = 0;
  230. pi->units = ""; // no units defined
  231. // initialize parameter 1 ("m_delay_5")
  232. pi = self->__commonstate.params + 1;
  233. pi->name = "delay";
  234. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  235. pi->defaultvalue = self->m_delay_5;
  236. pi->defaultref = 0;
  237. pi->hasinputminmax = false;
  238. pi->inputmin = 0;
  239. pi->inputmax = 1;
  240. pi->hasminmax = true;
  241. pi->outputmin = 0;
  242. pi->outputmax = 30;
  243. pi->exp = 0;
  244. pi->units = ""; // no units defined
  245. // initialize parameter 2 ("m_bleed_6")
  246. pi = self->__commonstate.params + 2;
  247. pi->name = "bleed";
  248. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  249. pi->defaultvalue = self->m_bleed_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 = 70;
  257. pi->exp = 0;
  258. pi->units = ""; // no units defined
  259. // initialize parameter 3 ("m_cutoff_7")
  260. pi = self->__commonstate.params + 3;
  261. pi->name = "cutoff";
  262. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  263. pi->defaultvalue = self->m_cutoff_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 = 6000;
  271. pi->exp = 0;
  272. pi->units = ""; // no units defined
  273. // initialize parameter 4 ("m_repeats_8")
  274. pi = self->__commonstate.params + 4;
  275. pi->name = "repeats";
  276. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  277. pi->defaultvalue = self->m_repeats_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;
  284. pi->outputmax = 100;
  285. pi->exp = 0;
  286. pi->units = ""; // no units defined
  287. // initialize parameter 5 ("m_feedforward_9")
  288. pi = self->__commonstate.params + 5;
  289. pi->name = "feedforward";
  290. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  291. pi->defaultvalue = self->m_feedforward_9;
  292. pi->defaultref = 0;
  293. pi->hasinputminmax = false;
  294. pi->inputmin = 0;
  295. pi->inputmax = 1;
  296. pi->hasminmax = true;
  297. pi->outputmin = 70;
  298. pi->outputmax = 100;
  299. pi->exp = 0;
  300. pi->units = ""; // no units defined
  301. // initialize parameter 6 ("m_time_10")
  302. pi = self->__commonstate.params + 6;
  303. pi->name = "time";
  304. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  305. pi->defaultvalue = self->m_time_10;
  306. pi->defaultref = 0;
  307. pi->hasinputminmax = false;
  308. pi->inputmin = 0;
  309. pi->inputmax = 1;
  310. pi->hasminmax = true;
  311. pi->outputmin = 10;
  312. pi->outputmax = 1000;
  313. pi->exp = 0;
  314. pi->units = ""; // no units defined
  315. // initialize parameter 7 ("m_mix_11")
  316. pi = self->__commonstate.params + 7;
  317. pi->name = "mix";
  318. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  319. pi->defaultvalue = self->m_mix_11;
  320. pi->defaultref = 0;
  321. pi->hasinputminmax = false;
  322. pi->inputmin = 0;
  323. pi->inputmax = 1;
  324. pi->hasminmax = true;
  325. pi->outputmin = 0;
  326. pi->outputmax = 100;
  327. pi->exp = 0;
  328. pi->units = ""; // no units defined
  329. // initialize parameter 8 ("m_depth_12")
  330. pi = self->__commonstate.params + 8;
  331. pi->name = "depth";
  332. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  333. pi->defaultvalue = self->m_depth_12;
  334. pi->defaultref = 0;
  335. pi->hasinputminmax = false;
  336. pi->inputmin = 0;
  337. pi->inputmax = 1;
  338. pi->hasminmax = true;
  339. pi->outputmin = 0;
  340. pi->outputmax = 5;
  341. pi->exp = 0;
  342. pi->units = ""; // no units defined
  343. // initialize parameter 9 ("m_feedback_13")
  344. pi = self->__commonstate.params + 9;
  345. pi->name = "feedback";
  346. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  347. pi->defaultvalue = self->m_feedback_13;
  348. pi->defaultref = 0;
  349. pi->hasinputminmax = false;
  350. pi->inputmin = 0;
  351. pi->inputmax = 1;
  352. pi->hasminmax = true;
  353. pi->outputmin = -70;
  354. pi->outputmax = 70;
  355. pi->exp = 0;
  356. pi->units = ""; // no units defined
  357. return self;
  358. }
  359. /// Release all resources and memory used by a State object:
  360. void destroy(CommonState *cself) {
  361. State * self = (State *)cself;
  362. genlib_sysmem_freeptr(cself->params);
  363. delete self;
  364. }
  365. } // gen_exported::