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.

346 lines
9.6KB

  1. #include <string.h>
  2. #include "Fundamental.hpp"
  3. #include "dsp/digital.hpp"
  4. #define BUFFER_SIZE 512
  5. struct Scope : Module {
  6. enum ParamIds {
  7. X_SCALE_PARAM,
  8. X_POS_PARAM,
  9. Y_SCALE_PARAM,
  10. Y_POS_PARAM,
  11. TIME_PARAM,
  12. LISSAJOUS_PARAM,
  13. TRIG_PARAM,
  14. EXTERNAL_PARAM,
  15. NUM_PARAMS
  16. };
  17. enum InputIds {
  18. X_INPUT,
  19. Y_INPUT,
  20. TRIG_INPUT,
  21. NUM_INPUTS
  22. };
  23. enum OutputIds {
  24. NUM_OUTPUTS
  25. };
  26. enum LightIds {
  27. PLOT_LIGHT,
  28. LISSAJOUS_LIGHT,
  29. INTERNAL_LIGHT,
  30. EXTERNAL_LIGHT,
  31. NUM_LIGHTS
  32. };
  33. float bufferX[BUFFER_SIZE] = {};
  34. float bufferY[BUFFER_SIZE] = {};
  35. int bufferIndex = 0;
  36. float frameIndex = 0;
  37. SchmittTrigger sumTrigger;
  38. SchmittTrigger extTrigger;
  39. bool lissajous = false;
  40. bool external = false;
  41. SchmittTrigger resetTrigger;
  42. Scope() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS) {}
  43. void step() override;
  44. json_t *toJson() override {
  45. json_t *rootJ = json_object();
  46. json_object_set_new(rootJ, "lissajous", json_integer((int) lissajous));
  47. json_object_set_new(rootJ, "external", json_integer((int) external));
  48. return rootJ;
  49. }
  50. void fromJson(json_t *rootJ) override {
  51. json_t *sumJ = json_object_get(rootJ, "lissajous");
  52. if (sumJ)
  53. lissajous = json_integer_value(sumJ);
  54. json_t *extJ = json_object_get(rootJ, "external");
  55. if (extJ)
  56. external = json_integer_value(extJ);
  57. }
  58. void onReset() override {
  59. lissajous = false;
  60. external = false;
  61. }
  62. };
  63. void Scope::step() {
  64. // Modes
  65. if (sumTrigger.process(params[LISSAJOUS_PARAM].value)) {
  66. lissajous = !lissajous;
  67. }
  68. lights[PLOT_LIGHT].value = lissajous ? 0.0f : 1.0f;
  69. lights[LISSAJOUS_LIGHT].value = lissajous ? 1.0f : 0.0f;
  70. if (extTrigger.process(params[EXTERNAL_PARAM].value)) {
  71. external = !external;
  72. }
  73. lights[INTERNAL_LIGHT].value = external ? 0.0f : 1.0f;
  74. lights[EXTERNAL_LIGHT].value = external ? 1.0f : 0.0f;
  75. // Compute time
  76. float deltaTime = powf(2.0f, params[TIME_PARAM].value);
  77. int frameCount = (int)ceilf(deltaTime * engineGetSampleRate());
  78. // Add frame to buffer
  79. if (bufferIndex < BUFFER_SIZE) {
  80. if (++frameIndex > frameCount) {
  81. frameIndex = 0;
  82. bufferX[bufferIndex] = inputs[X_INPUT].value;
  83. bufferY[bufferIndex] = inputs[Y_INPUT].value;
  84. bufferIndex++;
  85. }
  86. }
  87. // Are we waiting on the next trigger?
  88. if (bufferIndex >= BUFFER_SIZE) {
  89. // Trigger immediately if external but nothing plugged in, or in Lissajous mode
  90. if (lissajous || (external && !inputs[TRIG_INPUT].active)) {
  91. bufferIndex = 0;
  92. frameIndex = 0;
  93. return;
  94. }
  95. // Reset the Schmitt trigger so we don't trigger immediately if the input is high
  96. if (frameIndex == 0) {
  97. resetTrigger.reset();
  98. }
  99. frameIndex++;
  100. // Must go below 0.1fV to trigger
  101. resetTrigger.setThresholds(params[TRIG_PARAM].value - 0.1f, params[TRIG_PARAM].value);
  102. float gate = external ? inputs[TRIG_INPUT].value : inputs[X_INPUT].value;
  103. // Reset if triggered
  104. float holdTime = 0.1f;
  105. if (resetTrigger.process(gate) || (frameIndex >= engineGetSampleRate() * holdTime)) {
  106. bufferIndex = 0; frameIndex = 0; return;
  107. }
  108. // Reset if we've waited too long
  109. if (frameIndex >= engineGetSampleRate() * holdTime) {
  110. bufferIndex = 0; frameIndex = 0; return;
  111. }
  112. }
  113. }
  114. struct ScopeDisplay : TransparentWidget {
  115. Scope *module;
  116. int frame = 0;
  117. std::shared_ptr<Font> font;
  118. struct Stats {
  119. float vrms, vpp, vmin, vmax;
  120. void calculate(float *values) {
  121. vrms = 0.0f;
  122. vmax = -INFINITY;
  123. vmin = INFINITY;
  124. for (int i = 0; i < BUFFER_SIZE; i++) {
  125. float v = values[i];
  126. vrms += v*v;
  127. vmax = fmaxf(vmax, v);
  128. vmin = fminf(vmin, v);
  129. }
  130. vrms = sqrtf(vrms / BUFFER_SIZE);
  131. vpp = vmax - vmin;
  132. }
  133. };
  134. Stats statsX, statsY;
  135. ScopeDisplay() {
  136. font = Font::load(assetPlugin(plugin, "res/fonts/Sudo.ttf"));
  137. }
  138. void drawWaveform(NVGcontext *vg, float *valuesX, float *valuesY) {
  139. if (!valuesX)
  140. return;
  141. nvgSave(vg);
  142. Rect b = Rect(Vec(0, 15), box.size.minus(Vec(0, 15*2)));
  143. nvgScissor(vg, b.pos.x, b.pos.y, b.size.x, b.size.y);
  144. nvgBeginPath(vg);
  145. // Draw maximum display left to right
  146. for (int i = 0; i < BUFFER_SIZE; i++) {
  147. float x, y;
  148. if (valuesY) {
  149. x = valuesX[i] / 2.0f + 0.5f;
  150. y = valuesY[i] / 2.0f + 0.5f;
  151. }
  152. else {
  153. x = (float)i / (BUFFER_SIZE - 1);
  154. y = valuesX[i] / 2.0f + 0.5f;
  155. }
  156. Vec p;
  157. p.x = b.pos.x + b.size.x * x;
  158. p.y = b.pos.y + b.size.y * (1.0f - y);
  159. if (i == 0)
  160. nvgMoveTo(vg, p.x, p.y);
  161. else
  162. nvgLineTo(vg, p.x, p.y);
  163. }
  164. nvgLineCap(vg, NVG_ROUND);
  165. nvgMiterLimit(vg, 2.0f);
  166. nvgStrokeWidth(vg, 1.5f);
  167. nvgGlobalCompositeOperation(vg, NVG_LIGHTER);
  168. nvgStroke(vg);
  169. nvgResetScissor(vg);
  170. nvgRestore(vg);
  171. }
  172. void drawTrig(NVGcontext *vg, float value) {
  173. Rect b = Rect(Vec(0, 15), box.size.minus(Vec(0, 15*2)));
  174. nvgScissor(vg, b.pos.x, b.pos.y, b.size.x, b.size.y);
  175. value = value / 2.0f + 0.5f;
  176. Vec p = Vec(box.size.x, b.pos.y + b.size.y * (1.0f - value));
  177. // Draw line
  178. nvgStrokeColor(vg, nvgRGBA(0xff, 0xff, 0xff, 0x10));
  179. {
  180. nvgBeginPath(vg);
  181. nvgMoveTo(vg, p.x - 13, p.y);
  182. nvgLineTo(vg, 0, p.y);
  183. nvgClosePath(vg);
  184. }
  185. nvgStroke(vg);
  186. // Draw indicator
  187. nvgFillColor(vg, nvgRGBA(0xff, 0xff, 0xff, 0x60));
  188. {
  189. nvgBeginPath(vg);
  190. nvgMoveTo(vg, p.x - 2, p.y - 4);
  191. nvgLineTo(vg, p.x - 9, p.y - 4);
  192. nvgLineTo(vg, p.x - 13, p.y);
  193. nvgLineTo(vg, p.x - 9, p.y + 4);
  194. nvgLineTo(vg, p.x - 2, p.y + 4);
  195. nvgClosePath(vg);
  196. }
  197. nvgFill(vg);
  198. nvgFontSize(vg, 9);
  199. nvgFontFaceId(vg, font->handle);
  200. nvgFillColor(vg, nvgRGBA(0x1e, 0x28, 0x2b, 0xff));
  201. nvgText(vg, p.x - 8, p.y + 3, "T", NULL);
  202. nvgResetScissor(vg);
  203. }
  204. void drawStats(NVGcontext *vg, Vec pos, const char *title, Stats *stats) {
  205. nvgFontSize(vg, 13);
  206. nvgFontFaceId(vg, font->handle);
  207. nvgTextLetterSpacing(vg, -2);
  208. nvgFillColor(vg, nvgRGBA(0xff, 0xff, 0xff, 0x40));
  209. nvgText(vg, pos.x + 6, pos.y + 11, title, NULL);
  210. nvgFillColor(vg, nvgRGBA(0xff, 0xff, 0xff, 0x80));
  211. char text[128];
  212. snprintf(text, sizeof(text), "pp % 06.2f max % 06.2f min % 06.2f", stats->vpp, stats->vmax, stats->vmin);
  213. nvgText(vg, pos.x + 22, pos.y + 11, text, NULL);
  214. }
  215. void draw(NVGcontext *vg) override {
  216. float gainX = powf(2.0f, roundf(module->params[Scope::X_SCALE_PARAM].value));
  217. float gainY = powf(2.0f, roundf(module->params[Scope::Y_SCALE_PARAM].value));
  218. float offsetX = module->params[Scope::X_POS_PARAM].value;
  219. float offsetY = module->params[Scope::Y_POS_PARAM].value;
  220. float valuesX[BUFFER_SIZE];
  221. float valuesY[BUFFER_SIZE];
  222. for (int i = 0; i < BUFFER_SIZE; i++) {
  223. int j = i;
  224. // Lock display to buffer if buffer update deltaTime <= 2^-11
  225. if (module->lissajous)
  226. j = (i + module->bufferIndex) % BUFFER_SIZE;
  227. valuesX[i] = (module->bufferX[j] + offsetX) * gainX / 10.0f;
  228. valuesY[i] = (module->bufferY[j] + offsetY) * gainY / 10.0f;
  229. }
  230. // Draw waveforms
  231. if (module->lissajous) {
  232. // X x Y
  233. if (module->inputs[Scope::X_INPUT].active || module->inputs[Scope::Y_INPUT].active) {
  234. nvgStrokeColor(vg, nvgRGBA(0x9f, 0xe4, 0x36, 0xc0));
  235. drawWaveform(vg, valuesX, valuesY);
  236. }
  237. }
  238. else {
  239. // Y
  240. if (module->inputs[Scope::Y_INPUT].active) {
  241. nvgStrokeColor(vg, nvgRGBA(0xe1, 0x02, 0x78, 0xc0));
  242. drawWaveform(vg, valuesY, NULL);
  243. }
  244. // X
  245. if (module->inputs[Scope::X_INPUT].active) {
  246. nvgStrokeColor(vg, nvgRGBA(0x28, 0xb0, 0xf3, 0xc0));
  247. drawWaveform(vg, valuesX, NULL);
  248. }
  249. float valueTrig = (module->params[Scope::TRIG_PARAM].value + offsetX) * gainX / 10.0f;
  250. drawTrig(vg, valueTrig);
  251. }
  252. // Calculate and draw stats
  253. if (++frame >= 4) {
  254. frame = 0;
  255. statsX.calculate(module->bufferX);
  256. statsY.calculate(module->bufferY);
  257. }
  258. drawStats(vg, Vec(0, 0), "X", &statsX);
  259. drawStats(vg, Vec(0, box.size.y - 15), "Y", &statsY);
  260. }
  261. };
  262. ScopeWidget::ScopeWidget() {
  263. Scope *module = new Scope();
  264. setModule(module);
  265. box.size = Vec(15*13, 380);
  266. {
  267. SVGPanel *panel = new SVGPanel();
  268. panel->box.size = box.size;
  269. panel->setBackground(SVG::load(assetPlugin(plugin, "res/Scope.svg")));
  270. addChild(panel);
  271. }
  272. addChild(createScrew<ScrewSilver>(Vec(15, 0)));
  273. addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 0)));
  274. addChild(createScrew<ScrewSilver>(Vec(15, 365)));
  275. addChild(createScrew<ScrewSilver>(Vec(box.size.x-30, 365)));
  276. {
  277. ScopeDisplay *display = new ScopeDisplay();
  278. display->module = module;
  279. display->box.pos = Vec(0, 44);
  280. display->box.size = Vec(box.size.x, 140);
  281. addChild(display);
  282. }
  283. addParam(createParam<RoundSmallBlackSnapKnob>(Vec(15, 209), module, Scope::X_SCALE_PARAM, -2.0f, 8.0f, 0.0f));
  284. addParam(createParam<RoundSmallBlackKnob>(Vec(15, 263), module, Scope::X_POS_PARAM, -10.0f, 10.0f, 0.0f));
  285. addParam(createParam<RoundSmallBlackSnapKnob>(Vec(61, 209), module, Scope::Y_SCALE_PARAM, -2.0f, 8.0f, 0.0f));
  286. addParam(createParam<RoundSmallBlackKnob>(Vec(61, 263), module, Scope::Y_POS_PARAM, -10.0f, 10.0f, 0.0f));
  287. addParam(createParam<RoundSmallBlackKnob>(Vec(107, 209), module, Scope::TIME_PARAM, -6.0f, -16.0f, -14.0f));
  288. addParam(createParam<CKD6>(Vec(106, 262), module, Scope::LISSAJOUS_PARAM, 0.0f, 1.0f, 0.0f));
  289. addParam(createParam<RoundSmallBlackKnob>(Vec(153, 209), module, Scope::TRIG_PARAM, -10.0f, 10.0f, 0.0f));
  290. addParam(createParam<CKD6>(Vec(152, 262), module, Scope::EXTERNAL_PARAM, 0.0f, 1.0f, 0.0f));
  291. addInput(createInput<PJ301MPort>(Vec(17, 319), module, Scope::X_INPUT));
  292. addInput(createInput<PJ301MPort>(Vec(63, 319), module, Scope::Y_INPUT));
  293. addInput(createInput<PJ301MPort>(Vec(154, 319), module, Scope::TRIG_INPUT));
  294. addChild(createLight<SmallLight<GreenLight>>(Vec(104, 251), module, Scope::PLOT_LIGHT));
  295. addChild(createLight<SmallLight<GreenLight>>(Vec(104, 296), module, Scope::LISSAJOUS_LIGHT));
  296. addChild(createLight<SmallLight<GreenLight>>(Vec(150, 251), module, Scope::INTERNAL_LIGHT));
  297. addChild(createLight<SmallLight<GreenLight>>(Vec(150, 296), module, Scope::EXTERNAL_LIGHT));
  298. }