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.

Warps.cpp 5.7KB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "plugin.hpp"
  2. #include "warps/dsp/modulator.h"
  3. struct Warps : Module {
  4. enum ParamIds {
  5. ALGORITHM_PARAM,
  6. TIMBRE_PARAM,
  7. STATE_PARAM,
  8. LEVEL1_PARAM,
  9. LEVEL2_PARAM,
  10. NUM_PARAMS
  11. };
  12. enum InputIds {
  13. LEVEL1_INPUT,
  14. LEVEL2_INPUT,
  15. ALGORITHM_INPUT,
  16. TIMBRE_INPUT,
  17. CARRIER_INPUT,
  18. MODULATOR_INPUT,
  19. NUM_INPUTS
  20. };
  21. enum OutputIds {
  22. MODULATOR_OUTPUT,
  23. AUX_OUTPUT,
  24. NUM_OUTPUTS
  25. };
  26. enum LightIds {
  27. CARRIER_GREEN_LIGHT, CARRIER_RED_LIGHT,
  28. ALGORITHM_LIGHT,
  29. NUM_LIGHTS = ALGORITHM_LIGHT + 3
  30. };
  31. int frame = 0;
  32. warps::Modulator modulator;
  33. warps::ShortFrame inputFrames[60] = {};
  34. warps::ShortFrame outputFrames[60] = {};
  35. dsp::SchmittTrigger stateTrigger;
  36. Warps() {
  37. config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
  38. configParam(ALGORITHM_PARAM, 0.0, 8.0, 0.0, "Modulation algorithm");
  39. configParam(TIMBRE_PARAM, 0.0, 1.0, 0.5, "Modulation timbre");
  40. configParam(STATE_PARAM, 0.0, 1.0, 0.0, "Internal oscillator state");
  41. configParam(LEVEL1_PARAM, 0.0, 1.0, 1.0, "External oscillator amplitude/internal oscillator frequency");
  42. configParam(LEVEL2_PARAM, 0.0, 1.0, 1.0, "Modulator amplitude");
  43. memset(&modulator, 0, sizeof(modulator));
  44. modulator.Init(96000.0f);
  45. }
  46. void process(const ProcessArgs& args) override {
  47. // State trigger
  48. warps::Parameters* p = modulator.mutable_parameters();
  49. if (stateTrigger.process(params[STATE_PARAM].getValue())) {
  50. p->carrier_shape = (p->carrier_shape + 1) % 4;
  51. }
  52. lights[CARRIER_GREEN_LIGHT].value = (p->carrier_shape == 1 || p->carrier_shape == 2) ? 1.0 : 0.0;
  53. lights[CARRIER_RED_LIGHT].value = (p->carrier_shape == 2 || p->carrier_shape == 3) ? 1.0 : 0.0;
  54. // Buffer loop
  55. if (++frame >= 60) {
  56. frame = 0;
  57. p->channel_drive[0] = clamp(params[LEVEL1_PARAM].getValue() + inputs[LEVEL1_INPUT].getVoltage() / 5.0f, 0.0f, 1.0f);
  58. p->channel_drive[1] = clamp(params[LEVEL2_PARAM].getValue() + inputs[LEVEL2_INPUT].getVoltage() / 5.0f, 0.0f, 1.0f);
  59. p->modulation_algorithm = clamp(params[ALGORITHM_PARAM].getValue() / 8.0f + inputs[ALGORITHM_INPUT].getVoltage() / 5.0f, 0.0f, 1.0f);
  60. {
  61. // TODO
  62. // Use the correct light color
  63. NVGcolor algorithmColor = nvgHSL(p->modulation_algorithm, 0.3, 0.4);
  64. lights[ALGORITHM_LIGHT + 0].setBrightness(algorithmColor.r);
  65. lights[ALGORITHM_LIGHT + 1].setBrightness(algorithmColor.g);
  66. lights[ALGORITHM_LIGHT + 2].setBrightness(algorithmColor.b);
  67. }
  68. p->modulation_parameter = clamp(params[TIMBRE_PARAM].getValue() + inputs[TIMBRE_INPUT].getVoltage() / 5.0f, 0.0f, 1.0f);
  69. p->frequency_shift_pot = params[ALGORITHM_PARAM].getValue() / 8.0;
  70. p->frequency_shift_cv = clamp(inputs[ALGORITHM_INPUT].getVoltage() / 5.0f, -1.0f, 1.0f);
  71. p->phase_shift = p->modulation_algorithm;
  72. p->note = 60.0 * params[LEVEL1_PARAM].getValue() + 12.0 * inputs[LEVEL1_INPUT].getNormalVoltage(2.0) + 12.0;
  73. p->note += log2f(96000.0f * args.sampleTime) * 12.0f;
  74. modulator.Process(inputFrames, outputFrames, 60);
  75. }
  76. inputFrames[frame].l = clamp((int)(inputs[CARRIER_INPUT].getVoltage() / 16.0 * 0x8000), -0x8000, 0x7fff);
  77. inputFrames[frame].r = clamp((int)(inputs[MODULATOR_INPUT].getVoltage() / 16.0 * 0x8000), -0x8000, 0x7fff);
  78. outputs[MODULATOR_OUTPUT].setVoltage((float)outputFrames[frame].l / 0x8000 * 5.0);
  79. outputs[AUX_OUTPUT].setVoltage((float)outputFrames[frame].r / 0x8000 * 5.0);
  80. }
  81. json_t* dataToJson() override {
  82. json_t* rootJ = json_object();
  83. warps::Parameters* p = modulator.mutable_parameters();
  84. json_object_set_new(rootJ, "shape", json_integer(p->carrier_shape));
  85. return rootJ;
  86. }
  87. void dataFromJson(json_t* rootJ) override {
  88. json_t* shapeJ = json_object_get(rootJ, "shape");
  89. warps::Parameters* p = modulator.mutable_parameters();
  90. if (shapeJ) {
  91. p->carrier_shape = json_integer_value(shapeJ);
  92. }
  93. }
  94. void onReset() override {
  95. warps::Parameters* p = modulator.mutable_parameters();
  96. p->carrier_shape = 0;
  97. }
  98. void onRandomize() override {
  99. warps::Parameters* p = modulator.mutable_parameters();
  100. p->carrier_shape = random::u32() % 4;
  101. }
  102. };
  103. struct AlgorithmLight : RedGreenBlueLight {
  104. AlgorithmLight() {
  105. box.size = Vec(71, 71);
  106. }
  107. };
  108. struct WarpsWidget : ModuleWidget {
  109. WarpsWidget(Warps* module) {
  110. setModule(module);
  111. setPanel(APP->window->loadSvg(asset::plugin(pluginInstance, "res/Warps.svg")));
  112. addChild(createWidget<ScrewSilver>(Vec(15, 0)));
  113. addChild(createWidget<ScrewSilver>(Vec(120, 0)));
  114. addChild(createWidget<ScrewSilver>(Vec(15, 365)));
  115. addChild(createWidget<ScrewSilver>(Vec(120, 365)));
  116. addParam(createParam<Rogan6PSWhite>(Vec(29, 52), module, Warps::ALGORITHM_PARAM));
  117. addParam(createParam<Rogan1PSWhite>(Vec(94, 173), module, Warps::TIMBRE_PARAM));
  118. addParam(createParam<TL1105>(Vec(16, 182), module, Warps::STATE_PARAM));
  119. addParam(createParam<Trimpot>(Vec(14, 213), module, Warps::LEVEL1_PARAM));
  120. addParam(createParam<Trimpot>(Vec(53, 213), module, Warps::LEVEL2_PARAM));
  121. addInput(createInput<PJ301MPort>(Vec(8, 273), module, Warps::LEVEL1_INPUT));
  122. addInput(createInput<PJ301MPort>(Vec(44, 273), module, Warps::LEVEL2_INPUT));
  123. addInput(createInput<PJ301MPort>(Vec(80, 273), module, Warps::ALGORITHM_INPUT));
  124. addInput(createInput<PJ301MPort>(Vec(116, 273), module, Warps::TIMBRE_INPUT));
  125. addInput(createInput<PJ301MPort>(Vec(8, 316), module, Warps::CARRIER_INPUT));
  126. addInput(createInput<PJ301MPort>(Vec(44, 316), module, Warps::MODULATOR_INPUT));
  127. addOutput(createOutput<PJ301MPort>(Vec(80, 316), module, Warps::MODULATOR_OUTPUT));
  128. addOutput(createOutput<PJ301MPort>(Vec(116, 316), module, Warps::AUX_OUTPUT));
  129. addChild(createLight<SmallLight<GreenRedLight>>(Vec(21, 167), module, Warps::CARRIER_GREEN_LIGHT));
  130. addChild(createLight<AlgorithmLight>(Vec(40, 63), module, Warps::ALGORITHM_LIGHT));
  131. }
  132. };
  133. Model* modelWarps = createModel<Warps, WarpsWidget>("Warps");