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.

1104 lines
31KB

  1. /*
  2. * DISTRHO Cardinal Plugin
  3. * Copyright (C) 2021-2022 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 3 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the LICENSE file.
  16. */
  17. /**
  18. * This file contains a substantial amount of code from VCVRack, adjusted for inline use
  19. * Copyright (C) 2016-2021 VCV.
  20. *
  21. * This program is free software: you can redistribute it and/or
  22. * modify it under the terms of the GNU General Public License as
  23. * published by the Free Software Foundation; either version 3 of
  24. * the License, or (at your option) any later version.
  25. */
  26. #pragma once
  27. #include <cmath>
  28. #include <cstdlib>
  29. #include <cstring>
  30. #include <algorithm>
  31. #include <list>
  32. #include <memory>
  33. #include <string>
  34. #include <vector>
  35. struct NVGcolor { float a; };
  36. struct NVGpaint {};
  37. inline NVGcolor nvgRGB(int r, int g, int b) { return {}; }
  38. inline NVGcolor nvgRGBA(int r, int g, int b, int a) { return {}; }
  39. inline NVGcolor nvgRGBf(float r, float g, float b) { return {}; }
  40. inline NVGcolor nvgRGBAf(float r, float g, float b, float a) { return {}; }
  41. inline void nvgBeginPath(void* vg) {}
  42. inline void nvgFillColor(void* vg, NVGcolor) {}
  43. inline void nvgFillPaint(void* vg, NVGpaint) {}
  44. inline void nvgFill(void* vg) {}
  45. inline void nvgStrokeColor(void* vg, NVGcolor) {}
  46. inline void nvgStrokeWidth(void* vg, float) {}
  47. inline void nvgStroke(void* vg) {}
  48. inline void nvgRect(void* vg, float a, float b, float c, float d) {}
  49. inline void nvgImageSize(void*, int, void*, void*) {}
  50. inline NVGpaint nvgImagePattern(void*, float, float, float, float, float, int handle, float) { return {}; }
  51. struct json_t {};
  52. json_t* json_integer(int) { return NULL; }
  53. json_t* json_object() { return NULL; }
  54. json_t* json_object_get(json_t*, const char*) { return NULL; }
  55. int json_integer_value(json_t*) { return 0; }
  56. void json_object_set_new(json_t*, const char*, json_t*) {}
  57. namespace rack {
  58. struct Quantity {
  59. virtual ~Quantity() {}
  60. virtual void setValue(float value) {}
  61. virtual float getValue() { return 0.f; }
  62. virtual float getMinValue() { return 0.f; }
  63. virtual float getMaxValue() { return 1.f; }
  64. virtual float getDefaultValue() { return 0.f; }
  65. // virtual float getDisplayValue();
  66. // virtual void setDisplayValue(float displayValue);
  67. // virtual int getDisplayPrecision();
  68. // virtual std::string getDisplayValueString();
  69. // virtual void setDisplayValueString(std::string s);
  70. virtual std::string getLabel() { return ""; }
  71. virtual std::string getUnit() { return ""; }
  72. // virtual std::string getString();
  73. // virtual void reset();
  74. // virtual void randomize();
  75. // bool isMin();
  76. // bool isMax();
  77. // void setMin();
  78. // void setMax();
  79. // void toggle();
  80. // void moveValue(float deltaValue);
  81. // float getRange();
  82. // bool isBounded();
  83. // float toScaled(float value);
  84. // float fromScaled(float scaledValue);
  85. // void setScaledValue(float scaledValue);
  86. // float getScaledValue();
  87. // void moveScaledValue(float deltaScaledValue);
  88. };
  89. namespace ui {
  90. struct Menu;
  91. }
  92. namespace math {
  93. inline int clamp(int x, int a, int b) {
  94. return std::max(std::min(x, b), a);
  95. }
  96. inline float clamp(float x, float a = 0.f, float b = 1.f) {
  97. return std::fmax(std::fmin(x, b), a);
  98. }
  99. struct Vec {
  100. float x = 0.f;
  101. float y = 0.f;
  102. Vec() {}
  103. Vec(float xy) : x(xy), y(xy) {}
  104. Vec(float x, float y) : x(x), y(y) {}
  105. Vec neg() const { return Vec(-x, -y); }
  106. Vec plus(Vec b) const { return Vec(x + b.x, y + b.y); }
  107. Vec minus(Vec b) const { return Vec(x - b.x, y - b.y); }
  108. Vec mult(float s) const { return Vec(x * s, y * s); }
  109. Vec mult(Vec b) const { return Vec(x * b.x, y * b.y); }
  110. };
  111. struct Rect {
  112. Vec pos;
  113. Vec size;
  114. };
  115. } // namespace math
  116. namespace engine {
  117. static constexpr const int PORT_MAX_CHANNELS = 16;
  118. struct Module;
  119. struct Engine {
  120. float getSampleRate() { return sampleRate; }
  121. // custom
  122. float sampleRate = 0.f;
  123. };
  124. struct Light {
  125. float value = 0.f;
  126. void setBrightness(float brightness) {
  127. value = brightness;
  128. }
  129. float getBrightness() {
  130. return value;
  131. }
  132. void setBrightnessSmooth(float brightness, float deltaTime, float lambda = 30.f) {
  133. if (brightness < value) {
  134. // Fade out light
  135. value += (brightness - value) * lambda * deltaTime;
  136. }
  137. else {
  138. // Immediately illuminate light
  139. value = brightness;
  140. }
  141. }
  142. void setSmoothBrightness(float brightness, float deltaTime) {
  143. setBrightnessSmooth(brightness, deltaTime);
  144. }
  145. void setBrightnessSmooth(float brightness, int frames = 1) {
  146. setBrightnessSmooth(brightness, frames / 44100.f);
  147. }
  148. };
  149. struct Param {
  150. float value = 0.f;
  151. float getValue() { return value; }
  152. void setValue(float value) { this->value = value; }
  153. };
  154. struct Port {
  155. union {
  156. float voltages[PORT_MAX_CHANNELS] = {};
  157. float value;
  158. };
  159. union {
  160. uint8_t channels = 0;
  161. uint8_t active;
  162. };
  163. Light plugLights[3];
  164. enum Type {
  165. INPUT,
  166. OUTPUT,
  167. };
  168. void setVoltage(float voltage, int channel = 0) { voltages[channel] = voltage; }
  169. float getVoltage(int channel = 0) { return voltages[channel]; }
  170. float getPolyVoltage(int channel) { return isMonophonic() ? getVoltage(0) : getVoltage(channel); }
  171. float getNormalVoltage(float normalVoltage, int channel = 0) { return isConnected() ? getVoltage(channel) : normalVoltage; }
  172. float getNormalPolyVoltage(float normalVoltage, int channel) { return isConnected() ? getPolyVoltage(channel) : normalVoltage; }
  173. float* getVoltages(int firstChannel = 0) { return &voltages[firstChannel]; }
  174. void readVoltages(float* v) {
  175. for (int c = 0; c < channels; c++) {
  176. v[c] = voltages[c];
  177. }
  178. }
  179. void writeVoltages(const float* v) {
  180. for (int c = 0; c < channels; c++) {
  181. voltages[c] = v[c];
  182. }
  183. }
  184. void clearVoltages() {
  185. for (int c = 0; c < channels; c++) {
  186. voltages[c] = 0.f;
  187. }
  188. }
  189. float getVoltageSum() {
  190. float sum = 0.f;
  191. for (int c = 0; c < channels; c++) {
  192. sum += voltages[c];
  193. }
  194. return sum;
  195. }
  196. float getVoltageRMS() {
  197. if (channels == 0) {
  198. return 0.f;
  199. }
  200. else if (channels == 1) {
  201. return std::fabs(voltages[0]);
  202. }
  203. else {
  204. float sum = 0.f;
  205. for (int c = 0; c < channels; c++) {
  206. sum += std::pow(voltages[c], 2);
  207. }
  208. return std::sqrt(sum);
  209. }
  210. }
  211. // template <typename T>
  212. // T getVoltageSimd(int firstChannel) {
  213. // return T::load(&voltages[firstChannel]);
  214. // }
  215. //
  216. // template <typename T>
  217. // T getPolyVoltageSimd(int firstChannel) {
  218. // return isMonophonic() ? getVoltage(0) : getVoltageSimd<T>(firstChannel);
  219. // }
  220. //
  221. // template <typename T>
  222. // T getNormalVoltageSimd(T normalVoltage, int firstChannel) {
  223. // return isConnected() ? getVoltageSimd<T>(firstChannel) : normalVoltage;
  224. // }
  225. //
  226. // template <typename T>
  227. // T getNormalPolyVoltageSimd(T normalVoltage, int firstChannel) {
  228. // return isConnected() ? getPolyVoltageSimd<T>(firstChannel) : normalVoltage;
  229. // }
  230. //
  231. // template <typename T>
  232. // void setVoltageSimd(T voltage, int firstChannel) {
  233. // voltage.store(&voltages[firstChannel]);
  234. // }
  235. void setChannels(int channels) {
  236. if (this->channels == 0) {
  237. return;
  238. }
  239. for (int c = channels; c < this->channels; c++) {
  240. voltages[c] = 0.f;
  241. }
  242. if (channels == 0) {
  243. channels = 1;
  244. }
  245. this->channels = channels;
  246. }
  247. int getChannels() { return channels; }
  248. bool isConnected() { return channels > 0; }
  249. bool isMonophonic() { return channels == 1; }
  250. bool isPolyphonic() { return channels > 1; }
  251. float normalize(float normalVoltage) { return getNormalVoltage(normalVoltage); }
  252. };
  253. struct Output : Port {};
  254. struct Input : Port {};
  255. struct PortInfo {
  256. Module* module = NULL;
  257. Port::Type type = Port::INPUT;
  258. int portId = -1;
  259. std::string name;
  260. std::string description;
  261. virtual ~PortInfo() {}
  262. virtual std::string getName() {
  263. if (name == "")
  264. return std::string("#") + std::to_string(portId + 1);
  265. return name;
  266. }
  267. std::string getFullName() {
  268. std::string name = getName();
  269. name += " ";
  270. name += (type == Port::INPUT) ? "input" : "output";
  271. return name;
  272. }
  273. virtual std::string getDescription() { return description; }
  274. };
  275. struct ParamQuantity : Quantity {
  276. Module* module = NULL;
  277. int paramId = -1;
  278. float minValue = 0.f;
  279. float maxValue = 1.f;
  280. float defaultValue = 0.f;
  281. std::string name;
  282. std::string unit;
  283. float displayBase = 0.f;
  284. float displayMultiplier = 1.f;
  285. float displayOffset = 0.f;
  286. int displayPrecision = 5;
  287. std::string description;
  288. bool resetEnabled = true;
  289. bool randomizeEnabled = true;
  290. bool smoothEnabled = false;
  291. bool snapEnabled = false;
  292. // Param* getParam();
  293. // /** If smoothEnabled is true, requests to the engine to smoothly move to a target value each sample. */
  294. // void setSmoothValue(float value);
  295. // float getSmoothValue();
  296. // void setValue(float value) override;
  297. // float getValue() override;
  298. float getMinValue() override { return minValue; }
  299. float getMaxValue() override { return maxValue; }
  300. float getDefaultValue() override { return defaultValue; }
  301. // float getDisplayValue() override;
  302. // void setDisplayValue(float displayValue) override;
  303. // std::string getDisplayValueString() override;
  304. // void setDisplayValueString(std::string s) override;
  305. // int getDisplayPrecision() override;
  306. // std::string getLabel() override;
  307. // std::string getUnit() override;
  308. // void reset() override;
  309. // void randomize() override;
  310. virtual std::string getDescription() { return description; }
  311. // virtual json_t* toJson();
  312. // virtual void fromJson(json_t* rootJ);
  313. };
  314. struct SwitchQuantity : ParamQuantity {
  315. // std::vector<std::string> labels;
  316. // std::string getDisplayValueString() override;
  317. // void setDisplayValueString(std::string s) override;
  318. };
  319. struct Module {
  320. std::vector<Param> params;
  321. std::vector<Input> inputs;
  322. std::vector<Output> outputs;
  323. std::vector<Light> lights;
  324. std::vector<ParamQuantity*> paramQuantities;
  325. std::vector<PortInfo*> inputInfos;
  326. std::vector<PortInfo*> outputInfos;
  327. // std::vector<LightInfo*> lightInfos;
  328. virtual ~Module() {
  329. for (ParamQuantity* paramQuantity : paramQuantities) {
  330. if (paramQuantity)
  331. delete paramQuantity;
  332. }
  333. for (PortInfo* inputInfo : inputInfos) {
  334. if (inputInfo)
  335. delete inputInfo;
  336. }
  337. for (PortInfo* outputInfo : outputInfos) {
  338. if (outputInfo)
  339. delete outputInfo;
  340. }
  341. // for (LightInfo* lightInfo : lightInfos) {
  342. // if (lightInfo)
  343. // delete lightInfo;
  344. // }
  345. }
  346. void config(int numParams, int numInputs, int numOutputs, int numLights = 0) {
  347. // // This method should only be called once.
  348. // assert(params.empty() && inputs.empty() && outputs.empty() && lights.empty() && paramQuantities.empty());
  349. params.resize(numParams);
  350. inputs.resize(numInputs);
  351. outputs.resize(numOutputs);
  352. lights.resize(numLights);
  353. paramQuantities.resize(numParams);
  354. for (int i = 0; i < numParams; i++) {
  355. configParam(i, 0.f, 1.f, 0.f);
  356. }
  357. inputInfos.resize(numInputs);
  358. for (int i = 0; i < numInputs; i++) {
  359. configInput(i);
  360. }
  361. outputInfos.resize(numOutputs);
  362. for (int i = 0; i < numOutputs; i++) {
  363. configOutput(i);
  364. }
  365. // lightInfos.resize(numLights);
  366. }
  367. template <class TParamQuantity = ParamQuantity>
  368. TParamQuantity* configParam(int paramId, float minValue, float maxValue, float defaultValue, std::string name = "", std::string unit = "", float displayBase = 0.f, float displayMultiplier = 1.f, float displayOffset = 0.f) {
  369. // assert(paramId < (int) params.size() && paramId < (int) paramQuantities.size());
  370. if (paramQuantities[paramId])
  371. delete paramQuantities[paramId];
  372. TParamQuantity* q = new TParamQuantity;
  373. q->ParamQuantity::module = this;
  374. q->ParamQuantity::paramId = paramId;
  375. q->ParamQuantity::minValue = minValue;
  376. q->ParamQuantity::maxValue = maxValue;
  377. q->ParamQuantity::defaultValue = defaultValue;
  378. q->ParamQuantity::name = name;
  379. q->ParamQuantity::unit = unit;
  380. q->ParamQuantity::displayBase = displayBase;
  381. q->ParamQuantity::displayMultiplier = displayMultiplier;
  382. q->ParamQuantity::displayOffset = displayOffset;
  383. paramQuantities[paramId] = q;
  384. Param* p = &params[paramId];
  385. p->value = q->getDefaultValue();
  386. return q;
  387. }
  388. template <class TSwitchQuantity = SwitchQuantity>
  389. TSwitchQuantity* configSwitch(int paramId, float minValue, float maxValue, float defaultValue, std::string name = "", std::vector<std::string> labels = {}) {
  390. TSwitchQuantity* sq = configParam<TSwitchQuantity>(paramId, minValue, maxValue, defaultValue, name);
  391. sq->labels = labels;
  392. return sq;
  393. }
  394. template <class TSwitchQuantity = SwitchQuantity>
  395. TSwitchQuantity* configButton(int paramId, std::string name = "") {
  396. TSwitchQuantity* sq = configParam<TSwitchQuantity>(paramId, 0.f, 1.f, 0.f, name);
  397. sq->randomizeEnabled = false;
  398. return sq;
  399. }
  400. template <class TPortInfo = PortInfo>
  401. TPortInfo* configInput(int portId, std::string name = "") {
  402. // assert(portId < (int) inputs.size() && portId < (int) inputInfos.size());
  403. if (inputInfos[portId])
  404. delete inputInfos[portId];
  405. TPortInfo* info = new TPortInfo;
  406. info->PortInfo::module = this;
  407. info->PortInfo::type = Port::INPUT;
  408. info->PortInfo::portId = portId;
  409. info->PortInfo::name = name;
  410. inputInfos[portId] = info;
  411. return info;
  412. }
  413. template <class TPortInfo = PortInfo>
  414. TPortInfo* configOutput(int portId, std::string name = "") {
  415. // assert(portId < (int) outputs.size() && portId < (int) outputInfos.size());
  416. if (outputInfos[portId])
  417. delete outputInfos[portId];
  418. TPortInfo* info = new TPortInfo;
  419. info->PortInfo::module = this;
  420. info->PortInfo::type = Port::OUTPUT;
  421. info->PortInfo::portId = portId;
  422. info->PortInfo::name = name;
  423. outputInfos[portId] = info;
  424. return info;
  425. }
  426. // template <class TLightInfo = LightInfo>
  427. // TLightInfo* configLight(int lightId, std::string name = "") {
  428. // assert(lightId < (int) lights.size() && lightId < (int) lightInfos.size());
  429. // if (lightInfos[lightId])
  430. // delete lightInfos[lightId];
  431. //
  432. // TLightInfo* info = new TLightInfo;
  433. // info->LightInfo::module = this;
  434. // info->LightInfo::lightId = lightId;
  435. // info->LightInfo::name = name;
  436. // lightInfos[lightId] = info;
  437. // return info;
  438. // }
  439. void configBypass(int inputId, int outputId) {
  440. // assert(inputId < (int) inputs.size());
  441. // assert(outputId < (int) outputs.size());
  442. // // Check that output is not yet routed
  443. // for (BypassRoute& br : bypassRoutes) {
  444. // assert(br.outputId != outputId);
  445. // }
  446. //
  447. // BypassRoute br;
  448. // br.inputId = inputId;
  449. // br.outputId = outputId;
  450. // bypassRoutes.push_back(br);
  451. }
  452. int getNumParams() { return params.size(); }
  453. Param& getParam(int index) { return params[index]; }
  454. int getNumInputs() { return inputs.size(); }
  455. Input& getInput(int index) { return inputs[index]; }
  456. int getNumOutputs() { return outputs.size(); }
  457. Output& getOutput(int index) { return outputs[index]; }
  458. int getNumLights() { return lights.size(); }
  459. Light& getLight(int index) { return lights[index]; }
  460. ParamQuantity* getParamQuantity(int index) { return paramQuantities[index]; }
  461. PortInfo* getInputInfo(int index) { return inputInfos[index]; }
  462. PortInfo* getOutputInfo(int index) { return outputInfos[index]; }
  463. // LightInfo* getLightInfo(int index) { return lightInfos[index]; }
  464. struct ProcessArgs {
  465. float sampleRate;
  466. float sampleTime;
  467. int64_t frame;
  468. };
  469. virtual void process(const ProcessArgs& args) {
  470. step();
  471. }
  472. virtual void step() {}
  473. // virtual void processBypass(const ProcessArgs& args);
  474. // virtual json_t* toJson();
  475. // virtual void fromJson(json_t* rootJ);
  476. /** Serializes the "params" object. */
  477. // virtual json_t* paramsToJson();
  478. // virtual void paramsFromJson(json_t* rootJ);
  479. virtual json_t* dataToJson() { return NULL; }
  480. virtual void dataFromJson(json_t* rootJ) {}
  481. struct SampleRateChangeEvent {
  482. float sampleRate;
  483. float sampleTime;
  484. };
  485. virtual void onSampleRateChange(const SampleRateChangeEvent& e) {
  486. onSampleRateChange();
  487. }
  488. struct ResetEvent {};
  489. virtual void onReset(const ResetEvent& e) {} // TODO
  490. virtual void onAdd() {}
  491. virtual void onRemove() {}
  492. virtual void onReset() {}
  493. virtual void onRandomize() {}
  494. virtual void onSampleRateChange() {}
  495. // private
  496. void doProcess(const ProcessArgs& args) {
  497. // if (!internal->bypassed)
  498. process(args);
  499. // else
  500. // processBypass(args);
  501. // if (args.frame % PORT_DIVIDER == 0) {
  502. // float portTime = args.sampleTime * PORT_DIVIDER;
  503. // for (Input& input : inputs) {
  504. // Port_step(&input, portTime);
  505. // }
  506. // for (Output& output : outputs) {
  507. // Port_step(&output, portTime);
  508. // }
  509. // }
  510. }
  511. };
  512. } // namespace engine
  513. namespace widget {
  514. struct BaseEvent {
  515. };
  516. struct Widget {
  517. math::Rect box;
  518. Widget* parent = NULL;
  519. std::list<Widget*> children;
  520. bool visible = true;
  521. bool requestedDelete = false;
  522. using BaseEvent = widget::BaseEvent;
  523. struct ActionEvent : BaseEvent {};
  524. virtual void onAction(const ActionEvent& e) {}
  525. struct ChangeEvent : BaseEvent {};
  526. virtual void onChange(const ChangeEvent& e) {}
  527. bool hasChild(Widget* child) { return false; }
  528. void addChild(Widget* child) {}
  529. void addChildBottom(Widget* child) {}
  530. void addChildBelow(Widget* child, Widget* sibling) {}
  531. void addChildAbove(Widget* child, Widget* sibling) {}
  532. void removeChild(Widget* child) {}
  533. void clearChildren() {}
  534. virtual void step() {}
  535. struct DrawArgs {
  536. void* vg = NULL;
  537. math::Rect clipBox;
  538. void* fb = NULL;
  539. };
  540. virtual void draw(const DrawArgs& args);
  541. };
  542. struct OpaqueWidget : Widget {
  543. };
  544. struct SvgWidget : Widget {
  545. void wrap() {}
  546. void setSvg(void* svg) {}
  547. };
  548. struct TransparentWidget : Widget {
  549. };
  550. } // namespace widget
  551. namespace app {
  552. static constexpr const float RACK_GRID_WIDTH = 15;
  553. static constexpr const float RACK_GRID_HEIGHT = 380;
  554. // static constexpr const math::Vec RACK_GRID_SIZE = math::Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT);
  555. // static constexpr const math::Vec RACK_OFFSET = RACK_GRID_SIZE.mult(math::Vec(2000, 100));
  556. struct CircularShadow : widget::TransparentWidget {
  557. float blurRadius;
  558. float opacity;
  559. };
  560. struct LightWidget : widget::TransparentWidget {
  561. NVGcolor bgColor, color, borderColor;
  562. };
  563. struct ModuleWidget : widget::OpaqueWidget {
  564. // plugin::Model* model = NULL;
  565. engine::Module* module = NULL;
  566. void setModel(void*) {}
  567. void setModule(void*) {}
  568. void setPanel(void*) {}
  569. void addParam(void*) {}
  570. void addInput(void*) {}
  571. void addOutput(void*) {}
  572. virtual void appendContextMenu(ui::Menu* menu) {}
  573. };
  574. struct MultiLightWidget : LightWidget {
  575. // std::vector<NVGcolor> baseColors;
  576. // int getNumColors();
  577. void addBaseColor(NVGcolor baseColor) {}
  578. // void setBrightnesses(const std::vector<float>& brightnesses);
  579. };
  580. struct ModuleLightWidget : MultiLightWidget {
  581. // engine::Module* module = NULL;
  582. // int firstLightId = -1;
  583. // ModuleLightWidget();
  584. // ~ModuleLightWidget();
  585. // engine::Light* getLight(int colorId);
  586. // engine::LightInfo* getLightInfo();
  587. // void createTooltip();
  588. // void destroyTooltip();
  589. };
  590. struct ParamWidget : widget::OpaqueWidget {
  591. engine::Module* module = NULL;
  592. int paramId = -1;
  593. virtual void initParamQuantity() {}
  594. engine::ParamQuantity* getParamQuantity() { return module ? module->paramQuantities[paramId] : NULL; }
  595. void createTooltip() {}
  596. void destroyTooltip() {}
  597. void createContextMenu();
  598. virtual void appendContextMenu(void* menu) {}
  599. void resetAction();
  600. };
  601. struct PortWidget : widget::OpaqueWidget {
  602. };
  603. struct Knob : ParamWidget {
  604. bool horizontal = false;
  605. bool smooth = true;
  606. bool snap = false;
  607. float speed = 1.f;
  608. bool forceLinear = false;
  609. float minAngle = -M_PI;
  610. float maxAngle = M_PI;
  611. };
  612. struct SliderKnob : Knob {
  613. };
  614. struct Switch : ParamWidget {
  615. bool momentary = false;
  616. };
  617. struct SvgKnob : Knob {
  618. CircularShadow* shadow;
  619. void setSvg(void* svg) {}
  620. };
  621. struct SvgPanel : widget::Widget {
  622. // widget::FramebufferWidget* fb;
  623. // widget::SvgWidget* sw;
  624. // PanelBorder* panelBorder;
  625. void setBackground(void* svg) {}
  626. };
  627. struct SvgPort : PortWidget {
  628. // widget::FramebufferWidget* fb;
  629. CircularShadow* shadow;
  630. // widget::SvgWidget* sw;
  631. void setSvg(void* svg) {}
  632. };
  633. struct SvgScrew : widget::Widget {
  634. // widget::FramebufferWidget* fb;
  635. widget::SvgWidget* sw;
  636. void setSvg(void* svg) {}
  637. };
  638. struct SvgSlider : app::SliderKnob {
  639. // widget::FramebufferWidget* fb;
  640. widget::SvgWidget* background;
  641. // widget::SvgWidget* handle;
  642. math::Vec minHandlePos, maxHandlePos;
  643. void setBackgroundSvg(void* svg) {}
  644. void setHandleSvg(void* svg) {}
  645. void setHandlePos(math::Vec minHandlePos, math::Vec maxHandlePos) {}
  646. void setHandlePosCentered(math::Vec minHandlePosCentered, math::Vec maxHandlePosCentered) {}
  647. };
  648. struct SvgSwitch : Switch {
  649. // widget::FramebufferWidget* fb;
  650. // CircularShadow* shadow;
  651. // widget::SvgWidget* sw;
  652. // std::vector<std::shared_ptr<window::Svg>> frames;
  653. bool latch = false;
  654. void addFrame(void* svg) {}
  655. };
  656. } // namespace app
  657. namespace asset {
  658. const char* plugin(void* instance, const char* path) {
  659. return NULL;
  660. }
  661. } // namespace asset
  662. namespace componentlibrary {
  663. static constexpr const NVGcolor SCHEME_LIGHT_GRAY = {};
  664. template <typename TBase = app::ModuleLightWidget>
  665. struct TSvgLight : TBase {
  666. // widget::FramebufferWidget* fb;
  667. // widget::SvgWidget* sw;
  668. void setSvg(void* svg) {}
  669. };
  670. using SvgLight = TSvgLight<>;
  671. template <typename TBase = app::ModuleLightWidget>
  672. struct TGrayModuleLightWidget : TBase {
  673. };
  674. using GrayModuleLightWidget = TGrayModuleLightWidget<>;
  675. template <typename TBase = GrayModuleLightWidget>
  676. struct TWhiteLight : TBase {
  677. };
  678. using WhiteLight = TWhiteLight<>;
  679. template <typename TBase = GrayModuleLightWidget>
  680. struct TRedLight : TBase {
  681. };
  682. using RedLight = TRedLight<>;
  683. template <typename TBase = GrayModuleLightWidget>
  684. struct TGreenLight : TBase {
  685. };
  686. using GreenLight = TGreenLight<>;
  687. template <typename TBase = GrayModuleLightWidget>
  688. struct TBlueLight : TBase {
  689. };
  690. using BlueLight = TBlueLight<>;
  691. template <typename TBase = GrayModuleLightWidget>
  692. struct TYellowLight : TBase {
  693. };
  694. using YellowLight = TYellowLight<>;
  695. template <typename TBase = GrayModuleLightWidget>
  696. struct TGreenRedLight : TBase {
  697. };
  698. using GreenRedLight = TGreenRedLight<>;
  699. template <typename TBase = GrayModuleLightWidget>
  700. struct TRedGreenBlueLight : TBase {
  701. };
  702. using RedGreenBlueLight = TRedGreenBlueLight<>;
  703. template <typename TBase>
  704. struct LargeLight : TSvgLight<TBase> {
  705. };
  706. template <typename TBase>
  707. struct MediumLight : TSvgLight<TBase> {
  708. };
  709. template <typename TBase>
  710. struct SmallLight : TSvgLight<TBase> {
  711. };
  712. template <typename TBase>
  713. struct TinyLight : TSvgLight<TBase> {
  714. };
  715. struct ScrewBlack : app::SvgScrew {
  716. };
  717. } // namespace componentlibrary
  718. namespace dsp {
  719. inline float sinc(float x) {
  720. if (x == 0.f)
  721. return 1.f;
  722. x *= M_PI;
  723. return std::sin(x) / x;
  724. }
  725. // template <typename T>
  726. // T sinc(T x) {
  727. // T zeromask = (x == 0.f);
  728. // x *= M_PI;
  729. // x = simd::sin(x) / x;
  730. // return simd::ifelse(zeromask, 1.f, x);
  731. // }
  732. template <typename T>
  733. inline T blackmanHarris(T p) {
  734. return
  735. + T(0.35875)
  736. - T(0.48829) * std::cos(2 * T(M_PI) * p)
  737. + T(0.14128) * std::cos(4 * T(M_PI) * p)
  738. - T(0.01168) * std::cos(6 * T(M_PI) * p);
  739. }
  740. inline void blackmanHarrisWindow(float* x, int len) {
  741. for (int i = 0; i < len; i++) {
  742. x[i] *= blackmanHarris(float(i) / (len - 1));
  743. }
  744. }
  745. inline void boxcarLowpassIR(float* out, int len, float cutoff = 0.5f) {
  746. for (int i = 0; i < len; i++) {
  747. float t = i - (len - 1) / 2.f;
  748. out[i] = 2 * cutoff * sinc(2 * cutoff * t);
  749. }
  750. }
  751. template <int OVERSAMPLE, int QUALITY, typename T = float>
  752. struct Decimator {
  753. T inBuffer[OVERSAMPLE * QUALITY];
  754. float kernel[OVERSAMPLE * QUALITY];
  755. int inIndex;
  756. Decimator(float cutoff = 0.9f) {
  757. boxcarLowpassIR(kernel, OVERSAMPLE * QUALITY, cutoff * 0.5f / OVERSAMPLE);
  758. blackmanHarrisWindow(kernel, OVERSAMPLE * QUALITY);
  759. reset();
  760. }
  761. void reset() {
  762. inIndex = 0;
  763. std::memset(inBuffer, 0, sizeof(inBuffer));
  764. }
  765. T process(T* in) {
  766. std::memcpy(&inBuffer[inIndex], in, OVERSAMPLE * sizeof(T));
  767. inIndex += OVERSAMPLE;
  768. inIndex %= OVERSAMPLE * QUALITY;
  769. T out = 0.f;
  770. for (int i = 0; i < OVERSAMPLE * QUALITY; i++) {
  771. int index = inIndex - 1 - i;
  772. index = (index + OVERSAMPLE * QUALITY) % (OVERSAMPLE * QUALITY);
  773. out += kernel[i] * inBuffer[index];
  774. }
  775. return out;
  776. }
  777. };
  778. struct PulseGenerator {
  779. float remaining = 0.f;
  780. void reset() { remaining = 0.f; }
  781. bool process(float deltaTime) {
  782. if (remaining > 0.f) {
  783. remaining -= deltaTime;
  784. return true;
  785. }
  786. return false;
  787. }
  788. void trigger(float duration = 1e-3f) {
  789. if (duration > remaining) {
  790. remaining = duration;
  791. }
  792. }
  793. };
  794. } // namespace dsp
  795. namespace event {
  796. // using Base = widget::BaseEvent;
  797. // using PositionBase = widget::Widget::PositionBaseEvent;
  798. // using KeyBase = widget::Widget::KeyBaseEvent;
  799. // using TextBase = widget::Widget::TextBaseEvent;
  800. // using Hover = widget::Widget::HoverEvent;
  801. // using Button = widget::Widget::ButtonEvent;
  802. // using DoubleClick = widget::Widget::DoubleClickEvent;
  803. // using HoverKey = widget::Widget::HoverKeyEvent;
  804. // using HoverText = widget::Widget::HoverTextEvent;
  805. // using HoverScroll = widget::Widget::HoverScrollEvent;
  806. // using Enter = widget::Widget::EnterEvent;
  807. // using Leave = widget::Widget::LeaveEvent;
  808. // using Select = widget::Widget::SelectEvent;
  809. // using Deselect = widget::Widget::DeselectEvent;
  810. // using SelectKey = widget::Widget::SelectKeyEvent;
  811. // using SelectText = widget::Widget::SelectTextEvent;
  812. // using DragBase = widget::Widget::DragBaseEvent;
  813. // using DragStart = widget::Widget::DragStartEvent;
  814. // using DragEnd = widget::Widget::DragEndEvent;
  815. // using DragMove = widget::Widget::DragMoveEvent;
  816. // using DragHover = widget::Widget::DragHoverEvent;
  817. // using DragEnter = widget::Widget::DragEnterEvent;
  818. // using DragLeave = widget::Widget::DragLeaveEvent;
  819. // using DragDrop = widget::Widget::DragDropEvent;
  820. // using PathDrop = widget::Widget::PathDropEvent;
  821. using Action = widget::Widget::ActionEvent;
  822. using Change = widget::Widget::ChangeEvent;
  823. // using Dirty = widget::Widget::DirtyEvent;
  824. // using Reposition = widget::Widget::RepositionEvent;
  825. // using Resize = widget::Widget::ResizeEvent;
  826. // using Add = widget::Widget::AddEvent;
  827. // using Remove = widget::Widget::RemoveEvent;
  828. // using Show = widget::Widget::ShowEvent;
  829. // using Hide = widget::Widget::HideEvent;
  830. } // namespace event
  831. namespace plugin {
  832. struct Model {
  833. virtual ~Model() {}
  834. virtual engine::Module* createModule() = 0;
  835. };
  836. struct Plugin {
  837. };
  838. } // namespace plugin
  839. namespace ui {
  840. struct Menu : widget::OpaqueWidget {
  841. // Menu* parentMenu = NULL;
  842. // Menu* childMenu = NULL;
  843. // MenuEntry* activeEntry = NULL;
  844. // BNDcornerFlags cornerFlags = BND_CORNER_NONE;
  845. // void setChildMenu(Menu* menu) {}
  846. };
  847. struct MenuEntry : widget::OpaqueWidget {
  848. };
  849. struct MenuItem : MenuEntry {
  850. std::string text;
  851. std::string rightText;
  852. bool disabled = false;
  853. };
  854. struct MenuLabel : MenuEntry {
  855. std::string text;
  856. };
  857. } // namespace ui
  858. namespace window {
  859. static constexpr const float SVG_DPI = 75.f;
  860. static constexpr const float MM_PER_IN = 25.4f;
  861. inline float in2px(float in) { return in * SVG_DPI; }
  862. inline math::Vec in2px(math::Vec in) { return in.mult(SVG_DPI); }
  863. inline float mm2px(float mm) { return mm * (SVG_DPI / MM_PER_IN); }
  864. inline math::Vec mm2px(math::Vec mm) { return mm.mult(SVG_DPI / MM_PER_IN); }
  865. struct Image {
  866. int handle;
  867. };
  868. struct Window {
  869. std::shared_ptr<Image> loadImage(const std::string&) { return {}; }
  870. void* loadSvg(const void*) { return NULL; }
  871. };
  872. };
  873. using namespace app;
  874. using namespace componentlibrary;
  875. using namespace engine;
  876. using namespace math;
  877. using namespace ui;
  878. using namespace widget;
  879. using namespace window;
  880. using plugin::Plugin;
  881. using plugin::Model;
  882. template <class TModule, class TModuleWidget>
  883. plugin::Model* createModel(std::string slug) {
  884. struct TModel : plugin::Model {
  885. engine::Module* createModule() override {
  886. return new TModule;
  887. }
  888. };
  889. return new TModel;
  890. }
  891. template <typename T>
  892. T* construct() { return NULL; }
  893. template <typename T, typename F, typename V, typename... Args>
  894. T* construct(F f, V v, Args... args) { return NULL; }
  895. template <class TWidget>
  896. TWidget* createWidget(math::Vec pos) {
  897. return NULL;
  898. }
  899. template <class TParamWidget>
  900. TParamWidget* createParam(math::Vec pos, engine::Module* module, int paramId) {
  901. return NULL;
  902. }
  903. template <class TParamWidget>
  904. TParamWidget* createParamCentered(math::Vec pos, engine::Module* module, int paramId) {
  905. return NULL;
  906. }
  907. template <class TPortWidget>
  908. TPortWidget* createInput(math::Vec pos, engine::Module* module, int inputId) {
  909. return NULL;
  910. }
  911. template <class TPortWidget>
  912. TPortWidget* createInputCentered(math::Vec pos, engine::Module* module, int inputId) {
  913. return NULL;
  914. }
  915. template <class TPortWidget>
  916. TPortWidget* createOutput(math::Vec pos, engine::Module* module, int outputId) {
  917. return NULL;
  918. }
  919. template <class TPortWidget>
  920. TPortWidget* createOutputCentered(math::Vec pos, engine::Module* module, int outputId) {
  921. return NULL;
  922. }
  923. template <class TModuleLightWidget>
  924. TModuleLightWidget* createLight(math::Vec pos, engine::Module* module, int firstLightId) {
  925. return NULL;
  926. }
  927. template <class TModuleLightWidget>
  928. TModuleLightWidget* createLightCentered(math::Vec pos, engine::Module* module, int firstLightId) {
  929. return NULL;
  930. }
  931. struct Context {
  932. engine::Engine _engine;
  933. window::Window _window;
  934. engine::Engine* engine = &_engine;
  935. window::Window* window = &_window;
  936. };
  937. Context* contextGet();
  938. void contextSet(Context* context);
  939. } // namespace rack
  940. #define APP rack::contextGet()