desc: Overstratum Tape Machine
author: Overstratum Audio
about:
  Tape machine based on the DSP of Airwindows "ToTape9" by Chris Johnson
  (https://www.airwindows.com/totape9/), ported directly from the original MIT-licensed
  source code (github.com/airwindows/airwindows, plugins/LinuxVST/src/ToTape9).
  GUI, auto calibration, drive/amount control architecture and level compensation
  by Overstratum Audio.

// ============================================================================================
//  Airwindows ToTape9 - license of the DSP core this plugin is based on:
//
//  MIT License
//  Copyright (c) 2018 Chris Johnson
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in all
//  copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//  SOFTWARE.
// ============================================================================================

// --- hidden sliders (indices and defaults are part of saved sessions: do not change) ---
slider1:0<0,1,0.01>-Drive
slider2:1<0,1,0.01>-Amount
slider3:0.5<0,1,0.01>-Tilt
slider4:0.5<0,1,0.01>-Shape
slider5:0<0,1,0.01>-Flutter
slider6:0.5<0,1,0.01>-Flutter Speed
slider7:0.5<0,1,0.01>-Bias
slider8:0.5<0,1,0.01>-Head Bump
slider9:68.75<25,200,0.01>-Head Frequency
slider10:0<-60,60,0.1>-Internal Trim (dB)
slider11:0<0,1,1{Off,On}>-Bypass Hidden
slider12:0<0,1,1{Off,On}>-Delta Hidden
slider13:0<-24,24,0.1>-Output Gain (dB)
// Auto Gain: default ON. Sessions saved before this slider existed load with the default -> ON,
// which keeps their sound (at Drive 0 identical; above, the level bump of the old version is evened out).
slider14:1<0,1,1{Off,On}>-Auto Gain

in_pin:left input
in_pin:right input
out_pin:left output
out_pin:right output

@init
gfx_clear = -1;

// ---------------- memory ----------------
dsp_mem_top = 0;
function dsp_alloc(n) local(p) ( p = dsp_mem_top; dsp_mem_top += n; p; );

// ToTape9 state (array names follow the Airwindows source)
dsp_dL = dsp_alloc(1002); dsp_dR = dsp_alloc(1002);          // flutter delay lines
dsp_gslew = dsp_alloc(27);                                   // bias: 9 x [prevSampL, prevSampR, threshold]
dsp_hdbA = dsp_alloc(11); dsp_hdbB = dsp_alloc(11);          // head bump bandpasses (layout below)
dsp_avg32L = dsp_alloc(33); dsp_avg32R = dsp_alloc(33); dsp_avg16L = dsp_alloc(17); dsp_avg16R = dsp_alloc(17);
dsp_avg8L = dsp_alloc(9); dsp_avg8R = dsp_alloc(9); dsp_avg4L = dsp_alloc(5); dsp_avg4R = dsp_alloc(5);
dsp_avg2L = dsp_alloc(3); dsp_avg2R = dsp_alloc(3);
dsp_post32L = dsp_alloc(33); dsp_post32R = dsp_alloc(33); dsp_post16L = dsp_alloc(17); dsp_post16R = dsp_alloc(17);
dsp_post8L = dsp_alloc(9); dsp_post8R = dsp_alloc(9); dsp_post4L = dsp_alloc(5); dsp_post4R = dsp_alloc(5);
dsp_post2L = dsp_alloc(3); dsp_post2R = dsp_alloc(3);
dsp_interL = dsp_alloc(18); dsp_interR = dsp_alloc(18);      // ClipOnly3 intermediate samples
dsp_slewL = dsp_alloc(34); dsp_slewR = dsp_alloc(34);        // ClipOnly3 slew history
dsp_state_end = dsp_mem_top;
dsp_tab_on = dsp_alloc(11); dsp_tab_off = dsp_alloc(11);     // level compensation tables
ui_mem = dsp_alloc(512);                                     // GUI tables (written here, read by @gfx)

// head bump array layout (hdb_ enum of the original)
dsp_H_FREQ = 0; dsp_H_RESO = 1; dsp_H_A0 = 2; dsp_H_A1 = 3; dsp_H_A2 = 4; dsp_H_B1 = 5; dsp_H_B2 = 6;
dsp_H_SL1 = 7; dsp_H_SL2 = 8; dsp_H_SR1 = 9; dsp_H_SR2 = 10;
dsp_U32 = 4294967295;                                        // UINT32_MAX
dsp_PHI = 1.618033988749894848204586;

// ---------------- 32 bit xorshift of the original (fpd), exact for unsigned 32 bit values ----------------
function dsp_xor32(a, b) local(ah, al, bh, bl) (
  ah = floor(a / 65536); al = a - ah * 65536;
  bh = floor(b / 65536); bl = b - bh * 65536;
  (ah ~ bh) * 65536 + (al ~ bl);
);
function dsp_xorshift(s) local(t) (
  t = s * 8192; t -= floor(t / 4294967296) * 4294967296; s = dsp_xor32(s, t);   // s ^= s << 13
  s = dsp_xor32(s, floor(s / 131072));                                            // s ^= s >> 17
  t = s * 32;   t -= floor(t / 4294967296) * 4294967296; s = dsp_xor32(s, t);   // s ^= s << 5
  s;
);


// ---------------- ToTape9 building blocks ----------------
dsp_DEN_T = 1.18 * 10^(-23); dsp_DEN_N = 1.18 * 10^(-17);
// TapeHack2: cascaded averages (state in the global pair dsp_dkL / dsp_dkR)
function dsp_avg_stage(arrL, arrR, n, pos) local(k) (
  arrL[pos] = dsp_dkL; arrR[pos] = dsp_dkR; dsp_dkL = 0; dsp_dkR = 0;
  k = 0; loop(n, dsp_dkL += arrL[k]; dsp_dkR += arrR[k]; k += 1;);
  dsp_dkL /= n; dsp_dkR /= n;
);
// TapeHack: degenerate Taylor series of sin()
function dsp_tapehack(x) local(a2, e) (
  x = max(min(x, 2.305929007734908), -2.305929007734908);
  a2 = x * x; e = x * a2;
  x -= e / 6; e *= a2; x += e / 69; e *= a2; x -= e / 2530.08; e *= a2; x += e / 224985.6; e *= a2; x -= e / 9979200;
  x;
);
// ClipOnly3
function dsp_cliponly3(x, fpd, slew, inter) local(noise, k, fs, pc) (
  // works on the globals dsp_cpos / dsp_cneg / dsp_clast (copied in and out per channel)
  noise = 1 - (fpd / dsp_U32) * 0.076;
  dsp_cpos ? ( dsp_clast = x < dsp_clast ? 0.9085097 * noise + x * (1 - noise) : 0.94; );
  dsp_cpos = 0;
  x > 0.9085097 ? ( dsp_cpos = 1; x = 0.9085097 * noise + dsp_clast * (1 - noise); );
  dsp_cneg ? ( dsp_clast = x > dsp_clast ? -0.9085097 * noise + x * (1 - noise) : -0.94; );
  dsp_cneg = 0;
  x < -0.9085097 ? ( dsp_cneg = 1; x = -0.9085097 * noise + dsp_clast * (1 - noise); );
  slew[dsp_spacing * 2] = abs(dsp_clast - x);
  k = dsp_spacing * 2; loop(dsp_spacing * 2, slew[k - 1] = slew[k]; k -= 1;);
  inter[dsp_spacing] = x; x = dsp_clast;                 // latency: one 44.1 kHz sample
  k = dsp_spacing; loop(dsp_spacing, inter[k - 1] = inter[k]; k -= 1;);
  dsp_clast = inter[0];
  (dsp_cpos || dsp_cneg) ? ( k = dsp_spacing; loop(dsp_spacing, dsp_clast += inter[k]; k -= 1;); dsp_clast /= dsp_spacing; );
  fs = 0; k = dsp_spacing * 2; loop(dsp_spacing * 2 + 1, fs < slew[k] ? fs = slew[k]; k -= 1;);
  pc = 0.94 / (1 + fs * 1.3986013);
  max(-pc, min(pc, x));
);

// ---------------- reset (startup values of the original constructor) ----------------
memset(0, 0, dsp_state_end);
dsp_iirEncL = dsp_iirDecL = dsp_avgEncL = dsp_avgDecL = 0; dsp_compEncL = dsp_compDecL = 1;
dsp_iirEncR = dsp_iirDecR = dsp_avgEncR = dsp_avgDecR = 0; dsp_compEncR = dsp_compDecR = 1;
dsp_sweepL = dsp_sweepR = $pi; dsp_nextmaxL = dsp_nextmaxR = 0.5; dsp_gcount = 0;
dsp_hysL = dsp_hysR = 0; dsp_headBumpL = dsp_headBumpR = 0;
dsp_avgPos = 0; dsp_lastDarkL = dsp_lastDarkR = 0;
dsp_lastSampleL = dsp_lastSampleR = 0;
dsp_wasPosClipL = dsp_wasNegClipL = dsp_wasPosClipR = dsp_wasNegClipR = 0;
dsp_fpdL = 16386 + floor(rand(dsp_U32 - 16386));
dsp_fpdR = 16386 + floor(rand(dsp_U32 - 16386));

// ---------------- calibration (the GUI only raises cal_request) ----------------
dsp_meas_n = 0; dsp_meas_peak = 0; cal_request = 0;
dsp_trim_pre = 10^(slider10 / 20); dsp_trim_post = 1 / max(dsp_trim_pre, 0.000001);

// ---------------- output stage: measured level compensation tables ----------------
// Measured with a 1:1 C port of this DSP on 5 songs + pink noise, input calibrated to -0.1 dBFS
// (AUTO CALIBRATE), Tilt/Shape/Bias/Head Bump at default, 48 and 96 kHz. 11 nodes over Drive*Amount.
// AUTO GAIN ON : loudness (BS.1770 K-weighted) stays at the Drive 0 level (spread between songs < 0.45 dB).
// AUTO GAIN OFF: peaks are restored to the dry peak level -> same headroom as dry, the drive makes it louder
//                (the smallest measured value per node is used, so the dry peak is never exceeded).
dsp_tab_on[0] =  0.00; dsp_tab_on[1] = -0.88; dsp_tab_on[2] = -1.57; dsp_tab_on[3] = -2.09; dsp_tab_on[4] = -2.45; dsp_tab_on[5] = -2.65;
dsp_tab_on[6] = -2.68; dsp_tab_on[7] = -2.54; dsp_tab_on[8] = -2.20; dsp_tab_on[9] = -1.64; dsp_tab_on[10] = -0.82;
dsp_tab_off[0] = 0.69; dsp_tab_off[1] = 0.66; dsp_tab_off[2] = 0.65; dsp_tab_off[3] = 0.66; dsp_tab_off[4] = 0.70; dsp_tab_off[5] = 0.90;
dsp_tab_off[6] = 1.59; dsp_tab_off[7] = 2.48; dsp_tab_off[8] = 3.51; dsp_tab_off[9] = 4.70; dsp_tab_off[10] = 6.09;
function dsp_tab_db(tab, v) local(x, i, f) (
  x = min(1, max(0, v)) * 10; i = min(9, floor(x)); f = x - i;
  tab[i] + (tab[i + 1] - tab[i]) * f;
);
dsp_comp_s = 1;                   // smoothed compensation gain (linear)
dsp_out_s = 10^(slider13 / 20);   // smoothed manual output gain (linear)

// ---------------- GUI setup (static tables, ui_ namespace; read only by @gfx) ----------------
ui_bw = 800; ui_bh = 652;
ui_nk = 9; ui_nb = 4; ui_nr = 2; ui_btn_w = 98; ui_btn_h = 34;
ui_k_sl = ui_mem; ui_k_min = ui_mem + 16; ui_k_max = ui_mem + 32; ui_k_def = ui_mem + 48; ui_k_x = ui_mem + 64;
ui_k_y = ui_mem + 80; ui_k_r = ui_mem + 96; ui_k_step = ui_mem + 112; ui_k_fmt = ui_mem + 128; ui_k_name = ui_mem + 144;
ui_k_short = ui_mem + 160; ui_k_help = ui_mem + 176;
ui_b_sl = ui_mem + 224; ui_b_x = ui_mem + 232; ui_b_y = ui_mem + 240; ui_b_lbl = ui_mem + 248; ui_b_help = ui_mem + 256;
ui_r_sl = ui_mem + 272; ui_r_min = ui_mem + 276; ui_r_max = ui_mem + 280; ui_r_x = ui_mem + 284; ui_r_y = ui_mem + 288;
ui_r_name = ui_mem + 292; ui_r_help = ui_mem + 296;
// knob: index, slider, min, max, default, x, y, radius, step, format (0 = 0..1 as %, 1 = 0.00, 2 = Hz), short, help
// (full name = short name unless overridden below)
function ui_setk(i, sl, mn, mx, df, x, y, r, st, fm, short, help) (
  ui_k_sl[i] = sl; ui_k_min[i] = mn; ui_k_max[i] = mx; ui_k_def[i] = df; ui_k_x[i] = x; ui_k_y[i] = y; ui_k_r[i] = r;
  ui_k_step[i] = st; ui_k_fmt[i] = fm; ui_k_name[i] = short; ui_k_short[i] = short; ui_k_help[i] = help;
);
function ui_setb(i, sl, x, y, lbl, help) ( ui_b_sl[i] = sl; ui_b_x[i] = x; ui_b_y[i] = y; ui_b_lbl[i] = lbl; ui_b_help[i] = help; );
function ui_setr(i, sl, mn, mx, x, y, name, help) ( ui_r_sl[i] = sl; ui_r_min[i] = mn; ui_r_max[i] = mx; ui_r_x[i] = x; ui_r_y[i] = y; ui_r_name[i] = name; ui_r_help[i] = help; );
ui_rcx = 194; ui_rcy = 230; ui_rcx2 = ui_bw - ui_rcx;   // reel centers (mirror-symmetric)
// small knob row in three mirror-symmetric groups: FLUTTER (2) | TONE (3) | HEAD BUMP (2)
ui_ky = 520; ui_ksp = 98; ui_kgap = 118;
ui_kx1 = 400 - ui_ksp - ui_kgap; ui_kx0 = ui_kx1 - ui_ksp;     // flutter group
ui_kx5 = 400 + ui_ksp + ui_kgap; ui_kx6 = ui_kx5 + ui_ksp;     // head bump group
ui_setk(0, 1, 0, 1, 0,   ui_rcx,  ui_rcy, 96, 0.01, 0, "DRIVE", "");
ui_setk(1, 2, 0, 1, 1,   ui_rcx2, ui_rcy, 96, 0.01, 0, "AMOUNT", "Overall depth of the whole tape effect. 0 % = clean, 100 % = full tape.");
ui_setk(2, 3, 0, 1, 0.5, 400 - ui_ksp, ui_ky, 24, 0.01, 1, "TILT", "Tone balance of the tape: lower = warmer and fuller, higher = brighter.");
ui_setk(3, 4, 0, 1, 0.5, 400,          ui_ky, 24, 0.01, 1, "SHAPE", "How the tape squeezes the highs: lower = softer, higher = firmer.");
ui_setk(4, 5, 0, 1, 0,   ui_kx0,       ui_ky, 24, 0.01, 1, "FLUTTER", "Adds a gentle pitch wobble, like a real tape transport.");
ui_setk(5, 6, 0, 1, 0.5, ui_kx1,       ui_ky, 24, 0.01, 1, "SPEED", "How fast the flutter wobbles. Only works when FLUTTER is above 0.");
ui_setk(6, 7, 0, 1, 0.5, 400 + ui_ksp, ui_ky, 24, 0.01, 1, "BIAS", "Recording bias: changes how the tape distorts. Center = neutral.");
ui_setk(7, 8, 0, 1, 0.5, ui_kx5,       ui_ky, 24, 0.01, 1, "BUMP", "Adds the typical low-end bump of tape heads.");
ui_setk(8, 9, 25, 200, 68.75, ui_kx6,       ui_ky, 24, 0.25, 2, "FREQ", "Where the head bump sits in the bass. Only works when HEAD BUMP is above 0.");
ui_setb(0, 11, 120, 24, "BYPASS", "Turns the whole plugin off for an honest before/after check.");
ui_setb(1, 12, ui_bw - 120 - ui_btn_w, 24, "DELTA", "Plays only what the tape adds or removes (the difference to the dry signal).");
ui_setb(2, 14, 351, 236, "AUTO GAIN", "");
ui_setb(3, 0, 351, 112, "MEASURE", "Play the loudest part of your song, then click. After 7 s TRIM is set so the tape is driven right.");
// readouts that are dragged like faders: TRIM and OUTPUT
ui_setr(0, 10, -60, 60, 351, 154, "TRIM", "Level into the tape (taken back at the output). Drag up/down, Ctrl = fine, double-click = 0 dB.");
ui_setr(1, 13, -24, 24, 351, 278, "OUT", "Total level change of the tape signal: AUTO GAIN + your own trim. Drag = trim, Ctrl = fine, double-click = trim 0 dB.");
ui_rw = 98; ui_rh = 26;
ui_k_name[5] = "FLUTTER SPEED"; ui_k_name[7] = "HEAD BUMP"; ui_k_name[8] = "HEAD BUMP FREQUENCY";

@slider
// all parameters are read in @block (the GUI writes sliders directly, without @slider)

@block
// ---------------- trim + calibration start (thread-safe point) ----------------
dsp_trim_pre = 10^(slider10 / 20); dsp_trim_post = 1 / max(dsp_trim_pre, 0.000001);
cal_request ? ( cal_request = 0; dsp_meas_n = max(srate, 44100) * 7; dsp_meas_peak = 0; );

// ---------------- Overstratum control architecture -> ToTape9 parameters A..I ----------------
// DRIVE and AMOUNT are combined: AMOUNT scales every parameter towards its neutral value.
dsp_amt = slider2;
dsp_eff = slider1 * dsp_amt;
dsp_pA = 0.5 + dsp_eff * 0.5;                    // input
dsp_pI = 0.5 - dsp_eff * 0.32;                   // output (internal level coupling of the drive)
dsp_pB = 0.5 + (slider3 - 0.5) * dsp_amt;        // tilt
dsp_pC = min(max(0.5 + (slider4 - 0.5) * dsp_amt, 0.0001), 0.9999);   // shape
dsp_pD = slider5 * dsp_amt;                      // flutter
dsp_pE = slider6;                                // flutter speed
dsp_pF = 0.5 + (slider7 - 0.5) * dsp_amt;        // bias
dsp_pG = slider8 * dsp_amt;                      // head bump
dsp_pH = sqrt((slider9 - 25) / 175);             // head bump frequency (slider in Hz)

// ---------------- ToTape9 coefficients (as in processDoubleReplacing) ----------------
dsp_os = srate / 44100;                         // overallscale
dsp_spacing = max(1, min(16, floor(dsp_os)));
dsp_slewsing = max(2, min(32, floor(dsp_os * 2)));
dsp_inputGain = (dsp_pA * 2) ^ 2;
dsp_dublyAmount = dsp_pB * 2;
dsp_outlyAmount = max(-1, (1 - dsp_pB) * -2);
dsp_iirEncFreq = (1 - dsp_pC) / dsp_os;
dsp_iirDecFreq = dsp_pC / dsp_os;
dsp_flutDepth = min(498, (dsp_pD ^ 6) * dsp_os * 50);
dsp_flutFreq = (0.02 * dsp_pE ^ 3) / dsp_os;
dsp_bias = dsp_pF * 2 - 1;
dsp_underBias = ((dsp_bias ^ 4) * 0.25) / dsp_os;
dsp_overBias = ((1 - dsp_bias) ^ 3) / dsp_os;
dsp_bias > 0 ? dsp_underBias = 0;
dsp_bias < 0 ? dsp_overBias = 1 / dsp_os;
dsp_i = 26;                                     // threshold9 .. threshold1, each step * golden ratio
loop(9, dsp_gslew[dsp_i] = dsp_overBias; dsp_overBias *= dsp_PHI; dsp_i -= 3;);
dsp_headBumpDrive = (dsp_pG * 0.1) / dsp_os;
dsp_headBumpMix = dsp_pG * 0.5;
dsp_hdbA[dsp_H_FREQ] = ((dsp_pH * dsp_pH) * 175 + 25) / srate;
dsp_hdbB[dsp_H_FREQ] = dsp_hdbA[dsp_H_FREQ] * 0.9375;
dsp_hdbA[dsp_H_RESO] = dsp_hdbB[dsp_H_RESO] = 0.618033988749894848204586;
dsp_hdbA[dsp_H_A1] = dsp_hdbB[dsp_H_A1] = 0;
dsp_K = tan($pi * dsp_hdbA[dsp_H_FREQ]); dsp_nm = 1 / (1 + dsp_K / dsp_hdbA[dsp_H_RESO] + dsp_K * dsp_K);
dsp_hdbA[dsp_H_A0] = dsp_K / dsp_hdbA[dsp_H_RESO] * dsp_nm; dsp_hdbA[dsp_H_A2] = -dsp_hdbA[dsp_H_A0];
dsp_hdbA[dsp_H_B1] = 2 * (dsp_K * dsp_K - 1) * dsp_nm; dsp_hdbA[dsp_H_B2] = (1 - dsp_K / dsp_hdbA[dsp_H_RESO] + dsp_K * dsp_K) * dsp_nm;
dsp_K = tan($pi * dsp_hdbB[dsp_H_FREQ]); dsp_nm = 1 / (1 + dsp_K / dsp_hdbB[dsp_H_RESO] + dsp_K * dsp_K);
dsp_hdbB[dsp_H_A0] = dsp_K / dsp_hdbB[dsp_H_RESO] * dsp_nm; dsp_hdbB[dsp_H_A2] = -dsp_hdbB[dsp_H_A0];
dsp_hdbB[dsp_H_B1] = 2 * (dsp_K * dsp_K - 1) * dsp_nm; dsp_hdbB[dsp_H_B2] = (1 - dsp_K / dsp_hdbB[dsp_H_RESO] + dsp_K * dsp_K) * dsp_nm;
dsp_outputGain = dsp_pI * 2;

// ---------------- output stage ----------------
dsp_comp_db = dsp_tab_db(slider14 > 0.5 ? dsp_tab_on : dsp_tab_off, dsp_eff);   // fixed values, no level following
dsp_comp_t  = 10^(dsp_comp_db / 20);
dsp_out_t   = 10^(max(-24, min(24, slider13)) / 20);
dsp_out_c   = 1 - exp(-1 / (0.02 * srate));     // 20 ms smoothing: click-free knob moves and AUTO GAIN switching

@sample
dsp_dry0 = spl0; dsp_dry1 = spl1;

// ---- 1. auto calibrate: 7 s peak measurement, sets TRIM so the input peaks at -0.1 dBFS ----
dsp_meas_n > 0 ? (
  dsp_meas_peak = max(dsp_meas_peak, max(abs(spl0), abs(spl1)));
  dsp_meas_n -= 1;
  dsp_meas_n <= 0 ? (
    slider10 = min(max(-0.1 - 20 * log10(max(dsp_meas_peak, 0.000001)), -60), 60);
    slider_automate(slider10);
    dsp_trim_pre = 10^(slider10 / 20); dsp_trim_post = 1 / max(dsp_trim_pre, 0.000001);
  );
);

// ---- 2. pre trim ----
dsp_xL = spl0 * dsp_trim_pre;
dsp_xR = spl1 * dsp_trim_pre;

// ==================== Airwindows ToTape9 (per sample) ====================
abs(dsp_xL) < dsp_DEN_T ? dsp_xL = dsp_fpdL * dsp_DEN_N;     // denormal guard of the original
abs(dsp_xR) < dsp_DEN_T ? dsp_xR = dsp_fpdR * dsp_DEN_N;
dsp_inputGain != 1 ? ( dsp_xL *= dsp_inputGain; dsp_xR *= dsp_inputGain; );

// Dubly encode
dsp_iirEncL = dsp_iirEncL * (1 - dsp_iirEncFreq) + dsp_xL * dsp_iirEncFreq;
dsp_hp = (dsp_xL - dsp_iirEncL) * 2.848; dsp_hp += dsp_avgEncL; dsp_avgEncL = (dsp_xL - dsp_iirEncL) * 1.152;
dsp_hp = max(-1, min(1, dsp_hp)); dsp_dub = abs(dsp_hp);
dsp_dub > 0 ? (
  dsp_adj = log(1 + 255 * dsp_dub) / 2.40823996531; dsp_adj > 0 ? dsp_dub /= dsp_adj;
  dsp_compEncL = dsp_compEncL * (1 - dsp_iirEncFreq) + dsp_dub * dsp_iirEncFreq;
  dsp_xL += dsp_hp * dsp_compEncL * dsp_dublyAmount;
);
dsp_iirEncR = dsp_iirEncR * (1 - dsp_iirEncFreq) + dsp_xR * dsp_iirEncFreq;
dsp_hp = (dsp_xR - dsp_iirEncR) * 2.848; dsp_hp += dsp_avgEncR; dsp_avgEncR = (dsp_xR - dsp_iirEncR) * 1.152;
dsp_hp = max(-1, min(1, dsp_hp)); dsp_dub = abs(dsp_hp);
dsp_dub > 0 ? (
  dsp_adj = log(1 + 255 * dsp_dub) / 2.40823996531; dsp_adj > 0 ? dsp_dub /= dsp_adj;
  dsp_compEncR = dsp_compEncR * (1 - dsp_iirEncFreq) + dsp_dub * dsp_iirEncFreq;
  dsp_xR += dsp_hp * dsp_compEncR * dsp_dublyAmount;
);

// Flutter
dsp_flutDepth > 0 ? (
  (dsp_gcount < 0 || dsp_gcount > 999) ? dsp_gcount = 999;
  dsp_dL[dsp_gcount] = dsp_xL;
  dsp_cnt = dsp_gcount;
  dsp_off = dsp_flutDepth + dsp_flutDepth * sin(dsp_sweepL);
  dsp_sweepL += dsp_nextmaxL * dsp_flutFreq;
  dsp_sweepL > 2 * $pi ? (
    dsp_sweepL -= 2 * $pi;
    dsp_flutA = 0.24 + dsp_fpdL / dsp_U32 * 0.74;
    dsp_fpdL = dsp_xorshift(dsp_fpdL);
    dsp_flutB = 0.24 + dsp_fpdL / dsp_U32 * 0.74;
    dsp_nextmaxL = abs(dsp_flutA - sin(dsp_sweepR + dsp_nextmaxR)) < abs(dsp_flutB - sin(dsp_sweepR + dsp_nextmaxR)) ? dsp_flutA : dsp_flutB;
  );
  dsp_cnt += floor(dsp_off); dsp_fr = dsp_off - floor(dsp_off);
  dsp_xL = dsp_dL[dsp_cnt - (dsp_cnt > 999 ? 1000 : 0)] * (1 - dsp_fr) + dsp_dL[dsp_cnt + 1 - (dsp_cnt + 1 > 999 ? 1000 : 0)] * dsp_fr;
  dsp_dR[dsp_gcount] = dsp_xR;
  dsp_cnt = dsp_gcount;
  dsp_off = dsp_flutDepth + dsp_flutDepth * sin(dsp_sweepR);
  dsp_sweepR += dsp_nextmaxR * dsp_flutFreq;
  dsp_sweepR > 2 * $pi ? (
    dsp_sweepR -= 2 * $pi;
    dsp_flutA = 0.24 + dsp_fpdR / dsp_U32 * 0.74;
    dsp_fpdR = dsp_xorshift(dsp_fpdR);
    dsp_flutB = 0.24 + dsp_fpdR / dsp_U32 * 0.74;
    dsp_nextmaxR = abs(dsp_flutA - sin(dsp_sweepL + dsp_nextmaxL)) < abs(dsp_flutB - sin(dsp_sweepL + dsp_nextmaxL)) ? dsp_flutA : dsp_flutB;
  );
  dsp_cnt += floor(dsp_off); dsp_fr = dsp_off - floor(dsp_off);
  dsp_xR = dsp_dR[dsp_cnt - (dsp_cnt > 999 ? 1000 : 0)] * (1 - dsp_fr) + dsp_dR[dsp_cnt + 1 - (dsp_cnt + 1 > 999 ? 1000 : 0)] * dsp_fr;
  dsp_gcount -= 1;
);

// Bias
abs(dsp_bias) > 0.001 ? (
  dsp_i = 0;
  loop(9,
    dsp_underBias > 0 ? (
      dsp_stuck = abs(dsp_xL - dsp_gslew[dsp_i] / 0.975) / dsp_underBias;
      dsp_stuck < 1 ? dsp_xL = dsp_xL * dsp_stuck + (dsp_gslew[dsp_i] / 0.975) * (1 - dsp_stuck);
      dsp_stuck = abs(dsp_xR - dsp_gslew[dsp_i + 1] / 0.975) / dsp_underBias;
      dsp_stuck < 1 ? dsp_xR = dsp_xR * dsp_stuck + (dsp_gslew[dsp_i + 1] / 0.975) * (1 - dsp_stuck);
    );
    (dsp_xL - dsp_gslew[dsp_i]) > dsp_gslew[dsp_i + 2] ? dsp_xL = dsp_gslew[dsp_i] + dsp_gslew[dsp_i + 2];
    -(dsp_xL - dsp_gslew[dsp_i]) > dsp_gslew[dsp_i + 2] ? dsp_xL = dsp_gslew[dsp_i] - dsp_gslew[dsp_i + 2];
    dsp_gslew[dsp_i] = dsp_xL * 0.975;
    (dsp_xR - dsp_gslew[dsp_i + 1]) > dsp_gslew[dsp_i + 2] ? dsp_xR = dsp_gslew[dsp_i + 1] + dsp_gslew[dsp_i + 2];
    -(dsp_xR - dsp_gslew[dsp_i + 1]) > dsp_gslew[dsp_i + 2] ? dsp_xR = dsp_gslew[dsp_i + 1] - dsp_gslew[dsp_i + 2];
    dsp_gslew[dsp_i + 1] = dsp_xR * 0.975;
    dsp_i += 3;
  );
);

// tiny hysteresis
dsp_ah = (1 - abs(dsp_xL)) * (1 - abs(dsp_xL)) * 0.012;
dsp_hysL = max(min(dsp_hysL + dsp_xL * abs(dsp_xL), 0.011449), -0.011449) * 0.999;
dsp_xL += dsp_hysL * dsp_ah;
dsp_ah = (1 - abs(dsp_xR)) * (1 - abs(dsp_xR)) * 0.012;
dsp_hysR = max(min(dsp_hysR + dsp_xR * abs(dsp_xR), 0.011449), -0.011449) * 0.999;
dsp_xR += dsp_hysR * dsp_ah;

// TapeHack2 pre filter: cascaded averages, depth set by the slew
dsp_dkL = dsp_xL; dsp_dkR = dsp_xR;
dsp_avgPos > 31 ? dsp_avgPos = 0;
dsp_slewsing > 31 ? dsp_avg_stage(dsp_avg32L, dsp_avg32R, 32, dsp_avgPos);
dsp_slewsing > 15 ? dsp_avg_stage(dsp_avg16L, dsp_avg16R, 16, dsp_avgPos % 16);
dsp_slewsing > 7  ? dsp_avg_stage(dsp_avg8L,  dsp_avg8R,  8,  dsp_avgPos % 8);
dsp_slewsing > 3  ? dsp_avg_stage(dsp_avg4L,  dsp_avg4R,  4,  dsp_avgPos % 4);
dsp_slewsing > 1  ? dsp_avg_stage(dsp_avg2L,  dsp_avg2R,  2,  dsp_avgPos % 2);
dsp_avgSlewL = min(abs(dsp_lastDarkL - dsp_xL) * 0.12 * dsp_os, 1);
dsp_avgSlewL = 1 - (1 - dsp_avgSlewL * 1 - dsp_avgSlewL);
dsp_xL = dsp_xL * (1 - dsp_avgSlewL) + dsp_dkL * dsp_avgSlewL; dsp_lastDarkL = dsp_dkL;
dsp_avgSlewR = min(abs(dsp_lastDarkR - dsp_xR) * 0.12 * dsp_os, 1);
dsp_avgSlewR = 1 - (1 - dsp_avgSlewR * 1 - dsp_avgSlewR);
dsp_xR = dsp_xR * (1 - dsp_avgSlewR) + dsp_dkR * dsp_avgSlewR; dsp_lastDarkR = dsp_dkR;

// TapeHack: degenerate Taylor series of sin()
dsp_xL = dsp_tapehack(dsp_xL);
dsp_xR = dsp_tapehack(dsp_xR);

// TapeHack2 post filter (same slew depth)
dsp_dkL = dsp_xL; dsp_dkR = dsp_xR;
dsp_avgPos > 31 ? dsp_avgPos = 0;
dsp_slewsing > 31 ? dsp_avg_stage(dsp_post32L, dsp_post32R, 32, dsp_avgPos);
dsp_slewsing > 15 ? dsp_avg_stage(dsp_post16L, dsp_post16R, 16, dsp_avgPos % 16);
dsp_slewsing > 7  ? dsp_avg_stage(dsp_post8L,  dsp_post8R,  8,  dsp_avgPos % 8);
dsp_slewsing > 3  ? dsp_avg_stage(dsp_post4L,  dsp_post4R,  4,  dsp_avgPos % 4);
dsp_slewsing > 1  ? dsp_avg_stage(dsp_post2L,  dsp_post2R,  2,  dsp_avgPos % 2);
dsp_avgPos += 1;
dsp_xL = dsp_xL * (1 - dsp_avgSlewL) + dsp_dkL * dsp_avgSlewL;
dsp_xR = dsp_xR * (1 - dsp_avgSlewR) + dsp_dkR * dsp_avgSlewR;

// HeadBump
dsp_hbL = 0; dsp_hbR = 0;
dsp_headBumpMix > 0 ? (
  dsp_headBumpL += dsp_xL * dsp_headBumpDrive;
  dsp_headBumpL -= dsp_headBumpL * dsp_headBumpL * dsp_headBumpL * (0.0618 / sqrt(dsp_os));
  dsp_headBumpR += dsp_xR * dsp_headBumpDrive;
  dsp_headBumpR -= dsp_headBumpR * dsp_headBumpR * dsp_headBumpR * (0.0618 / sqrt(dsp_os));
  dsp_q = dsp_headBumpL * dsp_hdbA[dsp_H_A0] + dsp_hdbA[dsp_H_SL1];
  dsp_hdbA[dsp_H_SL1] = dsp_headBumpL * dsp_hdbA[dsp_H_A1] - dsp_q * dsp_hdbA[dsp_H_B1] + dsp_hdbA[dsp_H_SL2];
  dsp_hdbA[dsp_H_SL2] = dsp_headBumpL * dsp_hdbA[dsp_H_A2] - dsp_q * dsp_hdbA[dsp_H_B2];
  dsp_hbL = dsp_q * dsp_hdbB[dsp_H_A0] + dsp_hdbB[dsp_H_SL1];
  dsp_hdbB[dsp_H_SL1] = dsp_q * dsp_hdbB[dsp_H_A1] - dsp_hbL * dsp_hdbB[dsp_H_B1] + dsp_hdbB[dsp_H_SL2];
  dsp_hdbB[dsp_H_SL2] = dsp_q * dsp_hdbB[dsp_H_A2] - dsp_hbL * dsp_hdbB[dsp_H_B2];
  dsp_q = dsp_headBumpR * dsp_hdbA[dsp_H_A0] + dsp_hdbA[dsp_H_SR1];
  dsp_hdbA[dsp_H_SR1] = dsp_headBumpR * dsp_hdbA[dsp_H_A1] - dsp_q * dsp_hdbA[dsp_H_B1] + dsp_hdbA[dsp_H_SR2];
  dsp_hdbA[dsp_H_SR2] = dsp_headBumpR * dsp_hdbA[dsp_H_A2] - dsp_q * dsp_hdbA[dsp_H_B2];
  dsp_hbR = dsp_q * dsp_hdbB[dsp_H_A0] + dsp_hdbB[dsp_H_SR1];
  dsp_hdbB[dsp_H_SR1] = dsp_q * dsp_hdbB[dsp_H_A1] - dsp_hbR * dsp_hdbB[dsp_H_B1] + dsp_hdbB[dsp_H_SR2];
  dsp_hdbB[dsp_H_SR2] = dsp_q * dsp_hdbB[dsp_H_A2] - dsp_hbR * dsp_hdbB[dsp_H_B2];
);
dsp_xL += dsp_hbL * dsp_headBumpMix;
dsp_xR += dsp_hbR * dsp_headBumpMix;

// Dubly decode
dsp_iirDecL = dsp_iirDecL * (1 - dsp_iirDecFreq) + dsp_xL * dsp_iirDecFreq;
dsp_hp = (dsp_xL - dsp_iirDecL) * 2.628; dsp_hp += dsp_avgDecL; dsp_avgDecL = (dsp_xL - dsp_iirDecL) * 1.372;
dsp_hp = max(-1, min(1, dsp_hp)); dsp_dub = abs(dsp_hp);
dsp_dub > 0 ? (
  dsp_adj = log(1 + 255 * dsp_dub) / 2.40823996531; dsp_adj > 0 ? dsp_dub /= dsp_adj;
  dsp_compDecL = dsp_compDecL * (1 - dsp_iirDecFreq) + dsp_dub * dsp_iirDecFreq;
  dsp_xL += dsp_hp * dsp_compDecL * dsp_outlyAmount;
);
dsp_iirDecR = dsp_iirDecR * (1 - dsp_iirDecFreq) + dsp_xR * dsp_iirDecFreq;
dsp_hp = (dsp_xR - dsp_iirDecR) * 2.628; dsp_hp += dsp_avgDecR; dsp_avgDecR = (dsp_xR - dsp_iirDecR) * 1.372;
dsp_hp = max(-1, min(1, dsp_hp)); dsp_dub = abs(dsp_hp);
dsp_dub > 0 ? (
  dsp_adj = log(1 + 255 * dsp_dub) / 2.40823996531; dsp_adj > 0 ? dsp_dub /= dsp_adj;
  dsp_compDecR = dsp_compDecR * (1 - dsp_iirDecFreq) + dsp_dub * dsp_iirDecFreq;
  dsp_xR += dsp_hp * dsp_compDecR * dsp_outlyAmount;
);

dsp_outputGain != 1 ? ( dsp_xL *= dsp_outputGain; dsp_xR *= dsp_outputGain; );

// ClipOnly3
dsp_cpos = dsp_wasPosClipL; dsp_cneg = dsp_wasNegClipL; dsp_clast = dsp_lastSampleL;
dsp_xL = dsp_cliponly3(dsp_xL, dsp_fpdL, dsp_slewL, dsp_interL);
dsp_wasPosClipL = dsp_cpos; dsp_wasNegClipL = dsp_cneg; dsp_lastSampleL = dsp_clast;
dsp_cpos = dsp_wasPosClipR; dsp_cneg = dsp_wasNegClipR; dsp_clast = dsp_lastSampleR;
dsp_xR = dsp_cliponly3(dsp_xR, dsp_fpdR, dsp_slewR, dsp_interR);
dsp_wasPosClipR = dsp_cpos; dsp_wasNegClipR = dsp_cneg; dsp_lastSampleR = dsp_clast;

// the original advances its noise generators every sample (its 64 bit dither itself is disabled)
dsp_fpdL = dsp_xorshift(dsp_fpdL);
dsp_fpdR = dsp_xorshift(dsp_fpdR);
// ==================== end ToTape9 ====================

// ---- 3. post trim (undoes the calibration gain) ----
dsp_pL = dsp_xL * dsp_trim_post;
dsp_pR = dsp_xR * dsp_trim_post;

// ---- 4. level compensation: fixed table value for the current drive, applied after the
//         tape clipper, so the saturation itself is untouched ----
dsp_comp_s += (dsp_comp_t - dsp_comp_s) * dsp_out_c;
dsp_pL *= dsp_comp_s;
dsp_pR *= dsp_comp_s;

// ---- 5. AMOUNT as dry/wet (at 0 % exactly the dry signal) ----
spl0 = dsp_dry0 + (dsp_pL - dsp_dry0) * dsp_amt;
spl1 = dsp_dry1 + (dsp_pR - dsp_dry1) * dsp_amt;

// ---- 6. manual output gain, bypass, delta ----
dsp_out_s += (dsp_out_t - dsp_out_s) * dsp_out_c;
slider11 > 0.5 ? (
  spl0 = dsp_dry0; spl1 = dsp_dry1;                     // bypass: untouched input
) : slider12 > 0.5 ? (
  spl0 -= dsp_dry0; spl1 -= dsp_dry1;                   // delta: level-matched difference (without OUTPUT)
) : (
  spl0 *= dsp_out_s; spl1 *= dsp_out_s;
);

@gfx 800 652
gfx_clear = -1;

// =====================================================================================
//  GUI (vintage console style, see design_tooltips_vintage.txt)
//  Thread rules: only ui_ globals and locals; writes only sliders and the flag cal_request.
// =====================================================================================

// ---------- helpers ----------
function ui_bevel_box(x, y, w, h) local() (
  gfx_set(0.26, 0.26, 0.24, 1); gfx_line(x, y, x + w - 1, y); gfx_line(x, y, x, y + h - 1);
  gfx_set(0.07, 0.07, 0.06, 1); gfx_line(x, y + h - 1, x + w - 1, y + h - 1); gfx_line(x + w - 1, y, x + w - 1, y + h - 1);
);
function ui_inv_bevel(x, y, w, h) local() (
  gfx_set(0.07, 0.07, 0.06, 1); gfx_line(x, y, x + w - 1, y); gfx_line(x, y, x, y + h - 1);
  gfx_set(0.26, 0.26, 0.24, 1); gfx_line(x, y + h - 1, x + w - 1, y + h - 1); gfx_line(x + w - 1, y, x + w - 1, y + h - 1);
);
function ui_text_c(x, y, w, h, str) local(tw, th) (
  gfx_measurestr(str, tw, th);
  gfx_x = x + (w - tw) * 0.5; gfx_y = y + (h - th) * 0.5;
  gfx_drawstr(str);
);
function ui_screw(cx, cy) local(dx) (
  gfx_set(0.06, 0.06, 0.05, 0.85); gfx_circle(cx + 1, cy + 1, 7, 1, 1);
  gfx_set(0.42, 0.41, 0.38, 1); gfx_circle(cx, cy, 7, 1, 1);
  gfx_set(0.10, 0.10, 0.09, 1); gfx_circle(cx, cy, 5, 1, 1);
  dx = 3.6; gfx_set(0.52, 0.50, 0.46, 1);
  gfx_line(cx - dx, cy + dx, cx + dx, cy - dx); gfx_line(cx - dx + 1, cy + dx, cx + dx, cy - dx + 1);
);
function ui_caption(cx, y, w, str) local(tw, th, x0, x1, ym) (
  gfx_setfont(5); gfx_measurestr(str, tw, th);
  gfx_set(0.55, 0.53, 0.48, 1); gfx_x = cx - tw * 0.5; gfx_y = y; gfx_drawstr(str);
  x0 = cx - w * 0.5; x1 = cx + w * 0.5; ym = y + th * 0.5;
  tw * 0.5 + 8 < w * 0.5 ? (
    gfx_set(0.26, 0.26, 0.24, 1); gfx_line(x0, ym, cx - tw * 0.5 - 8, ym); gfx_line(cx + tw * 0.5 + 8, ym, x1, ym);
    gfx_set(0.07, 0.07, 0.06, 1); gfx_line(x0, ym + 1, cx - tw * 0.5 - 8, ym + 1); gfx_line(cx + tw * 0.5 + 8, ym + 1, x1, ym + 1);
  );
);
// LED jewel (amber, or red when hot = 1)
function ui_jewel(x, y, r, on, hot) local() (
  on ? (
    hot ? gfx_set(0.90, 0.25, 0.12, 0.35) : gfx_set(0.95, 0.45, 0.15, 0.35); gfx_circle(x, y, r * 1.55, 1, 1);
    hot ? gfx_set(0.92, 0.30, 0.16, 1) : gfx_set(0.98, 0.55, 0.20, 1); gfx_circle(x, y, r, 1, 1);
    gfx_set(1.00, 0.85, 0.60, 1); gfx_circle(x - r * 0.25, y - r * 0.25, r * 0.33, 1, 1);
  ) : (
    gfx_set(0.10, 0.07, 0.05, 1); gfx_circle(x, y, r, 1, 1);
    gfx_set(0.22, 0.15, 0.10, 1); gfx_circle(x - r * 0.25, y - r * 0.25, r * 0.33, 1, 1);
  );
);
// annular sector as a convex quad strip (a = 0 at 12 o'clock, clockwise)
function ui_sector(cx, cy, r0, r1, a0, a1, segs) local(i, aa, ab, sa, ca, sb, cb) (
  i = 0;
  loop(segs,
    aa = a0 + (a1 - a0) * i / segs; ab = a0 + (a1 - a0) * (i + 1) / segs;
    sa = sin(aa); ca = cos(aa); sb = sin(ab); cb = cos(ab);
    gfx_triangle(cx + sa * r0, cy - ca * r0, cx + sa * r1, cy - ca * r1, cx + sb * r1, cy - cb * r1, cx + sb * r0, cy - cb * r0);
    i += 1;
  );
);

// ---------- values ----------
function ui_norm(k) local(v) (
  v = slider(ui_k_sl[k]);
  min(1, max(0, (v - ui_k_min[k]) / (ui_k_max[k] - ui_k_min[k])));
);
// a control is drawn "lit" only when it currently changes the sound
function ui_active(k) local() (
  slider11 > 0.5 ? 0
  : k != 1 && slider2 <= 0 ? 0
  : k == 5 ? slider5 > 0
  : k == 8 ? slider8 > 0
  : 1;
);
function ui_fmt(k) local(v) (
  v = slider(ui_k_sl[k]);
  ui_k_fmt[k] == 0 ? sprintf(#ui_val, "%d %%", floor(v * 100 + 0.5))
  : ui_k_fmt[k] == 1 ? sprintf(#ui_val, "%.2f", v)
  : sprintf(#ui_val, "%.0f Hz", v);
);
// scale numbers = real values at that position
function ui_tick_label(k, v) local(iv) (
  ui_k_fmt[k] == 0 ? sprintf(#ui_tmp, "%d", floor(v * 100 + 0.5))
  : ui_k_fmt[k] == 2 ? sprintf(#ui_tmp, "%d", floor(v + 0.5))
  : ( iv = floor(v + 0.5);
      abs(v - iv) < 0.001 ? sprintf(#ui_tmp, "%d", iv)
      : abs(v * 10 - floor(v * 10 + 0.5)) < 0.001 ? sprintf(#ui_tmp, "%.1f", v)
      : sprintf(#ui_tmp, "%.2f", v); );
);
function ui_pointer(cx, cy, a, r0, r1) local(ca, sa) (
  ca = cos(a); sa = sin(a);
  gfx_line(cx + sa * r0, cy - ca * r0, cx + sa * r1, cy - ca * r1);
  gfx_line(cx + sa * r0 + ca * 0.8, cy - ca * r0 + sa * 0.8, cx + sa * r1 + ca * 0.8, cy - ca * r1 + sa * 0.8);
);

// ---------- small knob ----------
function ui_draw_knob(k) local(cx, cy, r, n, i, a, a0, a1, ca, sa, rr, rr2, major, norm, act, cr, lbl_w, ly, mn, mx)
(
  cx = ui_k_x[k]; cy = ui_k_y[k]; r = ui_k_r[k]; mn = ui_k_min[k]; mx = ui_k_max[k];
  norm = ui_norm(k); act = ui_active(k);
  a0 = -0.75 * $pi; a1 = 0.75 * $pi;
  n = 20; i = 0;
  loop(n + 1,
    a = a0 + (a1 - a0) * i / n; ca = cos(a); sa = sin(a);
    major = (i % 5) == 0; rr = r + 5; rr2 = major ? r + 11 : r + 8;
    major ? gfx_set(0.70, 0.67, 0.60, 0.9) : gfx_set(0.45, 0.43, 0.39, 0.8);
    gfx_line(cx + sa * rr, cy - ca * rr, cx + sa * rr2, cy - ca * rr2);
    (i % 10) == 0 ? (                     // numbers only at min / center / max: no overlap between neighbours
      gfx_setfont(3); gfx_set(0.55, 0.53, 0.48, 0.9);
      ui_tick_label(k, mn + (mx - mn) * i / n);
      ui_text_c(cx + sa * (r + 21) - 12, cy - ca * (r + 21) - 6, 24, 12, #ui_tmp);
    );
    i += 1;
  );
  gfx_set(0.06, 0.06, 0.05, 0.85); gfx_circle(cx + 3, cy + 4, r + 1, 1, 1);
  gfx_set(0.20, 0.17, 0.13, 1); gfx_circle(cx, cy, r, 1, 1);
  i = 0;
  loop(40,
    a = i * 2 * $pi / 40; ca = cos(a); sa = sin(a);
    gfx_set(0.29, 0.25, 0.19, 1); gfx_line(cx + sa * (r - 3), cy - ca * (r - 3), cx + sa * r, cy - ca * r);
    i += 1;
  );
  gfx_set(0.10, 0.09, 0.08, 1); gfx_circle(cx, cy, r - 4, 1, 1);
  gfx_set(0.16, 0.15, 0.13, 1); gfx_circle(cx - 2, cy - 2, r - 7, 1, 1);
  gfx_set(0.11, 0.10, 0.09, 1); gfx_circle(cx, cy, r - 8, 1, 1);
  cr = floor(r * 0.4 + 0.5);
  gfx_set(0.30, 0.30, 0.29, 1); gfx_circle(cx, cy, cr, 1, 1);
  i = 0;
  loop(cr - 1,
    gfx_set(0.58 + 0.03 * (i % 3), 0.58 + 0.03 * (i % 3), 0.56 + 0.03 * (i % 3), 1);
    gfx_circle(cx, cy, cr - 1 - i * 0.9, 0, 1);
    i += 1;
  );
  gfx_set(0.20, 0.20, 0.19, 1); gfx_circle(cx, cy, cr, 0, 1);
  act ? gfx_set(0.92, 0.86, 0.72, 1) : gfx_set(0.50, 0.48, 0.43, 1);
  ui_pointer(cx, cy, a0 + (a1 - a0) * norm, cr + 1, r - 3);
  // two-line label plate: name small on top, live value below
  lbl_w = 76; ly = cy + r + 14;
  gfx_set(0.12, 0.12, 0.11, 1); gfx_rect(cx - lbl_w / 2, ly, lbl_w, 30, 1);
  ui_inv_bevel(cx - lbl_w / 2, ly, lbl_w, 30);
  gfx_setfont(5);
  act ? gfx_set(0.62, 0.60, 0.54, 1) : gfx_set(0.40, 0.38, 0.35, 1);
  ui_text_c(cx - lbl_w / 2, ly + 2, lbl_w, 12, ui_k_short[k]);
  ui_fmt(k);
  gfx_setfont(2);
  act ? gfx_set(0.78, 0.75, 0.68, 1) : gfx_set(0.46, 0.44, 0.40, 1);
  ui_text_c(cx - lbl_w / 2, ly + 14, lbl_w, 14, #ui_val);
);

// ---------- tape reel (hero control: DRIVE / AMOUNT) ----------
// side = -1 (left, supply reel) / +1 (right, take-up reel): the name plate sits on the outer side,
// the tape leaves through the inner bottom gap of the scale.
function ui_draw_reel(k, side)
local(cx, cy, r, rw, n, i, a, a0, a1, ca, sa, rr, rr2, major, norm, act, f, px, py, gl, g, tp, w, wa, lbl_w, lx, ly)
(
  cx = ui_k_x[k]; cy = ui_k_y[k]; r = ui_k_r[k]; rw = r + 16;
  norm = ui_norm(k); act = ui_active(k);
  a0 = -0.75 * $pi; a1 = 0.75 * $pi;

  // warm halo, grows with the value
  gl = act ? 0.25 + norm : 0;
  i = 0;
  loop(10, gfx_set(0.98, 0.55, 0.20, 0.014 * gl); gfx_circle(cx, cy, rw + 26 - i * 3, 1, 1); i += 1;);

  // scale 0..100 %
  n = 50; i = 0;
  loop(n + 1,
    a = a0 + (a1 - a0) * i / n; ca = cos(a); sa = sin(a);
    major = (i % 5) == 0; rr = rw + 5; rr2 = major ? rw + 13 : rw + 9;
    major ? gfx_set(0.70, 0.67, 0.60, 0.9) : gfx_set(0.45, 0.43, 0.39, 0.8);
    gfx_line(cx + sa * rr, cy - ca * rr, cx + sa * rr2, cy - ca * rr2);
    major ? (
      gfx_setfont(3); gfx_set(0.62, 0.60, 0.54, 0.95);
      ui_tick_label(k, i / n);
      ui_text_c(cx + sa * (rw + 23) - 11, cy - ca * (rw + 23) - 6, 22, 12, #ui_tmp);
    );
    i += 1;
  );

  // recessed well
  gfx_set(0.30, 0.29, 0.26, 1); gfx_circle(cx, cy, rw + 3, 0, 1);
  gfx_set(0.12, 0.12, 0.11, 1); gfx_circle(cx, cy, rw, 1, 1);
  gfx_set(0.07, 0.07, 0.06, 1);
  gfx_arc(cx, cy, rw, -0.75 * $pi, 0.25 * $pi, 1); gfx_arc(cx, cy, rw - 1, -0.75 * $pi, 0.25 * $pi, 1);
  gfx_set(0.26, 0.26, 0.24, 1); gfx_arc(cx, cy, rw, 0.25 * $pi, 1.25 * $pi, 1);

  // LED ring (31 jewels), amber turning hotter towards 100 %
  n = 30; i = 0;
  loop(n + 1,
    a = a0 + (a1 - a0) * i / n; ca = cos(a); sa = sin(a);
    px = cx + sa * (r + 8); py = cy - ca * (r + 8);
    f = norm > 0.001 ? min(1, max(0, norm * n - i + 1)) : 0;
    !act ? f *= 0.18;
    g = 0.62 - 0.30 * i / n;
    gfx_set(0.10, 0.07, 0.05, 1); gfx_circle(px, py, 2.4, 1, 1);
    f > 0 ? (
      gfx_set(0.95, g - 0.10, 0.15, 0.30 * f); gfx_circle(px, py, 4.6, 1, 1);
      gfx_set(0.98, g, 0.20, f); gfx_circle(px, py, 2.4, 1, 1);
      gfx_set(1.00, 0.85, 0.60, f); gfx_circle(px - 0.6, py - 0.6, 0.9, 1, 1);
    );
    i += 1;
  );

  // reel flange: brushed aluminium NAB reel
  gfx_set(0.04, 0.04, 0.03, 0.85); gfx_circle(cx + 4, cy + 6, r + 1, 1, 1);
  gfx_set(0.46, 0.46, 0.45, 1); gfx_circle(cx, cy, r, 1, 1);
  i = 0;
  loop(50,
    gfx_set(0.50 + 0.035 * (i % 3), 0.50 + 0.035 * (i % 3), 0.49 + 0.035 * (i % 3), 1);
    gfx_circle(cx, cy, r - 1 - i * 1.3, 0, 1);
    i += 1;
  );
  gfx_set(0.74, 0.74, 0.72, 1); gfx_circle(cx, cy, r, 0, 1);
  gfx_set(0.30, 0.30, 0.29, 1); gfx_circle(cx, cy, r - 4, 0, 1);
  gfx_set(0.68, 0.68, 0.66, 0.8); gfx_circle(cx, cy, r - 5, 0, 1);

  // three windows showing the tape pack (pack radius grows with the value)
  tp = 34 + norm * 50;
  w = 0;
  loop(3,
    wa = ui_rot + w * 2 * $pi / 3;
    gfx_set(0.05, 0.05, 0.045, 1); ui_sector(cx, cy, 32, r - 10, wa - 0.62, wa + 0.62, 10);
    gfx_set(0.23, 0.15, 0.09, 1);  ui_sector(cx, cy, 32, tp, wa - 0.62, wa + 0.62, 10);
    gfx_set(0.36, 0.25, 0.15, 0.8); gfx_arc(cx, cy, tp - 3, wa - 0.60, wa + 0.60, 1);
    gfx_set(0.13, 0.08, 0.05, 1);  gfx_arc(cx, cy, tp, wa - 0.62, wa + 0.62, 1);
    // window edge: shadow on the leading edge, light on the trailing edge
    sa = sin(wa - 0.62); ca = cos(wa - 0.62);
    gfx_set(0.22, 0.22, 0.21, 1); gfx_line(cx + sa * 32, cy - ca * 32, cx + sa * (r - 10), cy - ca * (r - 10));
    sa = sin(wa + 0.62); ca = cos(wa + 0.62);
    gfx_set(0.78, 0.78, 0.76, 0.8); gfx_line(cx + sa * 32, cy - ca * 32, cx + sa * (r - 10), cy - ca * (r - 10));
    gfx_set(0.78, 0.78, 0.76, 0.6); gfx_arc(cx, cy, r - 10, wa - 0.62, wa + 0.62, 1);
    w += 1;
  );

  // hub with three drive dogs and a chrome cap
  gfx_set(0.06, 0.06, 0.05, 0.6); gfx_circle(cx + 1, cy + 2, 30, 1, 1);
  gfx_set(0.16, 0.16, 0.15, 1); gfx_circle(cx, cy, 29, 1, 1);
  gfx_set(0.30, 0.30, 0.29, 1); gfx_circle(cx, cy, 29, 0, 1);
  w = 0;
  loop(3,
    wa = ui_rot + $pi / 3 + w * 2 * $pi / 3; sa = sin(wa); ca = cos(wa);
    gfx_set(0.08, 0.08, 0.07, 1); gfx_circle(cx + sa * 21, cy - ca * 21, 4.5, 1, 1);
    gfx_set(0.40, 0.40, 0.38, 1); gfx_circle(cx + sa * 21, cy - ca * 21, 4.5, 0, 1);
    w += 1;
  );
  gfx_set(0.30, 0.30, 0.29, 1); gfx_circle(cx, cy, 14, 1, 1);
  i = 0;
  loop(13,
    gfx_set(0.58 + 0.03 * (i % 3), 0.58 + 0.03 * (i % 3), 0.56 + 0.03 * (i % 3), 1);
    gfx_circle(cx, cy, 13 - i * 0.9, 0, 1);
    i += 1;
  );
  gfx_set(1, 1, 1, 0.10); gfx_circle(cx - 4, cy - 4, 5, 1, 1);
  gfx_set(0.10, 0.10, 0.09, 1); gfx_circle(cx, cy, 3, 1, 1);
  // index mark on the cap (turns with the reel)
  sa = sin(ui_rot); ca = cos(ui_rot);
  gfx_set(0.20, 0.20, 0.19, 1); gfx_line(cx + sa * 5, cy - ca * 5, cx + sa * 12, cy - ca * 12);

  // name plate (outer side, below the well)
  lbl_w = 124; lx = cx + side * 34 - lbl_w / 2; ly = cy + rw + 8;
  gfx_set(0.12, 0.12, 0.11, 1); gfx_rect(lx, ly, lbl_w, 22, 1);
  ui_inv_bevel(lx, ly, lbl_w, 22);
  gfx_set(0.30, 0.29, 0.26, 1); gfx_rect(lx + 2, ly + 2, lbl_w - 4, 18, 0);
  ui_fmt(k); sprintf(#ui_tmp, "%s  %s", ui_k_short[k], #ui_val);
  gfx_setfont(2);
  act ? gfx_set(0.95, 0.70, 0.35, 1) : gfx_set(0.46, 0.44, 0.40, 1);
  ui_text_c(lx, ly, lbl_w, 22, #ui_tmp);
);

// ---------- tape path: reels -> guide rollers -> head block ----------
function ui_roller(cx, cy) local(sa, ca) (
  gfx_set(0.05, 0.05, 0.04, 0.8); gfx_circle(cx + 2, cy + 3, 10, 1, 1);
  gfx_set(0.62, 0.62, 0.60, 1); gfx_circle(cx, cy, 10, 1, 1);
  gfx_set(0.80, 0.80, 0.78, 1); gfx_circle(cx, cy, 10, 0, 1);
  gfx_set(0.20, 0.20, 0.19, 1); gfx_circle(cx, cy, 4, 1, 1);
  sa = sin(ui_rot * 4); ca = cos(ui_rot * 4);
  gfx_set(0.35, 0.35, 0.34, 1); gfx_line(cx + sa * 5, cy - ca * 5, cx + sa * 9, cy - ca * 9);
);
function ui_tape_line(x0, y0, x1, y1) local() (
  gfx_set(0.10, 0.06, 0.04, 0.6); gfx_line(x0, y0 + 2, x1, y1 + 2, 1);
  gfx_set(0.24, 0.15, 0.09, 1);   gfx_line(x0, y0, x1, y1, 1); gfx_line(x0, y0 + 1, x1, y1 + 1, 1);
  gfx_set(0.40, 0.28, 0.17, 0.7); gfx_line(x0, y0 - 1, x1, y1 - 1, 1);
);
function ui_draw_transport() local(exl, eyl, exr, eyr, ty, i, hx)
(
  ty = 384;                                         // top edge of the heads / guide rollers
  // the tape becomes visible at the flange edge, through the bottom gap of the reel scales
  exl = ui_k_x[0] + sin(0.86 * $pi) * (ui_k_r[0] + 1); eyl = ui_k_y[0] - cos(0.86 * $pi) * (ui_k_r[0] + 1);
  exr = ui_k_x[1] - sin(0.86 * $pi) * (ui_k_r[1] + 1); eyr = ui_k_y[1] - cos(0.86 * $pi) * (ui_k_r[1] + 1);

  // head block cover
  gfx_set(0.06, 0.06, 0.05, 0.85); gfx_rect(333, 394, 136, 46, 1);
  gfx_set(0.13, 0.12, 0.10, 1); gfx_rect(331, 392, 136, 46, 1);
  ui_bevel_box(331, 392, 136, 46);
  // three chrome heads: ERASE / REC / PLAY
  i = 0;
  loop(3,
    hx = 364 + i * 26;
    gfx_set(0.05, 0.05, 0.04, 0.8); gfx_rect(hx + 1, ty + 3, 20, 20, 1);
    gfx_set(0.55, 0.55, 0.54, 1); gfx_roundrect(hx, ty + 1, 20, 20, 4, 1);
    gfx_set(0.76, 0.76, 0.74, 1); gfx_rect(hx + 2, ty + 2, 16, 6, 1);
    gfx_set(0.62, 0.62, 0.60, 1); gfx_rect(hx + 2, ty + 8, 16, 11, 1);
    gfx_set(0.15, 0.15, 0.14, 1); gfx_line(hx + 10, ty + 2, hx + 10, ty + 19);   // head gap
    i += 1;
  );
  gfx_setfont(3); gfx_set(0.50, 0.48, 0.44, 1);
  ui_text_c(331, 410, 136, 11, "ERASE  REC  PLAY");
  gfx_setfont(3); gfx_set(0.40, 0.38, 0.35, 1);
  ui_text_c(331, 423, 136, 11, "AIRWINDOWS TOTAPE9");
  // run lamp (lit while the transport plays)
  ui_jewel(447, 400, 3, (play_state & 1) && slider11 < 0.5, 0);

  // guide rollers and tape (tape wraps over the roller tops and runs across the heads)
  ui_roller(318, ty + 10);
  ui_roller(482, ty + 10);
  ui_tape_line(exl, eyl, 310, ty + 2);
  ui_tape_line(310, ty + 2, 318, ty - 1);
  ui_tape_line(318, ty - 1, 482, ty - 1);
  ui_tape_line(482, ty - 1, 490, ty + 2);
  ui_tape_line(490, ty + 2, exr, eyr);
);

// ---------- push switch ----------
function ui_draw_button(b) local(x, y, w, h, on, hot)
(
  x = ui_b_x[b]; y = ui_b_y[b]; w = ui_btn_w; h = ui_btn_h;
  b == 3 ? ( on = dsp_meas_n > 0; hot = 1; ) : ( on = slider(ui_b_sl[b]) > 0.5; hot = 0; );
  gfx_set(0.07, 0.07, 0.06, 1); gfx_rect(x - 3, y - 3, w + 6, h + 6, 1);
  gfx_set(0.26, 0.26, 0.24, 1); gfx_line(x - 3, y + h + 2, x + w + 2, y + h + 2);
  on ? gfx_set(0.22, 0.21, 0.19, 1) : gfx_set(0.16, 0.16, 0.15, 1);
  gfx_rect(x, y, w, h, 1);
  on ? gfx_set(0.36, 0.35, 0.32, 1) : gfx_set(0.27, 0.27, 0.25, 1);
  gfx_line(x, y, x + w - 1, y);
  gfx_set(0.08, 0.08, 0.07, 1); gfx_line(x, y + h - 1, x + w - 1, y + h - 1);
  ui_jewel(x + 14, y + h / 2, 4.5, on, hot);
  gfx_setfont(2);
  on ? gfx_set(0.92, 0.90, 0.84, 1) : gfx_set(0.50, 0.48, 0.44, 1);
  b == 3 && on ? (
    sprintf(#ui_tmp, "%d s LEFT", ceil(dsp_meas_n / max(srate, 44100)));
    ui_text_c(x + 24, y, w - 28, h, #ui_tmp);
  ) : ui_text_c(x + 24, y, w - 28, h, ui_b_lbl[b]);
);

// ---------- draggable readout (TRIM / OUTPUT) ----------
function ui_draw_readout(i, hov) local(x, y, v)
(
  x = ui_r_x[i]; y = ui_r_y[i];
  v = i == 1 ? dsp_comp_db + slider(ui_r_sl[i]) : slider(ui_r_sl[i]);   // OUT = AUTO GAIN + manual trim
  gfx_set(0.07, 0.07, 0.06, 1); gfx_rect(x, y, ui_rw, ui_rh, 1);
  ui_inv_bevel(x, y, ui_rw, ui_rh);
  hov ? ( gfx_set(0.95, 0.70, 0.35, 0.35); gfx_rect(x + 1, y + 1, ui_rw - 2, ui_rh - 2, 0); );
  sprintf(#ui_tmp, "%s %+.1f dB", ui_r_name[i], v);
  gfx_setfont(2);
  (i == 0 && dsp_meas_n > 0) ? gfx_set(0.55, 0.42, 0.25, 1) : gfx_set(0.95, 0.70, 0.35, 1);
  ui_text_c(x, y, ui_rw, ui_rh, #ui_tmp);
);

// ---------- offscreen buffer ----------
gfx_getimgdim(1, ui_cw, ui_ch);
(ui_cw != ui_bw || ui_ch != ui_bh) ? gfx_setimgdim(1, ui_bw, ui_bh);
gfx_dest = 1;
gfx_setfont(1, "Arial", 22, 'b');
gfx_setfont(2, "Arial", 12, 'b');
gfx_setfont(3, "Arial", 10);
gfx_setfont(4, "Arial", 13);
gfx_setfont(5, "Arial", 10, 'b');

// reel rotation (only while the transport plays)
ui_now = time_precise();
ui_dt = ui_last_t > 0 ? min(0.2, ui_now - ui_last_t) : 0.03;
ui_last_t = ui_now;
(play_state & 1) && slider11 < 0.5 ? ui_rot += ui_dt * 1.4;
ui_rot > 2 * $pi ? ui_rot -= 2 * $pi;

// chassis, walnut side cheeks, faceplate
gfx_set(0.11, 0.11, 0.10, 1); gfx_rect(0, 0, ui_bw, ui_bh, 1);
ui_i = 0;
loop(2,
  ui_x0 = ui_i ? ui_bw - 22 : 0;
  gfx_set(0.20, 0.12, 0.07, 1); gfx_rect(ui_x0, 0, 22, ui_bh, 1);
  ui_j = 0;
  loop(9,
    gfx_set(0.14 + 0.03 * (ui_j % 3), 0.08 + 0.02 * (ui_j % 3), 0.04, 0.55);
    gfx_line(ui_x0 + 2 + ui_j * 2.2, 0, ui_x0 + 3 + ui_j * 2.2 + sin(ui_j * 1.7) * 2, ui_bh);
    ui_j += 1;
  );
  gfx_set(0.30, 0.19, 0.11, 1); gfx_line(ui_x0, 0, ui_x0, ui_bh); gfx_line(ui_x0, 0, ui_x0 + 21, 0);
  gfx_set(0.06, 0.04, 0.02, 1); gfx_line(ui_x0 + 21, 0, ui_x0 + 21, ui_bh); gfx_line(ui_x0, ui_bh - 1, ui_x0 + 21, ui_bh - 1);
  ui_i += 1;
);
gfx_set(0.17, 0.17, 0.16, 1); gfx_rect(26, 8, ui_bw - 52, ui_bh - 16, 1);
ui_bevel_box(26, 8, ui_bw - 52, ui_bh - 16);
ui_screw(40, 24); ui_screw(ui_bw - 40, 24); ui_screw(40, ui_bh - 24); ui_screw(ui_bw - 40, ui_bh - 24);

// header badge
gfx_set(0.12, 0.12, 0.11, 1); gfx_rect(240, 16, 320, 50, 1);
ui_inv_bevel(240, 16, 320, 50);
gfx_set(0.30, 0.29, 0.26, 1); gfx_rect(243, 19, 314, 44, 0);
gfx_setfont(1); gfx_set(0.78, 0.75, 0.68, 1);
ui_text_c(240, 18, 320, 28, "TAPE MACHINE");
gfx_setfont(5); gfx_set(0.58, 0.55, 0.50, 1);
ui_text_c(240, 46, 320, 14, "OVERSTRATUM AUDIO");

// center column captions
ui_caption(400, 90, 104, "CALIBRATE");
ui_caption(400, 214, 104, "OUTPUT");
ui_caption((ui_k_x[4] + ui_k_x[5]) / 2, 446, 176, "FLUTTER");
ui_caption(400, 446, 280, "TONE");
ui_caption((ui_k_x[7] + ui_k_x[8]) / 2, 446, 176, "HEAD BUMP");

// ---------- mouse (base-canvas coordinates) ----------
ui_scale = min(gfx_w / ui_bw, gfx_h / ui_bh);
ui_scale <= 0 ? ui_scale = 1;
ui_ox = (gfx_w - ui_bw * ui_scale) * 0.5;
ui_oy = (gfx_h - ui_bh * ui_scale) * 0.5;
ui_mx = (mouse_x - ui_ox) / ui_scale;
ui_my = (mouse_y - ui_oy) / ui_scale;
ui_lmb = mouse_cap & 1;

ui_hover_k = -1; ui_hover_b = -1; ui_hover_r = -1;
ui_i = 0;
loop(ui_nk,
  ui_rr = ui_k_r[ui_i] > 40 ? ui_k_r[ui_i] + 16 : ui_k_r[ui_i] + 14;
  (sqr(ui_mx - ui_k_x[ui_i]) + sqr(ui_my - ui_k_y[ui_i]) <= sqr(ui_rr)) ? ui_hover_k = ui_i;
  ui_i += 1;
);
ui_i = 0;
loop(ui_nb,
  (ui_mx >= ui_b_x[ui_i] && ui_mx <= ui_b_x[ui_i] + ui_btn_w && ui_my >= ui_b_y[ui_i] && ui_my <= ui_b_y[ui_i] + ui_btn_h) ? ui_hover_b = ui_i;
  ui_i += 1;
);
ui_i = 0;
loop(ui_nr,
  (ui_mx >= ui_r_x[ui_i] && ui_mx <= ui_r_x[ui_i] + ui_rw && ui_my >= ui_r_y[ui_i] && ui_my <= ui_r_y[ui_i] + ui_rh) ? ui_hover_r = ui_i;
  ui_i += 1;
);

ui_lmb && !ui_lmb_prev ? (
  ui_drag_k = ui_hover_k; ui_drag_r = ui_hover_r; ui_drag_y = ui_my;
  ui_drag_v = ui_drag_k >= 0 ? slider(ui_k_sl[ui_drag_k]) : ui_drag_r >= 0 ? slider(ui_r_sl[ui_drag_r]) : 0;
  ui_click_id = ui_drag_k >= 0 ? ui_drag_k : ui_drag_r >= 0 ? 100 + ui_drag_r : -1;
  (ui_now - ui_click_t) < 0.3 && ui_click_id >= 0 && ui_click_id == ui_click_last ? (
    // double click: reset to default
    ui_drag_k >= 0 ? (
      slider(ui_k_sl[ui_drag_k]) = ui_k_def[ui_drag_k]; slider_automate(2 ^ (ui_k_sl[ui_drag_k] - 1));
    ) : (
      slider(ui_r_sl[ui_drag_r]) = 0; slider_automate(2 ^ (ui_r_sl[ui_drag_r] - 1));
    );
    ui_drag_k = -1; ui_drag_r = -1;
  );
  ui_click_t = ui_now; ui_click_last = ui_click_id;
  ui_hover_b >= 0 ? (
    ui_hover_b == 3 ? (
      dsp_meas_n <= 0 ? ( cal_request = 1; );     // calibration starts in @block
    ) : (
      slider(ui_b_sl[ui_hover_b]) = slider(ui_b_sl[ui_hover_b]) > 0.5 ? 0 : 1;
      slider_automate(2 ^ (ui_b_sl[ui_hover_b] - 1));
    );
  );
);
ui_lmb && ui_drag_k >= 0 ? (
  ui_range = ui_k_max[ui_drag_k] - ui_k_min[ui_drag_k];
  ui_sens = (mouse_cap & 4) ? 1000 : ui_k_r[ui_drag_k] > 40 ? 300 : 200;    // ctrl = fine
  ui_newv = ui_drag_v + (ui_drag_y - ui_my) / ui_sens * ui_range;
  ui_newv = min(ui_k_max[ui_drag_k], max(ui_k_min[ui_drag_k], ui_newv));
  ui_newv = floor(ui_newv / ui_k_step[ui_drag_k] + 0.5) * ui_k_step[ui_drag_k];
  ui_newv != slider(ui_k_sl[ui_drag_k]) ? (
    slider(ui_k_sl[ui_drag_k]) = ui_newv; slider_automate(2 ^ (ui_k_sl[ui_drag_k] - 1));
  );
);
ui_lmb && ui_drag_r >= 0 ? (
  ui_newv = ui_drag_v + (ui_drag_y - ui_my) * ((mouse_cap & 4) ? 0.04 : 0.2);
  ui_newv = min(ui_r_max[ui_drag_r], max(ui_r_min[ui_drag_r], ui_newv));
  ui_newv = floor(ui_newv * 10 + 0.5) / 10;
  ui_newv != slider(ui_r_sl[ui_drag_r]) ? (
    slider(ui_r_sl[ui_drag_r]) = ui_newv; slider_automate(2 ^ (ui_r_sl[ui_drag_r] - 1));
  );
);
!ui_lmb ? ( ui_drag_k = -1; ui_drag_r = -1; );
ui_lmb_prev = ui_lmb;
ui_act_k = ui_drag_k >= 0 ? ui_drag_k : ui_drag_r >= 0 ? -1 : ui_hover_k;
ui_act_r = ui_drag_r >= 0 ? ui_drag_r : ui_drag_k >= 0 ? -1 : ui_hover_r;

// ---------- draw controls ----------
ui_draw_reel(0, -1);
ui_draw_reel(1, 1);
ui_draw_transport();
ui_i = 2; loop(ui_nk - 2, ui_draw_knob(ui_i); ui_i += 1;);
ui_i = 0; loop(ui_nb, ui_draw_button(ui_i); ui_i += 1;);
ui_draw_readout(0, ui_act_r == 0);
ui_draw_readout(1, ui_act_r == 1);

// ---------- footer help box ----------
gfx_set(0.07, 0.07, 0.06, 1); gfx_rect(56, 598, ui_bw - 112, 38, 1);
ui_inv_bevel(56, 598, ui_bw - 112, 38);
gfx_setfont(2); gfx_set(0.95, 0.92, 0.84, 1);
ui_act_k >= 0 ? (
  ui_fmt(ui_act_k);
  sprintf(#ui_line1, "%s: %s", ui_k_name[ui_act_k], #ui_val);
  ui_act_k == 0 ? (
    slider14 > 0.5 ? ( #ui_line2 = "Drives the tape harder for more color and glue. AUTO GAIN is on, so the loudness stays the same."; )
                   : ( #ui_line2 = "Drives the tape harder for more color and glue. AUTO GAIN is off, so more drive = louder."; );
  ) : ( #ui_line2 = ui_k_help[ui_act_k]; );
) : ui_act_r >= 0 ? (
  ui_act_r == 1 ? (
    sprintf(#ui_line1, "OUTPUT: %+.1f dB  (AUTO GAIN %+.1f dB, your trim %+.1f dB)", dsp_comp_db + slider13, dsp_comp_db, slider13);
  ) : ( sprintf(#ui_line1, "%s: %+.1f dB", ui_r_name[ui_act_r], slider(ui_r_sl[ui_act_r])); );
  #ui_line2 = ui_r_help[ui_act_r];
) : ui_hover_b == 0 ? (
  slider11 > 0.5 ? ( #ui_line1 = "BYPASS: ON (whole plugin is off)"; ) : ( #ui_line1 = "BYPASS: OFF (plugin is active)"; );
  #ui_line2 = ui_b_help[0];
) : ui_hover_b == 1 ? (
  slider12 > 0.5 ? ( #ui_line1 = "DELTA: ON (you hear only the difference)"; ) : ( #ui_line1 = "DELTA: OFF (normal output)"; );
  #ui_line2 = ui_b_help[1];
) : ui_hover_b == 2 ? (
  slider14 > 0.5 ? (
    sprintf(#ui_line1, "AUTO GAIN: ON (loudness stays the same, now %+.1f dB)", dsp_comp_db);
    #ui_line2 = "Driving harder keeps the same loudness. Switch off to use the gained headroom: more drive = louder.";
  ) : (
    sprintf(#ui_line1, "AUTO GAIN: OFF (peaks stay at the dry level, now %+.1f dB)", dsp_comp_db);
    #ui_line2 = "Dry and tape have the same peak level, so more drive makes it louder. Switch on to keep the loudness.";
  );
) : ui_hover_b == 3 ? (
  dsp_meas_n > 0 ? ( sprintf(#ui_line1, "CALIBRATE: MEASURING (%d s left)", ceil(dsp_meas_n / max(srate, 44100))); )
                      : ( #ui_line1 = "CALIBRATE: READY"; );
  #ui_line2 = ui_b_help[3];
) : (
  #ui_line1 = "TAPE MACHINE";
  #ui_line2 = "Point at a reel, knob or switch to see what it does. Drag up/down, Ctrl = fine, double-click = reset.";
);
gfx_x = 64; gfx_y = 602; gfx_drawstr(#ui_line1);
gfx_setfont(4); gfx_set(0.86, 0.84, 0.78, 1);
gfx_x = 64; gfx_y = 618; gfx_drawstr(#ui_line2);

// ---------- blit scaled to window ----------
gfx_dest = -1;
gfx_set(0.11, 0.11, 0.10, 1); gfx_rect(0, 0, gfx_w, gfx_h, 1);
gfx_x = ui_ox; gfx_y = ui_oy;
gfx_blit(1, ui_scale, 0, 0, 0, ui_bw, ui_bh, ui_ox, ui_oy, ui_bw * ui_scale, ui_bh * ui_scale);
