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_mix_9;
  30. double m_rate_8;
  31. double m_cutoff_10;
  32. double m_morph_7;
  33. double m_time_5;
  34. double m_repeats_4;
  35. double m_depth_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_repeats_4 = 75;
  49. m_time_5 = 500;
  50. m_depth_6 = 1;
  51. m_morph_7 = 50;
  52. m_rate_8 = 2;
  53. m_mix_9 = 100;
  54. m_cutoff_10 = 2500;
  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_5140 = (m_mix_9 * 0.01);
  71. double mstosamps_5165 = (m_time_5 * (samplerate * 0.001));
  72. double expr_5147 = safediv(((m_cutoff_10 * 2) * 3.1415926535898), 48000);
  73. double sin_5148 = sin(expr_5147);
  74. double clamp_5149 = ((sin_5148 <= 1e-05) ? 1e-05 : ((sin_5148 >= 0.99999) ? 0.99999 : sin_5148));
  75. double mul_5144 = (m_repeats_4 * 0.01);
  76. double mul_5168 = (m_morph_7 * -1.4);
  77. double add_5170 = (mul_5168 + 70);
  78. double abs_5169 = fabs(add_5170);
  79. double mul_5142 = (abs_5169 * 0.01);
  80. double mul_5166 = (m_morph_7 * 1.4);
  81. double sub_5167 = (mul_5166 - 70);
  82. double mul_5143 = (sub_5167 * 0.01);
  83. int lt_5177 = (m_morph_7 < 50);
  84. double mul_5181 = (m_morph_7 * -0.6);
  85. double add_5180 = (mul_5181 + 30);
  86. double mul_5176 = (lt_5177 * add_5180);
  87. int gt_5172 = (m_morph_7 > 50);
  88. double mul_5171 = (gt_5172 * m_morph_7);
  89. double mul_5173 = (mul_5171 * -0.6);
  90. double add_5174 = (mul_5173 + 130);
  91. double clamp_5175 = ((add_5174 <= 70) ? 70 : ((add_5174 >= 100) ? 100 : add_5174));
  92. double mul_5141 = (clamp_5175 * 0.01);
  93. // the main sample loop;
  94. while ((__n--)) {
  95. const double in1 = (*(__in1++));
  96. double tap_5164 = m_delay_3.read_cubic(mstosamps_5165);
  97. double mix_5185 = (m_y_1 + (clamp_5149 * (tap_5164 - m_y_1)));
  98. double mix_5146 = mix_5185;
  99. double mul_5161 = (mix_5146 * mul_5144);
  100. double dcblock_5138 = m_dcblock_11(mul_5161);
  101. double clamp_5139 = ((dcblock_5138 <= -1) ? -1 : ((dcblock_5138 >= 1) ? 1 : dcblock_5138));
  102. m_cycle_12.freq(m_rate_8);
  103. double cycle_5152 = m_cycle_12(__sinedata);
  104. double cycleindex_5153 = m_cycle_12.phase();
  105. double add_5151 = (cycle_5152 + 1);
  106. double mul_5150 = (add_5151 * 0.5);
  107. double mul_5154 = (m_depth_6 * mul_5150);
  108. double add_5178 = (mul_5176 + mul_5154);
  109. double mstosamps_5145 = (add_5178 * (samplerate * 0.001));
  110. double tap_5159 = m_delay_2.read_linear(mstosamps_5145);
  111. double mul_5157 = (tap_5159 * mul_5141);
  112. double mul_5155 = (tap_5159 * mul_5143);
  113. double add_5160 = (mix_5146 + mul_5155);
  114. double mul_5156 = (add_5160 * mul_5142);
  115. double add_5179 = (mul_5156 + mul_5157);
  116. double mul_5162 = (add_5179 * mul_5140);
  117. double out1 = (mul_5162 + in1);
  118. double y0_next_5182 = mix_5146;
  119. m_delay_3.write((clamp_5139 + in1));
  120. m_delay_2.write(add_5160);
  121. m_y_1 = y0_next_5182;
  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_repeats(double _value) {
  130. m_repeats_4 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  131. };
  132. inline void set_time(double _value) {
  133. m_time_5 = (_value < 20 ? 20 : (_value > 1000 ? 1000 : _value));
  134. };
  135. inline void set_depth(double _value) {
  136. m_depth_6 = (_value < 0 ? 0 : (_value > 5 ? 5 : _value));
  137. };
  138. inline void set_morph(double _value) {
  139. m_morph_7 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  140. };
  141. inline void set_rate(double _value) {
  142. m_rate_8 = (_value < 0.1 ? 0.1 : (_value > 10 ? 10 : _value));
  143. };
  144. inline void set_mix(double _value) {
  145. m_mix_9 = (_value < 0 ? 0 : (_value > 100 ? 100 : _value));
  146. };
  147. inline void set_cutoff(double _value) {
  148. m_cutoff_10 = (_value < 500 ? 500 : (_value > 6000 ? 6000 : _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_repeats(value); break;
  178. case 1: self->set_time(value); break;
  179. case 2: self->set_depth(value); break;
  180. case 3: self->set_morph(value); break;
  181. case 4: self->set_rate(value); break;
  182. case 5: self->set_mix(value); break;
  183. case 6: self->set_cutoff(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_repeats_4; break;
  192. case 1: *value = self->m_time_5; break;
  193. case 2: *value = self->m_depth_6; break;
  194. case 3: *value = self->m_morph_7; break;
  195. case 4: *value = self->m_rate_8; break;
  196. case 5: *value = self->m_mix_9; break;
  197. case 6: *value = self->m_cutoff_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_repeats_4")
  215. pi = self->__commonstate.params + 0;
  216. pi->name = "repeats";
  217. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  218. pi->defaultvalue = self->m_repeats_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_time_5")
  229. pi = self->__commonstate.params + 1;
  230. pi->name = "time";
  231. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  232. pi->defaultvalue = self->m_time_5;
  233. pi->defaultref = 0;
  234. pi->hasinputminmax = false;
  235. pi->inputmin = 0;
  236. pi->inputmax = 1;
  237. pi->hasminmax = true;
  238. pi->outputmin = 20;
  239. pi->outputmax = 1000;
  240. pi->exp = 0;
  241. pi->units = ""; // no units defined
  242. // initialize parameter 2 ("m_depth_6")
  243. pi = self->__commonstate.params + 2;
  244. pi->name = "depth";
  245. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  246. pi->defaultvalue = self->m_depth_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;
  253. pi->outputmax = 5;
  254. pi->exp = 0;
  255. pi->units = ""; // no units defined
  256. // initialize parameter 3 ("m_morph_7")
  257. pi = self->__commonstate.params + 3;
  258. pi->name = "morph";
  259. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  260. pi->defaultvalue = self->m_morph_7;
  261. pi->defaultref = 0;
  262. pi->hasinputminmax = false;
  263. pi->inputmin = 0;
  264. pi->inputmax = 1;
  265. pi->hasminmax = true;
  266. pi->outputmin = 0;
  267. pi->outputmax = 100;
  268. pi->exp = 0;
  269. pi->units = ""; // no units defined
  270. // initialize parameter 4 ("m_rate_8")
  271. pi = self->__commonstate.params + 4;
  272. pi->name = "rate";
  273. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  274. pi->defaultvalue = self->m_rate_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.1;
  281. pi->outputmax = 10;
  282. pi->exp = 0;
  283. pi->units = ""; // no units defined
  284. // initialize parameter 5 ("m_mix_9")
  285. pi = self->__commonstate.params + 5;
  286. pi->name = "mix";
  287. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  288. pi->defaultvalue = self->m_mix_9;
  289. pi->defaultref = 0;
  290. pi->hasinputminmax = false;
  291. pi->inputmin = 0;
  292. pi->inputmax = 1;
  293. pi->hasminmax = true;
  294. pi->outputmin = 0;
  295. pi->outputmax = 100;
  296. pi->exp = 0;
  297. pi->units = ""; // no units defined
  298. // initialize parameter 6 ("m_cutoff_10")
  299. pi = self->__commonstate.params + 6;
  300. pi->name = "cutoff";
  301. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  302. pi->defaultvalue = self->m_cutoff_10;
  303. pi->defaultref = 0;
  304. pi->hasinputminmax = false;
  305. pi->inputmin = 0;
  306. pi->inputmax = 1;
  307. pi->hasminmax = true;
  308. pi->outputmin = 500;
  309. pi->outputmax = 6000;
  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::