/* 2B Studio · Toxic Studio – Interaktionen (by EINswarm) Räume · Strg+Enter · Verlauf-Karten · Job-Timer · Sporen & Wellen-Glut */ (() => { "use strict"; if (window.__tbStudio) return; window.__tbStudio = true; const $ = (s, r = document) => r.querySelector(s); const $$ = (s, r = document) => Array.from(r.querySelectorAll(s)); const reduce = matchMedia("(prefers-reduced-motion: reduce)"); const shown = el => !!el && el.getClientRects().length > 0 && getComputedStyle(el).visibility !== "hidden"; /* ---------- Räume ---------- */ const ROOMS = { generate: { kicker: "KI-Musikstudio · Raum Generate", title: "Generate" }, master: { kicker: "KI-Musikstudio · Raum Master", title: "Master" }, studio: { kicker: "KI-Musikstudio · Raum STUDIO", title: "STUDIO" }, }; function syncRoom() { const room = shown($("#tb-studio-panel")) ? "studio" : shown($("#tb-master-panel")) ? "master" : "generate"; if (document.body.dataset.room === room) return; document.body.dataset.room = room; const t = ROOMS[room], k = $("[data-room-kicker]"), h = $("[data-room-title]"); if (k) k.textContent = t.kicker; if (h) h.textContent = t.title; $$(".tb-room").forEach(b => b.classList.toggle("is-active", b.id === "room-" + room)); $$("#room-generate, #room-master, #room-studio").forEach(b => b.setAttribute("aria-current", b.id === "room-" + room ? "page" : "false")); document.title = (room === "master" ? "Master" : room === "studio" ? "STUDIO" : "Generate") + " · 2B Studio"; } /* STUDIO bridge: Chips + Waben-Aktionen → verstecktes Gradio-Feld */ function stBridge(cmd) { const input = $("#st-bridge textarea") || $("#st-bridge input") || $("#st-bridge"); const btn = $("#st-bridge-btn"); if (!input || !btn) return; const set = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")?.set || Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set; if (set) set.call(input, cmd); else input.value = cmd; input.dispatchEvent(new Event("input", { bubbles: true })); btn.click(); } document.addEventListener("click", e => { const chip = e.target.closest("[data-st-chip]"); if (chip) { e.preventDefault(); stBridge("chip:" + chip.dataset.stChip); return; } const toggle = e.target.closest("[data-st-toggle]"); if (toggle) { e.preventDefault(); stBridge("toggle:" + toggle.dataset.stToggle); return; } const remove = e.target.closest("[data-st-remove]"); if (remove) { e.preventDefault(); stBridge("remove:" + remove.dataset.stRemove); return; } const up = e.target.closest("[data-st-up]"); if (up) { e.preventDefault(); stBridge("up:" + up.dataset.stUp); return; } const down = e.target.closest("[data-st-down]"); if (down) { e.preventDefault(); stBridge("down:" + down.dataset.stDown); return; } const collapse = e.target.closest("[data-st-collapse]"); if (collapse) { e.preventDefault(); stBridge("collapse:" + collapse.dataset.stCollapse); return; } }); document.addEventListener("change", e => { const el = e.target.closest("[data-st-macro]"); if (!el) return; stBridge(`macro:${el.dataset.stMacro}:${el.dataset.stMacroKey}:${el.value}`); }); /* ---------- Tastatur: Strg/Cmd + Enter = Generieren ---------- */ document.addEventListener("keydown", e => { if (e.key !== "Enter" || !(e.ctrlKey || e.metaKey) || e.altKey) return; const room = document.body.dataset.room || "generate"; const btn = room === "studio" ? $("#st-send") : room === "master" ? $("#tb-master-run") : $("#tb-generate"); if (document.body.classList.contains("tb-busy")) { e.preventDefault(); return; } if (btn && !btn.disabled && shown(btn)) { e.preventDefault(); btn.click(); flash(btn); } }); function flash(el) { el.animate?.([{ filter: "brightness(1.6)" }, { filter: "none" }], { duration: 380 }); } /* ---------- Verlauf-Karten wählen den Take im (versteckten) Radio ---------- */ document.addEventListener("click", e => { const pick = e.target.closest("[data-track-pick]"); if (!pick) return; const scope = pick.dataset.scope === "master" ? "#lib-master" : "#lib-create"; const id = pick.dataset.trackPick; const input = $$(`${scope} input[type="radio"]`).find(i => i.value === id); if (input) { input.click(); $$(".tb-take").forEach(t => t.classList.toggle("is-current", t.contains(pick))); } }); /* ---------- Flow-Navigation: weiches Scrollen ---------- */ document.addEventListener("click", e => { const a = e.target.closest(".tb-flow a[href^='#']"); if (!a) return; const el = $(a.getAttribute("href")); if (!el) return; e.preventDefault(); el.scrollIntoView({ behavior: reduce.matches ? "auto" : "smooth", block: "start" }); }); /* ---------- Job-Timer, Phasen und „läuft"-Karte im Verlauf ---------- */ let jobStart = 0, jobTimer = 0; function fmt(s) { s = Math.max(0, Math.floor(s)); return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`; } function modelLabel() { const input = $("#tb-model input"); return (input && input.value || "Modell").split(" · ")[0]; } function pendingCard(on) { const list = $("#tb-side .tb-takes") || $("#tb-side .tb-history"); $$(".tb-take--pending").forEach(n => n.remove()); if (!on || !list || document.body.dataset.room !== "generate") return; const bars = Array.from({ length: 36 }, (_, i) => ``).join(""); const card = document.createElement("article"); card.className = "tb-take tb-take--pending"; card.innerHTML = `
Job läuft0:00 ${modelLabel().replace(/[<>&]/g, "")} · in der Warteschlange
`; const target = $("#tb-side .tb-takes"); if (target) target.prepend(card); else list.appendChild(card); } function syncLoader() { const box = $("#tb-loading"), on = shown(box) && !!$(".tb-loader", box); document.body.classList.toggle("tb-busy", on); const bar = $("#tb-generate-bar"); if (bar) bar.classList.toggle("is-busy", on); if (on && !jobStart) { jobStart = performance.now(); pendingCard(true); if (bar && !$("[data-bar-time]", bar)) { const tag = document.createElement("span"); tag.className = "tb-genbar__busy"; tag.setAttribute("aria-live", "polite"); tag.innerHTML = 'Job läuft · 0:00'; bar.appendChild(tag); } jobTimer = setInterval(() => { const s = (performance.now() - jobStart) / 1000; $$("[data-loader-time], [data-pending-time], [data-bar-time]").forEach(n => n.textContent = fmt(s)); const phase = s < 8 ? 0 : s < 40 ? 1 : 2; const label = ["Worker wird geweckt", "Modell rechnet", "Analyse & Feinschliff"][phase]; $$("[data-loader-phase]").forEach(n => n.textContent = label); $$(".tb-loader__steps li").forEach((li, i) => { li.classList.toggle("is-on", i === phase); li.classList.toggle("is-done", i < phase); }); }, 250); } else if (!on && jobStart) { jobStart = 0; clearInterval(jobTimer); pendingCard(false); // Fertig: Player in Sicht holen, damit der neue Take sofort hörbar ist const player = $("#tb-player"); const pr = player && player.getBoundingClientRect(); if (pr && (pr.bottom < 0 || pr.top > innerHeight - 80)) { player.scrollIntoView({ behavior: reduce.matches ? "auto" : "smooth", block: "start" }); } } } /* ---------- Sporen-Drift & Wellen-Glut (ohne Mikrofon) ---------- */ const fx = (() => { let cv, ctx, w = 0, h = 0, parts = [], sprite, raf = 0, last = 0, energy = .2, running = false; const R = (a, b) => a + Math.random() * (b - a); function makeSprite() { const c = document.createElement("canvas"); c.width = c.height = 64; const g = c.getContext("2d"); const gr = g.createRadialGradient(32, 32, 0, 32, 32, 32); gr.addColorStop(0, "rgba(212, 175, 55,1)"); gr.addColorStop(.18, "rgba(212, 175, 55,.85)"); gr.addColorStop(.45, "rgba(212, 175, 55,.22)"); gr.addColorStop(1, "rgba(212, 175, 55,0)"); g.fillStyle = gr; g.fillRect(0, 0, 64, 64); return c; } function resize() { const d = Math.min(devicePixelRatio || 1, 1.5); w = innerWidth; h = innerHeight; cv.width = w * d | 0; cv.height = h * d | 0; ctx.setTransform(d, 0, 0, d, 0, 0); seed(); } function seed() { parts = []; const n = w < 700 ? 18 : 40; for (let i = 0; i < n; i++) parts.push({ x: R(0, w), y: R(0, h), r: Math.random() < .12 ? R(7, 13) : R(2, 5.5), vy: -R(4, 20), ph: R(0, 6.3), f: R(.2, .7), a: R(.1, .42), tw: R(.5, 2) }); } function playing() { if (document.body.classList.contains("tb-busy")) return true; return $$("audio").some(a => !a.paused && !a.ended); } function tick(t) { const dt = Math.min(.05, (t - (last || t)) / 1000), time = t / 1000; last = t; ctx.clearRect(0, 0, w, h); energy += ((playing() ? 1 : .22) - energy) * dt * 1.5; const beat = Math.pow(Math.max(0, Math.cos(time * Math.PI * 128 / 60)), 12); const amp = (14 + 26 * energy) * (.65 + .35 * beat); for (const [yy, al, lw, sp] of [[.86, .06, 14, 1], [.86, .2, 1.5, 1], [.91, .09, 1.1, -.7]]) { ctx.beginPath(); for (let x = 0; x <= w; x += 6) { const env = Math.sin(Math.PI * x / w); const y = h * yy + env * amp * (Math.sin(x * .011 + time * 1.7 * sp) * .6 + Math.sin(x * .027 - time * 2.9 * sp) * .3 + Math.sin(x * .061 + time * 5.3) * .1 * energy); x ? ctx.lineTo(x, y) : ctx.moveTo(x, y); } ctx.strokeStyle = `rgba(212, 175, 55,${al * (.6 + .6 * energy)})`; ctx.lineWidth = lw; ctx.stroke(); } ctx.globalCompositeOperation = "lighter"; for (const p of parts) { p.y += p.vy * dt * (1 + energy * .8); p.x += Math.sin(time * p.f + p.ph) * 8 * dt; if (p.y < -20) { p.y = h + 20; p.x = R(0, w); } const a = p.a * (.55 + .45 * Math.sin(time * p.tw + p.ph)); ctx.globalAlpha = Math.max(0, a); const s = p.r * 4; ctx.drawImage(sprite, p.x - s / 2, p.y - s / 2, s, s); } ctx.globalAlpha = 1; ctx.globalCompositeOperation = "source-over"; raf = requestAnimationFrame(tick); } function start() { if (!cv) return; const want = !reduce.matches && !document.hidden && !matchMedia("(prefers-contrast: more)").matches; if (!want) { running = false; cancelAnimationFrame(raf); ctx.clearRect(0, 0, w, h); return; } if (!running) { running = true; last = 0; raf = requestAnimationFrame(tick); } } function init(canvas) { cv = canvas; ctx = cv.getContext("2d"); sprite = makeSprite(); resize(); addEventListener("resize", resize); document.addEventListener("visibilitychange", start); reduce.addEventListener?.("change", start); start(); } return { init }; })(); /* ---------- Start, sobald Gradio gerendert hat ---------- */ let ready = false; function boot() { if (!$("#tb-shell")) return false; if (!ready) { ready = true; document.body.dataset.room = ""; const cv = $(".tb-fx"); if (cv) fx.init(cv); } syncRoom(); syncLoader(); return true; } let pending = false; const mo = new MutationObserver(() => { if (pending) return; pending = true; requestAnimationFrame(() => { pending = false; boot(); }); }); function attach() { mo.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ["class", "style", "hidden"] }); boot(); } if (document.body) attach(); else document.addEventListener("DOMContentLoaded", attach); })();