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.

185 lines
5.4KB

  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. double m_resolution_1;
  25. double samplerate;
  26. int vectorsize;
  27. int __exception;
  28. // re-initialize all member variables;
  29. inline void reset(double __sr, int __vs) {
  30. __exception = 0;
  31. vectorsize = __vs;
  32. samplerate = __sr;
  33. m_resolution_1 = 6;
  34. genlib_reset_complete(this);
  35. };
  36. // the signal processing routine;
  37. inline int perform(t_sample ** __ins, t_sample ** __outs, int __n) {
  38. vectorsize = __n;
  39. const t_sample * __in1 = __ins[0];
  40. t_sample * __out1 = __outs[0];
  41. t_sample * __out2 = __outs[1];
  42. if (__exception) {
  43. return __exception;
  44. } else if (( (__in1 == 0) || (__out1 == 0) || (__out2 == 0) )) {
  45. __exception = GENLIB_ERR_NULL_BUFFER;
  46. return __exception;
  47. };
  48. // the main sample loop;
  49. while ((__n--)) {
  50. const double in1 = (*(__in1++));
  51. double mul_50 = (in1 * m_resolution_1);
  52. double ceil_49 = ceil(mul_50);
  53. double div_48 = safediv(ceil_49, m_resolution_1);
  54. double out1 = div_48;
  55. double add_45 = (mul_50 + 0.5);
  56. double floor_46 = floor(add_45);
  57. double sub_44 = (floor_46 - 0.5);
  58. double div_47 = safediv(sub_44, m_resolution_1);
  59. double out2 = div_47;
  60. // assign results to output buffer;
  61. (*(__out1++)) = out1;
  62. (*(__out2++)) = out2;
  63. };
  64. return __exception;
  65. };
  66. inline void set_resolution(double _value) {
  67. m_resolution_1 = (_value < 1 ? 1 : (_value > 16 ? 16 : _value));
  68. };
  69. } State;
  70. ///
  71. /// Configuration for the genlib API
  72. ///
  73. /// Number of signal inputs and outputs
  74. const int gen_kernel_numins = 1;
  75. const int gen_kernel_numouts = 2;
  76. int num_inputs() { return gen_kernel_numins; }
  77. int num_outputs() { return gen_kernel_numouts; }
  78. int num_params() { return 1; }
  79. /// Assistive lables for the signal inputs and outputs
  80. const char * gen_kernel_innames[] = { "in1" };
  81. const char * gen_kernel_outnames[] = { "out1", "out2" };
  82. /// Invoke the signal process of a State object
  83. int perform(CommonState *cself, t_sample **ins, long numins, t_sample **outs, long numouts, long n) {
  84. State * self = (State *)cself;
  85. return self->perform(ins, outs, n);
  86. }
  87. /// Reset all parameters and stateful operators of a State object
  88. void reset(CommonState *cself) {
  89. State * self = (State *)cself;
  90. self->reset(cself->sr, cself->vs);
  91. }
  92. /// Set a parameter of a State object
  93. void setparameter(CommonState *cself, long index, t_param value, void *ref) {
  94. State * self = (State *)cself;
  95. switch (index) {
  96. case 0: self->set_resolution(value); break;
  97. default: break;
  98. }
  99. }
  100. /// Get the value of a parameter of a State object
  101. void getparameter(CommonState *cself, long index, t_param *value) {
  102. State *self = (State *)cself;
  103. switch (index) {
  104. case 0: *value = self->m_resolution_1; break;
  105. default: break;
  106. }
  107. }
  108. /// Allocate and configure a new State object and it's internal CommonState:
  109. void * create(t_param sr, long vs) {
  110. State *self = new State;
  111. self->reset(sr, vs);
  112. ParamInfo *pi;
  113. self->__commonstate.inputnames = gen_kernel_innames;
  114. self->__commonstate.outputnames = gen_kernel_outnames;
  115. self->__commonstate.numins = gen_kernel_numins;
  116. self->__commonstate.numouts = gen_kernel_numouts;
  117. self->__commonstate.sr = sr;
  118. self->__commonstate.vs = vs;
  119. self->__commonstate.params = (ParamInfo *)genlib_sysmem_newptr(1 * sizeof(ParamInfo));
  120. self->__commonstate.numparams = 1;
  121. // initialize parameter 0 ("m_resolution_1")
  122. pi = self->__commonstate.params + 0;
  123. pi->name = "resolution";
  124. pi->paramtype = GENLIB_PARAMTYPE_FLOAT;
  125. pi->defaultvalue = self->m_resolution_1;
  126. pi->defaultref = 0;
  127. pi->hasinputminmax = false;
  128. pi->inputmin = 0;
  129. pi->inputmax = 1;
  130. pi->hasminmax = true;
  131. pi->outputmin = 1;
  132. pi->outputmax = 16;
  133. pi->exp = 0;
  134. pi->units = "bits"; // no units defined
  135. return self;
  136. }
  137. /// Release all resources and memory used by a State object:
  138. void destroy(CommonState *cself) {
  139. State * self = (State *)cself;
  140. genlib_sysmem_freeptr(cself->params);
  141. delete self;
  142. }
  143. } // gen_exported::