@mthines/reaper-mcp 0.80.0 → 0.80.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,13 @@
1
- // mcp_ui.jsfx-inc: the reaper-mcp @gfx toolkit, v1.2.0
1
+ // mcp_ui.jsfx-inc: the reaper-mcp @gfx toolkit, v1.3.0
2
2
  //
3
3
  // Canonical source. `write_jsfx` pastes the body below (everything after this header) into the
4
4
  // plugin's own @init section, so a generated plugin is ONE file with no dependency on this one,
5
5
  // the bridge or the MCP server. Edit it here, never in a generated plugin.
6
6
  //
7
7
  // Principles: fast (redraw only when something changed, no allocation in @gfx, coefficients
8
- // belong in the plugin's update()), simple (five widgets), progressive disclosure (a core view
8
+ // belong in the plugin's update()), simple (few widgets), progressive disclosure (a core view
9
9
  // that fills the window and never moves, an Advanced drawer that slides up over it).
10
+ // Every plugin shows its name, small and spaced, top-left (ui_name; a window under 150 px hides it).
10
11
  //
11
12
  // Gestures on a knob (the FabFilter/Serum convention): drag up/down, Shift-drag = 10x finer,
12
13
  // wheel (Shift = finer), double-click = type a value, Cmd-click / Ctrl-click / Alt-click = reset
@@ -15,13 +16,26 @@
15
16
  // "+" menu > "Send all keyboard input to plug-in" does Esc reach the plugin; then it closes the
16
17
  // value box, the About dialog or the drawer (and the window closes with its close button).
17
18
  //
18
- // Memory 0..1023 is reserved for the toolkit: the plugin's own buffers start at ui_mem.
19
+ // Memory map (indexes into the plugin's memory; the plugin's own buffers start at ui_mem):
20
+ // 0..1023 toolkit core: slider metadata (ui_min/ui_max/ui_def/ui_stp, 65 each), meters (ui_mtr)
21
+ // 1024..1151 ui_fg frequency graph trace, ui_gn (96) points in dB, log-spaced 20 Hz..20 kHz
22
+ // 1152..1167 ui_ge band edges: the slider index of each draggable handle (16)
23
+ // 1168..1199 ui_grm gain reduction per band in dB (8) -- slots 8..31 spare
24
+ // 1200..1327 ui_kb MIDI keyboard: velocity per note 0..127 (0 = off)
25
+ // 1328..1455 ui_fi the input trace for the before/after view (same 96 points as ui_fg)
26
+ // 1456..2047 spare for the toolkit
27
+ // 2048.. ui_mem the plugin
19
28
  // Every global starts with ui_. Sliders must be UNNAMED (slider1:0.5<0,1,0.01>Label): named
20
29
  // sliders do not keep their value in sliderN, which the toolkit reads through slider(idx).
21
30
  // EEL2: max()/min() take exactly two arguments; no 1e-9 style literals.
22
31
  //
32
+ // Redraw: ui_boot(0) draws only when a slider or the mouse changed; ui_boot(1) redraws every frame
33
+ // (a ui_meter needs it); ui_boot(2) is activity-driven: the graph feeds below (ui_gfeed, ui_gset,
34
+ // ui_gr, ui_glevel, ui_kkey) mark the UI dirty only when a value moved by a visible step, so a
35
+ // plugin with a spectrum, gain-reduction bars and a keyboard stays idle on silence.
36
+ //
23
37
  // Plugin side:
24
- // @init ... ui_boot(animated); // animated = 1 when the UI has a meter
38
+ // @init ... ui_boot(animated); // 0 static, 1 every frame (ui_meter), 2 activity-driven
25
39
  // @block ui_changed() ? update(); // a slider moved by a click or drag in @gfx
26
40
  // @gfx 380 220
27
41
  // ui_begin(core_rows, adv_rows) ? (
@@ -31,7 +45,8 @@
31
45
  // );
32
46
  // @serialize file_var(0, ui_adv_open); // added for you when the plugin has none
33
47
 
34
- ui_ver = 1.2;
48
+ ui_ver = 1.3;
49
+ ui_hm = 0; ui_ho = 0; // the header knobs' sliders (ui_header), 0 = none
35
50
 
36
51
  // Design tokens: the bridge status page's palette (apps/bridge-ui/src/styles.css). The accent is
37
52
  // the plugin's own: write_jsfx emits ui_theme($xRRGGBB) after this block (from the plugin's name,
@@ -53,13 +68,15 @@ ui_grain = 0.3; // grain strength: the tile is bg-level noise of +/-0.06,
53
68
  ui_img_bg = 120; ui_img_tile = 121;
54
69
 
55
70
  // Filled in by write_jsfx's metadata block (shown in the About dialog): never the user's request.
56
- strcpy(#ui_name, "Plugin"); strcpy(#ui_date, ""); strcpy(#ui_tkv, "1.2.0");
71
+ strcpy(#ui_name, "Plugin"); strcpy(#ui_date, ""); strcpy(#ui_tkv, "1.3.0");
57
72
  ui_pver = 0; ui_nctl = 0; ui_akind = 0; ui_example = 0;
58
73
 
59
74
  // Per-slider metadata (index 1..64), filled by write_jsfx from the slider lines.
60
75
  ui_min = 0; ui_max = 65; ui_def = 130; ui_stp = 195;
61
76
  ui_mtr = 400; // meter state: 2 slots per meter (level in dB, last time)
62
- ui_mem = 1024; // first free memory index for the plugin's own buffers
77
+ ui_gn = 96; // frequency graph trace points (log-spaced); at most 128
78
+ ui_fg = 1024; ui_ge = 1152; ui_grm = 1168; ui_kb = 1200; ui_fi = 1328; ui_gt = ui_fg; // graph widgets' memory, see the map above
79
+ ui_mem = 2048; // first free memory index for the plugin's own buffers
63
80
  // REAPER re-runs @init when playback starts (and on sample-rate changes): the UI state below
64
81
  // is set once, so pressing Play never closes the drawer or drops a drag. Plugin code must not
65
82
  // assign ui_adv_open either (only @serialize's file_var restores it).
@@ -69,6 +86,9 @@ ui_ready ? ( ui_hash = -1; ) : (
69
86
  ui_p = -1; // drawer position 0..1 (animated); -1 = not drawn yet
70
87
  ui_ed = 0; // slider whose value box is open (typing a value), 0 = none
71
88
  ui_about = 0; // About dialog open
89
+ ui_glv = -90; // live level for the transfer curve (-90 = none)
90
+ ui_fsel = 0; // selected band of the frequency graph (the plugin reads it)
91
+ ui_gi = 0; loop(128, ui_fg[ui_gi] = -120; ui_fi[ui_gi] = -120; ui_gi += 1;); ui_fcmp = 0; // an empty trace
72
92
  ui_ready = 1;
73
93
  );
74
94
 
@@ -278,54 +298,34 @@ function ui_keys() local(c) (
278
298
 
279
299
  // ---- layout -------------------------------------------------------------------------------
280
300
 
281
- // Returns 1 when the frame needs drawing. Nothing changed = nothing drawn, the old frame stays.
282
- // The core rows keep one fixed layout; the Advanced drawer slides over them and never resizes them.
283
- function ui_begin(core_rows, adv_rows) local(h, i, now, e, top) (
284
- ui_s = max(gfx_ext_retina, 1);
285
- ui_pad = 10 * ui_s; ui_lh = 14 * ui_s;
286
- ui_bar = adv_rows > 0 ? 28 * ui_s : 0;
287
- ui_down = mouse_cap & 1;
288
- ui_press = ui_down && !ui_was_down;
289
- ui_rel = !ui_down && ui_was_down;
290
- ui_rpress = (mouse_cap & 2) && !ui_was_r;
291
- ui_was_down = ui_down; ui_was_r = mouse_cap & 2;
292
- ui_keys();
293
- ui_modal = ui_ed > 0 || ui_about; // a value box or the About dialog takes every click
294
-
295
- // the drawer glides open/closed in about 0.15 s
296
- now = time_precise();
297
- ui_p < 0 ? ui_p = ui_adv_open;
298
- ui_p = ui_adv_open ? min(1, ui_p + (now - ui_lt) / 0.15) : max(0, ui_p - (now - ui_lt) / 0.15);
299
- ui_lt = now;
300
-
301
- h = gfx_w * 7 + gfx_h * 13 + mouse_x * 3 + mouse_y * 5 + mouse_cap * 11 + ui_adv_open * 17
302
- + mouse_wheel * 23 + floor(now * 2) * 19 + ui_p * 29 + ui_ed * 31 + ui_edv * 37 + ui_about * 41;
303
- i = 1; loop(64, h += slider(i) * (i + 0.37); i += 1;);
304
- ui_dirty = ui_animated || h != ui_hash || (ui_p > 0 && ui_p < 1);
305
- ui_hash = h;
306
-
307
- ui_dirty ? (
308
- ui_bg();
309
- gfx_setfont(1, "Arial", 12 * ui_s);
310
- gfx_setfont(2, "Arial", 9.5 * ui_s, 'b');
311
- gfx_setfont(3, "Arial", 10.5 * ui_s);
312
- gfx_setfont(4, "Arial", 15 * ui_s, 'b');
301
+ // The plugin's name, small and spaced capitals with an accent dot, top-left in the padding above the
302
+ // first row. Drawn first, so the Advanced drawer and its dimming sit over it like over the core view.
303
+ function ui_namedraw() local(x, y, i, c, n) (
304
+ gfx_w >= 150 * ui_s ? (
305
+ gfx_setfont(5);
306
+ ui_caps(#ui_name);
307
+ x = ui_pad; y = 4 * ui_s;
308
+ ui_c(ui_cAccent, 0.9); gfx_circle(x + 1.5 * ui_s, y + 4.5 * ui_s, 1.5 * ui_s, 1, 1);
309
+ x += 8 * ui_s;
310
+ ui_c(ui_cMuted, 0.85);
311
+ i = 0; n = strlen(#ui_txt);
312
+ loop(n,
313
+ x < ui_nlim ? (
314
+ c = str_getchar(#ui_txt, i);
315
+ c == 32 ? ( x += 5 * ui_s; ) : (
316
+ sprintf(#ui_tmp, "%c", c);
317
+ gfx_x = x; gfx_y = y; gfx_drawstr(#ui_tmp);
318
+ gfx_measurestr(#ui_tmp, ui_tw, ui_th);
319
+ x += ui_tw + 1.6 * ui_s;
320
+ );
321
+ );
322
+ i += 1;
323
+ );
313
324
  gfx_setfont(1);
314
- ui_core = 1;
315
- ui_blocked = adv_rows > 0 && (ui_adv_open || ui_p > 0);
316
- // core: the window minus the drawer's closed bar (or the footer line when there is no drawer)
317
- ui_rowh = (gfx_h - ui_pad - (adv_rows > 0 ? ui_bar + 4 * ui_s : ui_pad + (ui_credit ? 6 * ui_s : 0))) / max(1, core_rows);
318
- ui_y = ui_pad; ui_mi = 0;
319
- // the drawer: rows of a comfortable fixed height, leaving a strip of the core view above it
320
- ui_arowh = adv_rows > 0 ? min(100 * ui_s, max(60 * ui_s, (gfx_h - 40 * ui_s - ui_bar - ui_pad) / adv_rows)) : 0;
321
- top = max(8 * ui_s, gfx_h - ui_bar - adv_rows * ui_arowh - ui_pad);
322
- e = ui_p * ui_p * (3 - 2 * ui_p);
323
- ui_sy = gfx_h - ui_bar - (gfx_h - ui_bar - top) * e;
324
- ui_e = e;
325
325
  );
326
- ui_dirty;
327
326
  );
328
327
 
328
+
329
329
  // The footer credit: "reaper-mcp" in the bottom-right corner; a click opens the About dialog.
330
330
  // Hidden in a narrow window.
331
331
  function ui_footer(y, h) local(x, hit) (
@@ -437,25 +437,32 @@ function ui_aboutdraw() local(w, h, x, y, lh, nd, rows, bx, cy, hitx) (
437
437
  );
438
438
 
439
439
  function ui_end() (
440
+ ui_gany != ui_gcur ? ( gfx_setcursor(ui_gany == 2 ? 32645 : (ui_gany ? 32644 : 32512)); ui_gcur = ui_gany; ); // a band handle: resize cursor
441
+ ui_gany = 0;
440
442
  ui_bar == 0 ? ui_footer(gfx_h - ui_pad - 6 * ui_s, ui_pad + 6 * ui_s);
441
443
  ui_ed ? ui_popover() : ui_about ? ui_aboutdraw();
442
444
  mouse_wheel = 0;
443
445
  );
444
446
 
447
+ // The next ui_row is `span` rows tall (a graph wants more height than a row of knobs). Count it in
448
+ // ui_begin's core_rows: a 2-row graph and one row of knobs is ui_begin(3, ...).
449
+ function ui_tall(span) ( ui_rspan = max(1, floor(span)); );
450
+
445
451
  function ui_row(n) (
446
452
  ui_ci = 0; ui_n = max(1, n);
447
453
  ui_cw = (gfx_w - 2 * ui_pad) / ui_n;
448
- ui_ry0 = ui_y; ui_y += ui_rowh;
454
+ ui_rs = max(1, ui_rspan); ui_rspan = 1;
455
+ ui_ry0 = ui_y; ui_y += ui_rowh * ui_rs;
449
456
  );
450
457
 
451
458
  function ui_cell() (
452
- ui_rx = ui_pad + ui_ci * ui_cw; ui_ry = ui_ry0; ui_rw = ui_cw; ui_rh = ui_rowh;
459
+ ui_rx = ui_pad + ui_ci * ui_cw; ui_ry = ui_ry0; ui_rw = ui_cw; ui_rh = ui_rowh * max(1, ui_rs);
453
460
  ui_ci += 1;
454
461
  ui_hit = mouse_x >= ui_rx && mouse_x < ui_rx + ui_rw && mouse_y >= ui_ry && mouse_y < ui_ry + ui_rh
455
462
  && !(ui_core && ui_blocked) && !ui_modal;
456
463
  // every widget is one block centred in its cell: label, body (a knob's height), value.
457
464
  // The same for every cell of a row, so labels and values line up across widget types.
458
- ui_kr = max(4 * ui_s, min(64 * ui_s, min(ui_rw / 2 - 8 * ui_s, (ui_rh - 2 * ui_lh - 22 * ui_s) / 2)));
465
+ ui_kr = max(4 * ui_s, min(64 * ui_s, min(ui_rw / 2 - 8 * ui_s, (ui_rowh - 2 * ui_lh - 22 * ui_s) / 2))); // a tall cell keeps the knob size of a normal row
459
466
  ui_lby = ui_ry + (ui_rh - 2 * ui_lh - 12 * ui_s - 2 * ui_kr) / 2;
460
467
  ui_by = ui_lby + ui_lh + 6 * ui_s; ui_bh = 2 * ui_kr;
461
468
  );
@@ -503,6 +510,93 @@ function ui_ctl(idx, fmt, label, x, y) local(lo, hi, st, t, fine) (
503
510
  ui_active == idx && ui_rel ? ( slider_automate(2 ^ (idx - 1), 1); ui_active = 0; );
504
511
  );
505
512
 
513
+ // Mix and Output in the top right of every plugin that has them: call ui_header(mix_idx, out_idx) in @gfx
514
+ // before ui_begin (0 = this plugin has no such slider). Two small knobs with the usual gestures; the
515
+ // label shows the value while you hover or drag. Do not draw those two sliders as widgets as well.
516
+ function ui_header(m, o) ( ui_hm = max(0, floor(m)); ui_ho = max(0, floor(o)); );
517
+
518
+ // One small header knob whose right edge is xr; returns the x of the next one to its left.
519
+ function ui_hknob(idx, label, fmt, xr) local(r, cx, cy, lo, hi, t, t0, a0, a1, av, aa, th, hot, tw0) (
520
+ r = 8 * ui_s; cx = xr - r - 2 * ui_s; cy = 15 * ui_s;
521
+ ui_hit = !(ui_core && ui_blocked) && !ui_modal && mouse_x >= xr - 62 * ui_s && mouse_x < xr && mouse_y >= 4 * ui_s && mouse_y < 26 * ui_s;
522
+ ui_ctl(idx, fmt, label, cx, cy + 12 * ui_s);
523
+ lo = ui_min[idx]; hi = ui_max[idx];
524
+ t = min(1, max(0, (slider(idx) - lo) / (hi - lo)));
525
+ t0 = (lo < 0 && hi > 0) ? -lo / (hi - lo) : 0;
526
+ hot = ui_active == idx || ui_hit;
527
+ th = max(2 * ui_s, r * 0.22);
528
+ a0 = -2.35; a1 = 2.35; av = a0 + (a1 - a0) * t; aa = a0 + (a1 - a0) * t0;
529
+ ui_c(hot ? ui_cTrackHot : ui_cTrack, 1); ui_ring(cx, cy, r - th, r, a0, a1);
530
+ abs(t - t0) > 0.002 ? ( ui_c(ui_active == idx ? ui_cAccentAct : (hot ? ui_cAccentHi : ui_cAccent), 1); ui_ring(cx, cy, r - th, r, min(aa, av), max(aa, av)); );
531
+ ui_cm(ui_cPanel2, ui_cLine, hot ? 0.9 : 0.4); gfx_circle(cx, cy, r - th - ui_s, 1, 1);
532
+ ui_c(hot ? ui_cAccent : ui_cText, 1); gfx_circle(cx + sin(av) * (r - th - 3 * ui_s), cy - cos(av) * (r - th - 3 * ui_s), max(1, ui_s), 1, 1);
533
+ gfx_setfont(2);
534
+ hot ? ui_fmtv(fmt, slider(idx)) : ui_caps(label);
535
+ gfx_measurestr(#ui_txt, ui_tw, ui_th);
536
+ ui_c(ui_active == idx ? ui_cAccent : (hot ? ui_cText : ui_cMuted), 1);
537
+ gfx_x = cx - r - 5 * ui_s - ui_tw; gfx_y = cy - ui_th / 2; gfx_drawstr(#ui_txt);
538
+ gfx_setfont(1);
539
+ xr - 66 * ui_s;
540
+ );
541
+
542
+ // Returns 1 when the frame needs drawing. Nothing changed = nothing drawn, the old frame stays.
543
+ // The core rows keep one fixed layout; the Advanced drawer slides over them and never resizes them.
544
+ function ui_begin(core_rows, adv_rows) local(h, i, now, e, top) (
545
+ ui_s = max(gfx_ext_retina, 1);
546
+ ui_pad = 10 * ui_s; ui_lh = 14 * ui_s;
547
+ ui_bar = adv_rows > 0 ? 28 * ui_s : 0;
548
+ ui_down = mouse_cap & 1;
549
+ ui_press = ui_down && !ui_was_down;
550
+ ui_rel = !ui_down && ui_was_down;
551
+ ui_rpress = (mouse_cap & 2) && !ui_was_r;
552
+ ui_was_down = ui_down; ui_was_r = mouse_cap & 2;
553
+ ui_keys();
554
+ ui_modal = ui_ed > 0 || ui_about; // a value box or the About dialog takes every click
555
+
556
+ // the drawer glides open/closed in about 0.15 s
557
+ now = time_precise();
558
+ ui_p < 0 ? ui_p = ui_adv_open;
559
+ ui_p = ui_adv_open ? min(1, ui_p + (now - ui_lt) / 0.15) : max(0, ui_p - (now - ui_lt) / 0.15);
560
+ ui_lt = now;
561
+
562
+ h = gfx_w * 7 + gfx_h * 13 + mouse_x * 3 + mouse_y * 5 + mouse_cap * 11 + ui_adv_open * 17
563
+ + mouse_wheel * 23 + floor(now * 2) * 19 + ui_p * 29 + ui_ed * 31 + ui_edv * 37 + ui_about * 41
564
+ + ui_fh * 43 + ui_fh2 * 47 + ui_fh3 * 53 + ui_fh4 * 59 + ui_fsel * 61 + ui_fcmp * 67;
565
+ i = 1; loop(64, h += slider(i) * (i + 0.37); i += 1;);
566
+ ui_dirty = ui_animated == 1 || h != ui_hash || (ui_p > 0 && ui_p < 1);
567
+ ui_hash = h;
568
+
569
+ ui_dirty ? (
570
+ ui_bg();
571
+ gfx_setfont(1, "Arial", 12 * ui_s);
572
+ gfx_setfont(2, "Arial", 9.5 * ui_s, 'b');
573
+ gfx_setfont(3, "Arial", 10.5 * ui_s);
574
+ gfx_setfont(4, "Arial", 15 * ui_s, 'b');
575
+ gfx_setfont(5, "Arial", 8.5 * ui_s, 'b');
576
+ gfx_setfont(1);
577
+ ui_ytop = ui_pad + ((ui_hm || ui_ho) ? 20 : 8) * ui_s; // below the name (and the header knobs)
578
+ ui_nlim = gfx_w - ui_pad - ((ui_hm > 0) + (ui_ho > 0)) * 66 * ui_s;
579
+ ui_namedraw();
580
+ ui_core = 1;
581
+ ui_blocked = adv_rows > 0 && (ui_adv_open || ui_p > 0);
582
+ (ui_hm || ui_ho) && gfx_w >= 240 * ui_s ? (
583
+ i = gfx_w - ui_pad;
584
+ ui_ho ? i = ui_hknob(ui_ho, "Output", 1, i);
585
+ ui_hm ? i = ui_hknob(ui_hm, "Mix", 2, i);
586
+ );
587
+ // core: the window minus the drawer's closed bar (or the footer line when there is no drawer)
588
+ ui_rowh = (gfx_h - ui_ytop - (adv_rows > 0 ? ui_bar + 4 * ui_s : ui_pad + (ui_credit ? 6 * ui_s : 0))) / max(1, core_rows);
589
+ ui_y = ui_ytop; ui_mi = 0;
590
+ // the drawer: rows of a comfortable fixed height, leaving a strip of the core view above it
591
+ ui_arowh = adv_rows > 0 ? min(100 * ui_s, max(60 * ui_s, (gfx_h - 40 * ui_s - ui_bar - ui_pad) / adv_rows)) : 0;
592
+ top = max(8 * ui_s, gfx_h - ui_bar - adv_rows * ui_arowh - ui_pad);
593
+ e = ui_p * ui_p * (3 - 2 * ui_p);
594
+ ui_sy = gfx_h - ui_bar - (gfx_h - ui_bar - top) * e;
595
+ ui_e = e;
596
+ );
597
+ ui_dirty;
598
+ );
599
+
506
600
  // ---- widgets ------------------------------------------------------------------------------
507
601
 
508
602
  // A ring knob: dim track, one accent stroke for the value (from 0 when the range crosses it),
@@ -625,3 +719,398 @@ function ui_section(label) local(y, hit, k) (
625
719
  ui_y = y + ui_bar; ui_rowh = ui_arowh;
626
720
  ui_e > 0;
627
721
  );
722
+
723
+ // ---- graphs: feeds (call from @block, at most once per FFT or MIDI event) -------------------------
724
+ // Each feed keeps a running hash of its values rounded to a visible step, so with ui_boot(2) the UI
725
+ // redraws only when something on screen would look different.
726
+
727
+ // One trace point (dB). Internal: use ui_gset or ui_gfeed.
728
+ function ui_gput(i, v) local(q0, q1) (
729
+ q0 = floor((ui_gt[i] + 120) / 1.5); q1 = floor((v + 120) / 1.5);
730
+ ui_fh += (q1 - q0) * (1 + i % 7);
731
+ ui_gt[i] = v;
732
+ );
733
+
734
+ // Trace point i (0..ui_gn-1, log-spaced from 20 Hz to 20 kHz) in dB: any curve the plugin computes.
735
+ function ui_gset(i, db) ( ui_gput(min(ui_gn - 1, max(0, floor(i))), min(12, max(-120, db))); );
736
+
737
+ // Spectrum trace from n linear FFT magnitudes in buf (bin i = i * srate / 2 / n Hz; scale them so a
738
+ // full-scale sine reads about 1). Fast attack, slow release, so a held tone stays and silence fades.
739
+ function ui_gfeed(buf, n) local(p, b0, b1, m, j, db, old, nyq) (
740
+ nyq = srate / 2; p = 0;
741
+ loop(ui_gn,
742
+ b0 = min(n - 1, floor(20 * exp(p / ui_gn * 6.907755) / nyq * n));
743
+ b1 = min(n - 1, max(b0, floor(20 * exp((p + 1) / ui_gn * 6.907755) / nyq * n) - 1));
744
+ m = 0; j = b0;
745
+ loop(b1 - b0 + 1, m = max(m, buf[j]); j += 1;);
746
+ // a point narrower than one bin: lean between the two bins around its centre (no stairs at the low end)
747
+ b1 == b0 && b0 < n - 1 ? (
748
+ j = 20 * exp((p + 0.5) / ui_gn * 6.907755) / nyq * n - b0;
749
+ m = buf[b0] * (1 - min(1, max(0, j))) + buf[b0 + 1] * min(1, max(0, j));
750
+ );
751
+ db = 20 * log10(max(m, 0.000001));
752
+ old = ui_gt[p];
753
+ ui_gput(p, db > old ? old + (db - old) * 0.7 : old + (db - old) * 0.12);
754
+ p += 1;
755
+ );
756
+ );
757
+
758
+ // The same for the input, drawn as a second, lighter line while ui_gshow(1): the before/after view. Feed it
759
+ // only while it is shown (a second FFT costs).
760
+ function ui_gfeed2(buf, n) ( ui_gt = ui_fi; ui_gfeed(buf, n); ui_gt = ui_fg; );
761
+ // Show (1) or hide (0) the input trace; call it in @gfx before ui_freqgraph with the compare toggle's slider.
762
+ function ui_gshow(on) ( ui_fcmp = on > 0.5; );
763
+
764
+ // Gain reduction of band k (0..7) in dB, as a positive number (6 = 6 dB of reduction).
765
+ function ui_gr(k, db) local(q0, q1) (
766
+ k = min(7, max(0, floor(k))); db = min(48, max(0, db));
767
+ q0 = floor(ui_grm[k] / 0.5); q1 = floor(db / 0.5);
768
+ ui_fh2 += (q1 - q0) * (1 + k);
769
+ ui_grm[k] = db;
770
+ );
771
+
772
+ // The live input level in dB for the transfer curve's dot (-90 or less hides the dot).
773
+ function ui_glevel(db) (
774
+ ui_glv = min(6, max(-90, db));
775
+ ui_fh4 = floor((ui_glv + 90) / 1);
776
+ );
777
+
778
+ // A MIDI note on (vel 1..127) or off (vel 0), from @block's midirecv loop.
779
+ function ui_kkey(note, vel) local(n, q0, q1) (
780
+ n = min(127, max(0, floor(note)));
781
+ q0 = ui_kb[n] > 0; q1 = vel > 0;
782
+ ui_fh3 += (q1 - q0) * (n + 1);
783
+ ui_kb[n] = q1 ? min(127, max(1, vel)) : 0;
784
+ );
785
+
786
+ // ---- frequency graph ----------------------------------------------------------------------------
787
+
788
+ // x of a frequency and the frequency of an x on the current graph (log axis, 20 Hz to 20 kHz).
789
+ function ui_fxof(f) ( ui_gx + ui_gw * log(max(f, 20) / 20) / 6.907755; );
790
+ function ui_fof(x) ( 20 * exp(min(1, max(0, (x - ui_gx) / ui_gw)) * 6.907755); );
791
+ function ui_gyof(db) ( ui_gy + ui_gh * min(1, max(0, 0 - db / 72)); );
792
+
793
+ // A log frequency graph filling the current cell: gridlines and labels, the fed trace (ui_gfeed /
794
+ // ui_gset) and the selected band. A click between handles selects that band (ui_fsel, 0 = lowest).
795
+ // Draw its draggable band edges right after it with ui_fhandle, one per slider in Hz, in ascending order.
796
+ function ui_freqgraph(label) local(px, py, pw, ph, i, f, m, mult, x, y, y0, x0, hot, near, ex, sel, lo, hi, a, b, n, gb) (
797
+ ui_cell();
798
+ ui_gpn = ui_gnb; ui_gk = 0;
799
+ px = ui_rx + 2 * ui_s; py = ui_ry + 4 * ui_s; pw = ui_rw - 4 * ui_s; ph = ui_rh - 8 * ui_s;
800
+ ui_gx = px + 8 * ui_s; ui_gw = pw - 16 * ui_s; ui_gy = py + 6 * ui_s; ui_gh = ph - 22 * ui_s;
801
+ ui_gh > 20 * ui_s && ui_gw > 40 * ui_s ? (
802
+ ui_cm(ui_cPanel2, ui_cLine, 0.3); ui_rrect(px, py, pw, ph, 8 * ui_s);
803
+ ui_c(ui_cLine, 1); gfx_roundrect(px, py, pw - 1, ph - 1, 8 * ui_s, 1);
804
+ hot = mouse_x >= ui_gx && mouse_x < ui_gx + ui_gw && mouse_y >= ui_gy && mouse_y < ui_gy + ui_gh
805
+ && !(ui_core && ui_blocked) && !ui_modal;
806
+ gb = ui_gy + ui_gh;
807
+
808
+ // the selected band
809
+ n = min(ui_gpn, 15);
810
+ sel = min(ui_fsel, n);
811
+ lo = sel == 0 ? 20 : slider(ui_ge[sel - 1]);
812
+ hi = sel == n ? 20000 : slider(ui_ge[sel]);
813
+ a = ui_fxof(lo); b = ui_fxof(hi);
814
+ ui_c(ui_cAccent, 0.09); gfx_rect(a, ui_gy, max(1, b - a), ui_gh);
815
+ ui_c(ui_cAccent, 0.55); gfx_rect(a, ui_gy, max(1, b - a), max(1, ui_s));
816
+
817
+ // horizontal dB lines every 12 dB
818
+ i = 1; loop(5, y = ui_gyof(0 - 12 * i); ui_c(ui_cMuted, 0.1); gfx_rect(ui_gx, y, ui_gw, max(1, ui_s)); i += 1;);
819
+ // vertical frequency lines and labels: 50 100 200 500 1k 2k 5k 10k
820
+ gfx_setfont(2);
821
+ i = 0;
822
+ loop(8,
823
+ m = (i - 1) % 3; mult = m == 0 ? 1 : (m == 1 ? 2 : 5);
824
+ f = i == 0 ? 50 : 100 * 10 ^ floor((i - 1) / 3) * mult;
825
+ x = ui_fxof(f);
826
+ ui_c(ui_cMuted, (f == 100 || f == 1000 || f == 10000) ? 0.2 : 0.09); gfx_rect(x, ui_gy, max(1, ui_s), ui_gh);
827
+ (f == 100 || f == 1000 || f == 10000 || ui_gw > 300 * ui_s) ? (
828
+ f >= 1000 ? sprintf(#ui_txt, "%dk", f / 1000) : sprintf(#ui_txt, "%d", f);
829
+ gfx_measurestr(#ui_txt, ui_tw, ui_th);
830
+ ui_c(ui_cMuted, 0.75); gfx_x = x - ui_tw / 2; gfx_y = gb + 4 * ui_s; gfx_drawstr(#ui_txt);
831
+ );
832
+ i += 1;
833
+ );
834
+ ui_caps(label); gfx_measurestr(#ui_txt, ui_tw, ui_th);
835
+ ui_c(ui_cMuted, 0.5); gfx_x = ui_gx + ui_gw - ui_tw - 2 * ui_s; gfx_y = ui_gy + ui_gh - ui_th - 3 * ui_s; gfx_drawstr(#ui_txt);
836
+ gfx_setfont(1);
837
+
838
+ // the trace: a filled area under a line
839
+ i = 1; x0 = ui_gx; y0 = ui_gyof(ui_fg[0]);
840
+ loop(ui_gn - 1,
841
+ x = ui_gx + ui_gw * i / (ui_gn - 1); y = ui_gyof(ui_fg[i]);
842
+ (y0 < gb - 0.5 || y < gb - 0.5) ? (
843
+ ui_c(ui_cAccent, 0.16);
844
+ a = floor(x0); b = floor(x); y = (y0 + y) / 2;
845
+ gfx_rect(a, y, max(1, b - a), gb - y);
846
+ y = ui_gyof(ui_fg[i]);
847
+ ui_c(ui_cAccent, 0.85); gfx_line(x0, y0, x, y, 1);
848
+ );
849
+ x0 = x; y0 = y; i += 1;
850
+ );
851
+
852
+ // the input trace over it, with a legend (before/after)
853
+ ui_fcmp ? (
854
+ i = 1; x0 = ui_gx; y0 = ui_gyof(ui_fi[0]);
855
+ loop(ui_gn - 1,
856
+ x = ui_gx + ui_gw * i / (ui_gn - 1); y = ui_gyof(ui_fi[i]);
857
+ (y0 < gb - 0.5 || y < gb - 0.5) ? ( ui_c(ui_cText, 0.6); gfx_line(x0, y0, x, y, 1); );
858
+ x0 = x; y0 = y; i += 1;
859
+ );
860
+ gfx_setfont(2);
861
+ ui_caps("Output"); gfx_measurestr(#ui_txt, ui_tw, ui_th); a = ui_tw;
862
+ ui_c(ui_cAccent, 1); gfx_x = ui_gx + ui_gw - a - 4 * ui_s; gfx_y = ui_gy + 34 * ui_s; gfx_drawstr(#ui_txt);
863
+ ui_caps("Input"); gfx_measurestr(#ui_txt, ui_tw, ui_th);
864
+ ui_c(ui_cText, 0.7); gfx_x = ui_gx + ui_gw - a - ui_tw - 16 * ui_s; gfx_y = ui_gy + 34 * ui_s; gfx_drawstr(#ui_txt);
865
+ gfx_setfont(1);
866
+ );
867
+
868
+ // a press between handles picks that band
869
+ near = 0; i = 0;
870
+ loop(n, ex = ui_fxof(slider(ui_ge[i])); abs(mouse_x - ex) < 9 * ui_s ? near = 1; i += 1;);
871
+ hot && ui_press && !near ? (
872
+ f = ui_fof(mouse_x); sel = 0; i = 0;
873
+ loop(n, slider(ui_ge[i]) < f ? sel += 1; i += 1;);
874
+ ui_fsel = sel;
875
+ );
876
+ );
877
+ hot;
878
+ );
879
+
880
+ // The grip of band edge k at f: a pill with the frequency, set as ui_pbx / ui_pby / ui_ppw (and the
881
+ // text in #ui_txt, font 2 measured).
882
+ function ui_fpill(f, k) (
883
+ gfx_setfont(2); ui_fmtv(3, f); gfx_measurestr(#ui_txt, ui_tw, ui_th);
884
+ ui_ppw = ui_tw + 12 * ui_s;
885
+ ui_pbx = min(ui_gx + ui_gw - ui_ppw, max(ui_gx, ui_fxof(f) - ui_ppw / 2));
886
+ ui_pby = ui_gy + 3 * ui_s + (k % 2) * 17 * ui_s;
887
+ );
888
+
889
+ // One draggable band edge, bound to slider idx (Hz). Drag = move it (kept between its neighbours),
890
+ // wheel = nudge, double-click = type a value, Cmd/Ctrl/Alt-click = reset. Call right after ui_freqgraph.
891
+ function ui_fhandle(idx, label) local(k, f, x, hit, t, lo, hi, nf, hot, drag) (
892
+ k = ui_gk; ui_ge[k] = idx; ui_gk += 1; ui_gnb = ui_gk;
893
+ ui_gk > ui_gpn ? ui_hash = -1; // the graph learns its edges one frame late: draw once more
894
+ f = slider(idx); x = ui_fxof(f);
895
+ ui_fpill(f, k);
896
+ hit = !(ui_core && ui_blocked) && !ui_modal && (
897
+ (abs(mouse_x - x) < 9 * ui_s && mouse_y >= ui_gy && mouse_y < ui_gy + ui_gh) ||
898
+ (mouse_x >= ui_pbx && mouse_x < ui_pbx + ui_ppw && mouse_y >= ui_pby && mouse_y < ui_pby + 14 * ui_s));
899
+ hit && ui_press ? (
900
+ t = time_precise();
901
+ (mouse_cap & 52) ? ui_reset(idx) :
902
+ (ui_lci == idx && t - ui_lct < 0.45) ? ui_edopen(idx, 3, label, x, ui_gy + ui_gh / 2) :
903
+ ( ui_active = idx; ui_gdx = mouse_x - x; ui_lci = idx; ui_lct = t; ui_press = 0; );
904
+ );
905
+ ui_active == idx && ui_down ? (
906
+ lo = k > 0 ? slider(ui_ge[k - 1]) * 1.1 : ui_min[idx];
907
+ hi = k + 1 < ui_gpn ? slider(ui_ge[k + 1]) / 1.1 : ui_max[idx];
908
+ nf = ui_fof(mouse_x - ui_gdx);
909
+ ui_set(idx, ui_q(idx, min(hi, max(lo, nf))));
910
+ );
911
+ hit && mouse_wheel != 0 ? ui_set(idx, ui_q(idx, slider(idx) * (mouse_wheel > 0 ? 1.04 : 0.96)));
912
+ ui_active == idx && ui_rel ? ( slider_automate(2 ^ (idx - 1), 1); ui_active = 0; );
913
+ drag = ui_active == idx; hot = hit || drag;
914
+ hot ? ui_gany = 1;
915
+ // the line down to the axis, then the pill on top of it
916
+ f = slider(idx); x = ui_fxof(f); ui_fpill(f, k);
917
+ ui_c(drag ? ui_cAccentAct : (hot ? ui_cAccentHi : ui_cAccent), hot ? 0.9 : 0.6);
918
+ gfx_rect(x, ui_pby + 14 * ui_s, max(1, ui_s), ui_gy + ui_gh - ui_pby - 14 * ui_s);
919
+ ui_cm(ui_cPanel2, ui_cLine, hot ? 0.3 : 0.6); ui_rrect(ui_pbx, ui_pby, ui_ppw, 14 * ui_s, 7 * ui_s);
920
+ ui_c(hot ? ui_cAccent : ui_cLine, 1); gfx_roundrect(ui_pbx, ui_pby, ui_ppw - 1, 14 * ui_s - 1, 7 * ui_s, 1);
921
+ ui_c(drag ? ui_cAccent : ui_cText, 1);
922
+ gfx_x = ui_pbx + 6 * ui_s; gfx_y = ui_pby + (14 * ui_s - ui_th) / 2; gfx_drawstr(#ui_txt);
923
+ gfx_setfont(1);
924
+ );
925
+
926
+ // ---- per-band thresholds ---------------------------------------------------------------------------
927
+
928
+ // A threshold line across each band of the frequency graph, one slider per band (thr_first, thr_first + 1, ...,
929
+ // in dB, -60..0 on the graph's dB axis) and one ratio slider per band (ratio_first, ...). Hover a band
930
+ // and drag up or down to move its threshold, wheel to change its ratio, a click selects it (ui_fsel);
931
+ // Shift-drag moves the band's gain instead (gain_first, ...), double-click types a threshold, Cmd/Ctrl/Alt-click
932
+ // resets it (with Shift, the gain). n is the number of bands (the handles + 1).
933
+ // Call it after ui_freqgraph and its ui_fhandles.
934
+ function ui_fthresh(thr_first, ratio_first, gain_first, n) local(k, a, b, x, y, idx, rdx, gdx, lo, hi, hot, drag, gdrag, near, i, sel, t, sh) (
935
+ n = max(1, min(8, floor(n)));
936
+ k = 0;
937
+ loop(n,
938
+ idx = thr_first + k; rdx = ratio_first + k; gdx = gain_first + k;
939
+ lo = k == 0 ? 20 : slider(ui_ge[k - 1]); hi = k == n - 1 ? 20000 : slider(ui_ge[k]);
940
+ a = ui_fxof(lo); b = ui_fxof(hi);
941
+ near = 0; i = 0;
942
+ loop(n - 1, abs(mouse_x - ui_fxof(slider(ui_ge[i]))) < 9 * ui_s ? near = 1; i += 1;);
943
+ hot = !near && !(ui_core && ui_blocked) && !ui_modal && ui_active == 0 && mouse_x >= a && mouse_x < b
944
+ && mouse_y >= ui_gy && mouse_y < ui_gy + ui_gh;
945
+ hot && ui_press ? (
946
+ ui_fsel = k; t = time_precise();
947
+ sh = mouse_cap & 8; // Shift: this drag moves the band's gain instead of its threshold
948
+ (mouse_cap & 52) ? ui_reset(sh ? gdx : idx) :
949
+ (ui_lci == idx && t - ui_lct < 0.45) ? ui_edopen(idx, 1, "Threshold", (a + b) / 2, ui_gyof(slider(idx))) :
950
+ ( ui_active = sh ? gdx : idx; ui_ftd = 1; ui_ly = mouse_y; ui_acc = slider(ui_active); ui_lci = idx; ui_lct = t; );
951
+ ui_press = 0;
952
+ );
953
+ ui_ftd && ui_active == idx && ui_down ? ( // only a drag this widget started: a band knob on the same slider drags itself
954
+ ui_acc = min(ui_max[idx], max(ui_min[idx], ui_acc - (mouse_y - ui_ly) * 72 / ui_gh));
955
+ ui_ly = mouse_y;
956
+ ui_set(idx, ui_q(idx, ui_acc));
957
+ );
958
+ hot && mouse_wheel != 0 ? ui_set(rdx, ui_q(rdx, slider(rdx) + sign(mouse_wheel) * 0.5));
959
+ ui_ftd && ui_active == gdx && ui_down ? (
960
+ ui_acc = min(ui_max[gdx], max(ui_min[gdx], ui_acc - (mouse_y - ui_ly) * 0.15));
961
+ ui_ly = mouse_y;
962
+ ui_set(gdx, ui_q(gdx, ui_acc));
963
+ );
964
+ ui_active == idx && ui_rel ? ( slider_automate(2 ^ (idx - 1), 1); ui_active = 0; ui_ftd = 0; );
965
+ ui_active == gdx && ui_rel ? ( slider_automate(2 ^ (gdx - 1), 1); ui_active = 0; ui_ftd = 0; );
966
+ gdrag = ui_active == gdx; drag = ui_active == idx || gdrag;
967
+ (hot || drag) ? ui_gany = 2;
968
+ sel = k == ui_fsel;
969
+ // the band's wash when hovered, then its threshold line (bright when selected)
970
+ (hot || drag) ? ( ui_c(ui_cAccent, 0.07); gfx_rect(a, ui_gy, max(1, b - a), ui_gh); );
971
+ y = ui_gyof(slider(idx));
972
+ ui_c(drag ? ui_cAccentAct : (sel || hot ? ui_cAccentHi : ui_cAccent), drag ? 1 : (sel ? 0.95 : (hot ? 0.8 : 0.4)));
973
+ gfx_rect(a + 2 * ui_s, y, max(1, b - a - 4 * ui_s), max(1, sel ? 2 * ui_s : ui_s));
974
+ (sel || hot || drag) && b - a > 60 * ui_s ? (
975
+ gfx_setfont(2);
976
+ (gdrag || (hot && (mouse_cap & 8) && !drag)) ? sprintf(#ui_txt, "%+.1f dB gain", slider(gdx)) :
977
+ sprintf(#ui_txt, "%.0f dB %.1f:1", slider(idx), slider(rdx));
978
+ gfx_measurestr(#ui_txt, ui_tw, ui_th);
979
+ ui_c(drag || sel ? ui_cAccent : ui_cText, 1);
980
+ gfx_x = a + 6 * ui_s; gfx_y = y - ui_th - 3 * ui_s; gfx_drawstr(#ui_txt);
981
+ gfx_setfont(1);
982
+ );
983
+ k += 1;
984
+ );
985
+ );
986
+
987
+ // A knob for the selected band's slider: slider first_idx + ui_fsel of n, its label prefixed with the
988
+ // band's name (Low / Mid / High for 3 bands, Low / High for 2, else Band N).
989
+ function ui_bandknob(first_idx, n, label, fmt) local(k) (
990
+ n = max(1, floor(n)); k = max(0, min(floor(ui_fsel), n - 1));
991
+ n == 3 ? strcpy(#ui_bn, k == 0 ? "Low" : (k == 1 ? "Mid" : "High")) :
992
+ n == 2 ? strcpy(#ui_bn, k == 0 ? "Low" : "High") : sprintf(#ui_bn, "Band %d", k + 1);
993
+ sprintf(#ui_bl, "%s %s", #ui_bn, label);
994
+ ui_knob(first_idx + k, #ui_bl, fmt);
995
+ );
996
+
997
+ // ---- gain reduction ---------------------------------------------------------------------------------
998
+
999
+ // n bars (1..8) side by side, one per band, hanging from the top: the more reduction, the longer the
1000
+ // bar (0..24 dB), amber from 6 dB, red from 12. Feed them with ui_gr(k, dB). A click on a bar selects
1001
+ // that band like a click on the graph does (ui_fsel).
1002
+ function ui_grmeter(n, label) local(k, bw, gap, gw, x0, x, h, v, fh, sw, bx, hit, selected) (
1003
+ ui_cell();
1004
+ n = max(1, min(8, floor(n)));
1005
+ bw = 10 * ui_s; gap = max(2 * ui_s, min(8 * ui_s, (ui_rw - 8 * ui_s - n * bw) / max(1, n - 1)));
1006
+ gw = n * bw + (n - 1) * gap; x0 = ui_rx + (ui_rw - gw) / 2; h = ui_bh;
1007
+ gfx_setfont(2);
1008
+ sw = (gw + gap) / n;
1009
+ k = 0;
1010
+ loop(n,
1011
+ bx = x0 + k * (bw + gap);
1012
+ hit = ui_hit && mouse_x >= bx - gap / 2 && mouse_x < bx + bw + gap / 2;
1013
+ hit && ui_press ? ( ui_fsel = k; ui_press = 0; );
1014
+ selected = k == ui_fsel;
1015
+ v = ui_grm[k]; fh = h * min(1, v / 24);
1016
+ ui_cm(ui_cPanel2, ui_cLine, 0.5); ui_rrect(bx, ui_by, bw, h, bw / 2);
1017
+ selected ? ( ui_c(ui_cAccent, 0.6); gfx_roundrect(bx - ui_s, ui_by - ui_s, bw + 2 * ui_s - 1, h + 2 * ui_s - 1, bw / 2 + ui_s, 1); );
1018
+ fh > 1 ? (
1019
+ ui_c(v >= 12 ? ui_cBad : (v >= 6 ? ui_cWarn : ui_cAccent), 1);
1020
+ ui_rrect(bx, ui_by, bw, fh, min(bw / 2, fh / 2));
1021
+ );
1022
+ sw >= 30 * ui_s ? sprintf(#ui_txt, "%.1f", v) : sprintf(#ui_txt, "%.0f", v);
1023
+ gfx_measurestr(#ui_txt, ui_tw, ui_th);
1024
+ ui_c(selected ? ui_cAccent : ui_cMuted, 1);
1025
+ gfx_x = bx + (bw - ui_tw) / 2; gfx_y = ui_by + ui_bh + 6 * ui_s + (ui_lh - ui_th) / 2; gfx_drawstr(#ui_txt);
1026
+ k += 1;
1027
+ );
1028
+ gfx_setfont(1);
1029
+ ui_label(label, 0);
1030
+ );
1031
+
1032
+ // A compressor's transfer curve, input dB across and output dB up (-60..0), the 1:1 line behind it,
1033
+ // the threshold marked on the axis, and a dot at the live input level (ui_glevel). thr_idx and
1034
+ // ratio_idx are the first threshold (dB) and ratio sliders; with a frequency graph it shows the selected
1035
+ // band's (slider + ui_fsel), knee_db the soft-knee width (0 = hard).
1036
+ function ui_tcurve(x, t, r, w) local(d) (
1037
+ d = x - t;
1038
+ (w > 0 && d > 0 - w / 2 && d < w / 2) ? x + (1 / r - 1) * (d + w / 2) * (d + w / 2) / (2 * w) :
1039
+ d <= 0 ? x : t + d / r;
1040
+ );
1041
+ function ui_transfer(thr_idx, ratio_idx, knee_db) local(k, t, r, bs, bx, by, i, x, y, x0, y0, lv, ly, tx) (
1042
+ ui_cell();
1043
+ k = max(0, min(floor(ui_fsel), ui_gnb)); t = slider(thr_idx + k); r = max(1, slider(ratio_idx + k));
1044
+ bs = min(ui_rw - 16 * ui_s, ui_rh - 26 * ui_s);
1045
+ bs > 30 * ui_s ? (
1046
+ bx = ui_rx + (ui_rw - bs) / 2; by = ui_ry + 6 * ui_s;
1047
+ ui_cm(ui_cPanel2, ui_cLine, 0.3); ui_rrect(bx, by, bs, bs, 8 * ui_s);
1048
+ i = 1; loop(4, x = bx + bs * i / 5; ui_c(ui_cMuted, 0.1); gfx_rect(x, by, max(1, ui_s), bs); gfx_rect(bx, by + bs * i / 5, bs, max(1, ui_s)); i += 1;);
1049
+ ui_c(ui_cMuted, 0.3); gfx_line(bx, by + bs, bx + bs, by, 1);
1050
+ tx = bx + bs * (min(0, max(-60, t)) + 60) / 60;
1051
+ ui_c(ui_cWarn, 0.35); gfx_rect(tx, by, max(1, ui_s), bs);
1052
+ ui_c(ui_cLine, 1); gfx_roundrect(bx, by, bs - 1, bs - 1, 8 * ui_s, 1);
1053
+ // the curve
1054
+ x0 = bx; y0 = by + bs * (0 - ui_tcurve(-60, t, r, knee_db)) / 60; i = 1;
1055
+ loop(48,
1056
+ x = bx + bs * i / 48; y = by + bs * (0 - max(-60, ui_tcurve(-60 + 60 * i / 48, t, r, knee_db))) / 60;
1057
+ ui_c(ui_cAccent, 1); gfx_line(x0, y0, x, y, 1); gfx_line(x0, y0 + 1, x, y + 1, 1);
1058
+ x0 = x; y0 = y; i += 1;
1059
+ );
1060
+ // the live level
1061
+ ui_glv > -60 ? (
1062
+ lv = min(0, ui_glv); x = bx + bs * (lv + 60) / 60;
1063
+ ly = by + bs * (0 - max(-60, ui_tcurve(lv, t, r, knee_db))) / 60;
1064
+ ui_c(ui_cAccent, 0.3); gfx_rect(x, ly, max(1, ui_s), by + bs - ly);
1065
+ ui_c(ui_cAccent, 0.25); gfx_circle(x, ly, 6 * ui_s, 1, 1);
1066
+ ui_c(ui_cAccentAct, 1); gfx_circle(x, ly, 3.5 * ui_s, 1, 1);
1067
+ );
1068
+ gfx_setfont(2);
1069
+ sprintf(#ui_txt, "%.0f dB %.1f:1", t, r);
1070
+ gfx_measurestr(#ui_txt, ui_tw, ui_th); ui_c(ui_cMuted, 1);
1071
+ gfx_x = ui_rx + (ui_rw - ui_tw) / 2; gfx_y = by + bs + 6 * ui_s; gfx_drawstr(#ui_txt);
1072
+ gfx_setfont(1);
1073
+ );
1074
+ );
1075
+
1076
+ // ---- MIDI keyboard ------------------------------------------------------------------------------
1077
+
1078
+ // Piano keys lo..hi (MIDI notes; start and end on white keys: C, D, E, F, G, A, B), the notes fed with
1079
+ // ui_kkey lit in the accent. Display only: a JSFX cannot send MIDI from @gfx.
1080
+ function ui_keyboard(lo, hi, label) local(nw, n, pc, wi, ww, kx, ky, kw, kh, bk, x, v, k) (
1081
+ ui_cell();
1082
+ nw = 0; n = lo;
1083
+ loop(hi - lo + 1, ((2 ^ (n % 12)) & 1354) ? 0 : nw += 1; n += 1;);
1084
+ kw = ui_rw - 8 * ui_s; kx = ui_rx + 4 * ui_s; ky = ui_ry + 16 * ui_s;
1085
+ kh = min(ui_rh - 20 * ui_s, 90 * ui_s);
1086
+ nw > 0 && kh > 16 * ui_s ? (
1087
+ ww = kw / nw;
1088
+ gfx_setfont(2); ui_caps(label);
1089
+ ui_c(ui_cMuted, 1); gfx_x = kx; gfx_y = ui_ry + 2 * ui_s; gfx_drawstr(#ui_txt);
1090
+ gfx_setfont(1);
1091
+ // white keys
1092
+ wi = 0; n = lo;
1093
+ loop(hi - lo + 1,
1094
+ ((2 ^ (n % 12)) & 1354) ? 0 : (
1095
+ x = kx + wi * ww; v = ui_kb[n] > 0;
1096
+ v ? ui_c(ui_cAccent, 1) : ui_cm(ui_cPanel2, ui_cText, 0.82);
1097
+ ui_rrect(x + 0.5 * ui_s, ky, ww - ui_s, kh, 3 * ui_s);
1098
+ wi += 1;
1099
+ );
1100
+ n += 1;
1101
+ );
1102
+ // black keys on top, centred on the boundary after the white key before them
1103
+ wi = 0; n = lo;
1104
+ loop(hi - lo + 1,
1105
+ ((2 ^ (n % 12)) & 1354) ? (
1106
+ wi > 0 ? (
1107
+ bk = ww * 0.6; x = kx + wi * ww - bk / 2; v = ui_kb[n] > 0;
1108
+ ui_c(ui_cLine, 1); ui_rrect(x - ui_s, ky, bk + 2 * ui_s, kh * 0.62 + ui_s, 3 * ui_s);
1109
+ v ? ui_cm(ui_cAccent, ui_cBg, 0.35) : ui_cm(ui_cBg, ui_cPanel2, 0.5);
1110
+ ui_rrect(x, ky, bk, kh * 0.62, 3 * ui_s);
1111
+ );
1112
+ ) : wi += 1;
1113
+ n += 1;
1114
+ );
1115
+ );
1116
+ );