const PAGE = 16; const state = { doc: null, saved: null, preset: 0, page: 2, bank: 0, kind: null, index: 0, }; const $ = (id) => document.getElementById(id); // Colours are stored as the picker gives them, 0–255, as MidiSuite writes them. function cssColour(rgb) { return `rgb(${rgb.join(",")})`; } function ink(rgb) { const [r, g, b] = rgb; return (r * 0.3 + g * 0.59 + b * 0.11) > 150 ? "#111" : "#f4f6fb"; } function hexColour(rgb) { return `#${rgb.map((part) => part.toString(16).padStart(2, "0")).join("")}`; } function setStatus(text, kind) { const node = $("status"); node.textContent = text; node.className = kind || ""; } function dirty() { return state.doc !== null && JSON.stringify(state.doc) !== state.saved; } function preset() { return state.doc.presets[state.preset]; } function selected() { if (!state.doc || !state.kind) return null; const block = preset(); if (state.kind === "pad") return block.pads[state.page * PAGE + state.index]; if (state.kind === "knob") return block.knobs[state.bank * 8 + state.index]; return block.buttons[state.index]; } function typeChoices(kind, current) { const known = (state.doc.types && state.doc.types[kind]) || []; const options = known.map((item) => ({ value: item.value, name: item.name })); if (!options.some((item) => item.value === current)) { const hex = current.toString(16).padStart(2, "0"); options.push({ value: current, name: `0x${hex}` }); } return options; } async function request(url, options) { const response = await fetch(url, options); const type = response.headers.get("Content-Type") || ""; if (type.includes("application/json")) { const body = await response.json(); if (!response.ok) throw new Error(body.error || response.statusText); return body; } if (!response.ok) throw new Error(response.statusText); return response.blob(); } function mark() { const unsaved = dirty(); $("save").disabled = !state.doc; $("export").disabled = !state.doc; $("save").classList.toggle("dirty", unsaved); return unsaved; } function touch() { if (mark()) setStatus("Not saved on the pad.", "dirty"); else setStatus("No unsaved edits.", "ok"); } function showEmpty(text, kind) { const node = $("empty"); node.hidden = false; node.textContent = text; node.className = kind || ""; $("board").classList.remove("ready"); } async function load(manual) { if (manual && dirty() && !window.confirm("Load from the pad and drop these edits?")) return; setStatus("Loading the pad…"); showEmpty("Loading the pad…"); try { state.doc = await request("/api/load", { method: "POST" }); state.saved = JSON.stringify(state.doc); render(); setStatus("Loaded from the pad.", "ok"); } catch (error) { setStatus(error.message, "err"); showEmpty(error.message, "err"); } } async function save() { if (!state.doc) return; setStatus("Saving…"); $("save").disabled = true; try { const body = await request("/api/save", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(state.doc), }); state.saved = JSON.stringify(state.doc); setStatus(body.message, "ok"); } catch (error) { setStatus(error.message, "err"); } mark(); } async function exportFile() { if (!state.doc) return; try { const blob = await request("/api/export", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(state.doc), }); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = "smc-pad-preset.bin"; link.click(); URL.revokeObjectURL(url); } catch (error) { setStatus(error.message, "err"); } } async function importFile(file) { setStatus("Reading file…"); try { const raw = new Uint8Array(await file.arrayBuffer()); state.doc = await request("/api/import", { method: "POST", headers: { "Content-Type": "application/octet-stream" }, body: raw, }); render(); setStatus("Imported. Save to pad to store it.", "dirty"); } catch (error) { setStatus(error.message, "err"); } } function render() { if (!state.doc) return; $("board").classList.add("ready"); renderGlobal(); renderPresets(); renderPages(); renderPads(); renderKnobs(); renderButtons(); renderInspector(); mark(); } function renderGlobal() { const row = $("global"); row.replaceChildren(); const block = state.doc.global; if (!block) return; const fields = [ ["Tempo", "tempo", 1, 999], ["Swing", "swing", 0, 255], ["Pad curve", "curve", 0, 255], ["Transpose", "transpose", -128, 127], ]; for (const [label, key, min, max] of fields) { row.append(field(label, numberInput(block, key, min, max))); } } function renderPresets() { const row = $("presets"); row.replaceChildren(); state.doc.presets.forEach((_preset, index) => { const button = document.createElement("button"); button.type = "button"; button.textContent = String(index + 1); button.classList.toggle("selected", index === state.preset); button.addEventListener("click", () => { state.preset = index; render(); }); row.append(button); }); } function renderPages() { const select = $("page"); const current = select.value; select.replaceChildren(); const order = [2, 3, 0, 1, 4, 5, 6, 7]; const names = { 2: "Bank 1", 3: "Bank 2" }; for (const page of order) { const pads = preset().pads.slice(page * PAGE, page * PAGE + PAGE); const option = document.createElement("option"); option.value = String(page); const name = names[page] || `Page ${page + 1}`; option.textContent = `${name} · notes ${pads[0].note}–${pads[PAGE - 1].note}`; select.append(option); } select.value = current || String(state.page); state.page = Number(select.value); const pads = preset().pads.slice(state.page * PAGE, state.page * PAGE + PAGE); $("page-notes").textContent = `notes ${pads[0].note}–${pads[PAGE - 1].note}`; } function paint(node, rgb) { node.style.background = cssColour(rgb); node.style.color = ink(rgb); } function renderPads() { const grid = $("pads"); grid.replaceChildren(); const pads = preset().pads; for (let row = 3; row >= 0; row -= 1) { for (let col = 0; col < 4; col += 1) { const index = row * 4 + col; const pad = pads[state.page * PAGE + index]; const button = document.createElement("button"); button.type = "button"; button.className = "pad"; button.textContent = String(index + 1); paint(button, pad.rgb); button.style.opacity = pad.led ? "" : "0.35"; button.classList.toggle("selected", state.kind === "pad" && state.index === index); button.addEventListener("click", () => select("pad", index)); grid.append(button); } } } function renderKnobs() { const row = $("knobs"); row.replaceChildren(); for (let index = 0; index < 8; index += 1) { const knob = preset().knobs[state.bank * 8 + index]; const button = document.createElement("button"); button.type = "button"; button.className = "knob"; button.textContent = String(state.bank * 8 + index + 1); button.title = `CC ${knob.number}`; button.classList.toggle("selected", state.kind === "knob" && state.index === index); button.addEventListener("click", () => select("knob", index)); row.append(button); } } function renderButtons() { const row = $("buttons"); row.replaceChildren(); preset().buttons.forEach((button, index) => { const node = document.createElement("button"); node.type = "button"; node.className = "btn"; node.textContent = button.name; node.classList.toggle("selected", state.kind === "button" && state.index === index); node.addEventListener("click", () => select("button", index)); row.append(node); }); } let colourTimer = 0; function paintColour(index, rgb) { const edited = state.preset; preset().pads[index].rgb = rgb.slice(); renderPads(); window.clearTimeout(colourTimer); colourTimer = window.setTimeout(() => sendColour(edited, index, rgb), 180); } async function sendColour(preset, index, rgb) { try { await request("/api/colour", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ preset, index, rgb }), }); } catch (error) { setStatus(error.message, "err"); } } function select(kind, index) { state.kind = kind; state.index = index; render(); } function field(label, node) { const wrap = document.createElement("label"); wrap.className = "field"; wrap.append(label, node); return wrap; } function numberInput(item, key, min, max) { const input = document.createElement("input"); input.type = "number"; input.min = String(min); input.max = String(max); input.value = String(item[key]); input.addEventListener("input", () => { const value = Number(input.value); if (!Number.isInteger(value) || value < min || value > max) return; item[key] = value; if (key === "led") renderPads(); if (key === "note") { const pads = preset().pads.slice(state.page * PAGE, state.page * PAGE + PAGE); $("page-notes").textContent = `notes ${pads[0].note}–${pads[PAGE - 1].note}`; } touch(); }); return input; } function renderInspector() { const aside = $("inspector"); const item = selected(); aside.replaceChildren(); const title = document.createElement("h2"); if (!item) { title.textContent = "Control"; const note = document.createElement("p"); note.className = "muted"; note.textContent = "Pick a pad, knob, or button."; aside.append(title, note); return; } if (state.kind === "pad") title.textContent = `Pad ${state.index + 1}`; else if (state.kind === "knob") title.textContent = `Knob ${state.bank * 8 + state.index + 1}`; else title.textContent = item.name; aside.append(title); if (state.kind !== "pad") { const type = document.createElement("select"); typeChoices(state.kind, item.type).forEach((option) => { const node = document.createElement("option"); node.value = String(option.value); node.textContent = option.name; node.selected = option.value === item.type; type.append(node); }); type.addEventListener("change", () => { item.type = Number(type.value); touch(); }); aside.append(field(state.kind === "button" ? "Mode" : "Type", type)); } aside.append(field("Channel", numberInput(item, "channel", 1, 16))); if (state.kind === "pad") { aside.append(field("Note", numberInput(item, "note", 0, 127))); aside.append(field("Min velocity", numberInput(item, "min", 0, 127))); aside.append(field("Max velocity", numberInput(item, "max", 0, 127))); const colour = document.createElement("input"); colour.type = "color"; colour.value = hexColour(item.rgb); colour.addEventListener("input", () => { const hex = colour.value; const rgb = [1, 3, 5].map((at) => parseInt(hex.slice(at, at + 2), 16)); paintColour(state.page * PAGE + state.index, rgb); touch(); }); aside.append(field("Colour", colour)); aside.append(field("Led", numberInput(item, "led", 0, 255))); } else if (state.kind === "knob") { aside.append(field("CC", numberInput(item, "number", 0, 127))); aside.append(field("Speed", numberInput(item, "speed", 0, 127))); aside.append(field("Min", numberInput(item, "left", 0, 127))); aside.append(field("Max", numberInput(item, "right", 0, 127))); if (state.doc.relative) { const hint = document.createElement("p"); hint.className = "hint"; hint.textContent = "Relative sends the left value one way and the right value the other."; aside.append(hint); } } else { aside.append(field("CC", numberInput(item, "number", 0, 127))); aside.append(field("Value 1", numberInput(item, "min", 0, 127))); aside.append(field("Value 2", numberInput(item, "max", 0, 127))); aside.append(field("Led", numberInput(item, "led", 0, 255))); const sysex = document.createElement("input"); sysex.type = "text"; sysex.value = item.sysex; sysex.spellcheck = false; sysex.addEventListener("input", () => { const hex = sysex.value.replace(/\s+/g, "").toLowerCase(); if (hex.length % 2 || /[^0-9a-f]/.test(hex) || hex.length > 32) return; item.sysex = hex; touch(); }); aside.append(field("SysEx", sysex)); } const hint = document.createElement("p"); hint.className = "hint"; hint.textContent = "A pad colour lights the lamp as you pick it. Save keeps every edit after a power cycle."; aside.append(hint); } $("load").addEventListener("click", () => load(true)); $("save").addEventListener("click", save); $("export").addEventListener("click", exportFile); $("import").addEventListener("change", () => { const file = $("import").files[0]; $("import").value = ""; if (file) importFile(file); }); $("page").addEventListener("change", () => { state.page = Number($("page").value); state.kind = state.kind === "pad" ? null : state.kind; render(); }); $("bank").addEventListener("change", () => { state.bank = Number($("bank").value); state.kind = state.kind === "knob" ? null : state.kind; render(); }); load(false);