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.

106 lines
2.9KB

  1. //**************************************************************************************
  2. //BitCrusher Module for VCV Rack by Autodafe http://www.autodafe.net
  3. //
  4. //Based on code taken from the Fundamentals plugins by Andrew Belt http://www.vcvrack.com
  5. //And part of code on musicdsp.org: http://musicdsp.org/showArchiveComment.php?ArchiveID=139
  6. //**************************************************************************************
  7. #include "Autodafe.hpp"
  8. namespace rack_plugin_Autodafe {
  9. struct BitCrusher : Module {
  10. enum ParamIds {
  11. BITS_PARAM,
  12. RATE_PARAM,
  13. ATTEN_PARAM,
  14. NUM_PARAMS
  15. };
  16. enum InputIds {
  17. INPUT,
  18. CV_BITS,
  19. NUM_INPUTS
  20. };
  21. enum OutputIds {
  22. OUTPUT,
  23. NUM_OUTPUTS
  24. };
  25. float y = 0, cnt = 0;
  26. float decimate(float i, long int bits, float rate);
  27. BitCrusher();
  28. void step();
  29. };
  30. BitCrusher::BitCrusher() {
  31. params.resize(NUM_PARAMS);
  32. inputs.resize(NUM_INPUTS);
  33. outputs.resize(NUM_OUTPUTS);
  34. }
  35. float BitCrusher::decimate(float i, long int bits, float rate)
  36. {
  37. long int m = 1 << (bits - 1);
  38. cnt += rate;
  39. if (cnt >= 1)
  40. {
  41. cnt -= 1;
  42. y = (long int)(i * m) / (float)m;
  43. }
  44. return y;
  45. }
  46. void BitCrusher::step() {
  47. float in = inputs[INPUT].value / 5.0;
  48. long int bits = params[BITS_PARAM].value *16;
  49. float rate = params[RATE_PARAM].value ;
  50. float coeff = inputs[CV_BITS].value * params[ATTEN_PARAM].value *8/ 5.0;
  51. outputs[OUTPUT].value = 5.0* decimate(in, bits-coeff, rate);
  52. }
  53. struct BitCrusherWidget : ModuleWidget{
  54. BitCrusherWidget(BitCrusher *module);
  55. };
  56. BitCrusherWidget::BitCrusherWidget(BitCrusher *module) : ModuleWidget(module) {
  57. box.size = Vec(15 * 6, 380);
  58. {
  59. SVGPanel *panel = new SVGPanel();
  60. panel->box.size = box.size;
  61. panel->setBackground(SVG::load(assetPlugin(plugin, "res/BitCrusher.svg")));
  62. addChild(panel);
  63. }
  64. addChild(createScrew<ScrewSilver>(Vec(5, 0)));
  65. addChild(createScrew<ScrewSilver>(Vec(box.size.x - 20, 0)));
  66. addChild(createScrew<ScrewSilver>(Vec(5, 365)));
  67. addChild(createScrew<ScrewSilver>(Vec(box.size.x - 20, 365)));
  68. addParam(createParam<AutodafeKnobGreenBig>(Vec(20, 60), module, BitCrusher::BITS_PARAM, 0.2, 1.0, 1.0));
  69. addParam(createParam<AutodafeKnobGreen>(Vec(27, 140), module, BitCrusher::RATE_PARAM, 0.2, 1.0,1.0));
  70. addParam(createParam<AutodafeKnobGreen>(Vec(27, 250), module, BitCrusher::ATTEN_PARAM, -1.0, 1.0, 0.0));
  71. addInput(createInput<PJ301MPort>(Vec(32, 200), module, BitCrusher::CV_BITS));
  72. addInput(createInput<PJ301MPort>(Vec(10, 320), module, BitCrusher::INPUT));
  73. addOutput(createOutput<PJ301MPort>(Vec(48, 320), module, BitCrusher::OUTPUT));
  74. }
  75. } // namespace rack_plugin_Autodafe
  76. using namespace rack_plugin_Autodafe;
  77. RACK_PLUGIN_MODEL_INIT(Autodafe, BitCrusher) {
  78. return Model::create<BitCrusher, BitCrusherWidget>("Autodafe", "BitCrusher", "BitCrusher", EFFECT_TAG);
  79. }