/**
* OutputsBackendConfig — the editable, per-backend specialisation of the Outputs
* panel (backends-spec §4) plus the named-preset bar (§5).
*
* Layout: ONE preset bar (save-as / restore / rename / delete, per-backend
* namespace) on top, then a per-backend config section:
* - MIDI → output-port picker, number-of-CCs, per-output CC#/channel/name.
* - OSC → bridge URL + connect status + send-raw toggle, per-output path/range.
* - VCV/CV→ bridge URL + connect status + send-raw toggle, per-output polarity
* (uni 0–10 V / bipolar ±5 V). The browser drives + trains the VCV
* module over the same Deno bridge.
* - Synth/Particle/Editor → handled by ModeConfig in Drawers (no extra config here).
*
* Everything is editable inline; writes go through the shared MFParam store
* (ctx.setParam) — never a second data path. The full-depth modal reuses the
* same sections via BackendAdvanced.
*/
import { useEffect, useState } from 'react';
import type { ConsoleCtx } from '../console/types';
import type { BackendId } from './output-state';
import { defaultMidiSpec, defaultOscSpec, defaultVcvSpec } from './output-state';
import {
applyPreset,
deletePreset,
getPreset,
listPresets,
renamePreset,
savePreset,
type OutputPreset,
} from '../backends/presets';
import { MIDI_DEVICES, MIDI_DEVICES_BY_ID, type MidiDeviceParam } from '../midi-devices';
function num(s: string, fallback: number): number {
const v = parseFloat(s);
return Number.isFinite(v) ? v : fallback;
}
const cellInput: React.CSSProperties = {
width: '100%',
background: 'var(--bg-1)',
border: '1px solid var(--line)',
borderRadius: 'var(--r-1)',
color: 'var(--fg)',
fontFamily: 'var(--font-mono)',
fontSize: 'var(--fs-xs)',
padding: '3px 6px',
boxSizing: 'border-box',
};
const btn = (color: string): React.CSSProperties => ({
fontSize: 'var(--fs-xs)',
fontFamily: 'var(--font-mono)',
padding: '3px 9px',
cursor: 'pointer',
borderRadius: 'var(--r-pill)',
border: `1px solid ${color}`,
background: 'transparent',
color,
});
function SectionLabel({ children }: { children: React.ReactNode }) {
return (
{children}
);
}
function Th({ children }: { children: React.ReactNode }) {
return (
{children}
);
}
// ---- Named-preset bar (backends-spec §5) -----------------------------------
function PresetBar({ ctx, backend }: { ctx: ConsoleCtx; backend: BackendId }) {
const [presets, setPresets] = useState([]);
const [name, setName] = useState('');
const [selected, setSelected] = useState('');
const refresh = () => setPresets(listPresets(backend));
useEffect(() => {
refresh();
setSelected('');
setName('');
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [backend]);
const backendSettings = (): Record => {
if (backend === 'midi') return { outputId: ctx.midiOutputId, ccCount: ctx.midiCcCount };
if (backend === 'osc') return { url: ctx.oscUrl, sendRaw: ctx.oscSendRaw };
if (backend === 'vcv') return { url: ctx.vcvUrl, sendRaw: ctx.vcvSendRaw };
return {};
};
const applySettings = (s?: Record) => {
if (!s) return;
if (backend === 'midi') {
if ('outputId' in s) ctx.setMidiOutputId((s.outputId as string | null) ?? null);
if ('ccCount' in s) ctx.setMidiCcCount(Number(s.ccCount) || ctx.midiCcCount);
} else if (backend === 'osc') {
if ('url' in s) ctx.setOscUrl(String(s.url));
if ('sendRaw' in s) ctx.setOscSendRaw(Boolean(s.sendRaw));
} else if (backend === 'vcv') {
if ('url' in s) ctx.setVcvUrl(String(s.url));
if ('sendRaw' in s) ctx.setVcvSendRaw(Boolean(s.sendRaw));
}
};
const doSave = () => {
const n = name.trim();
if (!n) return;
savePreset(backend, n, ctx.params, backendSettings());
refresh();
setSelected(n);
};
const doRestore = (n: string) => {
const p = getPreset(backend, n);
if (!p) return;
ctx.setParams(applyPreset(ctx.params, p));
applySettings(p.settings);
};
const doDelete = () => {
if (!selected) return;
deletePreset(backend, selected);
refresh();
setSelected('');
};
const doRename = () => {
const to = name.trim();
if (!selected || !to) return;
if (renamePreset(backend, selected, to)) {
refresh();
setSelected(to);
}
};
return (
Presets · {backend}
setName(e.target.value)}
/>
);
}
// ---- Device template picker (synth-midi-cc templates) ----------------------
/**
* Pick an external synth (e.g. Moog Sub 37), see its parameters BY NAME, tick the
* ones to control, and "Apply" — this fills the per-output CC table (CC#, channel,
* name) from the device template and sets the CC count. Pure local state + the
* existing setParam/setMidiCcCount; the applied result lives in the shared params
* store, so it is saved/restored by the named-preset bar like any other config.
*/
function DeviceTemplatePicker({ ctx }: { ctx: ConsoleCtx }) {
const [deviceId, setDeviceId] = useState('');
const [selected, setSelected] = useState>(new Set());
const device = deviceId ? MIDI_DEVICES_BY_ID[deviceId] : undefined;
const limit = ctx.params.length; // engine output count caps how many CCs we can drive
const pickDevice = (id: string) => {
setDeviceId(id);
setSelected(new Set());
};
const toggle = (id: string) =>
setSelected((prev) => {
const next = new Set(prev);
if (next.has(id)) next.delete(id);
else next.add(id);
return next;
});
const selectFirst = (n: number) => {
if (!device) return;
setSelected(new Set(device.params.slice(0, n).map((p) => p.id)));
};
const apply = () => {
if (!device) return;
const chosen = device.params.filter((p) => selected.has(p.id)).slice(0, limit);
if (chosen.length === 0) return;
ctx.setMidiCcCount(chosen.length);
chosen.forEach((p, i) =>
ctx.setParam(i, {
name: p.label,
midi: { cc: p.cc, channel: device.default_channel, name: p.label, value: 0 },
}),
);
};
// Stable, first-seen-order grouping for the checklist.
const groups: { group: string; params: MidiDeviceParam[] }[] = [];
if (device) {
const byGroup = new Map();
for (const p of device.params) {
if (!byGroup.has(p.group)) {
const arr: MidiDeviceParam[] = [];
byGroup.set(p.group, arr);
groups.push({ group: p.group, params: arr });
}
byGroup.get(p.group)!.push(p);
}
}
const willApply = device ? Math.min(selected.size, limit) : 0;
const overflow = selected.size > limit;
return (
<>
Device template
{device && (
<>
>
)}
{device && (
<>
{device.manufacturer} · default ch {device.default_channel} · {selected.size} selected
{overflow ? ` (only the first ${limit} will map — engine has ${limit} outputs)` : ''}. Tick the parameters
the model should drive, then Apply. You can still tweak CC#/channel below.
The Deno OSC bridge process must be running locally (see manifold/osc-bridge). The browser sends over
WebSocket; the bridge encodes OSC and forwards over UDP.
The VCV Rack NISPS module AND the Deno OSC bridge (manifold/osc-bridge) must both be running. The browser drives
the module's inputs and forwards the verdict loop (thumbs up/down, explore-and-place) over the bridge, so the
module's embedded net trains in lock-step. Default module UDP port 7001.