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.

250 lines
7.8KB

  1. #include "gen_exported.h"
  2. namespace gen_exported {
  3. /*******************************************************************************************************************
  4. Cycling '74 License for Max-Generated Code for Export
  5. Copyright (c) 2016 Cycling '74
  6. The code that Max generates automatically and that end users are capable of exporting and using, and any
  7. associated documentation files (the “Software”) is a work of authorship for which Cycling '74 is the author
  8. and owner for copyright purposes. A license is hereby granted, free of charge, to any person obtaining a
  9. copy of the Software (“Licensee”) to use, copy, modify, merge, publish, and distribute copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  11. The Software is licensed to Licensee only for non-commercial use. Users who wish to make commercial use of the
  12. Software must contact the copyright owner to determine if a license for commercial use is available, and the
  13. terms and conditions for same, which may include fees or royalties. For commercial use, please send inquiries
  14. to licensing (at) cycling74.com. The determination of whether a use is commercial use or non-commercial use is based
  15. upon the use, not the user. The Software may be used by individuals, institutions, governments, corporations, or
  16. other business whether for-profit or non-profit so long as the use itself is not a commercialization of the
  17. materials or a use that generates or is intended to generate income, revenue, sales or profit.
  18. The above copyright notice and this license shall be included in all copies or substantial portions of the Software.
  19. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  20. THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  22. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. *******************************************************************************************************************/
  25. // global noise generator
  26. Noise noise;
  27. static const int GENLIB_LOOPCOUNT_BAIL = 100000;
  28. // The State struct contains all the state and procedures for the gendsp kernel
  29. typedef struct State {
  30. CommonState __commonstate;
  31. int __exception;
  32. int vectorsize;
  33. t_sample samplerate;
  34. t_sample m_resolution_1;
  35. // re-initialize all member variables;
  36. inline void reset(t_param __sr, int __vs) {
  37. __exception = 0;
  38. vectorsize = __vs;
  39. samplerate = __sr;
  40. m_resolution_1 = 6;
  41. genlib_reset_complete(this);
  42. };
  43. // the signal processing routine;
  44. inline int perform(t_sample ** __ins, t_sample ** __outs, int __n) {
  45. vectorsize = __n;
  46. const t_sample * __in1 = __ins[0];
  47. t_sample * __out1 = __outs[0];
  48. t_sample * __out2 = __outs[1];
  49. if (__exception) {
  50. return __exception;
  51. } else if (( (__in1 == 0) || (__out1 == 0) || (__out2 == 0) )) {
  52. __exception = GENLIB_ERR_NULL_BUFFER;
  53. return __exception;
  54. };
  55. // the main sample loop;
  56. while ((__n--)) {
  57. const t_sample in1 = (*(__in1++));
  58. t_sample mul_649 = (in1 * m_resolution_1);
  59. t_sample ceil_648 = ceil(mul_649);
  60. t_sample div_647 = safediv(ceil_648, m_resolution_1);
  61. t_sample out1 = div_647;
  62. t_sample add_644 = (mul_649 + 0.5);
  63. t_sample floor_645 = floor(add_644);
  64. t_sample sub_643 = (floor_645 - 0.5);
  65. t_sample div_646 = safediv(sub_643, m_resolution_1);
  66. t_sample out2 = div_646;
  67. // assign results to output buffer;
  68. (*(__out1++)) = out1;
  69. (*(__out2++)) = out2;
  70. };
  71. return __exception;
  72. };
  73. inline void set_resolution(t_param _value) {
  74. m_resolution_1 = (_value < 0 ? 0 : (_value > 1 ? 1 : _value));
  75. };
  76. } State;
  77. ///
  78. /// Configuration for the genlib API
  79. ///
  80. /// Number of signal inputs and outputs
  81. int gen_kernel_numins = 1;
  82. int gen_kernel_numouts = 2;
  83. int num_inputs() { return gen_kernel_numins; }
  84. int num_outputs() { return gen_kernel_numouts; }
  85. int num_params() { return 1; }
  86. /// Assistive lables for the signal inputs and outputs
  87. const char *gen_kernel_innames[] = { "in1" };
  88. const char *gen_kernel_outnames[] = { "out1", "out2" };
  89. /// Invoke the signal process of a State object
  90. int perform(CommonState *cself, t_sample **ins, long numins, t_sample **outs, long numouts, long n) {
  91. State* self = (State *)cself;
  92. return self->perform(ins, outs, n);
  93. }
  94. /// Reset all parameters and stateful operators of a State object
  95. void reset(CommonState *cself) {
  96. State* self = (State *)cself;
  97. self->reset(cself->sr, cself->vs);
  98. }
  99. /// Set a parameter of a State object
  100. void setparameter(CommonState *cself, long index, t_param value, void *ref) {
  101. State *self = (State *)cself;
  102. switch (index) {
  103. case 0: self->set_resolution(value); break;
  104. default: break;
  105. }
  106. }
  107. /// Get the value of a parameter of a State object
  108. void getparameter(CommonState *cself, long index, t_param *value) {
  109. State *self = (State *)cself;
  110. switch (index) {
  111. case 0: *value = self->m_resolution_1; break;
  112. default: break;
  113. }
  114. }
  115. /// Get the name of a parameter of a State object
  116. const char *getparametername(CommonState *cself, long index) {
  117. if (index >= 0 && index < cself->numparams) {
  118. return cself->params[index].name;
  119. }
  120. return 0;
  121. }
  122. /// Get the minimum value of a parameter of a State object
  123. t_param getparametermin(CommonState *cself, long index) {
  124. if (index >= 0 && index < cself->numparams) {
  125. return cself->params[index].outputmin;
  126. }
  127. return 0;
  128. }
  129. /// Get the maximum value of a parameter of a State object
  130. t_param getparametermax(CommonState *cself, long index) {
  131. if (index >= 0 && index < cself->numparams) {
  132. return cself->params[index].outputmax;
  133. }
  134. return 0;
  135. }
  136. /// Get parameter of a State object has a minimum and maximum value
  137. char getparameterhasminmax(CommonState *cself, long index) {
  138. if (index >= 0 && index < cself->numparams) {
  139. return cself->params[index].hasminmax;
  140. }
  141. return 0;
  142. }
  143. /// Get the units of a parameter of a State object
  144. const char *getparameterunits(CommonState *cself, long index) {
  145. if (index >= 0 && index < cself->numparams) {
  146. return cself->params[index].units;
  147. }
  148. return 0;
  149. }
  150. /// Get the size of the state of all parameters of a State object
  151. size_t getstatesize(CommonState *cself) {
  152. return genlib_getstatesize(cself, &getparameter);
  153. }
  154. /// Get the state of all parameters of a State object
  155. short getstate(CommonState *cself, char *state) {
  156. return genlib_getstate(cself, state, &getparameter);
  157. }
  158. /// set the state of all parameters of a State object
  159. short setstate(CommonState *cself, const char *state) {
  160. return genlib_setstate(cself, state, &setparameter);
  161. }
  162. /// Allocate and configure a new State object and it's internal CommonState:
  163. void *create(t_param sr, long vs) {
  164. State *self = new State;
  165. self->reset(sr, vs);
  166. ParamInfo *pi;
  167. self->__commonstate.inputnames = gen_kernel_innames;
  168. self->__commonstate.outputnames = gen_kernel_outnames;
  169. self->__commonstate.numins = gen_kernel_numins;
  170. self->__commonstate.numouts = gen_kernel_numouts;
  171. self->__commonstate.sr = sr;
  172. self->__commonstate.vs = vs;
  173. self->__commonstate.params = (ParamInfo *)genlib_sysmem_newptr(1 * sizeof(ParamInfo));
  174. self->__commonstate.numparams = 1;
  175. // initialize parameter 0 ("m_resolution_1")
  176. pi = self->__commonstate.params + 0;
  177. pi->name = "resolution";
  178. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  179. pi->defaultvalue = self->m_resolution_1;
  180. pi->defaultref = 0;
  181. pi->hasinputminmax = false;
  182. pi->inputmin = 0;
  183. pi->inputmax = 1;
  184. pi->hasminmax = true;
  185. pi->outputmin = 0;
  186. pi->outputmax = 1;
  187. pi->exp = 0;
  188. pi->units = ""; // no units defined
  189. return self;
  190. }
  191. /// Release all resources and memory used by a State object:
  192. void destroy(CommonState *cself) {
  193. State *self = (State *)cself;
  194. genlib_sysmem_freeptr(cself->params);
  195. delete self;
  196. }
  197. } // gen_exported::