From 4d047b7ac7d269347c389b468d91884b4595931f Mon Sep 17 00:00:00 2001 From: JotaChina Date: Sun, 28 Dec 2025 15:19:16 -0300 Subject: [PATCH] renderizando projetos no mmpCreator utilizando o lmms --- assets/js/creations/main.js | 115 +++++++++++++++++------------------- 1 file changed, 54 insertions(+), 61 deletions(-) diff --git a/assets/js/creations/main.js b/assets/js/creations/main.js index ad221ddd..7e236b29 100755 --- a/assets/js/creations/main.js +++ b/assets/js/creations/main.js @@ -27,9 +27,10 @@ window.ROOM_NAME = ROOM_NAME; const PROJECT_NAME = new URLSearchParams(window.location.search).get("project"); +const chosenFormat = allowed.has(fmt) ? fmt : "wav"; const body = { roomName: ROOM_NAME || null, - format, + chosenFormat, name: appState.global?.currentBeatBasslineName || appState.global?.projectName || @@ -148,76 +149,68 @@ document.addEventListener("DOMContentLoaded", () => { const renderAudioBtn = document.getElementById("render-audio-btn"); renderAudioBtn?.addEventListener("click", async () => { - const fmt = (prompt("Formato: wav / mp3 / ogg / flac", "wav") || "") - .trim() - .toLowerCase(); + const fmt = (prompt("Formato: wav / mp3 / ogg / flac", "wav") || "").trim().toLowerCase(); + if (!fmt) return; - if (!fmt) return; + const allowed = new Set(["wav", "mp3", "ogg", "flac"]); + const chosenFormat = allowed.has(fmt) ? fmt : "wav"; // ✅ - const allowed = new Set(["wav", "mp3", "ogg", "flac"]); - const format = allowed.has(fmt) ? fmt : "wav"; + const icon = renderAudioBtn.querySelector("i"); + const oldIconClass = icon?.className; + if (icon) icon.className = "fa-solid fa-spinner fa-spin"; + renderAudioBtn.style.pointerEvents = "none"; - const originalIcon = renderAudioBtn.className; - renderAudioBtn.className = "fa-solid fa-spinner fa-spin"; - renderAudioBtn.style.pointerEvents = "none"; + try { + showToast("🎛️ Renderizando no LMMS (servidor)...", "info", 4000); - try { - showToast("🎛️ Renderizando no LMMS (servidor)...", "info", 4000); + const roomName = new URLSearchParams(window.location.search).get("room"); + const baseUrl = `https://${window.location.hostname}:33001`; - // mesma lógica do socket: room vem da URL - const roomName = new URLSearchParams(window.location.search).get("room"); + const body = { + roomName: roomName || null, + format: chosenFormat, // ✅ sem shorthand + name: appState.global?.currentBeatBasslineName || appState.global?.projectName || "projeto", + }; - // usa a mesma porta do socket (o socket.js usa PORT_SOCK) :contentReference[oaicite:7]{index=7} - // se você não quiser mexer em imports, dá pra trocar por ':33001' direto. - const baseUrl = `https://${window.location.hostname}:33001`; + // ✅ Modo Local: manda XML direto (senão dá missing_xml_or_room) + if (!roomName) { + body.xml = generateXmlFromStateExported(); + } - const body = { - roomName: roomName || null, - format, - name: - appState.global?.currentBeatBasslineName || - appState.global?.projectName || - "projeto", - }; + const resp = await fetch(`${baseUrl}/render`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + }); - const resp = await fetch(`${baseUrl}/render`, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(body), - }); + if (!resp.ok) throw new Error(await resp.text()); - if (!resp.ok) { - const txt = await resp.text(); - throw new Error(txt || `HTTP ${resp.status}`); + const blob = await resp.blob(); + + let filename = `projeto.${chosenFormat}`; + const cd = resp.headers.get("Content-Disposition"); + const m = cd && /filename="?([^"]+)"?/i.exec(cd); + if (m?.[1]) filename = m[1]; + + const url = URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = url; + a.download = filename; + document.body.appendChild(a); + a.click(); + a.remove(); + setTimeout(() => URL.revokeObjectURL(url), 1500); + + showToast("✅ Render concluído!", "success", 3000); + } catch (e) { + console.error(e); + showToast("❌ Falha ao renderizar no LMMS.", "error", 6000); + alert("Falha ao renderizar no LMMS. Veja o console para detalhes."); + } finally { + if (icon && oldIconClass) icon.className = oldIconClass; + renderAudioBtn.style.pointerEvents = "auto"; } - - const blob = await resp.blob(); - - // tenta usar filename vindo do Content-Disposition - let filename = `projeto.${format}`; - const cd = resp.headers.get("Content-Disposition"); - const m = cd && /filename="?([^"]+)"?/i.exec(cd); - if (m?.[1]) filename = m[1]; - - const url = URL.createObjectURL(blob); - const a = document.createElement("a"); - a.href = url; - a.download = filename; - document.body.appendChild(a); - a.click(); - a.remove(); - setTimeout(() => URL.revokeObjectURL(url), 1500); - - showToast("✅ Render concluído!", "success", 3000); - } catch (e) { - console.error(e); - showToast("❌ Falha ao renderizar no LMMS.", "error", 6000); - alert("Falha ao renderizar no LMMS. Veja o console para detalhes."); - } finally { - renderAudioBtn.className = originalIcon; - renderAudioBtn.style.pointerEvents = "auto"; - } -}); + }); // Download projeto downloadPackageBtn?.addEventListener("click", generateMmpFile);