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.

359 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. Delay m_delay_5;
  25. Delta m_delta_14;
  26. Delta m_delta_11;
  27. Delta m_delta_20;
  28. Delta m_delta_17;
  29. Phasor m_phasor_10;
  30. Sah m_sah_13;
  31. Sah m_sah_12;
  32. Sah m_sah_15;
  33. Sah m_sah_16;
  34. Sah m_sah_21;
  35. Sah m_sah_19;
  36. Sah m_sah_18;
  37. Sah m_sah_22;
  38. t_sample samples_to_seconds;
  39. t_sample m_ratio_8;
  40. t_sample m_xfade_9;
  41. t_sample m_history_1;
  42. t_sample samplerate;
  43. t_sample m_history_3;
  44. t_sample m_history_2;
  45. t_sample m_blur_6;
  46. t_sample m_window_7;
  47. t_sample m_history_4;
  48. int vectorsize;
  49. int __exception;
  50. // re-initialize all member variables;
  51. inline void reset(t_sample __sr, int __vs) {
  52. __exception = 0;
  53. vectorsize = __vs;
  54. samplerate = __sr;
  55. m_history_1 = 0;
  56. m_history_2 = 0;
  57. m_history_3 = 0;
  58. m_history_4 = 0;
  59. m_delay_5.reset("m_delay_5", 88200);
  60. m_blur_6 = 0;
  61. m_window_7 = 100;
  62. m_ratio_8 = 1;
  63. m_xfade_9 = 1;
  64. samples_to_seconds = (1 / samplerate);
  65. m_phasor_10.reset(0);
  66. m_delta_11.reset(0);
  67. m_sah_12.reset(0);
  68. m_sah_13.reset(0);
  69. m_delta_14.reset(0);
  70. m_sah_15.reset(0);
  71. m_sah_16.reset(0);
  72. m_delta_17.reset(0);
  73. m_sah_18.reset(0);
  74. m_sah_19.reset(0);
  75. m_delta_20.reset(0);
  76. m_sah_21.reset(0);
  77. m_sah_22.reset(0);
  78. genlib_reset_complete(this);
  79. };
  80. // the signal processing routine;
  81. inline int perform(t_sample ** __ins, t_sample ** __outs, int __n) {
  82. vectorsize = __n;
  83. const t_sample * __in1 = __ins[0];
  84. t_sample * __out1 = __outs[0];
  85. t_sample * __out2 = __outs[1];
  86. if (__exception) {
  87. return __exception;
  88. } else if (( (__in1 == 0) || (__out1 == 0) || (__out2 == 0) )) {
  89. __exception = GENLIB_ERR_NULL_BUFFER;
  90. return __exception;
  91. };
  92. t_sample mstosamps_1223 = (m_window_7 * (samplerate * 0.001));
  93. t_sample rsub_1209 = (1 - m_ratio_8);
  94. t_sample mul_1208 = (rsub_1209 * 1000);
  95. t_sample div_1207 = safediv(mul_1208, m_window_7);
  96. samples_to_seconds = (1 / samplerate);
  97. // the main sample loop;
  98. while ((__n--)) {
  99. const t_sample in1 = (*(__in1++));
  100. t_sample noise_1159 = noise();
  101. t_sample abs_1177 = fabs(noise_1159);
  102. t_sample mul_1183 = (abs_1177 * m_blur_6);
  103. t_sample noise_1161 = noise();
  104. t_sample abs_1179 = fabs(noise_1161);
  105. t_sample mul_1189 = (abs_1179 * m_blur_6);
  106. t_sample noise_1160 = noise();
  107. t_sample abs_1178 = fabs(noise_1160);
  108. t_sample mul_1186 = (abs_1178 * m_blur_6);
  109. t_sample noise_1158 = noise();
  110. t_sample abs_1176 = fabs(noise_1158);
  111. t_sample mul_1180 = (abs_1176 * m_blur_6);
  112. t_sample phasor_1229 = m_phasor_10(div_1207, samples_to_seconds);
  113. t_sample add_1206 = ((m_history_4 + phasor_1229) + 0.5);
  114. t_sample mod_1205 = safemod(add_1206, 1);
  115. t_sample delta_1185 = m_delta_11(mod_1205);
  116. t_sample sah_1164 = m_sah_12(mul_1183, delta_1185, 0);
  117. t_sample sah_1184 = m_sah_13(mstosamps_1223, delta_1185, 0);
  118. t_sample mul_1173 = (sah_1184 * mod_1205);
  119. t_sample sub_1204 = (mod_1205 - 0.5);
  120. t_sample mul_1203 = (sub_1204 * 3.1415926535898);
  121. t_sample cos_1202 = cos(mul_1203);
  122. t_sample mul_1192 = (cos_1202 * cos_1202);
  123. t_sample add_1228 = ((m_history_3 + phasor_1229) + 0);
  124. t_sample mod_1227 = safemod(add_1228, 1);
  125. t_sample delta_1169 = m_delta_14(mod_1227);
  126. t_sample sah_1168 = m_sah_15(mul_1189, delta_1169, 0);
  127. t_sample sah_1190 = m_sah_16(mstosamps_1223, delta_1169, 0);
  128. t_sample mul_1175 = (sah_1190 * mod_1227);
  129. t_sample sub_1226 = (mod_1227 - 0.5);
  130. t_sample mul_1225 = (sub_1226 * 3.1415926535898);
  131. t_sample cos_1224 = cos(mul_1225);
  132. t_sample mul_1194 = (cos_1224 * cos_1224);
  133. t_sample add_1222 = ((m_history_2 + phasor_1229) + 0.25);
  134. t_sample mod_1221 = safemod(add_1222, 1);
  135. t_sample delta_1188 = m_delta_17(mod_1221);
  136. t_sample sah_1166 = m_sah_18(mul_1186, delta_1188, 0);
  137. t_sample sah_1187 = m_sah_19(mstosamps_1223, delta_1188, 0);
  138. t_sample mul_1174 = (sah_1187 * mod_1221);
  139. t_sample sub_1220 = (mod_1221 - 0.5);
  140. t_sample mul_1219 = (sub_1220 * 3.1415926535898);
  141. t_sample cos_1218 = cos(mul_1219);
  142. t_sample mul_1193 = (cos_1218 * cos_1218);
  143. t_sample add_1200 = ((m_history_1 + phasor_1229) + 0.75);
  144. t_sample mod_1199 = safemod(add_1200, 1);
  145. t_sample delta_1182 = m_delta_20(mod_1199);
  146. t_sample sah_1162 = m_sah_21(mul_1180, delta_1182, 0);
  147. t_sample sah_1181 = m_sah_22(mstosamps_1223, delta_1182, 0);
  148. t_sample mul_1172 = (sah_1181 * mod_1199);
  149. t_sample tap_1214 = m_delay_5.read_linear(mul_1175);
  150. t_sample tap_1215 = m_delay_5.read_linear(mul_1174);
  151. t_sample tap_1216 = m_delay_5.read_linear(mul_1173);
  152. t_sample tap_1217 = m_delay_5.read_linear(mul_1172);
  153. t_sample mul_1212 = (tap_1214 * mul_1194);
  154. t_sample mul_1201 = (tap_1216 * mul_1192);
  155. t_sample add_1245 = (mul_1201 + mul_1212);
  156. t_sample mix_1244 = (in1 + (m_xfade_9 * (add_1245 - in1)));
  157. t_sample out1 = mix_1244;
  158. t_sample mul_1211 = (tap_1215 * mul_1193);
  159. t_sample sub_1198 = (mod_1199 - 0.5);
  160. t_sample mul_1197 = (sub_1198 * 3.1415926535898);
  161. t_sample cos_1196 = cos(mul_1197);
  162. t_sample mul_1191 = (cos_1196 * cos_1196);
  163. t_sample mul_1195 = (tap_1217 * mul_1191);
  164. t_sample add_1247 = (mul_1195 + mul_1211);
  165. t_sample mix_1246 = (in1 + (m_xfade_9 * (add_1247 - in1)));
  166. t_sample out2 = mix_1246;
  167. t_sample history_1165_next_1240 = sah_1164;
  168. t_sample history_1170_next_1241 = sah_1168;
  169. t_sample history_1167_next_1242 = sah_1166;
  170. t_sample history_1163_next_1243 = sah_1162;
  171. m_delay_5.write(in1);
  172. m_history_4 = history_1165_next_1240;
  173. m_history_3 = history_1170_next_1241;
  174. m_history_2 = history_1167_next_1242;
  175. m_history_1 = history_1163_next_1243;
  176. m_delay_5.step();
  177. // assign results to output buffer;
  178. (*(__out1++)) = out1;
  179. (*(__out2++)) = out2;
  180. };
  181. return __exception;
  182. };
  183. inline void set_blur(t_sample _value) {
  184. m_blur_6 = (_value < 0 ? 0 : (_value > 0.25 ? 0.25 : _value));
  185. };
  186. inline void set_window(t_sample _value) {
  187. m_window_7 = (_value < 0.1 ? 0.1 : (_value > 1000 ? 1000 : _value));
  188. };
  189. inline void set_ratio(t_sample _value) {
  190. m_ratio_8 = (_value < 0.25 ? 0.25 : (_value > 4 ? 4 : _value));
  191. };
  192. inline void set_xfade(t_sample _value) {
  193. m_xfade_9 = (_value < 0 ? 0 : (_value > 1 ? 1 : _value));
  194. };
  195. } State;
  196. ///
  197. /// Configuration for the genlib API
  198. ///
  199. /// Number of signal inputs and outputs
  200. const int gen_kernel_numins = 1;
  201. const int gen_kernel_numouts = 2;
  202. int num_inputs() { return gen_kernel_numins; }
  203. int num_outputs() { return gen_kernel_numouts; }
  204. int num_params() { return 4; }
  205. /// Assistive lables for the signal inputs and outputs
  206. const char * gen_kernel_innames[] = { "in1" };
  207. const char * gen_kernel_outnames[] = { "out1", "out2" };
  208. /// Invoke the signal process of a State object
  209. int perform(CommonState *cself, t_sample **ins, long numins, t_sample **outs, long numouts, long n) {
  210. State * self = (State *)cself;
  211. return self->perform(ins, outs, n);
  212. }
  213. /// Reset all parameters and stateful operators of a State object
  214. void reset(CommonState *cself) {
  215. State * self = (State *)cself;
  216. self->reset(cself->sr, cself->vs);
  217. }
  218. /// Set a parameter of a State object
  219. void setparameter(CommonState *cself, long index, t_param value, void *ref) {
  220. State * self = (State *)cself;
  221. switch (index) {
  222. case 0: self->set_blur(value); break;
  223. case 1: self->set_window(value); break;
  224. case 2: self->set_ratio(value); break;
  225. case 3: self->set_xfade(value); break;
  226. default: break;
  227. }
  228. }
  229. /// Get the value of a parameter of a State object
  230. void getparameter(CommonState *cself, long index, t_param *value) {
  231. State *self = (State *)cself;
  232. switch (index) {
  233. case 0: *value = self->m_blur_6; break;
  234. case 1: *value = self->m_window_7; break;
  235. case 2: *value = self->m_ratio_8; break;
  236. case 3: *value = self->m_xfade_9; break;
  237. default: break;
  238. }
  239. }
  240. /// Allocate and configure a new State object and it's internal CommonState:
  241. void * create(t_param sr, long vs) {
  242. State *self = new State;
  243. self->reset(sr, vs);
  244. ParamInfo *pi;
  245. self->__commonstate.inputnames = gen_kernel_innames;
  246. self->__commonstate.outputnames = gen_kernel_outnames;
  247. self->__commonstate.numins = gen_kernel_numins;
  248. self->__commonstate.numouts = gen_kernel_numouts;
  249. self->__commonstate.sr = sr;
  250. self->__commonstate.vs = vs;
  251. self->__commonstate.params = (ParamInfo *)genlib_sysmem_newptr(4 * sizeof(ParamInfo));
  252. self->__commonstate.numparams = 4;
  253. // initialize parameter 0 ("m_blur_6")
  254. pi = self->__commonstate.params + 0;
  255. pi->name = "blur";
  256. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  257. pi->defaultvalue = self->m_blur_6;
  258. pi->defaultref = 0;
  259. pi->hasinputminmax = false;
  260. pi->inputmin = 0;
  261. pi->inputmax = 1;
  262. pi->hasminmax = true;
  263. pi->outputmin = 0;
  264. pi->outputmax = 0.25;
  265. pi->exp = 0;
  266. pi->units = ""; // no units defined
  267. // initialize parameter 1 ("m_window_7")
  268. pi = self->__commonstate.params + 1;
  269. pi->name = "window";
  270. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  271. pi->defaultvalue = self->m_window_7;
  272. pi->defaultref = 0;
  273. pi->hasinputminmax = false;
  274. pi->inputmin = 0;
  275. pi->inputmax = 1;
  276. pi->hasminmax = true;
  277. pi->outputmin = 0.1;
  278. pi->outputmax = 1000;
  279. pi->exp = 0;
  280. pi->units = ""; // no units defined
  281. // initialize parameter 2 ("m_ratio_8")
  282. pi = self->__commonstate.params + 2;
  283. pi->name = "ratio";
  284. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  285. pi->defaultvalue = self->m_ratio_8;
  286. pi->defaultref = 0;
  287. pi->hasinputminmax = false;
  288. pi->inputmin = 0;
  289. pi->inputmax = 1;
  290. pi->hasminmax = true;
  291. pi->outputmin = 0.25;
  292. pi->outputmax = 4;
  293. pi->exp = 0;
  294. pi->units = ""; // no units defined
  295. // initialize parameter 3 ("m_xfade_9")
  296. pi = self->__commonstate.params + 3;
  297. pi->name = "xfade";
  298. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  299. pi->defaultvalue = self->m_xfade_9;
  300. pi->defaultref = 0;
  301. pi->hasinputminmax = false;
  302. pi->inputmin = 0;
  303. pi->inputmax = 1;
  304. pi->hasminmax = true;
  305. pi->outputmin = 0;
  306. pi->outputmax = 1;
  307. pi->exp = 0;
  308. pi->units = ""; // no units defined
  309. return self;
  310. }
  311. /// Release all resources and memory used by a State object:
  312. void destroy(CommonState *cself) {
  313. State * self = (State *)cself;
  314. genlib_sysmem_freeptr(cself->params);
  315. delete self;
  316. }
  317. } // gen_exported::