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.

247 lines
7.3KB

  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_3;
  25. SineCycle m_cycle_7;
  26. SineData __sinedata;
  27. double m_depth_5;
  28. double m_tone_6;
  29. double samplerate;
  30. double m_rate_4;
  31. double m_y_2;
  32. double m_smth_1;
  33. int vectorsize;
  34. int __exception;
  35. // re-initialize all member variables;
  36. inline void reset(double __sr, int __vs) {
  37. __exception = 0;
  38. vectorsize = __vs;
  39. samplerate = __sr;
  40. m_smth_1 = 0;
  41. m_y_2 = 0;
  42. m_delay_3.reset("m_delay_3", 48000);
  43. m_rate_4 = 5;
  44. m_depth_5 = 1;
  45. m_tone_6 = 6000;
  46. m_cycle_7.reset(samplerate, 0);
  47. genlib_reset_complete(this);
  48. };
  49. // the signal processing routine;
  50. inline int perform(t_sample ** __ins, t_sample ** __outs, int __n) {
  51. vectorsize = __n;
  52. const t_sample * __in1 = __ins[0];
  53. t_sample * __out1 = __outs[0];
  54. if (__exception) {
  55. return __exception;
  56. } else if (( (__in1 == 0) || (__out1 == 0) )) {
  57. __exception = GENLIB_ERR_NULL_BUFFER;
  58. return __exception;
  59. };
  60. double expr_12601 = safediv(((m_tone_6 * 2) * 3.1415926535898), 48000);
  61. double sin_12592 = sin(expr_12601);
  62. double clamp_12593 = ((sin_12592 <= 1e-05) ? 1e-05 : ((sin_12592 >= 0.99999) ? 0.99999 : sin_12592));
  63. // the main sample loop;
  64. while ((__n--)) {
  65. const double in1 = (*(__in1++));
  66. double mix_12606 = (m_y_2 + (clamp_12593 * (in1 - m_y_2)));
  67. double mix_12590 = mix_12606;
  68. m_cycle_7.freq(m_rate_4);
  69. double cycle_12596 = m_cycle_7(__sinedata);
  70. double cycleindex_12597 = m_cycle_7.phase();
  71. double add_12595 = (cycle_12596 + 1);
  72. double mul_12594 = (add_12595 * 0.5);
  73. double mul_12598 = (m_depth_5 * mul_12594);
  74. double mstosamps_12589 = (mul_12598 * (samplerate * 0.001));
  75. double mix_12607 = (mstosamps_12589 + (0.999 * (m_smth_1 - mstosamps_12589)));
  76. double mix_12588 = mix_12607;
  77. double tap_12600 = m_delay_3.read_linear(mix_12588);
  78. double out1 = tap_12600;
  79. double y0_next_12602 = mix_12590;
  80. double smth_next_12603 = mix_12588;
  81. m_delay_3.write(mix_12590);
  82. m_y_2 = y0_next_12602;
  83. m_smth_1 = smth_next_12603;
  84. m_delay_3.step();
  85. // assign results to output buffer;
  86. (*(__out1++)) = out1;
  87. };
  88. return __exception;
  89. };
  90. inline void set_rate(double _value) {
  91. m_rate_4 = (_value < 0.1 ? 0.1 : (_value > 10 ? 10 : _value));
  92. };
  93. inline void set_depth(double _value) {
  94. m_depth_5 = (_value < 0 ? 0 : (_value > 5 ? 5 : _value));
  95. };
  96. inline void set_tone(double _value) {
  97. m_tone_6 = (_value < 500 ? 500 : (_value > 12000 ? 12000 : _value));
  98. };
  99. } State;
  100. ///
  101. /// Configuration for the genlib API
  102. ///
  103. /// Number of signal inputs and outputs
  104. int gen_kernel_numins = 1;
  105. int gen_kernel_numouts = 1;
  106. int num_inputs() { return gen_kernel_numins; }
  107. int num_outputs() { return gen_kernel_numouts; }
  108. int num_params() { return 3; }
  109. /// Assistive lables for the signal inputs and outputs
  110. const char * gen_kernel_innames[] = { "in1" };
  111. const char * gen_kernel_outnames[] = { "out1" };
  112. /// Invoke the signal process of a State object
  113. int perform(CommonState *cself, t_sample **ins, long numins, t_sample **outs, long numouts, long n) {
  114. State * self = (State *)cself;
  115. return self->perform(ins, outs, n);
  116. }
  117. /// Reset all parameters and stateful operators of a State object
  118. void reset(CommonState *cself) {
  119. State * self = (State *)cself;
  120. self->reset(cself->sr, cself->vs);
  121. }
  122. /// Set a parameter of a State object
  123. void setparameter(CommonState *cself, long index, double value, void *ref) {
  124. State * self = (State *)cself;
  125. switch (index) {
  126. case 0: self->set_rate(value); break;
  127. case 1: self->set_depth(value); break;
  128. case 2: self->set_tone(value); break;
  129. default: break;
  130. }
  131. }
  132. /// Get the value of a parameter of a State object
  133. void getparameter(CommonState *cself, long index, double *value) {
  134. State *self = (State *)cself;
  135. switch (index) {
  136. case 0: *value = self->m_rate_4; break;
  137. case 1: *value = self->m_depth_5; break;
  138. case 2: *value = self->m_tone_6; break;
  139. default: break;
  140. }
  141. }
  142. /// Allocate and configure a new State object and it's internal CommonState:
  143. void * create(double sr, long vs) {
  144. State *self = new State;
  145. self->reset(sr, vs);
  146. ParamInfo *pi;
  147. self->__commonstate.inputnames = gen_kernel_innames;
  148. self->__commonstate.outputnames = gen_kernel_outnames;
  149. self->__commonstate.numins = gen_kernel_numins;
  150. self->__commonstate.numouts = gen_kernel_numouts;
  151. self->__commonstate.sr = sr;
  152. self->__commonstate.vs = vs;
  153. self->__commonstate.params = (ParamInfo *)genlib_sysmem_newptr(3 * sizeof(ParamInfo));
  154. self->__commonstate.numparams = 3;
  155. // initialize parameter 0 ("m_rate_4")
  156. pi = self->__commonstate.params + 0;
  157. pi->name = "rate";
  158. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  159. pi->defaultvalue = self->m_rate_4;
  160. pi->defaultref = 0;
  161. pi->hasinputminmax = false;
  162. pi->inputmin = 0;
  163. pi->inputmax = 1;
  164. pi->hasminmax = true;
  165. pi->outputmin = 0.1;
  166. pi->outputmax = 10;
  167. pi->exp = 0;
  168. pi->units = ""; // no units defined
  169. // initialize parameter 1 ("m_depth_5")
  170. pi = self->__commonstate.params + 1;
  171. pi->name = "depth";
  172. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  173. pi->defaultvalue = self->m_depth_5;
  174. pi->defaultref = 0;
  175. pi->hasinputminmax = false;
  176. pi->inputmin = 0;
  177. pi->inputmax = 1;
  178. pi->hasminmax = true;
  179. pi->outputmin = 0;
  180. pi->outputmax = 5;
  181. pi->exp = 0;
  182. pi->units = ""; // no units defined
  183. // initialize parameter 2 ("m_tone_6")
  184. pi = self->__commonstate.params + 2;
  185. pi->name = "tone";
  186. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  187. pi->defaultvalue = self->m_tone_6;
  188. pi->defaultref = 0;
  189. pi->hasinputminmax = false;
  190. pi->inputmin = 0;
  191. pi->inputmax = 1;
  192. pi->hasminmax = true;
  193. pi->outputmin = 500;
  194. pi->outputmax = 12000;
  195. pi->exp = 0;
  196. pi->units = ""; // no units defined
  197. return self;
  198. }
  199. /// Release all resources and memory used by a State object:
  200. void destroy(CommonState *cself) {
  201. State * self = (State *)cself;
  202. genlib_sysmem_freeptr(cself->params);
  203. delete self;
  204. }
  205. } // gen_exported::