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.

226 lines
6.8KB

  1. /******************************************************************************
  2. * Copyright 2017-2018 Valerio Orlandini / Sonus Dept. <sonusdept@gmail.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *****************************************************************************/
  17. #include "sonusmodular.hpp"
  18. namespace rack_plugin_SonusModular {
  19. struct Mrcheb : Module
  20. {
  21. enum ParamIds
  22. {
  23. S1K,
  24. S2K,
  25. S3K,
  26. S4K,
  27. S5K,
  28. S6K,
  29. S7K,
  30. S8K,
  31. S9K,
  32. RANGE,
  33. NUM_PARAMS
  34. };
  35. enum InputIds
  36. {
  37. INPUT,
  38. NUM_INPUTS
  39. };
  40. enum OutputIds
  41. {
  42. S1,
  43. S2,
  44. S3,
  45. S4,
  46. S5,
  47. S6,
  48. S7,
  49. S8,
  50. S9,
  51. OUTPUT,
  52. NUM_OUTPUTS
  53. };
  54. enum LightIds
  55. {
  56. NUM_LIGHTS
  57. };
  58. Mrcheb() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  59. void step() override;
  60. };
  61. bool chebyshev_poly(const float &input, float &output, unsigned int number = 1)
  62. {
  63. output = 0.0;
  64. switch (number)
  65. {
  66. case 0:
  67. output = 1.0;
  68. break;
  69. case 1:
  70. output = input;
  71. break;
  72. case 2:
  73. output = 2.0 * pow(input, 2.0) - 1.0;
  74. break;
  75. case 3:
  76. output = 4.0 * pow(input, 3.0) - 3.0 * input;
  77. break;
  78. case 4:
  79. output = 8.0 * pow(input, 4.0) - 8.0 * pow(input, 2.0) + 1.0;
  80. break;
  81. case 5:
  82. output = 16.0 * pow(input, 5.0) - 20.0 * pow(input, 3.0) + 5.0 * input;
  83. break;
  84. case 6:
  85. output = 32.0 * pow(input, 6.0) - 48.0 * pow(input, 4.0) + 18.0 * pow(input, 2.0) - 1.0;
  86. break;
  87. case 7:
  88. output = 64.0 * pow(input, 7.0) - 112.0 * pow(input, 5.0) + 56.0 * pow(input, 3.0) - 7.0 * input;
  89. break;
  90. case 8:
  91. output = 128.0 * pow(input, 8.0) - 256.0 * pow(input, 6.0) + 160.0 * pow(input, 4.0) - 32.0 * pow(input, 2.0) + 1.0;
  92. break;
  93. case 9:
  94. output = 256.0 * pow(input, 9.0) - 576.0 * pow(input, 7.0) + 432.0 * pow(input, 5.0) - 120.0 * pow(input, 3.0) + 9.0 * input;
  95. break;
  96. case 10:
  97. output = 512 * pow(input, 10.0) - 1280 * pow(input, 8.0) + 1120 * pow(input, 6.0) - 400.0 * pow(input, 4.0) + 50.0 * pow(input, 2.0) - 1.0;
  98. break;
  99. }
  100. return true;
  101. }
  102. void Mrcheb::step()
  103. {
  104. float input = inputs[INPUT].value;
  105. float outs[9];
  106. float output = 0.0;
  107. float mix_sum = 0.0;
  108. bool bipolar = true;
  109. if (params[RANGE].value == 0.0)
  110. {
  111. bipolar = false;
  112. }
  113. if (bipolar)
  114. {
  115. if (input < -5.0)
  116. {
  117. input = -5.0;
  118. }
  119. if (input > 5.0)
  120. {
  121. input = 5.0;
  122. }
  123. input *= 0.2;
  124. }
  125. else
  126. {
  127. if (input < 0.0)
  128. {
  129. input = 0.0;
  130. }
  131. if (input > 10.0)
  132. {
  133. input = 10.0;
  134. }
  135. input *= 0.1;
  136. }
  137. for (int s = 0; s < 9; s++)
  138. {
  139. chebyshev_poly(input, outs[s], s + 1);
  140. }
  141. for (int s = 0; s < 9; s++)
  142. {
  143. bipolar ? outputs[s].value = outs[s] * 5.0 : outputs[s].value = outs[s] * 10.0;
  144. }
  145. for (int k = 0; k < 9; k++)
  146. {
  147. output += outputs[k].value * params[k].value;
  148. mix_sum += params[k].value;
  149. }
  150. mix_sum > 1.0 ? outputs[OUTPUT].value = output / mix_sum : outputs[OUTPUT].value = output;
  151. }
  152. struct MrchebWidget : ModuleWidget
  153. {
  154. MrchebWidget(Mrcheb *module);
  155. };
  156. MrchebWidget::MrchebWidget(Mrcheb *module) : ModuleWidget(module)
  157. {
  158. box.size = Vec(15 * 20, 380);
  159. {
  160. SVGPanel *panel = new SVGPanel();
  161. panel->box.size = box.size;
  162. panel->setBackground(SVG::load(assetPlugin(plugin, "res/mrcheb.svg")));
  163. addChild(panel);
  164. }
  165. addChild(Widget::create<SonusScrew>(Vec(0, 0)));
  166. addChild(Widget::create<SonusScrew>(Vec(box.size.x - 15, 0)));
  167. addChild(Widget::create<SonusScrew>(Vec(0, 365)));
  168. addChild(Widget::create<SonusScrew>(Vec(box.size.x - 15, 365)));
  169. addInput(Port::create<PJ301MPort>(Vec(58, 67), Port::INPUT, module, Mrcheb::INPUT));
  170. addOutput(Port::create<PJ301MPort>(Vec(222.7, 67), Port::OUTPUT, module, Mrcheb::OUTPUT));
  171. addOutput(Port::create<PJ301MPort>(Vec(20, 132), Port::OUTPUT, module, Mrcheb::S1));
  172. addOutput(Port::create<PJ301MPort>(Vec(58, 132), Port::OUTPUT, module, Mrcheb::S2));
  173. addOutput(Port::create<PJ301MPort>(Vec(96, 132), Port::OUTPUT, module, Mrcheb::S3));
  174. addOutput(Port::create<PJ301MPort>(Vec(20, 197), Port::OUTPUT, module, Mrcheb::S4));
  175. addOutput(Port::create<PJ301MPort>(Vec(58, 197), Port::OUTPUT, module, Mrcheb::S5));
  176. addOutput(Port::create<PJ301MPort>(Vec(96, 197), Port::OUTPUT, module, Mrcheb::S6));
  177. addOutput(Port::create<PJ301MPort>(Vec(20, 262), Port::OUTPUT, module, Mrcheb::S7));
  178. addOutput(Port::create<PJ301MPort>(Vec(58, 262), Port::OUTPUT, module, Mrcheb::S8));
  179. addOutput(Port::create<PJ301MPort>(Vec(96, 262), Port::OUTPUT, module, Mrcheb::S9));
  180. addParam(ParamWidget::create<SonusKnob>(Vec(178, 125), module, Mrcheb::S1K, 0.0, 1.0, 0.2));
  181. addParam(ParamWidget::create<SonusKnob>(Vec(216.5, 125), module, Mrcheb::S2K, 0.0, 1.0, 0.1));
  182. addParam(ParamWidget::create<SonusKnob>(Vec(255, 125), module, Mrcheb::S3K, 0.0, 1.0, 0.1));
  183. addParam(ParamWidget::create<SonusKnob>(Vec(178, 190), module, Mrcheb::S4K, 0.0, 1.0, 0.1));
  184. addParam(ParamWidget::create<SonusKnob>(Vec(216.5, 190), module, Mrcheb::S5K, 0.0, 1.0, 0.1));
  185. addParam(ParamWidget::create<SonusKnob>(Vec(255, 190), module, Mrcheb::S6K, 0.0, 1.0, 0.1));
  186. addParam(ParamWidget::create<SonusKnob>(Vec(178, 255), module, Mrcheb::S7K, 0.0, 1.0, 0.1));
  187. addParam(ParamWidget::create<SonusKnob>(Vec(216.5, 255), module, Mrcheb::S8K, 0.0, 1.0, 0.1));
  188. addParam(ParamWidget::create<SonusKnob>(Vec(255, 255), module, Mrcheb::S9K, 0.0, 1.0, 0.1));
  189. addParam(ParamWidget::create<CKSS>(Vec(127, 70), module, Mrcheb::RANGE, 0.0, 1.0, 1.0));
  190. }
  191. } // namespace rack_plugin_SonusModular
  192. using namespace rack_plugin_SonusModular;
  193. RACK_PLUGIN_MODEL_INIT(SonusModular, Mrcheb) {
  194. Model *modelMrcheb = Model::create<Mrcheb, MrchebWidget>("Sonus Modular", "Mrcheb", "Mrcheb | Chebyshev Waveshaper",WAVESHAPER_TAG, EFFECT_TAG);
  195. return modelMrcheb;
  196. }