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.

90 lines
2.9KB

  1. #include "RJModules.hpp"
  2. #include <iostream>
  3. #include <cmath>
  4. namespace rack_plugin_RJModules {
  5. // Thanks to http://10rem.net/blog/2013/01/13/a-simple-bitcrusher-and-sample-rate-reducer-in-cplusplus-for-a-windows-store-app
  6. struct BitCrush: Module {
  7. enum ParamIds {
  8. CH1_PARAM,
  9. CH2_PARAM,
  10. NUM_PARAMS
  11. };
  12. enum InputIds {
  13. CH1_INPUT,
  14. CH1_CV_INPUT,
  15. CH2_CV_INPUT,
  16. NUM_INPUTS
  17. };
  18. enum OutputIds {
  19. CH1_OUTPUT,
  20. NUM_OUTPUTS
  21. };
  22. BitCrush() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS) {}
  23. void step() override;
  24. };
  25. #define ROUND(f) ((float)((f > 0.0) ? floor(f + 0.5) : ceil(f - 0.5)))
  26. void BitCrush::step() {
  27. float ch1 = inputs[CH1_INPUT].value;
  28. float combined_input = params[CH1_PARAM].value * clamp(inputs[CH1_CV_INPUT].normalize(10.0f) / 10.0f, 0.0f, 1.0f);
  29. float combined_crush_floor = params[CH2_PARAM].value * clamp(inputs[CH2_CV_INPUT].normalize(10.0f) / 10.0f, 0.0f, 1.0f);
  30. // new_value = ( (old_value - old_min) / (old_max - old_min) ) * (new_max - new_min) + new_min
  31. float mapped_crush_floor = ((combined_crush_floor - 0.0) / (1.0 - 0.0) ) * (32.0 - 1.0) + 1.0;
  32. int crush_floor = 32 - static_cast<int>(mapped_crush_floor) + 1;
  33. float mapped_input = ((combined_input - 0.0) / (1.0 - 0.0) ) * (crush_floor - 1.0) + 1.0;
  34. int bit_depth = crush_floor - static_cast<int>(mapped_input) + 1;
  35. int max = pow(2, bit_depth) - 1;
  36. float ch1_crushed = ROUND((ch1 + 1.0) * max) / max - 1.0;
  37. outputs[CH1_OUTPUT].value = ch1_crushed;
  38. }
  39. struct BitCrushWidget: ModuleWidget {
  40. BitCrushWidget(BitCrush *module);
  41. };
  42. BitCrushWidget::BitCrushWidget(BitCrush *module) : ModuleWidget(module) {
  43. box.size = Vec(15*10, 380);
  44. {
  45. SVGPanel *panel = new SVGPanel();
  46. panel->box.size = box.size;
  47. panel->setBackground(SVG::load(assetPlugin(plugin, "res/BitCrush.svg")));
  48. addChild(panel);
  49. }
  50. addChild(Widget::create<ScrewSilver>(Vec(15, 0)));
  51. addChild(Widget::create<ScrewSilver>(Vec(box.size.x-30, 0)));
  52. addChild(Widget::create<ScrewSilver>(Vec(15, 365)));
  53. addChild(Widget::create<ScrewSilver>(Vec(box.size.x-30, 365)));
  54. addParam(ParamWidget::create<RoundBlackKnob>(Vec(57, 139), module, BitCrush::CH1_PARAM, 0.0, 1.0, 0.0));
  55. addParam(ParamWidget::create<RoundBlackKnob>(Vec(57, 219), module, BitCrush::CH2_PARAM, 0.0, 1.0, 0.0));
  56. addInput(Port::create<PJ301MPort>(Vec(22, 129), Port::INPUT, module, BitCrush::CH1_INPUT));
  57. addInput(Port::create<PJ301MPort>(Vec(22, 160), Port::INPUT, module, BitCrush::CH1_CV_INPUT));
  58. addInput(Port::create<PJ301MPort>(Vec(22, 241), Port::INPUT, module, BitCrush::CH2_CV_INPUT));
  59. addOutput(Port::create<PJ301MPort>(Vec(110, 145), Port::OUTPUT, module, BitCrush::CH1_OUTPUT));
  60. }
  61. } // namespace rack_plugin_RJModules
  62. using namespace rack_plugin_RJModules;
  63. RACK_PLUGIN_MODEL_INIT(RJModules, BitCrush) {
  64. Model *modelBitCrush = Model::create<BitCrush, BitCrushWidget>("RJModules", "BitCrush", "[FX] BitCrush", DISTORTION_TAG);
  65. return modelBitCrush;
  66. }