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.

143 lines
4.4KB

  1. #include <string.h>
  2. #include "AudibleInstruments.hpp"
  3. #include "warps/dsp/modulator.h"
  4. struct Warps : Module {
  5. enum ParamIds {
  6. ALGORITHM_PARAM,
  7. TIMBRE_PARAM,
  8. STATE_PARAM,
  9. LEVEL1_PARAM,
  10. LEVEL2_PARAM,
  11. NUM_PARAMS
  12. };
  13. enum InputIds {
  14. LEVEL1_INPUT,
  15. LEVEL2_INPUT,
  16. ALGORITHM_INPUT,
  17. TIMBRE_INPUT,
  18. CARRIER_INPUT,
  19. MODULATOR_INPUT,
  20. NUM_INPUTS
  21. };
  22. enum OutputIds {
  23. MODULATOR_OUTPUT,
  24. AUX_OUTPUT,
  25. NUM_OUTPUTS
  26. };
  27. int frame = 0;
  28. warps::Modulator modulator;
  29. warps::ShortFrame inputFrames[60] = {};
  30. warps::ShortFrame outputFrames[60] = {};
  31. float lights[1] = {};
  32. Trigger stateTrigger;
  33. Warps();
  34. void step();
  35. json_t *toJsonData() {
  36. json_t *root = json_object();
  37. warps::Parameters *p = modulator.mutable_parameters();
  38. json_object_set_new(root, "shape", json_integer(p->carrier_shape));
  39. return root;
  40. }
  41. void fromJsonData(json_t *root) {
  42. json_t *shapeJ = json_object_get(root, "shape");
  43. warps::Parameters *p = modulator.mutable_parameters();
  44. if (shapeJ) {
  45. p->carrier_shape = json_integer_value(shapeJ);
  46. }
  47. }
  48. };
  49. Warps::Warps() {
  50. params.resize(NUM_PARAMS);
  51. inputs.resize(NUM_INPUTS);
  52. outputs.resize(NUM_OUTPUTS);
  53. memset(&modulator, 0, sizeof(modulator));
  54. modulator.Init(96000.0f);
  55. }
  56. void Warps::step() {
  57. // State trigger
  58. warps::Parameters *p = modulator.mutable_parameters();
  59. if (stateTrigger.process(params[STATE_PARAM])) {
  60. p->carrier_shape = (p->carrier_shape + 1) % 4;
  61. }
  62. lights[0] = p->carrier_shape;
  63. // Buffer loop
  64. if (++frame >= 60) {
  65. frame = 0;
  66. p->channel_drive[0] = clampf(params[LEVEL1_PARAM] + getf(inputs[LEVEL1_INPUT]) / 5.0, 0.0, 1.0);
  67. p->channel_drive[1] = clampf(params[LEVEL2_PARAM] + getf(inputs[LEVEL2_INPUT]) / 5.0, 0.0, 1.0);
  68. p->modulation_algorithm = clampf(params[ALGORITHM_PARAM] / 8.0 + getf(inputs[ALGORITHM_PARAM]) / 5.0, 0.0, 1.0);
  69. p->modulation_parameter = clampf(params[TIMBRE_PARAM] + getf(inputs[TIMBRE_INPUT]) / 5.0, 0.0, 1.0);
  70. p->frequency_shift_pot = params[ALGORITHM_PARAM] / 8.0;
  71. p->frequency_shift_cv = clampf(getf(inputs[ALGORITHM_INPUT]) / 5.0, -1.0, 1.0);
  72. p->phase_shift = p->modulation_algorithm;
  73. p->note = 60.0 * params[LEVEL1_PARAM] + 12.0 * getf(inputs[LEVEL1_INPUT], 2.0) + 12.0;
  74. p->note += log2f(96000.0 / gSampleRate) * 12.0;
  75. modulator.Process(inputFrames, outputFrames, 60);
  76. }
  77. inputFrames[frame].l = clampf(getf(inputs[CARRIER_INPUT]) / 16.0 * 0x8000, -0x8000, 0x7fff);
  78. inputFrames[frame].r = clampf(getf(inputs[MODULATOR_INPUT]) / 16.0 * 0x8000, -0x8000, 0x7fff);
  79. setf(outputs[MODULATOR_OUTPUT], (float)outputFrames[frame].l / 0x8000 * 5.0);
  80. setf(outputs[AUX_OUTPUT], (float)outputFrames[frame].r / 0x8000 * 5.0);
  81. }
  82. struct WarpsModeLight : ModeValueLight {
  83. WarpsModeLight() {
  84. addColor(COLOR_BLACK_TRANSPARENT);
  85. addColor(COLOR_GREEN);
  86. addColor(COLOR_YELLOW);
  87. addColor(COLOR_RED);
  88. }
  89. };
  90. WarpsWidget::WarpsWidget() {
  91. Warps *module = new Warps();
  92. setModule(module);
  93. box.size = Vec(15*10, 380);
  94. {
  95. Panel *panel = new LightPanel();
  96. panel->backgroundImage = Image::load("plugins/AudibleInstruments/res/Warps.png");
  97. panel->box.size = box.size;
  98. addChild(panel);
  99. }
  100. addChild(createScrew<ScrewSilver>(Vec(15, 0)));
  101. addChild(createScrew<ScrewSilver>(Vec(120, 0)));
  102. addChild(createScrew<ScrewSilver>(Vec(15, 365)));
  103. addChild(createScrew<ScrewSilver>(Vec(120, 365)));
  104. addParam(createParam<Rogan6PSWhite>(Vec(30, 53), module, Warps::ALGORITHM_PARAM, 0.0, 8.0, 0.0));
  105. addParam(createParam<Rogan1PSWhite>(Vec(95, 173), module, Warps::TIMBRE_PARAM, 0.0, 1.0, 0.5));
  106. addParam(createParam<TL1105>(Vec(16, 182), module, Warps::STATE_PARAM, 0.0, 1.0, 0.0));
  107. addParam(createParam<Trimpot>(Vec(15, 214), module, Warps::LEVEL1_PARAM, 0.0, 1.0, 1.0));
  108. addParam(createParam<Trimpot>(Vec(54, 214), module, Warps::LEVEL2_PARAM, 0.0, 1.0, 1.0));
  109. addInput(createInput<PJ3410Port>(Vec(5, 270), module, Warps::LEVEL1_INPUT));
  110. addInput(createInput<PJ3410Port>(Vec(41, 270), module, Warps::LEVEL2_INPUT));
  111. addInput(createInput<PJ3410Port>(Vec(77, 270), module, Warps::ALGORITHM_INPUT));
  112. addInput(createInput<PJ3410Port>(Vec(113, 270), module, Warps::TIMBRE_INPUT));
  113. addInput(createInput<PJ3410Port>(Vec(5, 313), module, Warps::CARRIER_INPUT));
  114. addInput(createInput<PJ3410Port>(Vec(41, 313), module, Warps::MODULATOR_INPUT));
  115. addOutput(createOutput<PJ3410Port>(Vec(77, 313), module, Warps::MODULATOR_OUTPUT));
  116. addOutput(createOutput<PJ3410Port>(Vec(113, 313), module, Warps::AUX_OUTPUT));
  117. addChild(createValueLight<SmallLight<WarpsModeLight>>(Vec(20, 167), &module->lights[0]));
  118. }