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.

1227 lines
35KB

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