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.

2240 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. inline void setBrightness(float brightness) {
  367. value = brightness;
  368. }
  369. inline float getBrightness() {
  370. return value;
  371. }
  372. inline 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. inline void setSmoothBrightness(float brightness, float deltaTime) {
  383. setBrightnessSmooth(brightness, deltaTime);
  384. }
  385. inline 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. inline float getValue() { return value; }
  401. inline 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. inline void setVoltage(float voltage, int channel = 0) { voltages[channel] = voltage; }
  418. inline float getVoltage(int channel = 0) { return voltages[channel]; }
  419. inline float getPolyVoltage(int channel) { return isMonophonic() ? getVoltage(0) : getVoltage(channel); }
  420. inline float getNormalVoltage(float normalVoltage, int channel = 0) {
  421. return isConnected() ? getVoltage(channel) : normalVoltage;
  422. }
  423. inline float getNormalPolyVoltage(float normalVoltage, int channel) {
  424. return isConnected() ? getPolyVoltage(channel) : normalVoltage;
  425. }
  426. inline float* getVoltages(int firstChannel = 0) { return &voltages[firstChannel]; }
  427. inline void readVoltages(float* v) {
  428. for (int c = 0; c < channels; c++) {
  429. v[c] = voltages[c];
  430. }
  431. }
  432. inline void writeVoltages(const float* v) {
  433. for (int c = 0; c < channels; c++) {
  434. voltages[c] = v[c];
  435. }
  436. }
  437. inline void clearVoltages() {
  438. for (int c = 0; c < channels; c++) {
  439. voltages[c] = 0.f;
  440. }
  441. }
  442. inline 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. inline 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. inline 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. inline int getChannels() { return channels; }
  501. inline bool isConnected() { return channels > 0; }
  502. inline bool isMonophonic() { return channels == 1; }
  503. inline bool isPolyphonic() { return channels > 1; }
  504. inline 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. inline float getMinValue() override { return minValue; }
  552. inline float getMaxValue() override { return maxValue; }
  553. inline 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. for (int i = 0; i < numLights; i++) {
  618. configLight(i);
  619. }
  620. }
  621. template <class TParamQuantity = ParamQuantity>
  622. 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) {
  623. if (paramQuantities[paramId])
  624. delete paramQuantities[paramId];
  625. TParamQuantity* q = new TParamQuantity;
  626. q->ParamQuantity::module = this;
  627. q->ParamQuantity::paramId = paramId;
  628. q->ParamQuantity::minValue = minValue;
  629. q->ParamQuantity::maxValue = maxValue;
  630. q->ParamQuantity::defaultValue = defaultValue;
  631. q->ParamQuantity::name = name;
  632. q->ParamQuantity::unit = unit;
  633. q->ParamQuantity::displayBase = displayBase;
  634. q->ParamQuantity::displayMultiplier = displayMultiplier;
  635. q->ParamQuantity::displayOffset = displayOffset;
  636. paramQuantities[paramId] = q;
  637. Param* p = &params[paramId];
  638. p->value = q->getDefaultValue();
  639. return q;
  640. }
  641. template <class TSwitchQuantity = SwitchQuantity>
  642. TSwitchQuantity* configSwitch(int paramId, float minValue, float maxValue, float defaultValue, std::string name = "", std::vector<std::string> labels = {}) {
  643. TSwitchQuantity* sq = configParam<TSwitchQuantity>(paramId, minValue, maxValue, defaultValue, name);
  644. sq->labels = labels;
  645. return sq;
  646. }
  647. template <class TSwitchQuantity = SwitchQuantity>
  648. TSwitchQuantity* configButton(int paramId, std::string name = "") {
  649. TSwitchQuantity* sq = configParam<TSwitchQuantity>(paramId, 0.f, 1.f, 0.f, name);
  650. sq->randomizeEnabled = false;
  651. return sq;
  652. }
  653. template <class TPortInfo = PortInfo>
  654. TPortInfo* configInput(int portId, std::string name = "") {
  655. if (inputInfos[portId])
  656. delete inputInfos[portId];
  657. TPortInfo* info = new TPortInfo;
  658. info->PortInfo::module = this;
  659. info->PortInfo::type = Port::INPUT;
  660. info->PortInfo::portId = portId;
  661. info->PortInfo::name = name;
  662. inputInfos[portId] = info;
  663. return info;
  664. }
  665. template <class TPortInfo = PortInfo>
  666. TPortInfo* configOutput(int portId, std::string name = "") {
  667. if (outputInfos[portId])
  668. delete outputInfos[portId];
  669. TPortInfo* info = new TPortInfo;
  670. info->PortInfo::module = this;
  671. info->PortInfo::type = Port::OUTPUT;
  672. info->PortInfo::portId = portId;
  673. info->PortInfo::name = name;
  674. outputInfos[portId] = info;
  675. return info;
  676. }
  677. template <class TLightInfo = LightInfo>
  678. TLightInfo* configLight(int lightId, std::string name = "") {
  679. if (lightInfos[lightId])
  680. delete lightInfos[lightId];
  681. TLightInfo* info = new TLightInfo;
  682. info->LightInfo::module = this;
  683. info->LightInfo::lightId = lightId;
  684. info->LightInfo::name = name;
  685. lightInfos[lightId] = info;
  686. return info;
  687. }
  688. void configBypass(int inputId, int outputId) {
  689. // // Check that output is not yet routed
  690. // for (BypassRoute& br : bypassRoutes) {
  691. // assert(br.outputId != outputId);
  692. // }
  693. // BypassRoute br;
  694. // br.inputId = inputId;
  695. // br.outputId = outputId;
  696. // bypassRoutes.push_back(br);
  697. }
  698. int getNumParams() { return params.size(); }
  699. Param& getParam(int index) { return params[index]; }
  700. int getNumInputs() { return inputs.size(); }
  701. Input& getInput(int index) { return inputs[index]; }
  702. int getNumOutputs() { return outputs.size(); }
  703. Output& getOutput(int index) { return outputs[index]; }
  704. int getNumLights() { return lights.size(); }
  705. Light& getLight(int index) { return lights[index]; }
  706. ParamQuantity* getParamQuantity(int index) { return paramQuantities[index]; }
  707. PortInfo* getInputInfo(int index) { return inputInfos[index]; }
  708. PortInfo* getOutputInfo(int index) { return outputInfos[index]; }
  709. LightInfo* getLightInfo(int index) { return lightInfos[index]; }
  710. struct ProcessArgs {
  711. float sampleRate;
  712. float sampleTime;
  713. int64_t frame;
  714. };
  715. virtual void process(const ProcessArgs& args) {
  716. step();
  717. }
  718. virtual void step() {}
  719. // virtual void processBypass(const ProcessArgs& args);
  720. // virtual json_t* toJson();
  721. // virtual void fromJson(json_t* rootJ);
  722. // virtual json_t* paramsToJson();
  723. // virtual void paramsFromJson(json_t* rootJ);
  724. virtual json_t* dataToJson() { return NULL; }
  725. virtual void dataFromJson(json_t* rootJ) {}
  726. struct SampleRateChangeEvent {
  727. float sampleRate;
  728. float sampleTime;
  729. };
  730. virtual void onSampleRateChange(const SampleRateChangeEvent&) {
  731. onSampleRateChange();
  732. }
  733. struct ResetEvent {};
  734. virtual void onReset(const ResetEvent&) {} // TODO
  735. virtual void onAdd() {}
  736. virtual void onRemove() {}
  737. virtual void onReset() {}
  738. virtual void onRandomize() {}
  739. virtual void onSampleRateChange() {}
  740. // private
  741. void doProcess(const ProcessArgs& args) {
  742. // if (!internal->bypassed)
  743. process(args);
  744. // else
  745. // processBypass(args);
  746. // if (args.frame % PORT_DIVIDER == 0) {
  747. // float portTime = args.sampleTime * PORT_DIVIDER;
  748. // for (Input& input : inputs) {
  749. // Port_step(&input, portTime);
  750. // }
  751. // for (Output& output : outputs) {
  752. // Port_step(&output, portTime);
  753. // }
  754. // }
  755. }
  756. };
  757. } // namespace engine
  758. namespace widget {
  759. struct BaseEvent {
  760. };
  761. struct Widget {
  762. math::Rect box;
  763. Widget* parent = NULL;
  764. std::list<Widget*> children;
  765. bool visible = false;
  766. bool requestedDelete = false;
  767. virtual ~Widget() {}
  768. math::Rect getBox() { return {}; }
  769. void setBox(math::Rect box) {}
  770. math::Vec getPosition() { return {}; }
  771. void setPosition(math::Vec pos) {}
  772. math::Vec getSize() { return {}; }
  773. void setSize(math::Vec size) {}
  774. widget::Widget* getParent() { return NULL; }
  775. bool isVisible() { return false; }
  776. void setVisible(bool visible) {}
  777. void show() {}
  778. void hide() {}
  779. void requestDelete() {}
  780. virtual math::Rect getChildrenBoundingBox() { return {}; }
  781. virtual math::Rect getVisibleChildrenBoundingBox() { return {}; }
  782. bool isDescendantOf(Widget* ancestor) { return false; }
  783. virtual math::Vec getRelativeOffset(math::Vec v, Widget* ancestor) { return {}; }
  784. math::Vec getAbsoluteOffset(math::Vec v) { return {}; }
  785. virtual float getRelativeZoom(Widget* ancestor) { return 0.f; }
  786. float getAbsoluteZoom() { return 0.f; }
  787. virtual math::Rect getViewport(math::Rect r) { return {}; }
  788. template <class T> T* getAncestorOfType() { return NULL; }
  789. template <class T> T* getFirstDescendantOfType() { return NULL; }
  790. bool hasChild(Widget* child) { return false; }
  791. void addChild(Widget* child) {}
  792. void addChildBottom(Widget* child) {}
  793. void addChildBelow(Widget* child, Widget* sibling) {}
  794. void addChildAbove(Widget* child, Widget* sibling) {}
  795. void removeChild(Widget* child) {}
  796. void clearChildren() {}
  797. virtual void step() {}
  798. struct DrawArgs {
  799. NVGcontext* vg = NULL;
  800. math::Rect clipBox;
  801. void* fb = NULL;
  802. };
  803. virtual void draw(const DrawArgs&) {}
  804. // DEPRECATED virtual void draw(NVGcontext* vg) {}
  805. virtual void drawLayer(const DrawArgs& args, int layer) {}
  806. void drawChild(Widget* child, const DrawArgs& args, int layer = 0) {}
  807. using BaseEvent = widget::BaseEvent;
  808. struct PositionBaseEvent {
  809. math::Vec pos;
  810. };
  811. struct HoverEvent : BaseEvent, PositionBaseEvent {
  812. math::Vec mouseDelta;
  813. };
  814. virtual void onHover(const HoverEvent&) {}
  815. struct ButtonEvent : BaseEvent, PositionBaseEvent {
  816. int button;
  817. int action;
  818. int mods;
  819. };
  820. virtual void onButton(const ButtonEvent&) {}
  821. struct DoubleClickEvent : BaseEvent {};
  822. virtual void onDoubleClick(const DoubleClickEvent&) {}
  823. struct KeyBaseEvent {
  824. int key;
  825. int scancode;
  826. std::string keyName;
  827. int action;
  828. int mods;
  829. };
  830. struct HoverKeyEvent : BaseEvent, PositionBaseEvent, KeyBaseEvent {};
  831. virtual void onHoverKey(const HoverKeyEvent&) {}
  832. struct TextBaseEvent {
  833. int codepoint;
  834. };
  835. struct HoverTextEvent : BaseEvent, PositionBaseEvent, TextBaseEvent {};
  836. virtual void onHoverText(const HoverTextEvent&) {}
  837. struct HoverScrollEvent : BaseEvent, PositionBaseEvent {
  838. math::Vec scrollDelta;
  839. };
  840. virtual void onHoverScroll(const HoverScrollEvent&) {}
  841. struct EnterEvent : BaseEvent {};
  842. virtual void onEnter(const EnterEvent&) {}
  843. struct LeaveEvent : BaseEvent {};
  844. virtual void onLeave(const LeaveEvent&) {}
  845. struct SelectEvent : BaseEvent {};
  846. virtual void onSelect(const SelectEvent&) {}
  847. struct DeselectEvent : BaseEvent {};
  848. virtual void onDeselect(const DeselectEvent&) {}
  849. struct SelectKeyEvent : BaseEvent, KeyBaseEvent {};
  850. virtual void onSelectKey(const SelectKeyEvent&) {}
  851. struct SelectTextEvent : BaseEvent, TextBaseEvent {};
  852. virtual void onSelectText(const SelectTextEvent&) {}
  853. struct DragBaseEvent : BaseEvent {
  854. int button;
  855. };
  856. struct DragStartEvent : DragBaseEvent {};
  857. virtual void onDragStart(const DragStartEvent&) {}
  858. struct DragEndEvent : DragBaseEvent {};
  859. virtual void onDragEnd(const DragEndEvent&) {}
  860. struct DragMoveEvent : DragBaseEvent {
  861. math::Vec mouseDelta;
  862. };
  863. virtual void onDragMove(const DragMoveEvent&) {}
  864. struct DragHoverEvent : DragBaseEvent, PositionBaseEvent {
  865. Widget* origin = NULL;
  866. math::Vec mouseDelta;
  867. };
  868. virtual void onDragHover(const DragHoverEvent&) {
  869. }
  870. struct DragEnterEvent : DragBaseEvent {
  871. Widget* origin = NULL;
  872. };
  873. virtual void onDragEnter(const DragEnterEvent&) {}
  874. struct DragLeaveEvent : DragBaseEvent {
  875. Widget* origin = NULL;
  876. };
  877. virtual void onDragLeave(const DragLeaveEvent&) {}
  878. struct DragDropEvent : DragBaseEvent {
  879. Widget* origin = NULL;
  880. };
  881. virtual void onDragDrop(const DragDropEvent&) {}
  882. struct PathDropEvent : BaseEvent, PositionBaseEvent {
  883. PathDropEvent(const std::vector<std::string>& paths) : paths(paths) {}
  884. const std::vector<std::string>& paths;
  885. };
  886. virtual void onPathDrop(const PathDropEvent&) {}
  887. struct ActionEvent : BaseEvent {};
  888. virtual void onAction(const ActionEvent&) {}
  889. struct ChangeEvent : BaseEvent {};
  890. virtual void onChange(const ChangeEvent&) {}
  891. struct DirtyEvent : BaseEvent {};
  892. virtual void onDirty(const DirtyEvent&) {}
  893. struct RepositionEvent : BaseEvent {};
  894. virtual void onReposition(const RepositionEvent&) {}
  895. struct ResizeEvent : BaseEvent {};
  896. virtual void onResize(const ResizeEvent&) {}
  897. struct AddEvent : BaseEvent {};
  898. virtual void onAdd(const AddEvent&) {}
  899. struct RemoveEvent : BaseEvent {};
  900. virtual void onRemove(const RemoveEvent&) {}
  901. struct ShowEvent : BaseEvent {};
  902. virtual void onShow(const ShowEvent&) {}
  903. struct HideEvent : BaseEvent {};
  904. virtual void onHide(const HideEvent&) {}
  905. struct ContextCreateEvent : BaseEvent {
  906. NVGcontext* vg;
  907. };
  908. virtual void onContextCreate(const ContextCreateEvent&) {}
  909. struct ContextDestroyEvent : BaseEvent {
  910. NVGcontext* vg;
  911. };
  912. virtual void onContextDestroy(const ContextDestroyEvent&) {}
  913. };
  914. struct OpaqueWidget : Widget {
  915. };
  916. struct SvgWidget : Widget {
  917. std::shared_ptr<window::Svg> svg;
  918. void wrap() {}
  919. void setSvg(std::shared_ptr<window::Svg>) {}
  920. };
  921. struct TransparentWidget : Widget {
  922. };
  923. } // namespace widget
  924. namespace app {
  925. static constexpr const float RACK_GRID_WIDTH = 15;
  926. static constexpr const float RACK_GRID_HEIGHT = 380;
  927. static const math::Vec RACK_GRID_SIZE = math::Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT);
  928. static const math::Vec RACK_OFFSET = RACK_GRID_SIZE.mult(math::Vec(2000, 100));
  929. struct ParamWidget;
  930. struct PortWidget;
  931. struct CircularShadow : widget::TransparentWidget {
  932. float blurRadius;
  933. float opacity;
  934. };
  935. struct LightWidget : widget::TransparentWidget {
  936. NVGcolor bgColor, color, borderColor;
  937. };
  938. struct ModuleWidget : widget::OpaqueWidget {
  939. // plugin::Model* model = NULL;
  940. engine::Module* module = NULL;
  941. // plugin::Model* getModel() { return NULL; }
  942. // void setModel(plugin::Model*) {}
  943. engine::Module* getModule() { return NULL; }
  944. void setModule(engine::Module*) {}
  945. widget::Widget* getPanel() { return NULL; }
  946. void setPanel(widget::Widget*) {}
  947. void setPanel(std::shared_ptr<window::Svg>) {}
  948. void addParam(ParamWidget*) {}
  949. void addInput(PortWidget*) {}
  950. void addOutput(PortWidget*) {}
  951. ParamWidget* getParam(int paramId) { return NULL; }
  952. PortWidget* getInput(int portId) { return NULL; }
  953. PortWidget* getOutput(int portId) { return NULL; }
  954. std::vector<ParamWidget*> getParams() { return {}; }
  955. std::vector<PortWidget*> getPorts() { return {}; }
  956. std::vector<PortWidget*> getInputs() { return {}; }
  957. std::vector<PortWidget*> getOutputs() { return {}; }
  958. virtual void appendContextMenu(ui::Menu*) {}
  959. json_t* toJson() { return NULL; }
  960. void fromJson(json_t*) {}
  961. bool pasteJsonAction(json_t*) { return false; }
  962. void copyClipboard() {}
  963. bool pasteClipboardAction() { return false; }
  964. void load(std::string) {}
  965. void loadAction(std::string) {}
  966. void loadTemplate() {}
  967. void loadDialog() {}
  968. void save(std::string) {}
  969. void saveTemplate() {}
  970. void saveTemplateDialog() {}
  971. bool hasTemplate() { return false; }
  972. void clearTemplate() {}
  973. void clearTemplateDialog() {}
  974. void saveDialog() {}
  975. void disconnect() {}
  976. void resetAction() {}
  977. void randomizeAction() {}
  978. void appendDisconnectActions(/*history::ComplexAction*/void*) {}
  979. void disconnectAction() {}
  980. void cloneAction(bool cc = true) {}
  981. void bypassAction(bool) {}
  982. void removeAction() {}
  983. void createContextMenu() {}
  984. };
  985. struct MultiLightWidget : LightWidget {
  986. // std::vector<NVGcolor> baseColors;
  987. // int getNumColors();
  988. void addBaseColor(NVGcolor baseColor) {}
  989. // void setBrightnesses(const std::vector<float>& brightnesses);
  990. };
  991. struct ModuleLightWidget : MultiLightWidget {
  992. // engine::Module* module = NULL;
  993. // int firstLightId = -1;
  994. // ModuleLightWidget();
  995. // ~ModuleLightWidget();
  996. // engine::Light* getLight(int colorId);
  997. // engine::LightInfo* getLightInfo();
  998. // void createTooltip();
  999. // void destroyTooltip();
  1000. };
  1001. struct ParamWidget : widget::OpaqueWidget {
  1002. engine::Module* module = NULL;
  1003. int paramId = -1;
  1004. virtual void initParamQuantity() {}
  1005. engine::ParamQuantity* getParamQuantity() { return module ? module->paramQuantities[paramId] : NULL; }
  1006. void createTooltip() {}
  1007. void destroyTooltip() {}
  1008. void createContextMenu();
  1009. virtual void appendContextMenu(void* menu) {}
  1010. void resetAction();
  1011. };
  1012. struct PortWidget : widget::OpaqueWidget {
  1013. };
  1014. struct Knob : ParamWidget {
  1015. bool horizontal = false;
  1016. bool smooth = true;
  1017. bool snap = false;
  1018. float speed = 1.f;
  1019. bool forceLinear = false;
  1020. float minAngle = -M_PI;
  1021. float maxAngle = M_PI;
  1022. };
  1023. struct SliderKnob : Knob {
  1024. };
  1025. struct Switch : ParamWidget {
  1026. bool momentary = false;
  1027. };
  1028. struct SvgKnob : Knob {
  1029. // widget::FramebufferWidget* fb;
  1030. CircularShadow* shadow;
  1031. // widget::TransformWidget* tw;
  1032. widget::SvgWidget* sw;
  1033. void setSvg(std::shared_ptr<window::Svg>) {}
  1034. };
  1035. struct SvgPanel : widget::Widget {
  1036. // widget::FramebufferWidget* fb;
  1037. // widget::SvgWidget* sw;
  1038. // PanelBorder* panelBorder;
  1039. void setBackground(std::shared_ptr<window::Svg>) {}
  1040. };
  1041. struct SvgPort : PortWidget {
  1042. // widget::FramebufferWidget* fb;
  1043. CircularShadow* shadow;
  1044. // widget::SvgWidget* sw;
  1045. void setSvg(std::shared_ptr<window::Svg>) {}
  1046. };
  1047. struct SvgScrew : widget::Widget {
  1048. // widget::FramebufferWidget* fb;
  1049. widget::SvgWidget* sw;
  1050. void setSvg(std::shared_ptr<window::Svg>) {}
  1051. };
  1052. struct SvgSlider : app::SliderKnob {
  1053. // widget::FramebufferWidget* fb;
  1054. widget::SvgWidget* background;
  1055. widget::SvgWidget* handle;
  1056. math::Vec minHandlePos, maxHandlePos;
  1057. void setBackgroundSvg(std::shared_ptr<window::Svg>) {}
  1058. void setHandleSvg(std::shared_ptr<window::Svg>) {}
  1059. void setHandlePos(math::Vec minHandlePos, math::Vec maxHandlePos) {}
  1060. void setHandlePosCentered(math::Vec minHandlePosCentered, math::Vec maxHandlePosCentered) {}
  1061. };
  1062. struct SvgSwitch : Switch {
  1063. // widget::FramebufferWidget* fb;
  1064. // CircularShadow* shadow;
  1065. // widget::SvgWidget* sw;
  1066. // std::vector<std::shared_ptr<window::Svg>> frames;
  1067. bool latch = false;
  1068. void addFrame(std::shared_ptr<window::Svg>) {}
  1069. };
  1070. struct Scene : widget::OpaqueWidget {
  1071. // RackScrollWidget* rackScroll;
  1072. // RackWidget* rack;
  1073. widget::Widget* menuBar;
  1074. widget::Widget* browser;
  1075. math::Vec getMousePos() { return {}; }
  1076. };
  1077. } // namespace app
  1078. namespace asset {
  1079. inline std::string plugin(void* instance, const char* path) { return {}; }
  1080. inline std::string system(const char* path) { return {}; }
  1081. } // namespace asset
  1082. namespace componentlibrary {
  1083. static constexpr const NVGcolor SCHEME_BLACK_TRANSPARENT = {};
  1084. static constexpr const NVGcolor SCHEME_BLACK = {};
  1085. static constexpr const NVGcolor SCHEME_WHITE = {};
  1086. static constexpr const NVGcolor SCHEME_RED = {};
  1087. static constexpr const NVGcolor SCHEME_ORANGE = {};
  1088. static constexpr const NVGcolor SCHEME_YELLOW = {};
  1089. static constexpr const NVGcolor SCHEME_GREEN = {};
  1090. static constexpr const NVGcolor SCHEME_CYAN = {};
  1091. static constexpr const NVGcolor SCHEME_BLUE = {};
  1092. static constexpr const NVGcolor SCHEME_PURPLE = {};
  1093. static constexpr const NVGcolor SCHEME_LIGHT_GRAY = {};
  1094. static constexpr const NVGcolor SCHEME_DARK_GRAY = {};
  1095. template <typename TBase = app::ModuleLightWidget>
  1096. struct TSvgLight : TBase {
  1097. // widget::FramebufferWidget* fb;
  1098. // widget::SvgWidget* sw;
  1099. void setSvg(std::shared_ptr<window::Svg>) {}
  1100. };
  1101. using SvgLight = TSvgLight<>;
  1102. template <typename TBase = app::ModuleLightWidget>
  1103. struct TGrayModuleLightWidget : TBase {};
  1104. using GrayModuleLightWidget = TGrayModuleLightWidget<>;
  1105. template <typename TBase = GrayModuleLightWidget>
  1106. struct TWhiteLight : TBase {};
  1107. using WhiteLight = TWhiteLight<>;
  1108. template <typename TBase = GrayModuleLightWidget>
  1109. struct TRedLight : TBase {};
  1110. using RedLight = TRedLight<>;
  1111. template <typename TBase = GrayModuleLightWidget>
  1112. struct TGreenLight : TBase {};
  1113. using GreenLight = TGreenLight<>;
  1114. template <typename TBase = GrayModuleLightWidget>
  1115. struct TBlueLight : TBase {};
  1116. using BlueLight = TBlueLight<>;
  1117. template <typename TBase = GrayModuleLightWidget>
  1118. struct TYellowLight : TBase {};
  1119. using YellowLight = TYellowLight<>;
  1120. template <typename TBase = GrayModuleLightWidget>
  1121. struct TGreenRedLight : TBase {};
  1122. using GreenRedLight = TGreenRedLight<>;
  1123. template <typename TBase = GrayModuleLightWidget>
  1124. struct TRedGreenBlueLight : TBase {};
  1125. using RedGreenBlueLight = TRedGreenBlueLight<>;
  1126. template <typename TBase>
  1127. struct LargeLight : TSvgLight<TBase> {};
  1128. template <typename TBase>
  1129. struct MediumLight : TSvgLight<TBase> {};
  1130. template <typename TBase>
  1131. struct SmallLight : TSvgLight<TBase> {};
  1132. template <typename TBase>
  1133. struct TinyLight : TSvgLight<TBase> {};
  1134. template <typename TBase = GrayModuleLightWidget>
  1135. struct LargeSimpleLight : TBase {};
  1136. template <typename TBase = GrayModuleLightWidget>
  1137. struct MediumSimpleLight : TBase {};
  1138. template <typename TBase = GrayModuleLightWidget>
  1139. struct SmallSimpleLight : TBase {};
  1140. template <typename TBase = GrayModuleLightWidget>
  1141. struct TinySimpleLight : TBase {};
  1142. template <typename TBase>
  1143. struct RectangleLight : TBase {};
  1144. template <typename TBase>
  1145. struct VCVBezelLight : TBase {};
  1146. template <typename TBase>
  1147. using LEDBezelLight = VCVBezelLight<TBase>;
  1148. template <typename TBase>
  1149. struct PB61303Light : TBase {};
  1150. struct RoundKnob : app::SvgKnob {
  1151. widget::SvgWidget* bg;
  1152. };
  1153. struct RoundBlackKnob : RoundKnob {};
  1154. struct RoundSmallBlackKnob : RoundKnob {};
  1155. struct RoundLargeBlackKnob : RoundKnob {};
  1156. struct RoundBigBlackKnob : RoundKnob {};
  1157. struct RoundHugeBlackKnob : RoundKnob {};
  1158. struct RoundBlackSnapKnob : RoundBlackKnob {};
  1159. struct Davies1900hKnob : app::SvgKnob {};
  1160. struct Davies1900hWhiteKnob : Davies1900hKnob {};
  1161. struct Davies1900hBlackKnob : Davies1900hKnob {};
  1162. struct Davies1900hRedKnob : Davies1900hKnob {};
  1163. struct Davies1900hLargeWhiteKnob : Davies1900hKnob {};
  1164. struct Davies1900hLargeBlackKnob : Davies1900hKnob {};
  1165. struct Davies1900hLargeRedKnob : Davies1900hKnob {};
  1166. struct Rogan : app::SvgKnob {
  1167. widget::SvgWidget* bg;
  1168. widget::SvgWidget* fg;
  1169. };
  1170. struct Rogan6PSWhite : Rogan {};
  1171. struct Rogan5PSGray : Rogan {};
  1172. struct Rogan3PSBlue : Rogan {};
  1173. struct Rogan3PSRed : Rogan {};
  1174. struct Rogan3PSGreen : Rogan {};
  1175. struct Rogan3PSWhite : Rogan {};
  1176. struct Rogan3PBlue : Rogan {};
  1177. struct Rogan3PRed : Rogan {};
  1178. struct Rogan3PGreen : Rogan {};
  1179. struct Rogan3PWhite : Rogan {};
  1180. struct Rogan2SGray : Rogan {};
  1181. struct Rogan2PSBlue : Rogan {};
  1182. struct Rogan2PSRed : Rogan {};
  1183. struct Rogan2PSGreen : Rogan {};
  1184. struct Rogan2PSWhite : Rogan {};
  1185. struct Rogan2PBlue : Rogan {};
  1186. struct Rogan2PRed : Rogan {};
  1187. struct Rogan2PGreen : Rogan {};
  1188. struct Rogan2PWhite : Rogan {};
  1189. struct Rogan1PSBlue : Rogan {};
  1190. struct Rogan1PSRed : Rogan {};
  1191. struct Rogan1PSGreen : Rogan {};
  1192. struct Rogan1PSWhite : Rogan {};
  1193. struct Rogan1PBlue : Rogan {};
  1194. struct Rogan1PRed : Rogan {};
  1195. struct Rogan1PGreen : Rogan {};
  1196. struct Rogan1PWhite : Rogan {};
  1197. struct SynthTechAlco : app::SvgKnob {
  1198. widget::SvgWidget* bg;
  1199. };
  1200. struct Trimpot : app::SvgKnob {
  1201. widget::SvgWidget* bg;
  1202. };
  1203. struct BefacoBigKnob : app::SvgKnob {
  1204. widget::SvgWidget* bg;
  1205. };
  1206. struct BefacoTinyKnob : app::SvgKnob {
  1207. widget::SvgWidget* bg;
  1208. };
  1209. struct BefacoSlidePot : app::SvgSlider {};
  1210. struct VCVSlider : app::SvgSlider {};
  1211. using LEDSlider = VCVSlider;
  1212. struct VCVSliderHorizontal : app::SvgSlider {};
  1213. using LEDSliderHorizontal = VCVSliderHorizontal;
  1214. template <typename TBase, typename TLightBase = RedLight>
  1215. struct LightSlider : TBase {
  1216. app::ModuleLightWidget* light = NULL;
  1217. app::ModuleLightWidget* getLight() { return light; }
  1218. };
  1219. template <typename TBase>
  1220. struct VCVSliderLight : RectangleLight<TSvgLight<TBase>> {};
  1221. template <typename TBase>
  1222. using LEDSliderLight = VCVSliderLight<TBase>;
  1223. template <typename TLightBase = RedLight>
  1224. struct VCVLightSlider : LightSlider<VCVSlider, VCVSliderLight<TLightBase>> {};
  1225. template <typename TLightBase = RedLight>
  1226. using LEDLightSlider = VCVLightSlider<TLightBase>;
  1227. struct LEDSliderGreen : VCVLightSlider<GreenLight> {};
  1228. struct LEDSliderRed : VCVLightSlider<RedLight> {};
  1229. struct LEDSliderYellow : VCVLightSlider<YellowLight> {};
  1230. struct LEDSliderBlue : VCVLightSlider<BlueLight> {};
  1231. struct LEDSliderWhite : VCVLightSlider<WhiteLight> {};
  1232. template <typename TLightBase = RedLight>
  1233. struct VCVLightSliderHorizontal : LightSlider<VCVSliderHorizontal, TLightBase> {};
  1234. template <typename TLightBase = RedLight>
  1235. using LEDLightSliderHorizontal = VCVLightSliderHorizontal<TLightBase>;
  1236. struct PJ301MPort : app::SvgPort {};
  1237. struct PJ3410Port : app::SvgPort {};
  1238. struct CL1362Port : app::SvgPort {};
  1239. template <typename TSwitch>
  1240. struct MomentarySwitch : TSwitch {};
  1241. struct NKK : app::SvgSwitch {};
  1242. struct CKSS : app::SvgSwitch {};
  1243. struct CKSSThree : app::SvgSwitch {};
  1244. struct CKSSThreeHorizontal : app::SvgSwitch {};
  1245. struct CKD6 : app::SvgSwitch {};
  1246. struct TL1105 : app::SvgSwitch {};
  1247. struct VCVButton : app::SvgSwitch {};
  1248. using LEDButton = VCVButton;
  1249. struct VCVLatch : VCVButton {};
  1250. template <typename TLight>
  1251. struct VCVLightButton : VCVButton {
  1252. app::ModuleLightWidget* light = NULL;
  1253. app::ModuleLightWidget* getLight() { return light; }
  1254. };
  1255. template <typename TLight>
  1256. using LEDLightButton = VCVLightButton<TLight>;
  1257. template <typename TLight>
  1258. struct VCVLightLatch : VCVLightButton<TLight> {};
  1259. struct BefacoSwitch : app::SvgSwitch {};
  1260. struct BefacoPush : app::SvgSwitch {};
  1261. struct VCVBezel : app::SvgSwitch {};
  1262. using LEDBezel = VCVBezel;
  1263. struct VCVBezelLatch : VCVBezel {};
  1264. template <typename TLightBase = WhiteLight>
  1265. struct VCVLightBezel : VCVBezel {
  1266. app::ModuleLightWidget* light = NULL;
  1267. app::ModuleLightWidget* getLight() { return light; }
  1268. };
  1269. template <typename TLightBase = WhiteLight>
  1270. using LEDLightBezel = VCVLightBezel<TLightBase>;
  1271. template <typename TLightBase = WhiteLight>
  1272. struct VCVLightBezelLatch : VCVLightBezel<TLightBase> {};
  1273. struct PB61303 : app::SvgSwitch {};
  1274. struct ScrewSilver : app::SvgScrew {};
  1275. struct ScrewBlack : app::SvgScrew {};
  1276. } // namespace componentlibrary
  1277. namespace dsp {
  1278. static constexpr const float FREQ_C4 = 261.6256f;
  1279. static constexpr const float FREQ_A4 = 440.0000f;
  1280. static constexpr const float FREQ_SEMITONE = 1.0594630943592953f;
  1281. inline float sinc(float x) {
  1282. if (x == 0.f)
  1283. return 1.f;
  1284. x *= M_PI;
  1285. return std::sin(x) / x;
  1286. }
  1287. // template <typename T>
  1288. // T sinc(T x) {
  1289. // T zeromask = (x == 0.f);
  1290. // x *= M_PI;
  1291. // x = simd::sin(x) / x;
  1292. // return simd::ifelse(zeromask, 1.f, x);
  1293. // }
  1294. // template <typename T>
  1295. // T amplitudeToDb(T amp) {
  1296. // return simd::log10(amp) * 20;
  1297. // }
  1298. template <typename T>
  1299. T dbToAmplitude(T db) { return std::pow(10, db / 20); }
  1300. // template <typename T>
  1301. // T quadraticBipolar(T x) {
  1302. // return simd::sgn(x) * (x * x);
  1303. // }
  1304. template <typename T>
  1305. T cubic(T x) { return x * x * x; }
  1306. // template <typename T>
  1307. // T quarticBipolar(T x) {
  1308. // return simd::sgn(x) * (x * x * x * x);
  1309. // }
  1310. template <typename T>
  1311. T quintic(T x) { return x * x * x * x * x; }
  1312. // template <typename T>
  1313. // T sqrtBipolar(T x) {
  1314. // return simd::sgn(x) * simd::sqrt(x);
  1315. // }
  1316. // template <typename T>
  1317. // T exponentialBipolar(T b, T x) {
  1318. // return (simd::pow(b, x) - simd::pow(b, -x)) / (b - 1.f / b);
  1319. // }
  1320. template <size_t CHANNELS, typename T = float>
  1321. struct Frame { T samples[CHANNELS]; };
  1322. template <typename T>
  1323. inline T blackmanHarris(T p) {
  1324. return
  1325. + T(0.35875)
  1326. - T(0.48829) * std::cos(2 * T(M_PI) * p)
  1327. + T(0.14128) * std::cos(4 * T(M_PI) * p)
  1328. - T(0.01168) * std::cos(6 * T(M_PI) * p);
  1329. }
  1330. inline void blackmanHarrisWindow(float* x, int len) {
  1331. for (int i = 0; i < len; i++) {
  1332. x[i] *= blackmanHarris(float(i) / (len - 1));
  1333. }
  1334. }
  1335. inline void boxcarLowpassIR(float* out, int len, float cutoff = 0.5f) {
  1336. for (int i = 0; i < len; i++) {
  1337. float t = i - (len - 1) / 2.f;
  1338. out[i] = 2 * cutoff * sinc(2 * cutoff * t);
  1339. }
  1340. }
  1341. struct BooleanTrigger {
  1342. bool state = true;
  1343. void reset() {
  1344. state = true;
  1345. }
  1346. bool process(bool state) {
  1347. bool triggered = (state && !this->state);
  1348. this->state = state;
  1349. return triggered;
  1350. }
  1351. };
  1352. template <typename T = float>
  1353. struct TSchmittTrigger {
  1354. T state;
  1355. TSchmittTrigger() {
  1356. reset();
  1357. }
  1358. void reset() {
  1359. state = T::mask();
  1360. }
  1361. T process(T in, T offThreshold = 0.f, T onThreshold = 1.f) {
  1362. T on = (in >= onThreshold);
  1363. T off = (in <= offThreshold);
  1364. T triggered = ~state & on;
  1365. state = on | (state & ~off);
  1366. return triggered;
  1367. }
  1368. T isHigh() {
  1369. return state;
  1370. }
  1371. };
  1372. template <>
  1373. struct TSchmittTrigger<float> {
  1374. bool state = true;
  1375. void reset() {
  1376. state = true;
  1377. }
  1378. bool process(float in, float offThreshold = 0.f, float onThreshold = 1.f) {
  1379. if (state) {
  1380. if (in <= offThreshold) {
  1381. state = false;
  1382. }
  1383. }
  1384. else {
  1385. if (in >= onThreshold) {
  1386. state = true;
  1387. return true;
  1388. }
  1389. }
  1390. return false;
  1391. }
  1392. bool isHigh() {
  1393. return state;
  1394. }
  1395. };
  1396. typedef TSchmittTrigger<> SchmittTrigger;
  1397. struct PulseGenerator {
  1398. float remaining = 0.f;
  1399. void reset() { remaining = 0.f; }
  1400. bool process(float deltaTime) {
  1401. if (remaining > 0.f) {
  1402. remaining -= deltaTime;
  1403. return true;
  1404. }
  1405. return false;
  1406. }
  1407. void trigger(float duration = 1e-3f) {
  1408. if (duration > remaining) {
  1409. remaining = duration;
  1410. }
  1411. }
  1412. };
  1413. template <typename T = float>
  1414. struct TTimer {
  1415. T time = 0.f;
  1416. void reset() {
  1417. time = 0.f;
  1418. }
  1419. T process(T deltaTime) {
  1420. time += deltaTime;
  1421. return time;
  1422. }
  1423. T getTime() {
  1424. return time;
  1425. }
  1426. };
  1427. typedef TTimer<> Timer;
  1428. struct ClockDivider {
  1429. uint32_t clock = 0;
  1430. uint32_t division = 1;
  1431. void reset() { clock = 0; }
  1432. void setDivision(uint32_t division) { this->division = division; }
  1433. uint32_t getDivision() { return division; }
  1434. uint32_t getClock() { return clock; }
  1435. bool process() {
  1436. clock++;
  1437. if (clock >= division) {
  1438. clock = 0;
  1439. return true;
  1440. }
  1441. return false;
  1442. }
  1443. };
  1444. template <int OVERSAMPLE, int QUALITY, typename T = float>
  1445. struct Decimator {
  1446. T inBuffer[OVERSAMPLE * QUALITY];
  1447. float kernel[OVERSAMPLE * QUALITY];
  1448. int inIndex;
  1449. Decimator(float cutoff = 0.9f) {
  1450. boxcarLowpassIR(kernel, OVERSAMPLE * QUALITY, cutoff * 0.5f / OVERSAMPLE);
  1451. blackmanHarrisWindow(kernel, OVERSAMPLE * QUALITY);
  1452. reset();
  1453. }
  1454. void reset() {
  1455. inIndex = 0;
  1456. std::memset(inBuffer, 0, sizeof(inBuffer));
  1457. }
  1458. T process(T* in) {
  1459. std::memcpy(&inBuffer[inIndex], in, OVERSAMPLE * sizeof(T));
  1460. inIndex += OVERSAMPLE;
  1461. inIndex %= OVERSAMPLE * QUALITY;
  1462. T out = 0.f;
  1463. for (int i = 0; i < OVERSAMPLE * QUALITY; i++) {
  1464. int index = inIndex - 1 - i;
  1465. index = (index + OVERSAMPLE * QUALITY) % (OVERSAMPLE * QUALITY);
  1466. out += kernel[i] * inBuffer[index];
  1467. }
  1468. return out;
  1469. }
  1470. };
  1471. template <typename T, size_t S>
  1472. struct RingBuffer {
  1473. std::atomic<size_t> start{0};
  1474. std::atomic<size_t> end{0};
  1475. T data[S];
  1476. void push(T t) {
  1477. size_t i = end % S;
  1478. data[i] = t;
  1479. end++;
  1480. }
  1481. void pushBuffer(const T* t, int n) {
  1482. size_t i = end % S;
  1483. size_t e1 = i + n;
  1484. size_t e2 = (e1 < S) ? e1 : S;
  1485. std::memcpy(&data[i], t, sizeof(T) * (e2 - i));
  1486. if (e1 > S) {
  1487. std::memcpy(data, &t[S - i], sizeof(T) * (e1 - S));
  1488. }
  1489. end += n;
  1490. }
  1491. T shift() {
  1492. size_t i = start % S;
  1493. T t = data[i];
  1494. start++;
  1495. return t;
  1496. }
  1497. void shiftBuffer(T* t, size_t n) {
  1498. size_t i = start % S;
  1499. size_t s1 = i + n;
  1500. size_t s2 = (s1 < S) ? s1 : S;
  1501. std::memcpy(t, &data[i], sizeof(T) * (s2 - i));
  1502. if (s1 > S) {
  1503. std::memcpy(&t[S - i], data, sizeof(T) * (s1 - S));
  1504. }
  1505. start += n;
  1506. }
  1507. void clear() {
  1508. start = end.load();
  1509. }
  1510. bool empty() const {
  1511. return start >= end;
  1512. }
  1513. bool full() const {
  1514. return end - start >= S;
  1515. }
  1516. size_t size() const {
  1517. return end - start;
  1518. }
  1519. size_t capacity() const {
  1520. return S - size();
  1521. }
  1522. };
  1523. template <typename T, size_t S>
  1524. struct DoubleRingBuffer {
  1525. std::atomic<size_t> start{0};
  1526. std::atomic<size_t> end{0};
  1527. T data[2 * S];
  1528. void push(T t) {
  1529. size_t i = end % S;
  1530. data[i] = t;
  1531. data[i + S] = t;
  1532. end++;
  1533. }
  1534. T shift() {
  1535. size_t i = start % S;
  1536. T t = data[i];
  1537. start++;
  1538. return t;
  1539. }
  1540. void clear() {
  1541. start = end.load();
  1542. }
  1543. bool empty() const {
  1544. return start >= end;
  1545. }
  1546. bool full() const {
  1547. return end - start >= S;
  1548. }
  1549. size_t size() const {
  1550. return end - start;
  1551. }
  1552. size_t capacity() const {
  1553. return S - size();
  1554. }
  1555. T* endData() {
  1556. size_t i = end % S;
  1557. return &data[i];
  1558. }
  1559. void endIncr(size_t n) {
  1560. size_t i = end % S;
  1561. size_t e1 = i + n;
  1562. size_t e2 = (e1 < S) ? e1 : S;
  1563. std::memcpy(&data[S + i], &data[i], sizeof(T) * (e2 - i));
  1564. if (e1 > S) {
  1565. std::memcpy(data, &data[S], sizeof(T) * (e1 - S));
  1566. }
  1567. end += n;
  1568. }
  1569. const T* startData() const {
  1570. size_t i = start % S;
  1571. return &data[i];
  1572. }
  1573. void startIncr(size_t n) {
  1574. start += n;
  1575. }
  1576. };
  1577. template <typename T, size_t S, size_t N>
  1578. struct AppleRingBuffer {
  1579. size_t start = 0;
  1580. size_t end = 0;
  1581. T data[N];
  1582. void returnBuffer() {
  1583. size_t s = size();
  1584. std::memmove(data, &data[start], sizeof(T) * s);
  1585. start = 0;
  1586. end = s;
  1587. }
  1588. void push(T t) {
  1589. if (end + 1 > N) {
  1590. returnBuffer();
  1591. }
  1592. data[end++] = t;
  1593. }
  1594. T shift() {
  1595. return data[start++];
  1596. }
  1597. bool empty() const {
  1598. return start == end;
  1599. }
  1600. bool full() const {
  1601. return end - start == S;
  1602. }
  1603. size_t size() const {
  1604. return end - start;
  1605. }
  1606. size_t capacity() const {
  1607. return S - size();
  1608. }
  1609. T* endData(size_t n) {
  1610. if (end + n > N) {
  1611. returnBuffer();
  1612. }
  1613. return &data[end];
  1614. }
  1615. void endIncr(size_t n) {
  1616. end += n;
  1617. }
  1618. const T* startData() const {
  1619. return &data[start];
  1620. }
  1621. void startIncr(size_t n) {
  1622. start += n;
  1623. }
  1624. };
  1625. } // namespace dsp
  1626. namespace event {
  1627. using Base = widget::BaseEvent;
  1628. using PositionBase = widget::Widget::PositionBaseEvent;
  1629. using KeyBase = widget::Widget::KeyBaseEvent;
  1630. using TextBase = widget::Widget::TextBaseEvent;
  1631. using Hover = widget::Widget::HoverEvent;
  1632. using Button = widget::Widget::ButtonEvent;
  1633. using DoubleClick = widget::Widget::DoubleClickEvent;
  1634. using HoverKey = widget::Widget::HoverKeyEvent;
  1635. using HoverText = widget::Widget::HoverTextEvent;
  1636. using HoverScroll = widget::Widget::HoverScrollEvent;
  1637. using Enter = widget::Widget::EnterEvent;
  1638. using Leave = widget::Widget::LeaveEvent;
  1639. using Select = widget::Widget::SelectEvent;
  1640. using Deselect = widget::Widget::DeselectEvent;
  1641. using SelectKey = widget::Widget::SelectKeyEvent;
  1642. using SelectText = widget::Widget::SelectTextEvent;
  1643. using DragBase = widget::Widget::DragBaseEvent;
  1644. using DragStart = widget::Widget::DragStartEvent;
  1645. using DragEnd = widget::Widget::DragEndEvent;
  1646. using DragMove = widget::Widget::DragMoveEvent;
  1647. using DragHover = widget::Widget::DragHoverEvent;
  1648. using DragEnter = widget::Widget::DragEnterEvent;
  1649. using DragLeave = widget::Widget::DragLeaveEvent;
  1650. using DragDrop = widget::Widget::DragDropEvent;
  1651. using PathDrop = widget::Widget::PathDropEvent;
  1652. using Action = widget::Widget::ActionEvent;
  1653. using Change = widget::Widget::ChangeEvent;
  1654. using Dirty = widget::Widget::DirtyEvent;
  1655. using Reposition = widget::Widget::RepositionEvent;
  1656. using Resize = widget::Widget::ResizeEvent;
  1657. using Add = widget::Widget::AddEvent;
  1658. using Remove = widget::Widget::RemoveEvent;
  1659. using Show = widget::Widget::ShowEvent;
  1660. using Hide = widget::Widget::HideEvent;
  1661. } // namespace event
  1662. namespace plugin {
  1663. struct Model {
  1664. virtual ~Model() {}
  1665. virtual engine::Module* createModule() = 0;
  1666. };
  1667. struct Plugin {
  1668. };
  1669. } // namespace plugin
  1670. namespace random {
  1671. struct Xoroshiro128Plus {
  1672. uint64_t state[2] = {};
  1673. void seed(uint64_t s0, uint64_t s1) {
  1674. state[0] = s0;
  1675. state[1] = s1;
  1676. operator()();
  1677. }
  1678. bool isSeeded() { return state[0] || state[1]; }
  1679. static uint64_t rotl(uint64_t x, int k) { return (x << k) | (x >> (64 - k)); }
  1680. uint64_t operator()() {
  1681. uint64_t s0 = state[0];
  1682. uint64_t s1 = state[1];
  1683. uint64_t result = s0 + s1;
  1684. s1 ^= s0;
  1685. state[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14);
  1686. state[1] = rotl(s1, 36);
  1687. return result;
  1688. }
  1689. constexpr uint64_t min() const { return 0; }
  1690. constexpr uint64_t max() const { return UINT64_MAX; }
  1691. };
  1692. Xoroshiro128Plus& local();
  1693. template <typename T>
  1694. T get() { return local()(); }
  1695. template <>
  1696. inline uint32_t get() { return get<uint64_t>() >> 32; }
  1697. template <>
  1698. inline uint16_t get() { return get<uint64_t>() >> 48; }
  1699. template <>
  1700. inline uint8_t get() { return get<uint64_t>() >> 56; }
  1701. template <>
  1702. inline bool get() { return get<uint64_t>() >> 63; }
  1703. template <>
  1704. inline float get() { return get<uint32_t>() * 2.32830629e-10f; }
  1705. template <>
  1706. inline double get() { return get<uint64_t>() * 5.421010862427522e-20; }
  1707. inline uint64_t u64() { return get<uint64_t>(); }
  1708. inline uint32_t u32() { return get<uint32_t>(); }
  1709. inline float uniform() { return get<float>(); }
  1710. inline float normal() {
  1711. const float radius = std::sqrt(-2.f * std::log(1.f - get<float>()));
  1712. const float theta = 2.f * M_PI * get<float>();
  1713. return radius * std::sin(theta);
  1714. }
  1715. inline void buffer(uint8_t* out, size_t len) {
  1716. Xoroshiro128Plus& rng = local();
  1717. for (size_t i = 0; i < len; i += 4) {
  1718. uint64_t r = rng();
  1719. out[i] = r;
  1720. if (i + 1 < len)
  1721. out[i + 1] = r >> 8;
  1722. if (i + 2 < len)
  1723. out[i + 2] = r >> 16;
  1724. if (i + 3 < len)
  1725. out[i + 3] = r >> 24;
  1726. }
  1727. }
  1728. inline std::vector<uint8_t> vector(size_t len) {
  1729. std::vector<uint8_t> v(len);
  1730. buffer(v.data(), len);
  1731. return v;
  1732. }
  1733. } // namespace random
  1734. namespace settings {
  1735. const bool tooltips = false;
  1736. } // namespace settings
  1737. namespace string {
  1738. inline std::string fV(const char* format, va_list args) {
  1739. // va_lists cannot be reused but we need it twice, so clone args.
  1740. va_list args2;
  1741. va_copy(args2, args);
  1742. // Compute size of required buffer
  1743. int size = vsnprintf(NULL, 0, format, args);
  1744. if (size < 0)
  1745. return "";
  1746. // Create buffer
  1747. std::string s;
  1748. s.resize(size);
  1749. vsnprintf(&s[0], size + 1, format, args2);
  1750. return s;
  1751. }
  1752. __attribute__((format(printf, 1, 2)))
  1753. inline std::string f(const char* format, ...) {
  1754. va_list args;
  1755. va_start(args, format);
  1756. std::string s = fV(format, args);
  1757. va_end(args);
  1758. return s;
  1759. }
  1760. } // namespace string
  1761. namespace ui {
  1762. struct Button : widget::OpaqueWidget {
  1763. std::string text;
  1764. Quantity* quantity = NULL;
  1765. };
  1766. struct ChoiceButton : Button {
  1767. };
  1768. struct Menu : widget::OpaqueWidget {
  1769. // Menu* parentMenu = NULL;
  1770. // Menu* childMenu = NULL;
  1771. // MenuEntry* activeEntry = NULL;
  1772. // BNDcornerFlags cornerFlags = BND_CORNER_NONE;
  1773. // void setChildMenu(Menu* menu) {}
  1774. };
  1775. struct MenuEntry : widget::OpaqueWidget {
  1776. };
  1777. struct MenuItem : MenuEntry {
  1778. std::string text;
  1779. std::string rightText;
  1780. bool disabled = false;
  1781. virtual Menu* createChildMenu() { return NULL; }
  1782. };
  1783. struct MenuLabel : MenuEntry {
  1784. std::string text;
  1785. };
  1786. struct MenuSeparator : MenuEntry {
  1787. };
  1788. struct Tooltip : widget::Widget {
  1789. std::string text;
  1790. };
  1791. } // namespace ui
  1792. namespace window {
  1793. static constexpr const float SVG_DPI = 75.f;
  1794. static constexpr const float MM_PER_IN = 25.4f;
  1795. inline float in2px(float in) { return in * SVG_DPI; }
  1796. inline math::Vec in2px(math::Vec in) { return in.mult(SVG_DPI); }
  1797. inline float mm2px(float mm) { return mm * (SVG_DPI / MM_PER_IN); }
  1798. inline math::Vec mm2px(math::Vec mm) { return mm.mult(SVG_DPI / MM_PER_IN); }
  1799. struct Svg {
  1800. // NSVGimage* handle = NULL;
  1801. // ~Svg();
  1802. // /** Don't call this directly. Use `Svg::load()` for caching. */
  1803. // void loadFile(const std::string& filename);
  1804. // void loadString(const std::string& str);
  1805. // math::Vec getSize();
  1806. // int getNumShapes();
  1807. // int getNumPaths();
  1808. // int getNumPoints();
  1809. // void draw(NVGcontext* vg);
  1810. static std::shared_ptr<Svg> load(const std::string&) { return {}; }
  1811. };
  1812. inline void svgDraw(NVGcontext*, NSVGimage*) {}
  1813. struct Font {
  1814. int handle;
  1815. };
  1816. struct Image {
  1817. int handle;
  1818. };
  1819. struct Window {
  1820. inline std::shared_ptr<Font> loadFont(const std::string&) { return {}; }
  1821. inline std::shared_ptr<Image> loadImage(const std::string&) { return {}; }
  1822. inline std::shared_ptr<Svg> loadSvg(const std::string&) { return {}; }
  1823. };
  1824. };
  1825. using namespace app;
  1826. using namespace componentlibrary;
  1827. using namespace engine;
  1828. using namespace math;
  1829. using namespace ui;
  1830. using namespace widget;
  1831. using namespace window;
  1832. using plugin::Plugin;
  1833. using plugin::Model;
  1834. template <class TModule, class TModuleWidget>
  1835. plugin::Model* createModel(std::string) {
  1836. struct TModel : plugin::Model {
  1837. engine::Module* createModule() override {
  1838. return new TModule;
  1839. }
  1840. };
  1841. return new TModel;
  1842. }
  1843. template <typename T>
  1844. T* construct() { return NULL; }
  1845. template <typename T, typename F, typename V, typename... Args>
  1846. T* construct(F f, V v, Args... args) { return NULL; }
  1847. template <class TWidget>
  1848. inline TWidget* createWidget(math::Vec pos) {
  1849. return NULL;
  1850. }
  1851. template <class TWidget>
  1852. inline TWidget* createWidgetCentered(math::Vec) {
  1853. return NULL;
  1854. }
  1855. inline app::SvgPanel* createPanel(std::string) {
  1856. return NULL;
  1857. }
  1858. template <class TParamWidget>
  1859. inline TParamWidget* createParam(math::Vec, engine::Module*, int) {
  1860. return NULL;
  1861. }
  1862. template <class TParamWidget>
  1863. inline TParamWidget* createParamCentered(math::Vec, engine::Module*, int) {
  1864. return NULL;
  1865. }
  1866. template <class TPortWidget>
  1867. inline TPortWidget* createInput(math::Vec, engine::Module*, int) {
  1868. return NULL;
  1869. }
  1870. template <class TPortWidget>
  1871. inline TPortWidget* createInputCentered(math::Vec, engine::Module*, int) {
  1872. return NULL;
  1873. }
  1874. template <class TPortWidget>
  1875. inline TPortWidget* createOutput(math::Vec pos, engine::Module*, int) {
  1876. return NULL;
  1877. }
  1878. template <class TPortWidget>
  1879. inline TPortWidget* createOutputCentered(math::Vec pos, engine::Module*, int) {
  1880. return NULL;
  1881. }
  1882. template <class TModuleLightWidget>
  1883. inline TModuleLightWidget* createLight(math::Vec, engine::Module*, int) {
  1884. return NULL;
  1885. }
  1886. template <class TModuleLightWidget>
  1887. inline TModuleLightWidget* createLightCentered(math::Vec, engine::Module*, int) {
  1888. return NULL;
  1889. }
  1890. template <class TParamWidget>
  1891. inline TParamWidget* createLightParam(math::Vec, engine::Module*, int, int) {
  1892. return NULL;
  1893. }
  1894. template <class TParamWidget>
  1895. inline TParamWidget* createLightParamCentered(math::Vec, engine::Module*, int, int) {
  1896. return NULL;
  1897. }
  1898. template <class TMenu = ui::Menu>
  1899. inline TMenu* createMenu() {
  1900. return NULL;
  1901. }
  1902. template <class TMenuLabel = ui::MenuLabel>
  1903. inline TMenuLabel* createMenuLabel(std::string) {
  1904. return NULL;
  1905. }
  1906. template <class TMenuItem = ui::MenuItem>
  1907. inline TMenuItem* createMenuItem(std::string, std::string rt = "") {
  1908. return NULL;
  1909. }
  1910. template <class TMenuItem = ui::MenuItem>
  1911. inline TMenuItem* createMenuItem(std::string, std::string rightText, std::function<void()>, bool d = false, bool ac = false) {
  1912. return NULL;
  1913. }
  1914. template <class TMenuItem = ui::MenuItem>
  1915. inline ui::MenuItem* createCheckMenuItem(std::string, std::string, std::function<bool()>, std::function<void()>, bool d = false, bool ac = false) {
  1916. return NULL;
  1917. }
  1918. template <class TMenuItem = ui::MenuItem>
  1919. inline ui::MenuItem* createBoolMenuItem(std::string, std::string, std::function<bool()>, std::function<void(bool)>, bool d = false, bool ac = false) {
  1920. return NULL;
  1921. }
  1922. template <typename T>
  1923. inline ui::MenuItem* createBoolPtrMenuItem(std::string, std::string, T*) {
  1924. return NULL;
  1925. }
  1926. template <class TMenuItem = ui::MenuItem>
  1927. inline ui::MenuItem* createSubmenuItem(std::string, std::string, std::function<void(ui::Menu*)>, bool d = false) {
  1928. return NULL;
  1929. }
  1930. template <class TMenuItem = ui::MenuItem>
  1931. 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) {
  1932. return NULL;
  1933. }
  1934. template <typename T>
  1935. inline ui::MenuItem* createIndexPtrSubmenuItem(std::string, std::vector<std::string>, T*) {
  1936. return NULL;
  1937. }
  1938. struct Context {
  1939. app::Scene _scene;
  1940. engine::Engine _engine;
  1941. window::Window _window;
  1942. engine::Engine* engine = &_engine;
  1943. app::Scene* scene = &_scene;
  1944. window::Window* window = &_window;
  1945. };
  1946. Context* contextGet();
  1947. void contextSet(Context* context);
  1948. } // namespace rack
  1949. #define APP rack::contextGet()