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.

2237 lines
66KB

  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 <climits>
  28. #include <cmath>
  29. #include <cstdarg>
  30. #include <cstdlib>
  31. #include <cstring>
  32. #include <algorithm>
  33. #include <atomic>
  34. #include <functional>
  35. #include <list>
  36. #include <memory>
  37. #include <string>
  38. #include <vector>
  39. #define assert(x)
  40. #define ENUMS(name, count) name, name ## _LAST = name + (count) - 1
  41. #define VEC_ARGS(v) (v).x, (v).y
  42. #define RECT_ARGS(r) (r).pos.x, (r).pos.y, (r).size.x, (r).size.y
  43. #define GLFW_MOUSE_BUTTON_LEFT 0
  44. #define GLFW_MOUSE_BUTTON_RIGHT 0
  45. enum NVGalign {
  46. NVG_ALIGN_LEFT,
  47. NVG_ALIGN_RIGHT
  48. };
  49. struct NVGcolor { float a; };
  50. struct NVGpaint {};
  51. struct NVGcontext {};
  52. struct NSVGimage {};
  53. inline NVGcolor nvgRGB(int r, int g, int b) { return {}; }
  54. inline NVGcolor nvgRGBA(int r, int g, int b, int a) { return {}; }
  55. inline NVGcolor nvgRGBf(float r, float g, float b) { return {}; }
  56. inline NVGcolor nvgRGBAf(float r, float g, float b, float a) { return {}; }
  57. inline NVGcolor nvgTransRGBA(NVGcolor, int) { return {}; }
  58. inline NVGcolor nvgTransRGBAf(NVGcolor, float) { return {}; }
  59. inline void nvgBeginPath(NVGcontext* vg) {}
  60. inline void nvgFillColor(NVGcontext* vg, NVGcolor) {}
  61. inline void nvgFillPaint(NVGcontext* vg, NVGpaint) {}
  62. inline void nvgFill(NVGcontext* vg) {}
  63. inline void nvgStrokeColor(NVGcontext* vg, NVGcolor) {}
  64. inline void nvgStrokeWidth(NVGcontext* vg, float) {}
  65. inline void nvgStroke(NVGcontext* vg) {}
  66. inline void nvgRect(NVGcontext* vg, float a, float b, float c, float d) {}
  67. inline void nvgImageSize(NVGcontext*, int, void*, void*) {}
  68. inline NVGpaint nvgImagePattern(NVGcontext*, float, float, float, float, float, int handle, float) { return {}; }
  69. inline void nvgRoundedRect(NVGcontext* vg, float, float, float, float, float) {}
  70. inline void nvgFontSize(NVGcontext*, int) {}
  71. inline void nvgFontFaceId(NVGcontext*, int) {}
  72. inline void nvgTextLetterSpacing(NVGcontext*, float) {}
  73. inline void nvgText(NVGcontext*, float, float, const char*, const char*) {}
  74. inline void nvgTextAlign(NVGcontext*, NVGalign) {}
  75. inline void nvgSave(NVGcontext*) {}
  76. inline void nvgRestore(NVGcontext*) {}
  77. inline void nvgScale(NVGcontext*, float, float) {}
  78. struct json_t {};
  79. inline json_t* json_boolean(bool) { return NULL; }
  80. inline json_t* json_integer(int) { return NULL; }
  81. inline json_t* json_object(void) { return NULL; }
  82. inline json_t* json_object_get(json_t*, const char*) { return NULL; }
  83. inline bool json_is_true(json_t*) { return false; }
  84. inline bool json_boolean_value(json_t*) { return false; }
  85. inline int json_integer_value(json_t*) { return 0; }
  86. inline float json_number_value(json_t*) { return 0.f; }
  87. inline void json_object_set_new(json_t*, const char*, json_t*) {}
  88. inline json_t* json_array(void) { return NULL; }
  89. inline json_t* json_array_get(json_t*, int) { return NULL; }
  90. inline void json_array_insert_new(json_t*, int, json_t*) {}
  91. namespace rack {
  92. struct Quantity {
  93. virtual ~Quantity() {}
  94. virtual void setValue(float value) {}
  95. virtual float getValue() { return 0.f; }
  96. virtual float getMinValue() { return 0.f; }
  97. virtual float getMaxValue() { return 1.f; }
  98. virtual float getDefaultValue() { return 0.f; }
  99. // virtual float getDisplayValue();
  100. // virtual void setDisplayValue(float displayValue);
  101. // virtual int getDisplayPrecision();
  102. // virtual std::string getDisplayValueString();
  103. // virtual void setDisplayValueString(std::string s);
  104. virtual std::string getLabel() { return ""; }
  105. virtual std::string getUnit() { return ""; }
  106. // virtual std::string getString();
  107. // virtual void reset();
  108. // virtual void randomize();
  109. // bool isMin();
  110. // bool isMax();
  111. // void setMin();
  112. // void setMax();
  113. // void toggle();
  114. // void moveValue(float deltaValue);
  115. // float getRange();
  116. // bool isBounded();
  117. // float toScaled(float value);
  118. // float fromScaled(float scaledValue);
  119. // void setScaledValue(float scaledValue);
  120. // float getScaledValue();
  121. // void moveScaledValue(float deltaScaledValue);
  122. };
  123. namespace ui {
  124. struct Menu;
  125. }
  126. namespace window {
  127. struct Svg;
  128. }
  129. namespace math {
  130. template <typename T>
  131. inline bool isEven(T x) { return x % 2 == 0; }
  132. template <typename T>
  133. inline bool isOdd(T x) { return x % 2 != 0; }
  134. inline int clamp(int x, int a, int b) { return std::max(std::min(x, b), a); }
  135. inline int clampSafe(int x, int a, int b) { return (a <= b) ? clamp(x, a, b) : clamp(x, b, a); }
  136. inline float clamp(float x, float a = 0.f, float b = 1.f) { return std::fmax(std::fmin(x, b), a); }
  137. inline float clampSafe(float x, float a = 0.f, float b = 1.f) { return (a <= b) ? clamp(x, a, b) : clamp(x, b, a); }
  138. inline int eucMod(int a, int b) {
  139. int mod = a % b;
  140. if (mod < 0) {
  141. mod += b;
  142. }
  143. return mod;
  144. }
  145. inline int eucDiv(int a, int b) {
  146. int div = a / b;
  147. int mod = a % b;
  148. if (mod < 0) {
  149. div -= 1;
  150. }
  151. return div;
  152. }
  153. inline void eucDivMod(int a, int b, int* div, int* mod) {
  154. *div = a / b;
  155. *mod = a % b;
  156. if (*mod < 0) {
  157. *div -= 1;
  158. *mod += b;
  159. }
  160. }
  161. inline int log2(int n) {
  162. int i = 0;
  163. while (n >>= 1) {
  164. i++;
  165. }
  166. return i;
  167. }
  168. template <typename T>
  169. bool isPow2(T n) { return n > 0 && (n & (n - 1)) == 0; }
  170. template <typename T>
  171. T sgn(T x) { return x > 0 ? 1 : (x < 0 ? -1 : 0); }
  172. #if defined __clang__
  173. __attribute__((optnone))
  174. #else
  175. __attribute__((optimize("signed-zeros")))
  176. #endif
  177. inline float normalizeZero(float x) { return x + 0.f; }
  178. inline float eucMod(float a, float b) {
  179. float mod = std::fmod(a, b);
  180. if (mod < 0.f) {
  181. mod += b;
  182. }
  183. return mod;
  184. }
  185. inline bool isNear(float a, float b, float epsilon = 1e-6f) { return std::fabs(a - b) <= epsilon; }
  186. inline float chop(float x, float epsilon = 1e-6f) { return std::fabs(x) <= epsilon ? 0.f : x; }
  187. inline float rescale(float x, float xMin, float xMax, float yMin, float yMax) {
  188. return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
  189. }
  190. inline float crossfade(float a, float b, float p) { return a + (b - a) * p; }
  191. inline float interpolateLinear(const float* p, float x) {
  192. const int xi = x;
  193. const float xf = x - xi;
  194. return crossfade(p[xi], p[xi + 1], xf);
  195. }
  196. inline void complexMult(float ar, float ai, float br, float bi, float* cr, float* ci) {
  197. *cr = ar * br - ai * bi;
  198. *ci = ar * bi + ai * br;
  199. }
  200. struct Rect;
  201. struct Vec {
  202. float x = 0.f;
  203. float y = 0.f;
  204. Vec() {}
  205. Vec(float xy) : x(xy), y(xy) {}
  206. Vec(float x, float y) : x(x), y(y) {}
  207. float& operator[](int i) { return (i == 0) ? x : y; }
  208. const float& operator[](int i) const { return (i == 0) ? x : y; }
  209. Vec neg() const { return Vec(-x, -y); }
  210. Vec plus(Vec b) const { return Vec(x + b.x, y + b.y); }
  211. Vec minus(Vec b) const { return Vec(x - b.x, y - b.y); }
  212. Vec mult(float s) const { return Vec(x * s, y * s); }
  213. Vec mult(Vec b) const { return Vec(x * b.x, y * b.y); }
  214. Vec div(float s) const { return Vec(x / s, y / s); }
  215. Vec div(Vec b) const { return Vec(x / b.x, y / b.y); }
  216. float dot(Vec b) const { return x * b.x + y * b.y; }
  217. float arg() const { return std::atan2(y, x); }
  218. float norm() const { return std::hypot(x, y); }
  219. Vec normalize() const { return div(norm()); }
  220. float square() const { return x * x + y * y; }
  221. float area() const { return x * y; }
  222. Vec rotate(float angle) {
  223. float sin = std::sin(angle);
  224. float cos = std::cos(angle);
  225. return Vec(x * cos - y * sin, x * sin + y * cos);
  226. }
  227. Vec flip() const { return Vec(y, x); }
  228. Vec min(Vec b) const { return Vec(std::fmin(x, b.x), std::fmin(y, b.y)); }
  229. Vec max(Vec b) const { return Vec(std::fmax(x, b.x), std::fmax(y, b.y)); }
  230. Vec abs() const { return Vec(std::fabs(x), std::fabs(y)); }
  231. Vec round() const { return Vec(std::round(x), std::round(y)); }
  232. Vec floor() const { return Vec(std::floor(x), std::floor(y)); }
  233. Vec ceil() const { return Vec(std::ceil(x), std::ceil(y)); }
  234. bool equals(Vec b) const { return x == b.x && y == b.y; }
  235. bool isZero() const { return x == 0.f && y == 0.f; }
  236. bool isFinite() const { return std::isfinite(x) && std::isfinite(y); }
  237. Vec clamp(Rect bound) const;
  238. Vec clampSafe(Rect bound) const;
  239. Vec crossfade(Vec b, float p) { return this->plus(b.minus(*this).mult(p)); }
  240. bool isEqual(Vec b) const { return equals(b); }
  241. };
  242. struct Rect {
  243. Vec pos;
  244. Vec size;
  245. Rect() {}
  246. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  247. Rect(float posX, float posY, float sizeX, float sizeY) : pos(Vec(posX, posY)), size(Vec(sizeX, sizeY)) {}
  248. static Rect fromMinMax(Vec a, Vec b) { return Rect(a, b.minus(a)); }
  249. static Rect fromCorners(Vec a, Vec b) { return fromMinMax(a.min(b), a.max(b)); }
  250. static Rect inf() { return Rect(Vec(-INFINITY, -INFINITY), Vec(INFINITY, INFINITY)); }
  251. bool contains(Vec v) const {
  252. return (pos.x <= v.x) && (size.x == INFINITY || v.x < pos.x + size.x)
  253. && (pos.y <= v.y) && (size.y == INFINITY || v.y < pos.y + size.y);
  254. }
  255. bool contains(Rect r) const {
  256. return (pos.x <= r.pos.x) && (r.pos.x - size.x <= pos.x - r.size.x)
  257. && (pos.y <= r.pos.y) && (r.pos.y - size.y <= pos.y - r.size.y);
  258. }
  259. bool intersects(Rect r) const {
  260. return (r.size.x == INFINITY || pos.x < r.pos.x + r.size.x) && (size.x == INFINITY || r.pos.x < pos.x + size.x)
  261. && (r.size.y == INFINITY || pos.y < r.pos.y + r.size.y) && (size.y == INFINITY || r.pos.y < pos.y + size.y);
  262. }
  263. bool equals(Rect r) const { return pos.equals(r.pos) && size.equals(r.size); }
  264. float getLeft() const { return pos.x; }
  265. float getRight() const { return (size.x == INFINITY) ? INFINITY : (pos.x + size.x); }
  266. float getTop() const { return pos.y; }
  267. float getBottom() const { return (size.y == INFINITY) ? INFINITY : (pos.y + size.y); }
  268. float getWidth() const { return size.x; }
  269. float getHeight() const { return size.y; }
  270. Vec getCenter() const { return pos.plus(size.mult(0.5f)); }
  271. Vec getTopLeft() const { return pos; }
  272. Vec getTopRight() const { return Vec(getRight(), getTop()); }
  273. Vec getBottomLeft() const { return Vec(getLeft(), getBottom()); }
  274. Vec getBottomRight() const { return Vec(getRight(), getBottom()); }
  275. Rect clamp(Rect bound) const {
  276. Rect r;
  277. r.pos.x = math::clampSafe(pos.x, bound.pos.x, bound.pos.x + bound.size.x);
  278. r.pos.y = math::clampSafe(pos.y, bound.pos.y, bound.pos.y + bound.size.y);
  279. r.size.x = math::clamp(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x;
  280. r.size.y = math::clamp(pos.y + size.y, bound.pos.y, bound.pos.y + bound.size.y) - r.pos.y;
  281. return r;
  282. }
  283. Rect nudge(Rect bound) const {
  284. Rect r;
  285. r.size = size;
  286. r.pos.x = math::clampSafe(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  287. r.pos.y = math::clampSafe(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  288. return r;
  289. }
  290. Rect expand(Rect b) const {
  291. Rect r;
  292. r.pos.x = std::fmin(pos.x, b.pos.x);
  293. r.pos.y = std::fmin(pos.y, b.pos.y);
  294. r.size.x = std::fmax(pos.x + size.x, b.pos.x + b.size.x) - r.pos.x;
  295. r.size.y = std::fmax(pos.y + size.y, b.pos.y + b.size.y) - r.pos.y;
  296. return r;
  297. }
  298. Rect intersect(Rect b) const {
  299. Rect r;
  300. r.pos.x = std::fmax(pos.x, b.pos.x);
  301. r.pos.y = std::fmax(pos.y, b.pos.y);
  302. r.size.x = std::fmin(pos.x + size.x, b.pos.x + b.size.x) - r.pos.x;
  303. r.size.y = std::fmin(pos.y + size.y, b.pos.y + b.size.y) - r.pos.y;
  304. return r;
  305. }
  306. Rect zeroPos() const {
  307. return Rect(Vec(), size);
  308. }
  309. Rect grow(Vec delta) const {
  310. Rect r;
  311. r.pos = pos.minus(delta);
  312. r.size = size.plus(delta.mult(2.f));
  313. return r;
  314. }
  315. Rect shrink(Vec delta) const {
  316. Rect r;
  317. r.pos = pos.plus(delta);
  318. r.size = size.minus(delta.mult(2.f));
  319. return r;
  320. }
  321. Vec interpolate(Vec p) {
  322. return pos.plus(size.mult(p));
  323. }
  324. bool isContaining(Vec v) const { return contains(v); }
  325. bool isIntersecting(Rect r) const { return intersects(r); }
  326. bool isEqual(Rect r) const { return equals(r); }
  327. };
  328. inline Vec Vec::clamp(Rect bound) const {
  329. return Vec(math::clamp(x, bound.pos.x, bound.pos.x + bound.size.x),
  330. math::clamp(y, bound.pos.y, bound.pos.y + bound.size.y));
  331. }
  332. inline Vec Vec::clampSafe(Rect bound) const {
  333. return Vec(math::clampSafe(x, bound.pos.x, bound.pos.x + bound.size.x),
  334. math::clampSafe(y, bound.pos.y, bound.pos.y + bound.size.y));
  335. }
  336. inline Vec operator+(const Vec& a) { return a; }
  337. inline Vec operator-(const Vec& a) { return a.neg(); }
  338. inline Vec operator+(const Vec& a, const Vec& b) { return a.plus(b); }
  339. inline Vec operator-(const Vec& a, const Vec& b) { return a.minus(b); }
  340. inline Vec operator*(const Vec& a, const Vec& b) { return a.mult(b); }
  341. inline Vec operator*(const Vec& a, const float& b) { return a.mult(b); }
  342. inline Vec operator*(const float& a, const Vec& b) { return b.mult(a); }
  343. inline Vec operator/(const Vec& a, const Vec& b) { return a.div(b); }
  344. inline Vec operator/(const Vec& a, const float& b) { return a.div(b); }
  345. inline Vec operator+=(Vec& a, const Vec& b) { return a = a.plus(b); }
  346. inline Vec operator-=(Vec& a, const Vec& b) { return a = a.minus(b); }
  347. inline Vec operator*=(Vec& a, const Vec& b) { return a = a.mult(b); }
  348. inline Vec operator*=(Vec& a, const float& b) { return a = a.mult(b); }
  349. inline Vec operator/=(Vec& a, const Vec& b) { return a = a.div(b); }
  350. inline Vec operator/=(Vec& a, const float& b) { return a = a.div(b); }
  351. inline bool operator==(const Vec& a, const Vec& b) { return a.equals(b); }
  352. inline bool operator!=(const Vec& a, const Vec& b) { return !a.equals(b); }
  353. inline bool operator==(const Rect& a, const Rect& b) { return a.equals(b); }
  354. inline bool operator!=(const Rect& a, const Rect& b) { return !a.equals(b); }
  355. } // namespace math
  356. namespace engine {
  357. static constexpr const int PORT_MAX_CHANNELS = 16;
  358. struct Module;
  359. struct Engine {
  360. float getSampleRate() { return sampleRate; }
  361. // custom
  362. float sampleRate = 0.f;
  363. };
  364. struct Light {
  365. float value = 0.f;
  366. void setBrightness(float brightness) {
  367. value = brightness;
  368. }
  369. float getBrightness() {
  370. return value;
  371. }
  372. void setBrightnessSmooth(float brightness, float deltaTime, float lambda = 30.f) {
  373. if (brightness < value) {
  374. // Fade out light
  375. value += (brightness - value) * lambda * deltaTime;
  376. }
  377. else {
  378. // Immediately illuminate light
  379. value = brightness;
  380. }
  381. }
  382. void setSmoothBrightness(float brightness, float deltaTime) {
  383. setBrightnessSmooth(brightness, deltaTime);
  384. }
  385. void setBrightnessSmooth(float brightness, int frames = 1) {
  386. setBrightnessSmooth(brightness, frames / 44100.f);
  387. }
  388. };
  389. struct LightInfo {
  390. Module* module = NULL;
  391. int lightId = -1;
  392. std::string name;
  393. std::string description;
  394. virtual ~LightInfo() {}
  395. virtual std::string getName() { return name; }
  396. virtual std::string getDescription() { return description; }
  397. };
  398. struct Param {
  399. float value = 0.f;
  400. float getValue() { return value; }
  401. void setValue(float value) { this->value = value; }
  402. };
  403. struct Port {
  404. union {
  405. float voltages[PORT_MAX_CHANNELS] = {};
  406. float value;
  407. };
  408. union {
  409. uint8_t channels = 0;
  410. uint8_t active;
  411. };
  412. Light plugLights[3];
  413. enum Type {
  414. INPUT,
  415. OUTPUT,
  416. };
  417. void setVoltage(float voltage, int channel = 0) { voltages[channel] = voltage; }
  418. float getVoltage(int channel = 0) { return voltages[channel]; }
  419. float getPolyVoltage(int channel) { return isMonophonic() ? getVoltage(0) : getVoltage(channel); }
  420. float getNormalVoltage(float normalVoltage, int channel = 0) {
  421. return isConnected() ? getVoltage(channel) : normalVoltage;
  422. }
  423. float getNormalPolyVoltage(float normalVoltage, int channel) {
  424. return isConnected() ? getPolyVoltage(channel) : normalVoltage;
  425. }
  426. float* getVoltages(int firstChannel = 0) { return &voltages[firstChannel]; }
  427. void readVoltages(float* v) {
  428. for (int c = 0; c < channels; c++) {
  429. v[c] = voltages[c];
  430. }
  431. }
  432. void writeVoltages(const float* v) {
  433. for (int c = 0; c < channels; c++) {
  434. voltages[c] = v[c];
  435. }
  436. }
  437. void clearVoltages() {
  438. for (int c = 0; c < channels; c++) {
  439. voltages[c] = 0.f;
  440. }
  441. }
  442. float getVoltageSum() {
  443. float sum = 0.f;
  444. for (int c = 0; c < channels; c++) {
  445. sum += voltages[c];
  446. }
  447. return sum;
  448. }
  449. float getVoltageRMS() {
  450. if (channels == 0) {
  451. return 0.f;
  452. }
  453. else if (channels == 1) {
  454. return std::fabs(voltages[0]);
  455. }
  456. else {
  457. float sum = 0.f;
  458. for (int c = 0; c < channels; c++) {
  459. sum += std::pow(voltages[c], 2);
  460. }
  461. return std::sqrt(sum);
  462. }
  463. }
  464. // template <typename T>
  465. // T getVoltageSimd(int firstChannel) {
  466. // return T::load(&voltages[firstChannel]);
  467. // }
  468. //
  469. // template <typename T>
  470. // T getPolyVoltageSimd(int firstChannel) {
  471. // return isMonophonic() ? getVoltage(0) : getVoltageSimd<T>(firstChannel);
  472. // }
  473. //
  474. // template <typename T>
  475. // T getNormalVoltageSimd(T normalVoltage, int firstChannel) {
  476. // return isConnected() ? getVoltageSimd<T>(firstChannel) : normalVoltage;
  477. // }
  478. //
  479. // template <typename T>
  480. // T getNormalPolyVoltageSimd(T normalVoltage, int firstChannel) {
  481. // return isConnected() ? getPolyVoltageSimd<T>(firstChannel) : normalVoltage;
  482. // }
  483. //
  484. // template <typename T>
  485. // void setVoltageSimd(T voltage, int firstChannel) {
  486. // voltage.store(&voltages[firstChannel]);
  487. // }
  488. void setChannels(int channels) {
  489. if (this->channels == 0) {
  490. return;
  491. }
  492. for (int c = channels; c < this->channels; c++) {
  493. voltages[c] = 0.f;
  494. }
  495. if (channels == 0) {
  496. channels = 1;
  497. }
  498. this->channels = channels;
  499. }
  500. int getChannels() { return channels; }
  501. bool isConnected() { return channels > 0; }
  502. bool isMonophonic() { return channels == 1; }
  503. bool isPolyphonic() { return channels > 1; }
  504. float normalize(float normalVoltage) { return getNormalVoltage(normalVoltage); }
  505. };
  506. struct Output : Port {};
  507. struct Input : Port {};
  508. struct PortInfo {
  509. Module* module = NULL;
  510. Port::Type type = Port::INPUT;
  511. int portId = -1;
  512. std::string name;
  513. std::string description;
  514. virtual ~PortInfo() {}
  515. virtual std::string getName() {
  516. if (name == "")
  517. return std::string("#") + std::to_string(portId + 1);
  518. return name;
  519. }
  520. std::string getFullName() {
  521. std::string name = getName();
  522. name += " ";
  523. name += (type == Port::INPUT) ? "input" : "output";
  524. return name;
  525. }
  526. virtual std::string getDescription() { return description; }
  527. };
  528. struct ParamQuantity : Quantity {
  529. Module* module = NULL;
  530. int paramId = -1;
  531. float minValue = 0.f;
  532. float maxValue = 1.f;
  533. float defaultValue = 0.f;
  534. std::string name;
  535. std::string unit;
  536. float displayBase = 0.f;
  537. float displayMultiplier = 1.f;
  538. float displayOffset = 0.f;
  539. int displayPrecision = 5;
  540. std::string description;
  541. bool resetEnabled = true;
  542. bool randomizeEnabled = true;
  543. bool smoothEnabled = false;
  544. bool snapEnabled = false;
  545. // Param* getParam();
  546. // /** If smoothEnabled is true, requests to the engine to smoothly move to a target value each sample. */
  547. // void setSmoothValue(float value);
  548. // float getSmoothValue();
  549. // void setValue(float value) override;
  550. // float getValue() override;
  551. float getMinValue() override { return minValue; }
  552. float getMaxValue() override { return maxValue; }
  553. float getDefaultValue() override { return defaultValue; }
  554. // float getDisplayValue() override;
  555. // void setDisplayValue(float displayValue) override;
  556. // std::string getDisplayValueString() override;
  557. // void setDisplayValueString(std::string s) override;
  558. // int getDisplayPrecision() override;
  559. // std::string getLabel() override;
  560. // std::string getUnit() override;
  561. // void reset() override;
  562. // void randomize() override;
  563. virtual std::string getDescription() { return description; }
  564. // virtual json_t* toJson();
  565. // virtual void fromJson(json_t* rootJ);
  566. };
  567. struct SwitchQuantity : ParamQuantity {
  568. // std::vector<std::string> labels;
  569. // std::string getDisplayValueString() override;
  570. // void setDisplayValueString(std::string s) override;
  571. };
  572. struct Module {
  573. std::vector<Param> params;
  574. std::vector<Input> inputs;
  575. std::vector<Output> outputs;
  576. std::vector<Light> lights;
  577. std::vector<ParamQuantity*> paramQuantities;
  578. std::vector<PortInfo*> inputInfos;
  579. std::vector<PortInfo*> outputInfos;
  580. std::vector<LightInfo*> lightInfos;
  581. virtual ~Module() {
  582. for (ParamQuantity* paramQuantity : paramQuantities) {
  583. if (paramQuantity)
  584. delete paramQuantity;
  585. }
  586. for (PortInfo* inputInfo : inputInfos) {
  587. if (inputInfo)
  588. delete inputInfo;
  589. }
  590. for (PortInfo* outputInfo : outputInfos) {
  591. if (outputInfo)
  592. delete outputInfo;
  593. }
  594. for (LightInfo* lightInfo : lightInfos) {
  595. if (lightInfo)
  596. delete lightInfo;
  597. }
  598. }
  599. void config(int numParams, int numInputs, int numOutputs, int numLights = 0) {
  600. params.resize(numParams);
  601. inputs.resize(numInputs);
  602. outputs.resize(numOutputs);
  603. lights.resize(numLights);
  604. paramQuantities.resize(numParams);
  605. for (int i = 0; i < numParams; i++) {
  606. configParam(i, 0.f, 1.f, 0.f);
  607. }
  608. inputInfos.resize(numInputs);
  609. for (int i = 0; i < numInputs; i++) {
  610. configInput(i);
  611. }
  612. outputInfos.resize(numOutputs);
  613. for (int i = 0; i < numOutputs; i++) {
  614. configOutput(i);
  615. }
  616. lightInfos.resize(numLights);
  617. }
  618. template <class TParamQuantity = ParamQuantity>
  619. 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) {
  620. if (paramQuantities[paramId])
  621. delete paramQuantities[paramId];
  622. TParamQuantity* q = new TParamQuantity;
  623. q->ParamQuantity::module = this;
  624. q->ParamQuantity::paramId = paramId;
  625. q->ParamQuantity::minValue = minValue;
  626. q->ParamQuantity::maxValue = maxValue;
  627. q->ParamQuantity::defaultValue = defaultValue;
  628. q->ParamQuantity::name = name;
  629. q->ParamQuantity::unit = unit;
  630. q->ParamQuantity::displayBase = displayBase;
  631. q->ParamQuantity::displayMultiplier = displayMultiplier;
  632. q->ParamQuantity::displayOffset = displayOffset;
  633. paramQuantities[paramId] = q;
  634. Param* p = &params[paramId];
  635. p->value = q->getDefaultValue();
  636. return q;
  637. }
  638. template <class TSwitchQuantity = SwitchQuantity>
  639. TSwitchQuantity* configSwitch(int paramId, float minValue, float maxValue, float defaultValue, std::string name = "", std::vector<std::string> labels = {}) {
  640. TSwitchQuantity* sq = configParam<TSwitchQuantity>(paramId, minValue, maxValue, defaultValue, name);
  641. sq->labels = labels;
  642. return sq;
  643. }
  644. template <class TSwitchQuantity = SwitchQuantity>
  645. TSwitchQuantity* configButton(int paramId, std::string name = "") {
  646. TSwitchQuantity* sq = configParam<TSwitchQuantity>(paramId, 0.f, 1.f, 0.f, name);
  647. sq->randomizeEnabled = false;
  648. return sq;
  649. }
  650. template <class TPortInfo = PortInfo>
  651. TPortInfo* configInput(int portId, std::string name = "") {
  652. if (inputInfos[portId])
  653. delete inputInfos[portId];
  654. TPortInfo* info = new TPortInfo;
  655. info->PortInfo::module = this;
  656. info->PortInfo::type = Port::INPUT;
  657. info->PortInfo::portId = portId;
  658. info->PortInfo::name = name;
  659. inputInfos[portId] = info;
  660. return info;
  661. }
  662. template <class TPortInfo = PortInfo>
  663. TPortInfo* configOutput(int portId, std::string name = "") {
  664. if (outputInfos[portId])
  665. delete outputInfos[portId];
  666. TPortInfo* info = new TPortInfo;
  667. info->PortInfo::module = this;
  668. info->PortInfo::type = Port::OUTPUT;
  669. info->PortInfo::portId = portId;
  670. info->PortInfo::name = name;
  671. outputInfos[portId] = info;
  672. return info;
  673. }
  674. template <class TLightInfo = LightInfo>
  675. TLightInfo* configLight(int lightId, std::string name = "") {
  676. if (lightInfos[lightId])
  677. delete lightInfos[lightId];
  678. TLightInfo* info = new TLightInfo;
  679. info->LightInfo::module = this;
  680. info->LightInfo::lightId = lightId;
  681. info->LightInfo::name = name;
  682. lightInfos[lightId] = info;
  683. return info;
  684. }
  685. void configBypass(int inputId, int outputId) {
  686. // // Check that output is not yet routed
  687. // for (BypassRoute& br : bypassRoutes) {
  688. // assert(br.outputId != outputId);
  689. // }
  690. // BypassRoute br;
  691. // br.inputId = inputId;
  692. // br.outputId = outputId;
  693. // bypassRoutes.push_back(br);
  694. }
  695. int getNumParams() { return params.size(); }
  696. Param& getParam(int index) { return params[index]; }
  697. int getNumInputs() { return inputs.size(); }
  698. Input& getInput(int index) { return inputs[index]; }
  699. int getNumOutputs() { return outputs.size(); }
  700. Output& getOutput(int index) { return outputs[index]; }
  701. int getNumLights() { return lights.size(); }
  702. Light& getLight(int index) { return lights[index]; }
  703. ParamQuantity* getParamQuantity(int index) { return paramQuantities[index]; }
  704. PortInfo* getInputInfo(int index) { return inputInfos[index]; }
  705. PortInfo* getOutputInfo(int index) { return outputInfos[index]; }
  706. LightInfo* getLightInfo(int index) { return lightInfos[index]; }
  707. struct ProcessArgs {
  708. float sampleRate;
  709. float sampleTime;
  710. int64_t frame;
  711. };
  712. virtual void process(const ProcessArgs& args) {
  713. step();
  714. }
  715. virtual void step() {}
  716. // virtual void processBypass(const ProcessArgs& args);
  717. // virtual json_t* toJson();
  718. // virtual void fromJson(json_t* rootJ);
  719. // virtual json_t* paramsToJson();
  720. // virtual void paramsFromJson(json_t* rootJ);
  721. virtual json_t* dataToJson() { return NULL; }
  722. virtual void dataFromJson(json_t* rootJ) {}
  723. struct SampleRateChangeEvent {
  724. float sampleRate;
  725. float sampleTime;
  726. };
  727. virtual void onSampleRateChange(const SampleRateChangeEvent&) {
  728. onSampleRateChange();
  729. }
  730. struct ResetEvent {};
  731. virtual void onReset(const ResetEvent&) {} // TODO
  732. virtual void onAdd() {}
  733. virtual void onRemove() {}
  734. virtual void onReset() {}
  735. virtual void onRandomize() {}
  736. virtual void onSampleRateChange() {}
  737. // private
  738. void doProcess(const ProcessArgs& args) {
  739. // if (!internal->bypassed)
  740. process(args);
  741. // else
  742. // processBypass(args);
  743. // if (args.frame % PORT_DIVIDER == 0) {
  744. // float portTime = args.sampleTime * PORT_DIVIDER;
  745. // for (Input& input : inputs) {
  746. // Port_step(&input, portTime);
  747. // }
  748. // for (Output& output : outputs) {
  749. // Port_step(&output, portTime);
  750. // }
  751. // }
  752. }
  753. };
  754. } // namespace engine
  755. namespace widget {
  756. struct BaseEvent {
  757. };
  758. struct Widget {
  759. math::Rect box;
  760. Widget* parent = NULL;
  761. std::list<Widget*> children;
  762. bool visible = false;
  763. bool requestedDelete = false;
  764. virtual ~Widget() {}
  765. math::Rect getBox() { return {}; }
  766. void setBox(math::Rect box) {}
  767. math::Vec getPosition() { return {}; }
  768. void setPosition(math::Vec pos) {}
  769. math::Vec getSize() { return {}; }
  770. void setSize(math::Vec size) {}
  771. widget::Widget* getParent() { return NULL; }
  772. bool isVisible() { return false; }
  773. void setVisible(bool visible) {}
  774. void show() {}
  775. void hide() {}
  776. void requestDelete() {}
  777. virtual math::Rect getChildrenBoundingBox() { return {}; }
  778. virtual math::Rect getVisibleChildrenBoundingBox() { return {}; }
  779. bool isDescendantOf(Widget* ancestor) { return false; }
  780. virtual math::Vec getRelativeOffset(math::Vec v, Widget* ancestor) { return {}; }
  781. math::Vec getAbsoluteOffset(math::Vec v) { return {}; }
  782. virtual float getRelativeZoom(Widget* ancestor) { return 0.f; }
  783. float getAbsoluteZoom() { return 0.f; }
  784. virtual math::Rect getViewport(math::Rect r) { return {}; }
  785. template <class T> T* getAncestorOfType() { return NULL; }
  786. template <class T> T* getFirstDescendantOfType() { return NULL; }
  787. bool hasChild(Widget* child) { return false; }
  788. void addChild(Widget* child) {}
  789. void addChildBottom(Widget* child) {}
  790. void addChildBelow(Widget* child, Widget* sibling) {}
  791. void addChildAbove(Widget* child, Widget* sibling) {}
  792. void removeChild(Widget* child) {}
  793. void clearChildren() {}
  794. virtual void step() {}
  795. struct DrawArgs {
  796. NVGcontext* vg = NULL;
  797. math::Rect clipBox;
  798. void* fb = NULL;
  799. };
  800. virtual void draw(const DrawArgs&) {}
  801. // DEPRECATED virtual void draw(NVGcontext* vg) {}
  802. virtual void drawLayer(const DrawArgs& args, int layer) {}
  803. void drawChild(Widget* child, const DrawArgs& args, int layer = 0) {}
  804. using BaseEvent = widget::BaseEvent;
  805. struct PositionBaseEvent {
  806. math::Vec pos;
  807. };
  808. struct HoverEvent : BaseEvent, PositionBaseEvent {
  809. math::Vec mouseDelta;
  810. };
  811. virtual void onHover(const HoverEvent&) {}
  812. struct ButtonEvent : BaseEvent, PositionBaseEvent {
  813. int button;
  814. int action;
  815. int mods;
  816. };
  817. virtual void onButton(const ButtonEvent&) {}
  818. struct DoubleClickEvent : BaseEvent {};
  819. virtual void onDoubleClick(const DoubleClickEvent&) {}
  820. struct KeyBaseEvent {
  821. int key;
  822. int scancode;
  823. std::string keyName;
  824. int action;
  825. int mods;
  826. };
  827. struct HoverKeyEvent : BaseEvent, PositionBaseEvent, KeyBaseEvent {};
  828. virtual void onHoverKey(const HoverKeyEvent&) {}
  829. struct TextBaseEvent {
  830. int codepoint;
  831. };
  832. struct HoverTextEvent : BaseEvent, PositionBaseEvent, TextBaseEvent {};
  833. virtual void onHoverText(const HoverTextEvent&) {}
  834. struct HoverScrollEvent : BaseEvent, PositionBaseEvent {
  835. math::Vec scrollDelta;
  836. };
  837. virtual void onHoverScroll(const HoverScrollEvent&) {}
  838. struct EnterEvent : BaseEvent {};
  839. virtual void onEnter(const EnterEvent&) {}
  840. struct LeaveEvent : BaseEvent {};
  841. virtual void onLeave(const LeaveEvent&) {}
  842. struct SelectEvent : BaseEvent {};
  843. virtual void onSelect(const SelectEvent&) {}
  844. struct DeselectEvent : BaseEvent {};
  845. virtual void onDeselect(const DeselectEvent&) {}
  846. struct SelectKeyEvent : BaseEvent, KeyBaseEvent {};
  847. virtual void onSelectKey(const SelectKeyEvent&) {}
  848. struct SelectTextEvent : BaseEvent, TextBaseEvent {};
  849. virtual void onSelectText(const SelectTextEvent&) {}
  850. struct DragBaseEvent : BaseEvent {
  851. int button;
  852. };
  853. struct DragStartEvent : DragBaseEvent {};
  854. virtual void onDragStart(const DragStartEvent&) {}
  855. struct DragEndEvent : DragBaseEvent {};
  856. virtual void onDragEnd(const DragEndEvent&) {}
  857. struct DragMoveEvent : DragBaseEvent {
  858. math::Vec mouseDelta;
  859. };
  860. virtual void onDragMove(const DragMoveEvent&) {}
  861. struct DragHoverEvent : DragBaseEvent, PositionBaseEvent {
  862. Widget* origin = NULL;
  863. math::Vec mouseDelta;
  864. };
  865. virtual void onDragHover(const DragHoverEvent&) {
  866. }
  867. struct DragEnterEvent : DragBaseEvent {
  868. Widget* origin = NULL;
  869. };
  870. virtual void onDragEnter(const DragEnterEvent&) {}
  871. struct DragLeaveEvent : DragBaseEvent {
  872. Widget* origin = NULL;
  873. };
  874. virtual void onDragLeave(const DragLeaveEvent&) {}
  875. struct DragDropEvent : DragBaseEvent {
  876. Widget* origin = NULL;
  877. };
  878. virtual void onDragDrop(const DragDropEvent&) {}
  879. struct PathDropEvent : BaseEvent, PositionBaseEvent {
  880. PathDropEvent(const std::vector<std::string>& paths) : paths(paths) {}
  881. const std::vector<std::string>& paths;
  882. };
  883. virtual void onPathDrop(const PathDropEvent&) {}
  884. struct ActionEvent : BaseEvent {};
  885. virtual void onAction(const ActionEvent&) {}
  886. struct ChangeEvent : BaseEvent {};
  887. virtual void onChange(const ChangeEvent&) {}
  888. struct DirtyEvent : BaseEvent {};
  889. virtual void onDirty(const DirtyEvent&) {}
  890. struct RepositionEvent : BaseEvent {};
  891. virtual void onReposition(const RepositionEvent&) {}
  892. struct ResizeEvent : BaseEvent {};
  893. virtual void onResize(const ResizeEvent&) {}
  894. struct AddEvent : BaseEvent {};
  895. virtual void onAdd(const AddEvent&) {}
  896. struct RemoveEvent : BaseEvent {};
  897. virtual void onRemove(const RemoveEvent&) {}
  898. struct ShowEvent : BaseEvent {};
  899. virtual void onShow(const ShowEvent&) {}
  900. struct HideEvent : BaseEvent {};
  901. virtual void onHide(const HideEvent&) {}
  902. struct ContextCreateEvent : BaseEvent {
  903. NVGcontext* vg;
  904. };
  905. virtual void onContextCreate(const ContextCreateEvent&) {}
  906. struct ContextDestroyEvent : BaseEvent {
  907. NVGcontext* vg;
  908. };
  909. virtual void onContextDestroy(const ContextDestroyEvent&) {}
  910. };
  911. struct OpaqueWidget : Widget {
  912. };
  913. struct SvgWidget : Widget {
  914. std::shared_ptr<window::Svg> svg;
  915. void wrap() {}
  916. void setSvg(std::shared_ptr<window::Svg>) {}
  917. };
  918. struct TransparentWidget : Widget {
  919. };
  920. } // namespace widget
  921. namespace app {
  922. static constexpr const float RACK_GRID_WIDTH = 15;
  923. static constexpr const float RACK_GRID_HEIGHT = 380;
  924. static const math::Vec RACK_GRID_SIZE = math::Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT);
  925. static const math::Vec RACK_OFFSET = RACK_GRID_SIZE.mult(math::Vec(2000, 100));
  926. struct ParamWidget;
  927. struct PortWidget;
  928. struct CircularShadow : widget::TransparentWidget {
  929. float blurRadius;
  930. float opacity;
  931. };
  932. struct LightWidget : widget::TransparentWidget {
  933. NVGcolor bgColor, color, borderColor;
  934. };
  935. struct ModuleWidget : widget::OpaqueWidget {
  936. // plugin::Model* model = NULL;
  937. engine::Module* module = NULL;
  938. // plugin::Model* getModel() { return NULL; }
  939. // void setModel(plugin::Model*) {}
  940. engine::Module* getModule() { return NULL; }
  941. void setModule(engine::Module*) {}
  942. widget::Widget* getPanel() { return NULL; }
  943. void setPanel(widget::Widget*) {}
  944. void setPanel(std::shared_ptr<window::Svg>) {}
  945. void addParam(ParamWidget*) {}
  946. void addInput(PortWidget*) {}
  947. void addOutput(PortWidget*) {}
  948. ParamWidget* getParam(int paramId) { return NULL; }
  949. PortWidget* getInput(int portId) { return NULL; }
  950. PortWidget* getOutput(int portId) { return NULL; }
  951. std::vector<ParamWidget*> getParams() { return {}; }
  952. std::vector<PortWidget*> getPorts() { return {}; }
  953. std::vector<PortWidget*> getInputs() { return {}; }
  954. std::vector<PortWidget*> getOutputs() { return {}; }
  955. virtual void appendContextMenu(ui::Menu*) {}
  956. json_t* toJson() { return NULL; }
  957. void fromJson(json_t*) {}
  958. bool pasteJsonAction(json_t*) { return false; }
  959. void copyClipboard() {}
  960. bool pasteClipboardAction() { return false; }
  961. void load(std::string) {}
  962. void loadAction(std::string) {}
  963. void loadTemplate() {}
  964. void loadDialog() {}
  965. void save(std::string) {}
  966. void saveTemplate() {}
  967. void saveTemplateDialog() {}
  968. bool hasTemplate() { return false; }
  969. void clearTemplate() {}
  970. void clearTemplateDialog() {}
  971. void saveDialog() {}
  972. void disconnect() {}
  973. void resetAction() {}
  974. void randomizeAction() {}
  975. void appendDisconnectActions(/*history::ComplexAction*/void*) {}
  976. void disconnectAction() {}
  977. void cloneAction(bool cc = true) {}
  978. void bypassAction(bool) {}
  979. void removeAction() {}
  980. void createContextMenu() {}
  981. };
  982. struct MultiLightWidget : LightWidget {
  983. // std::vector<NVGcolor> baseColors;
  984. // int getNumColors();
  985. void addBaseColor(NVGcolor baseColor) {}
  986. // void setBrightnesses(const std::vector<float>& brightnesses);
  987. };
  988. struct ModuleLightWidget : MultiLightWidget {
  989. // engine::Module* module = NULL;
  990. // int firstLightId = -1;
  991. // ModuleLightWidget();
  992. // ~ModuleLightWidget();
  993. // engine::Light* getLight(int colorId);
  994. // engine::LightInfo* getLightInfo();
  995. // void createTooltip();
  996. // void destroyTooltip();
  997. };
  998. struct ParamWidget : widget::OpaqueWidget {
  999. engine::Module* module = NULL;
  1000. int paramId = -1;
  1001. virtual void initParamQuantity() {}
  1002. engine::ParamQuantity* getParamQuantity() { return module ? module->paramQuantities[paramId] : NULL; }
  1003. void createTooltip() {}
  1004. void destroyTooltip() {}
  1005. void createContextMenu();
  1006. virtual void appendContextMenu(void* menu) {}
  1007. void resetAction();
  1008. };
  1009. struct PortWidget : widget::OpaqueWidget {
  1010. };
  1011. struct Knob : ParamWidget {
  1012. bool horizontal = false;
  1013. bool smooth = true;
  1014. bool snap = false;
  1015. float speed = 1.f;
  1016. bool forceLinear = false;
  1017. float minAngle = -M_PI;
  1018. float maxAngle = M_PI;
  1019. };
  1020. struct SliderKnob : Knob {
  1021. };
  1022. struct Switch : ParamWidget {
  1023. bool momentary = false;
  1024. };
  1025. struct SvgKnob : Knob {
  1026. // widget::FramebufferWidget* fb;
  1027. CircularShadow* shadow;
  1028. // widget::TransformWidget* tw;
  1029. widget::SvgWidget* sw;
  1030. void setSvg(std::shared_ptr<window::Svg>) {}
  1031. };
  1032. struct SvgPanel : widget::Widget {
  1033. // widget::FramebufferWidget* fb;
  1034. // widget::SvgWidget* sw;
  1035. // PanelBorder* panelBorder;
  1036. void setBackground(std::shared_ptr<window::Svg>) {}
  1037. };
  1038. struct SvgPort : PortWidget {
  1039. // widget::FramebufferWidget* fb;
  1040. CircularShadow* shadow;
  1041. // widget::SvgWidget* sw;
  1042. void setSvg(std::shared_ptr<window::Svg>) {}
  1043. };
  1044. struct SvgScrew : widget::Widget {
  1045. // widget::FramebufferWidget* fb;
  1046. widget::SvgWidget* sw;
  1047. void setSvg(std::shared_ptr<window::Svg>) {}
  1048. };
  1049. struct SvgSlider : app::SliderKnob {
  1050. // widget::FramebufferWidget* fb;
  1051. widget::SvgWidget* background;
  1052. widget::SvgWidget* handle;
  1053. math::Vec minHandlePos, maxHandlePos;
  1054. void setBackgroundSvg(std::shared_ptr<window::Svg>) {}
  1055. void setHandleSvg(std::shared_ptr<window::Svg>) {}
  1056. void setHandlePos(math::Vec minHandlePos, math::Vec maxHandlePos) {}
  1057. void setHandlePosCentered(math::Vec minHandlePosCentered, math::Vec maxHandlePosCentered) {}
  1058. };
  1059. struct SvgSwitch : Switch {
  1060. // widget::FramebufferWidget* fb;
  1061. // CircularShadow* shadow;
  1062. // widget::SvgWidget* sw;
  1063. // std::vector<std::shared_ptr<window::Svg>> frames;
  1064. bool latch = false;
  1065. void addFrame(std::shared_ptr<window::Svg>) {}
  1066. };
  1067. struct Scene : widget::OpaqueWidget {
  1068. // RackScrollWidget* rackScroll;
  1069. // RackWidget* rack;
  1070. widget::Widget* menuBar;
  1071. widget::Widget* browser;
  1072. math::Vec getMousePos() { return {}; }
  1073. };
  1074. } // namespace app
  1075. namespace asset {
  1076. inline std::string plugin(void* instance, const char* path) { return {}; }
  1077. inline std::string system(const char* path) { return {}; }
  1078. } // namespace asset
  1079. namespace componentlibrary {
  1080. static constexpr const NVGcolor SCHEME_BLACK_TRANSPARENT = {};
  1081. static constexpr const NVGcolor SCHEME_BLACK = {};
  1082. static constexpr const NVGcolor SCHEME_WHITE = {};
  1083. static constexpr const NVGcolor SCHEME_RED = {};
  1084. static constexpr const NVGcolor SCHEME_ORANGE = {};
  1085. static constexpr const NVGcolor SCHEME_YELLOW = {};
  1086. static constexpr const NVGcolor SCHEME_GREEN = {};
  1087. static constexpr const NVGcolor SCHEME_CYAN = {};
  1088. static constexpr const NVGcolor SCHEME_BLUE = {};
  1089. static constexpr const NVGcolor SCHEME_PURPLE = {};
  1090. static constexpr const NVGcolor SCHEME_LIGHT_GRAY = {};
  1091. static constexpr const NVGcolor SCHEME_DARK_GRAY = {};
  1092. template <typename TBase = app::ModuleLightWidget>
  1093. struct TSvgLight : TBase {
  1094. // widget::FramebufferWidget* fb;
  1095. // widget::SvgWidget* sw;
  1096. void setSvg(std::shared_ptr<window::Svg>) {}
  1097. };
  1098. using SvgLight = TSvgLight<>;
  1099. template <typename TBase = app::ModuleLightWidget>
  1100. struct TGrayModuleLightWidget : TBase {};
  1101. using GrayModuleLightWidget = TGrayModuleLightWidget<>;
  1102. template <typename TBase = GrayModuleLightWidget>
  1103. struct TWhiteLight : TBase {};
  1104. using WhiteLight = TWhiteLight<>;
  1105. template <typename TBase = GrayModuleLightWidget>
  1106. struct TRedLight : TBase {};
  1107. using RedLight = TRedLight<>;
  1108. template <typename TBase = GrayModuleLightWidget>
  1109. struct TGreenLight : TBase {};
  1110. using GreenLight = TGreenLight<>;
  1111. template <typename TBase = GrayModuleLightWidget>
  1112. struct TBlueLight : TBase {};
  1113. using BlueLight = TBlueLight<>;
  1114. template <typename TBase = GrayModuleLightWidget>
  1115. struct TYellowLight : TBase {};
  1116. using YellowLight = TYellowLight<>;
  1117. template <typename TBase = GrayModuleLightWidget>
  1118. struct TGreenRedLight : TBase {};
  1119. using GreenRedLight = TGreenRedLight<>;
  1120. template <typename TBase = GrayModuleLightWidget>
  1121. struct TRedGreenBlueLight : TBase {};
  1122. using RedGreenBlueLight = TRedGreenBlueLight<>;
  1123. template <typename TBase>
  1124. struct LargeLight : TSvgLight<TBase> {};
  1125. template <typename TBase>
  1126. struct MediumLight : TSvgLight<TBase> {};
  1127. template <typename TBase>
  1128. struct SmallLight : TSvgLight<TBase> {};
  1129. template <typename TBase>
  1130. struct TinyLight : TSvgLight<TBase> {};
  1131. template <typename TBase = GrayModuleLightWidget>
  1132. struct LargeSimpleLight : TBase {};
  1133. template <typename TBase = GrayModuleLightWidget>
  1134. struct MediumSimpleLight : TBase {};
  1135. template <typename TBase = GrayModuleLightWidget>
  1136. struct SmallSimpleLight : TBase {};
  1137. template <typename TBase = GrayModuleLightWidget>
  1138. struct TinySimpleLight : TBase {};
  1139. template <typename TBase>
  1140. struct RectangleLight : TBase {};
  1141. template <typename TBase>
  1142. struct VCVBezelLight : TBase {};
  1143. template <typename TBase>
  1144. using LEDBezelLight = VCVBezelLight<TBase>;
  1145. template <typename TBase>
  1146. struct PB61303Light : TBase {};
  1147. struct RoundKnob : app::SvgKnob {
  1148. widget::SvgWidget* bg;
  1149. };
  1150. struct RoundBlackKnob : RoundKnob {};
  1151. struct RoundSmallBlackKnob : RoundKnob {};
  1152. struct RoundLargeBlackKnob : RoundKnob {};
  1153. struct RoundBigBlackKnob : RoundKnob {};
  1154. struct RoundHugeBlackKnob : RoundKnob {};
  1155. struct RoundBlackSnapKnob : RoundBlackKnob {};
  1156. struct Davies1900hKnob : app::SvgKnob {};
  1157. struct Davies1900hWhiteKnob : Davies1900hKnob {};
  1158. struct Davies1900hBlackKnob : Davies1900hKnob {};
  1159. struct Davies1900hRedKnob : Davies1900hKnob {};
  1160. struct Davies1900hLargeWhiteKnob : Davies1900hKnob {};
  1161. struct Davies1900hLargeBlackKnob : Davies1900hKnob {};
  1162. struct Davies1900hLargeRedKnob : Davies1900hKnob {};
  1163. struct Rogan : app::SvgKnob {
  1164. widget::SvgWidget* bg;
  1165. widget::SvgWidget* fg;
  1166. };
  1167. struct Rogan6PSWhite : Rogan {};
  1168. struct Rogan5PSGray : Rogan {};
  1169. struct Rogan3PSBlue : Rogan {};
  1170. struct Rogan3PSRed : Rogan {};
  1171. struct Rogan3PSGreen : Rogan {};
  1172. struct Rogan3PSWhite : Rogan {};
  1173. struct Rogan3PBlue : Rogan {};
  1174. struct Rogan3PRed : Rogan {};
  1175. struct Rogan3PGreen : Rogan {};
  1176. struct Rogan3PWhite : Rogan {};
  1177. struct Rogan2SGray : Rogan {};
  1178. struct Rogan2PSBlue : Rogan {};
  1179. struct Rogan2PSRed : Rogan {};
  1180. struct Rogan2PSGreen : Rogan {};
  1181. struct Rogan2PSWhite : Rogan {};
  1182. struct Rogan2PBlue : Rogan {};
  1183. struct Rogan2PRed : Rogan {};
  1184. struct Rogan2PGreen : Rogan {};
  1185. struct Rogan2PWhite : Rogan {};
  1186. struct Rogan1PSBlue : Rogan {};
  1187. struct Rogan1PSRed : Rogan {};
  1188. struct Rogan1PSGreen : Rogan {};
  1189. struct Rogan1PSWhite : Rogan {};
  1190. struct Rogan1PBlue : Rogan {};
  1191. struct Rogan1PRed : Rogan {};
  1192. struct Rogan1PGreen : Rogan {};
  1193. struct Rogan1PWhite : Rogan {};
  1194. struct SynthTechAlco : app::SvgKnob {
  1195. widget::SvgWidget* bg;
  1196. };
  1197. struct Trimpot : app::SvgKnob {
  1198. widget::SvgWidget* bg;
  1199. };
  1200. struct BefacoBigKnob : app::SvgKnob {
  1201. widget::SvgWidget* bg;
  1202. };
  1203. struct BefacoTinyKnob : app::SvgKnob {
  1204. widget::SvgWidget* bg;
  1205. };
  1206. struct BefacoSlidePot : app::SvgSlider {};
  1207. struct VCVSlider : app::SvgSlider {};
  1208. using LEDSlider = VCVSlider;
  1209. struct VCVSliderHorizontal : app::SvgSlider {};
  1210. using LEDSliderHorizontal = VCVSliderHorizontal;
  1211. template <typename TBase, typename TLightBase = RedLight>
  1212. struct LightSlider : TBase {
  1213. app::ModuleLightWidget* light = NULL;
  1214. app::ModuleLightWidget* getLight() { return light; }
  1215. };
  1216. template <typename TBase>
  1217. struct VCVSliderLight : RectangleLight<TSvgLight<TBase>> {};
  1218. template <typename TBase>
  1219. using LEDSliderLight = VCVSliderLight<TBase>;
  1220. template <typename TLightBase = RedLight>
  1221. struct VCVLightSlider : LightSlider<VCVSlider, VCVSliderLight<TLightBase>> {};
  1222. template <typename TLightBase = RedLight>
  1223. using LEDLightSlider = VCVLightSlider<TLightBase>;
  1224. struct LEDSliderGreen : VCVLightSlider<GreenLight> {};
  1225. struct LEDSliderRed : VCVLightSlider<RedLight> {};
  1226. struct LEDSliderYellow : VCVLightSlider<YellowLight> {};
  1227. struct LEDSliderBlue : VCVLightSlider<BlueLight> {};
  1228. struct LEDSliderWhite : VCVLightSlider<WhiteLight> {};
  1229. template <typename TLightBase = RedLight>
  1230. struct VCVLightSliderHorizontal : LightSlider<VCVSliderHorizontal, TLightBase> {};
  1231. template <typename TLightBase = RedLight>
  1232. using LEDLightSliderHorizontal = VCVLightSliderHorizontal<TLightBase>;
  1233. struct PJ301MPort : app::SvgPort {};
  1234. struct PJ3410Port : app::SvgPort {};
  1235. struct CL1362Port : app::SvgPort {};
  1236. template <typename TSwitch>
  1237. struct MomentarySwitch : TSwitch {};
  1238. struct NKK : app::SvgSwitch {};
  1239. struct CKSS : app::SvgSwitch {};
  1240. struct CKSSThree : app::SvgSwitch {};
  1241. struct CKSSThreeHorizontal : app::SvgSwitch {};
  1242. struct CKD6 : app::SvgSwitch {};
  1243. struct TL1105 : app::SvgSwitch {};
  1244. struct VCVButton : app::SvgSwitch {};
  1245. using LEDButton = VCVButton;
  1246. struct VCVLatch : VCVButton {};
  1247. template <typename TLight>
  1248. struct VCVLightButton : VCVButton {
  1249. app::ModuleLightWidget* light = NULL;
  1250. app::ModuleLightWidget* getLight() { return light; }
  1251. };
  1252. template <typename TLight>
  1253. using LEDLightButton = VCVLightButton<TLight>;
  1254. template <typename TLight>
  1255. struct VCVLightLatch : VCVLightButton<TLight> {};
  1256. struct BefacoSwitch : app::SvgSwitch {};
  1257. struct BefacoPush : app::SvgSwitch {};
  1258. struct VCVBezel : app::SvgSwitch {};
  1259. using LEDBezel = VCVBezel;
  1260. struct VCVBezelLatch : VCVBezel {};
  1261. template <typename TLightBase = WhiteLight>
  1262. struct VCVLightBezel : VCVBezel {
  1263. app::ModuleLightWidget* light = NULL;
  1264. app::ModuleLightWidget* getLight() { return light; }
  1265. };
  1266. template <typename TLightBase = WhiteLight>
  1267. using LEDLightBezel = VCVLightBezel<TLightBase>;
  1268. template <typename TLightBase = WhiteLight>
  1269. struct VCVLightBezelLatch : VCVLightBezel<TLightBase> {};
  1270. struct PB61303 : app::SvgSwitch {};
  1271. struct ScrewSilver : app::SvgScrew {};
  1272. struct ScrewBlack : app::SvgScrew {};
  1273. } // namespace componentlibrary
  1274. namespace dsp {
  1275. static constexpr const float FREQ_C4 = 261.6256f;
  1276. static constexpr const float FREQ_A4 = 440.0000f;
  1277. static constexpr const float FREQ_SEMITONE = 1.0594630943592953f;
  1278. inline float sinc(float x) {
  1279. if (x == 0.f)
  1280. return 1.f;
  1281. x *= M_PI;
  1282. return std::sin(x) / x;
  1283. }
  1284. // template <typename T>
  1285. // T sinc(T x) {
  1286. // T zeromask = (x == 0.f);
  1287. // x *= M_PI;
  1288. // x = simd::sin(x) / x;
  1289. // return simd::ifelse(zeromask, 1.f, x);
  1290. // }
  1291. // template <typename T>
  1292. // T amplitudeToDb(T amp) {
  1293. // return simd::log10(amp) * 20;
  1294. // }
  1295. template <typename T>
  1296. T dbToAmplitude(T db) { return std::pow(10, db / 20); }
  1297. // template <typename T>
  1298. // T quadraticBipolar(T x) {
  1299. // return simd::sgn(x) * (x * x);
  1300. // }
  1301. template <typename T>
  1302. T cubic(T x) { return x * x * x; }
  1303. // template <typename T>
  1304. // T quarticBipolar(T x) {
  1305. // return simd::sgn(x) * (x * x * x * x);
  1306. // }
  1307. template <typename T>
  1308. T quintic(T x) { return x * x * x * x * x; }
  1309. // template <typename T>
  1310. // T sqrtBipolar(T x) {
  1311. // return simd::sgn(x) * simd::sqrt(x);
  1312. // }
  1313. // template <typename T>
  1314. // T exponentialBipolar(T b, T x) {
  1315. // return (simd::pow(b, x) - simd::pow(b, -x)) / (b - 1.f / b);
  1316. // }
  1317. template <size_t CHANNELS, typename T = float>
  1318. struct Frame { T samples[CHANNELS]; };
  1319. template <typename T>
  1320. inline T blackmanHarris(T p) {
  1321. return
  1322. + T(0.35875)
  1323. - T(0.48829) * std::cos(2 * T(M_PI) * p)
  1324. + T(0.14128) * std::cos(4 * T(M_PI) * p)
  1325. - T(0.01168) * std::cos(6 * T(M_PI) * p);
  1326. }
  1327. inline void blackmanHarrisWindow(float* x, int len) {
  1328. for (int i = 0; i < len; i++) {
  1329. x[i] *= blackmanHarris(float(i) / (len - 1));
  1330. }
  1331. }
  1332. inline void boxcarLowpassIR(float* out, int len, float cutoff = 0.5f) {
  1333. for (int i = 0; i < len; i++) {
  1334. float t = i - (len - 1) / 2.f;
  1335. out[i] = 2 * cutoff * sinc(2 * cutoff * t);
  1336. }
  1337. }
  1338. struct BooleanTrigger {
  1339. bool state = true;
  1340. void reset() {
  1341. state = true;
  1342. }
  1343. bool process(bool state) {
  1344. bool triggered = (state && !this->state);
  1345. this->state = state;
  1346. return triggered;
  1347. }
  1348. };
  1349. template <typename T = float>
  1350. struct TSchmittTrigger {
  1351. T state;
  1352. TSchmittTrigger() {
  1353. reset();
  1354. }
  1355. void reset() {
  1356. state = T::mask();
  1357. }
  1358. T process(T in, T offThreshold = 0.f, T onThreshold = 1.f) {
  1359. T on = (in >= onThreshold);
  1360. T off = (in <= offThreshold);
  1361. T triggered = ~state & on;
  1362. state = on | (state & ~off);
  1363. return triggered;
  1364. }
  1365. T isHigh() {
  1366. return state;
  1367. }
  1368. };
  1369. template <>
  1370. struct TSchmittTrigger<float> {
  1371. bool state = true;
  1372. void reset() {
  1373. state = true;
  1374. }
  1375. bool process(float in, float offThreshold = 0.f, float onThreshold = 1.f) {
  1376. if (state) {
  1377. if (in <= offThreshold) {
  1378. state = false;
  1379. }
  1380. }
  1381. else {
  1382. if (in >= onThreshold) {
  1383. state = true;
  1384. return true;
  1385. }
  1386. }
  1387. return false;
  1388. }
  1389. bool isHigh() {
  1390. return state;
  1391. }
  1392. };
  1393. typedef TSchmittTrigger<> SchmittTrigger;
  1394. struct PulseGenerator {
  1395. float remaining = 0.f;
  1396. void reset() { remaining = 0.f; }
  1397. bool process(float deltaTime) {
  1398. if (remaining > 0.f) {
  1399. remaining -= deltaTime;
  1400. return true;
  1401. }
  1402. return false;
  1403. }
  1404. void trigger(float duration = 1e-3f) {
  1405. if (duration > remaining) {
  1406. remaining = duration;
  1407. }
  1408. }
  1409. };
  1410. template <typename T = float>
  1411. struct TTimer {
  1412. T time = 0.f;
  1413. void reset() {
  1414. time = 0.f;
  1415. }
  1416. T process(T deltaTime) {
  1417. time += deltaTime;
  1418. return time;
  1419. }
  1420. T getTime() {
  1421. return time;
  1422. }
  1423. };
  1424. typedef TTimer<> Timer;
  1425. struct ClockDivider {
  1426. uint32_t clock = 0;
  1427. uint32_t division = 1;
  1428. void reset() { clock = 0; }
  1429. void setDivision(uint32_t division) { this->division = division; }
  1430. uint32_t getDivision() { return division; }
  1431. uint32_t getClock() { return clock; }
  1432. bool process() {
  1433. clock++;
  1434. if (clock >= division) {
  1435. clock = 0;
  1436. return true;
  1437. }
  1438. return false;
  1439. }
  1440. };
  1441. template <int OVERSAMPLE, int QUALITY, typename T = float>
  1442. struct Decimator {
  1443. T inBuffer[OVERSAMPLE * QUALITY];
  1444. float kernel[OVERSAMPLE * QUALITY];
  1445. int inIndex;
  1446. Decimator(float cutoff = 0.9f) {
  1447. boxcarLowpassIR(kernel, OVERSAMPLE * QUALITY, cutoff * 0.5f / OVERSAMPLE);
  1448. blackmanHarrisWindow(kernel, OVERSAMPLE * QUALITY);
  1449. reset();
  1450. }
  1451. void reset() {
  1452. inIndex = 0;
  1453. std::memset(inBuffer, 0, sizeof(inBuffer));
  1454. }
  1455. T process(T* in) {
  1456. std::memcpy(&inBuffer[inIndex], in, OVERSAMPLE * sizeof(T));
  1457. inIndex += OVERSAMPLE;
  1458. inIndex %= OVERSAMPLE * QUALITY;
  1459. T out = 0.f;
  1460. for (int i = 0; i < OVERSAMPLE * QUALITY; i++) {
  1461. int index = inIndex - 1 - i;
  1462. index = (index + OVERSAMPLE * QUALITY) % (OVERSAMPLE * QUALITY);
  1463. out += kernel[i] * inBuffer[index];
  1464. }
  1465. return out;
  1466. }
  1467. };
  1468. template <typename T, size_t S>
  1469. struct RingBuffer {
  1470. std::atomic<size_t> start{0};
  1471. std::atomic<size_t> end{0};
  1472. T data[S];
  1473. void push(T t) {
  1474. size_t i = end % S;
  1475. data[i] = t;
  1476. end++;
  1477. }
  1478. void pushBuffer(const T* t, int n) {
  1479. size_t i = end % S;
  1480. size_t e1 = i + n;
  1481. size_t e2 = (e1 < S) ? e1 : S;
  1482. std::memcpy(&data[i], t, sizeof(T) * (e2 - i));
  1483. if (e1 > S) {
  1484. std::memcpy(data, &t[S - i], sizeof(T) * (e1 - S));
  1485. }
  1486. end += n;
  1487. }
  1488. T shift() {
  1489. size_t i = start % S;
  1490. T t = data[i];
  1491. start++;
  1492. return t;
  1493. }
  1494. void shiftBuffer(T* t, size_t n) {
  1495. size_t i = start % S;
  1496. size_t s1 = i + n;
  1497. size_t s2 = (s1 < S) ? s1 : S;
  1498. std::memcpy(t, &data[i], sizeof(T) * (s2 - i));
  1499. if (s1 > S) {
  1500. std::memcpy(&t[S - i], data, sizeof(T) * (s1 - S));
  1501. }
  1502. start += n;
  1503. }
  1504. void clear() {
  1505. start = end.load();
  1506. }
  1507. bool empty() const {
  1508. return start >= end;
  1509. }
  1510. bool full() const {
  1511. return end - start >= S;
  1512. }
  1513. size_t size() const {
  1514. return end - start;
  1515. }
  1516. size_t capacity() const {
  1517. return S - size();
  1518. }
  1519. };
  1520. template <typename T, size_t S>
  1521. struct DoubleRingBuffer {
  1522. std::atomic<size_t> start{0};
  1523. std::atomic<size_t> end{0};
  1524. T data[2 * S];
  1525. void push(T t) {
  1526. size_t i = end % S;
  1527. data[i] = t;
  1528. data[i + S] = t;
  1529. end++;
  1530. }
  1531. T shift() {
  1532. size_t i = start % S;
  1533. T t = data[i];
  1534. start++;
  1535. return t;
  1536. }
  1537. void clear() {
  1538. start = end.load();
  1539. }
  1540. bool empty() const {
  1541. return start >= end;
  1542. }
  1543. bool full() const {
  1544. return end - start >= S;
  1545. }
  1546. size_t size() const {
  1547. return end - start;
  1548. }
  1549. size_t capacity() const {
  1550. return S - size();
  1551. }
  1552. T* endData() {
  1553. size_t i = end % S;
  1554. return &data[i];
  1555. }
  1556. void endIncr(size_t n) {
  1557. size_t i = end % S;
  1558. size_t e1 = i + n;
  1559. size_t e2 = (e1 < S) ? e1 : S;
  1560. std::memcpy(&data[S + i], &data[i], sizeof(T) * (e2 - i));
  1561. if (e1 > S) {
  1562. std::memcpy(data, &data[S], sizeof(T) * (e1 - S));
  1563. }
  1564. end += n;
  1565. }
  1566. const T* startData() const {
  1567. size_t i = start % S;
  1568. return &data[i];
  1569. }
  1570. void startIncr(size_t n) {
  1571. start += n;
  1572. }
  1573. };
  1574. template <typename T, size_t S, size_t N>
  1575. struct AppleRingBuffer {
  1576. size_t start = 0;
  1577. size_t end = 0;
  1578. T data[N];
  1579. void returnBuffer() {
  1580. size_t s = size();
  1581. std::memmove(data, &data[start], sizeof(T) * s);
  1582. start = 0;
  1583. end = s;
  1584. }
  1585. void push(T t) {
  1586. if (end + 1 > N) {
  1587. returnBuffer();
  1588. }
  1589. data[end++] = t;
  1590. }
  1591. T shift() {
  1592. return data[start++];
  1593. }
  1594. bool empty() const {
  1595. return start == end;
  1596. }
  1597. bool full() const {
  1598. return end - start == S;
  1599. }
  1600. size_t size() const {
  1601. return end - start;
  1602. }
  1603. size_t capacity() const {
  1604. return S - size();
  1605. }
  1606. T* endData(size_t n) {
  1607. if (end + n > N) {
  1608. returnBuffer();
  1609. }
  1610. return &data[end];
  1611. }
  1612. void endIncr(size_t n) {
  1613. end += n;
  1614. }
  1615. const T* startData() const {
  1616. return &data[start];
  1617. }
  1618. void startIncr(size_t n) {
  1619. start += n;
  1620. }
  1621. };
  1622. } // namespace dsp
  1623. namespace event {
  1624. using Base = widget::BaseEvent;
  1625. using PositionBase = widget::Widget::PositionBaseEvent;
  1626. using KeyBase = widget::Widget::KeyBaseEvent;
  1627. using TextBase = widget::Widget::TextBaseEvent;
  1628. using Hover = widget::Widget::HoverEvent;
  1629. using Button = widget::Widget::ButtonEvent;
  1630. using DoubleClick = widget::Widget::DoubleClickEvent;
  1631. using HoverKey = widget::Widget::HoverKeyEvent;
  1632. using HoverText = widget::Widget::HoverTextEvent;
  1633. using HoverScroll = widget::Widget::HoverScrollEvent;
  1634. using Enter = widget::Widget::EnterEvent;
  1635. using Leave = widget::Widget::LeaveEvent;
  1636. using Select = widget::Widget::SelectEvent;
  1637. using Deselect = widget::Widget::DeselectEvent;
  1638. using SelectKey = widget::Widget::SelectKeyEvent;
  1639. using SelectText = widget::Widget::SelectTextEvent;
  1640. using DragBase = widget::Widget::DragBaseEvent;
  1641. using DragStart = widget::Widget::DragStartEvent;
  1642. using DragEnd = widget::Widget::DragEndEvent;
  1643. using DragMove = widget::Widget::DragMoveEvent;
  1644. using DragHover = widget::Widget::DragHoverEvent;
  1645. using DragEnter = widget::Widget::DragEnterEvent;
  1646. using DragLeave = widget::Widget::DragLeaveEvent;
  1647. using DragDrop = widget::Widget::DragDropEvent;
  1648. using PathDrop = widget::Widget::PathDropEvent;
  1649. using Action = widget::Widget::ActionEvent;
  1650. using Change = widget::Widget::ChangeEvent;
  1651. using Dirty = widget::Widget::DirtyEvent;
  1652. using Reposition = widget::Widget::RepositionEvent;
  1653. using Resize = widget::Widget::ResizeEvent;
  1654. using Add = widget::Widget::AddEvent;
  1655. using Remove = widget::Widget::RemoveEvent;
  1656. using Show = widget::Widget::ShowEvent;
  1657. using Hide = widget::Widget::HideEvent;
  1658. } // namespace event
  1659. namespace plugin {
  1660. struct Model {
  1661. virtual ~Model() {}
  1662. virtual engine::Module* createModule() = 0;
  1663. };
  1664. struct Plugin {
  1665. };
  1666. } // namespace plugin
  1667. namespace random {
  1668. struct Xoroshiro128Plus {
  1669. uint64_t state[2] = {};
  1670. void seed(uint64_t s0, uint64_t s1) {
  1671. state[0] = s0;
  1672. state[1] = s1;
  1673. operator()();
  1674. }
  1675. bool isSeeded() { return state[0] || state[1]; }
  1676. static uint64_t rotl(uint64_t x, int k) { return (x << k) | (x >> (64 - k)); }
  1677. uint64_t operator()() {
  1678. uint64_t s0 = state[0];
  1679. uint64_t s1 = state[1];
  1680. uint64_t result = s0 + s1;
  1681. s1 ^= s0;
  1682. state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14);
  1683. state[1] = rotl(s1, 36);
  1684. return result;
  1685. }
  1686. constexpr uint64_t min() const { return 0; }
  1687. constexpr uint64_t max() const { return UINT64_MAX; }
  1688. };
  1689. Xoroshiro128Plus& local();
  1690. template <typename T>
  1691. T get() { return local()(); }
  1692. template <>
  1693. inline uint32_t get() { return get<uint64_t>() >> 32; }
  1694. template <>
  1695. inline uint16_t get() { return get<uint64_t>() >> 48; }
  1696. template <>
  1697. inline uint8_t get() { return get<uint64_t>() >> 56; }
  1698. template <>
  1699. inline bool get() { return get<uint64_t>() >> 63; }
  1700. template <>
  1701. inline float get() { return get<uint32_t>() * 2.32830629e-10f; }
  1702. template <>
  1703. inline double get() { return get<uint64_t>() * 5.421010862427522e-20; }
  1704. inline uint64_t u64() { return get<uint64_t>(); }
  1705. inline uint32_t u32() { return get<uint32_t>(); }
  1706. inline float uniform() { return get<float>(); }
  1707. inline float normal() {
  1708. const float radius = std::sqrt(-2.f * std::log(1.f - get<float>()));
  1709. const float theta = 2.f * M_PI * get<float>();
  1710. return radius * std::sin(theta);
  1711. }
  1712. inline void buffer(uint8_t* out, size_t len) {
  1713. Xoroshiro128Plus& rng = local();
  1714. for (size_t i = 0; i < len; i += 4) {
  1715. uint64_t r = rng();
  1716. out[i] = r;
  1717. if (i + 1 < len)
  1718. out[i + 1] = r >> 8;
  1719. if (i + 2 < len)
  1720. out[i + 2] = r >> 16;
  1721. if (i + 3 < len)
  1722. out[i + 3] = r >> 24;
  1723. }
  1724. }
  1725. inline std::vector<uint8_t> vector(size_t len) {
  1726. std::vector<uint8_t> v(len);
  1727. buffer(v.data(), len);
  1728. return v;
  1729. }
  1730. } // namespace random
  1731. namespace settings {
  1732. const bool tooltips = false;
  1733. } // namespace settings
  1734. namespace string {
  1735. inline std::string fV(const char* format, va_list args) {
  1736. // va_lists cannot be reused but we need it twice, so clone args.
  1737. va_list args2;
  1738. va_copy(args2, args);
  1739. // Compute size of required buffer
  1740. int size = vsnprintf(NULL, 0, format, args);
  1741. if (size < 0)
  1742. return "";
  1743. // Create buffer
  1744. std::string s;
  1745. s.resize(size);
  1746. vsnprintf(&s[0], size + 1, format, args2);
  1747. return s;
  1748. }
  1749. __attribute__((format(printf, 1, 2)))
  1750. inline std::string f(const char* format, ...) {
  1751. va_list args;
  1752. va_start(args, format);
  1753. std::string s = fV(format, args);
  1754. va_end(args);
  1755. return s;
  1756. }
  1757. } // namespace string
  1758. namespace ui {
  1759. struct Button : widget::OpaqueWidget {
  1760. std::string text;
  1761. Quantity* quantity = NULL;
  1762. };
  1763. struct ChoiceButton : Button {
  1764. };
  1765. struct Menu : widget::OpaqueWidget {
  1766. // Menu* parentMenu = NULL;
  1767. // Menu* childMenu = NULL;
  1768. // MenuEntry* activeEntry = NULL;
  1769. // BNDcornerFlags cornerFlags = BND_CORNER_NONE;
  1770. // void setChildMenu(Menu* menu) {}
  1771. };
  1772. struct MenuEntry : widget::OpaqueWidget {
  1773. };
  1774. struct MenuItem : MenuEntry {
  1775. std::string text;
  1776. std::string rightText;
  1777. bool disabled = false;
  1778. virtual Menu* createChildMenu() { return NULL; }
  1779. };
  1780. struct MenuLabel : MenuEntry {
  1781. std::string text;
  1782. };
  1783. struct MenuSeparator : MenuEntry {
  1784. };
  1785. struct Tooltip : widget::Widget {
  1786. std::string text;
  1787. };
  1788. } // namespace ui
  1789. namespace window {
  1790. static constexpr const float SVG_DPI = 75.f;
  1791. static constexpr const float MM_PER_IN = 25.4f;
  1792. inline float in2px(float in) { return in * SVG_DPI; }
  1793. inline math::Vec in2px(math::Vec in) { return in.mult(SVG_DPI); }
  1794. inline float mm2px(float mm) { return mm * (SVG_DPI / MM_PER_IN); }
  1795. inline math::Vec mm2px(math::Vec mm) { return mm.mult(SVG_DPI / MM_PER_IN); }
  1796. struct Svg {
  1797. // NSVGimage* handle = NULL;
  1798. // ~Svg();
  1799. // /** Don't call this directly. Use `Svg::load()` for caching. */
  1800. // void loadFile(const std::string& filename);
  1801. // void loadString(const std::string& str);
  1802. // math::Vec getSize();
  1803. // int getNumShapes();
  1804. // int getNumPaths();
  1805. // int getNumPoints();
  1806. // void draw(NVGcontext* vg);
  1807. static std::shared_ptr<Svg> load(const std::string&) { return {}; }
  1808. };
  1809. inline void svgDraw(NVGcontext*, NSVGimage*) {}
  1810. struct Font {
  1811. int handle;
  1812. };
  1813. struct Image {
  1814. int handle;
  1815. };
  1816. struct Window {
  1817. inline std::shared_ptr<Font> loadFont(const std::string&) { return {}; }
  1818. inline std::shared_ptr<Image> loadImage(const std::string&) { return {}; }
  1819. inline std::shared_ptr<Svg> loadSvg(const std::string&) { return {}; }
  1820. };
  1821. };
  1822. using namespace app;
  1823. using namespace componentlibrary;
  1824. using namespace engine;
  1825. using namespace math;
  1826. using namespace ui;
  1827. using namespace widget;
  1828. using namespace window;
  1829. using plugin::Plugin;
  1830. using plugin::Model;
  1831. template <class TModule, class TModuleWidget>
  1832. plugin::Model* createModel(std::string) {
  1833. struct TModel : plugin::Model {
  1834. engine::Module* createModule() override {
  1835. return new TModule;
  1836. }
  1837. };
  1838. return new TModel;
  1839. }
  1840. template <typename T>
  1841. T* construct() { return NULL; }
  1842. template <typename T, typename F, typename V, typename... Args>
  1843. T* construct(F f, V v, Args... args) { return NULL; }
  1844. template <class TWidget>
  1845. inline TWidget* createWidget(math::Vec pos) {
  1846. return NULL;
  1847. }
  1848. template <class TWidget>
  1849. inline TWidget* createWidgetCentered(math::Vec) {
  1850. return NULL;
  1851. }
  1852. inline app::SvgPanel* createPanel(std::string) {
  1853. return NULL;
  1854. }
  1855. template <class TParamWidget>
  1856. inline TParamWidget* createParam(math::Vec, engine::Module*, int) {
  1857. return NULL;
  1858. }
  1859. template <class TParamWidget>
  1860. inline TParamWidget* createParamCentered(math::Vec, engine::Module*, int) {
  1861. return NULL;
  1862. }
  1863. template <class TPortWidget>
  1864. inline TPortWidget* createInput(math::Vec, engine::Module*, int) {
  1865. return NULL;
  1866. }
  1867. template <class TPortWidget>
  1868. inline TPortWidget* createInputCentered(math::Vec, engine::Module*, int) {
  1869. return NULL;
  1870. }
  1871. template <class TPortWidget>
  1872. inline TPortWidget* createOutput(math::Vec pos, engine::Module*, int) {
  1873. return NULL;
  1874. }
  1875. template <class TPortWidget>
  1876. inline TPortWidget* createOutputCentered(math::Vec pos, engine::Module*, int) {
  1877. return NULL;
  1878. }
  1879. template <class TModuleLightWidget>
  1880. inline TModuleLightWidget* createLight(math::Vec, engine::Module*, int) {
  1881. return NULL;
  1882. }
  1883. template <class TModuleLightWidget>
  1884. inline TModuleLightWidget* createLightCentered(math::Vec, engine::Module*, int) {
  1885. return NULL;
  1886. }
  1887. template <class TParamWidget>
  1888. inline TParamWidget* createLightParam(math::Vec, engine::Module*, int, int) {
  1889. return NULL;
  1890. }
  1891. template <class TParamWidget>
  1892. inline TParamWidget* createLightParamCentered(math::Vec, engine::Module*, int, int) {
  1893. return NULL;
  1894. }
  1895. template <class TMenu = ui::Menu>
  1896. inline TMenu* createMenu() {
  1897. return NULL;
  1898. }
  1899. template <class TMenuLabel = ui::MenuLabel>
  1900. inline TMenuLabel* createMenuLabel(std::string) {
  1901. return NULL;
  1902. }
  1903. template <class TMenuItem = ui::MenuItem>
  1904. inline TMenuItem* createMenuItem(std::string, std::string rt = "") {
  1905. return NULL;
  1906. }
  1907. template <class TMenuItem = ui::MenuItem>
  1908. inline TMenuItem* createMenuItem(std::string, std::string rightText, std::function<void()>, bool d = false, bool ac = false) {
  1909. return NULL;
  1910. }
  1911. template <class TMenuItem = ui::MenuItem>
  1912. inline ui::MenuItem* createCheckMenuItem(std::string, std::string, std::function<bool()>, std::function<void()>, bool d = false, bool ac = false) {
  1913. return NULL;
  1914. }
  1915. template <class TMenuItem = ui::MenuItem>
  1916. inline ui::MenuItem* createBoolMenuItem(std::string, std::string, std::function<bool()>, std::function<void(bool)>, bool d = false, bool ac = false) {
  1917. return NULL;
  1918. }
  1919. template <typename T>
  1920. inline ui::MenuItem* createBoolPtrMenuItem(std::string, std::string, T*) {
  1921. return NULL;
  1922. }
  1923. template <class TMenuItem = ui::MenuItem>
  1924. inline ui::MenuItem* createSubmenuItem(std::string, std::string, std::function<void(ui::Menu*)>, bool d = false) {
  1925. return NULL;
  1926. }
  1927. template <class TMenuItem = ui::MenuItem>
  1928. inline ui::MenuItem* createIndexSubmenuItem(std::string, std::vector<std::string>, std::function<size_t()>, std::function<void(size_t)>, bool d = false, bool ac = false) {
  1929. return NULL;
  1930. }
  1931. template <typename T>
  1932. inline ui::MenuItem* createIndexPtrSubmenuItem(std::string, std::vector<std::string>, T*) {
  1933. return NULL;
  1934. }
  1935. struct Context {
  1936. app::Scene _scene;
  1937. engine::Engine _engine;
  1938. window::Window _window;
  1939. engine::Engine* engine = &_engine;
  1940. app::Scene* scene = &_scene;
  1941. window::Window* window = &_window;
  1942. };
  1943. Context* contextGet();
  1944. void contextSet(Context* context);
  1945. } // namespace rack
  1946. #define APP rack::contextGet()