Padronização da interface e atualização do sync entre clientes
This commit is contained in:
1 parent
3f714c88de
commit
7d9d2b0f63
10 files changed
+796
-377
No files matched your search
@@ -23,23 +23,43 @@ import { sendAction } from "../socket.js";
|
||||
|
||||
export function renderAudioEditor() {
|
||||
const audioEditor = document.querySelector(".audio-editor");
|
||||
const existingTrackContainer = document.getElementById(
|
||||
"audio-track-container"
|
||||
);
|
||||
const existingTrackContainer = document.getElementById("audio-track-container");
|
||||
|
||||
if (!audioEditor || !existingTrackContainer) return;
|
||||
|
||||
// --- CORREÇÃO DO ERRO DOMException ---
|
||||
// Identificamos quem é o pai real do container de tracks (agora é .audio-tracks-wrapper)
|
||||
const tracksParent = existingTrackContainer.parentElement;
|
||||
|
||||
// --- CRIAÇÃO E RENDERIZAÇÃO DA RÉGUA ---
|
||||
let rulerWrapper = audioEditor.querySelector(".ruler-wrapper");
|
||||
// Buscamos a régua dentro desse pai correto
|
||||
let rulerWrapper = tracksParent.querySelector(".ruler-wrapper");
|
||||
|
||||
if (!rulerWrapper) {
|
||||
// Se a régua criada pelo JS ainda não existe, cria ela
|
||||
// (Ignora a régua estática do HTML se houver conflito, priorizando esta estrutura)
|
||||
rulerWrapper = document.createElement("div");
|
||||
rulerWrapper.className = "ruler-wrapper";
|
||||
rulerWrapper.innerHTML = `
|
||||
<div class="ruler-spacer"></div>
|
||||
<div class="timeline-ruler"></div>
|
||||
`;
|
||||
audioEditor.insertBefore(rulerWrapper, existingTrackContainer);
|
||||
|
||||
// Insere ANTES do container de tracks, dentro do pai correto
|
||||
tracksParent.insertBefore(rulerWrapper, existingTrackContainer);
|
||||
}
|
||||
|
||||
// Remove a régua estática antiga se ela ainda estiver lá atrapalhando (opcional, mas recomendado)
|
||||
const staticRuler = tracksParent.querySelector("#audio-timeline-ruler");
|
||||
if (staticRuler && staticRuler.parentElement === tracksParent) {
|
||||
staticRuler.remove();
|
||||
}
|
||||
// Remove elementos soltos antigos se existirem para evitar duplicação
|
||||
const oldLoopRegion = tracksParent.querySelector("#loop-region");
|
||||
const oldPlayhead = tracksParent.querySelector("#playhead");
|
||||
if(oldLoopRegion) oldLoopRegion.remove();
|
||||
if(oldPlayhead) oldPlayhead.remove();
|
||||
|
||||
const ruler = rulerWrapper.querySelector(".timeline-ruler");
|
||||
ruler.innerHTML = "";
|
||||
|
||||
@@ -243,7 +263,9 @@ export function renderAudioEditor() {
|
||||
|
||||
// Recriação Container Pistas (sem alterações)
|
||||
const newTrackContainer = existingTrackContainer.cloneNode(false);
|
||||
audioEditor.replaceChild(newTrackContainer, existingTrackContainer);
|
||||
|
||||
// Substitui no pai correto (tracksParent), não no audioEditor
|
||||
tracksParent.replaceChild(newTrackContainer, existingTrackContainer);
|
||||
|
||||
// Render Pistas (sem alterações)
|
||||
appState.audio.tracks.forEach((trackData) => {
|
||||
@@ -725,7 +747,6 @@ export function renderAudioEditor() {
|
||||
const constrainedLeftPx = Math.max(0, newLeftPx);
|
||||
let newStartTime = constrainedLeftPx / currentPixelsPerSecond;
|
||||
newStartTime = quantizeTime(newStartTime);
|
||||
// (Correção Bug 4 - remove Number())
|
||||
updateAudioClipProperties(clipId, {
|
||||
trackId: newTrackId,
|
||||
startTimeInSeconds: newStartTime,
|
||||
@@ -756,13 +777,8 @@ export function renderAudioEditor() {
|
||||
const clickX = event.clientX - rect.left;
|
||||
const absoluteX = clickX + scrollLeft;
|
||||
const newTime = absoluteX / currentPixelsPerSecond;
|
||||
// =================================================================
|
||||
// 👇 INÍCIO DA CORREÇÃO (Sincronia de Seek na Pista)
|
||||
// =================================================================
|
||||
// Sincronia de Seek na Pista)
|
||||
sendAction({ type: "SET_SEEK_TIME", seekTime: newTime });
|
||||
// seekAudioEditor(newTime); // 👈 Substituído
|
||||
// =================================================================
|
||||
// 👆 FIM DA CORREÇÃO
|
||||
};
|
||||
handleSeek(e); // Aplica no mousedown
|
||||
const onMouseMoveSeek = (moveEvent) => handleSeek(moveEvent);
|
||||
@@ -856,7 +872,7 @@ export function resetPlayheadVisual() {
|
||||
});
|
||||
}
|
||||
|
||||
// --- INÍCIO DA NOVA FUNÇÃO (Passo 4: A Função de Desenho) ---
|
||||
// --- A Função de Desenho) ---
|
||||
// (Adicionada ao final de audio_ui.js)
|
||||
|
||||
/**
|
||||
@@ -907,5 +923,4 @@ function createPatternViewElement(patternData) {
|
||||
});
|
||||
|
||||
return view;
|
||||
}
|
||||
// --- FIM DA NOVA FUNÇÃO ---
|
||||
}
|
||||
+23
-31
@@ -1,4 +1,6 @@
|
||||
// js/file.js
|
||||
//--------------------------------------------------------------
|
||||
// IMPORTS NECESSÁRIOS
|
||||
//--------------------------------------------------------------
|
||||
import { appState, saveStateToSession, resetProjectState, loadStateFromSession } from "./state.js";
|
||||
import { loadAudioForTrack } from "./pattern/pattern_state.js";
|
||||
import { renderAll, getSamplePathMap } from "./ui.js";
|
||||
@@ -7,21 +9,13 @@ import {
|
||||
initializeAudioContext,
|
||||
getMainGainNode,
|
||||
} from "./audio.js";
|
||||
import { DEFAULT_PROJECT_XML } from "./utils.js"
|
||||
import * as Tone from "https://esm.sh/tone";
|
||||
|
||||
import { sendAction } from "./socket.js";
|
||||
|
||||
const BLANK_PROJECT_XML = `<?xml version="1.0"?>
|
||||
<!DOCTYPE lmms-project>
|
||||
<lmms-project type="song" version="1.0" creatorversion="1.2.2" creator="LMMS">
|
||||
<head bpm="140" mastervol="100" timesig_numerator="4" timesig_denominator="4" masterpitch="0"/>
|
||||
<song>
|
||||
<trackcontainer width="600" height="300" type="song" visible="1" maximized="0" x="5" y="5" minimized="0">
|
||||
</trackcontainer>
|
||||
<timeline lp0pos="0" lp1pos="192" lpstate="0"/>
|
||||
<controllers/>
|
||||
</song>
|
||||
</lmms-project>`;
|
||||
//--------------------------------------------------------------
|
||||
//
|
||||
//--------------------------------------------------------------
|
||||
|
||||
export function handleLocalProjectReset() {
|
||||
console.log("Recebido comando de reset. Limpando estado local...");
|
||||
@@ -89,7 +83,7 @@ export async function loadProjectFromServer(fileName) {
|
||||
export async function parseMmpContent(xmlString) {
|
||||
resetProjectState();
|
||||
initializeAudioContext();
|
||||
appState.global.justReset = xmlString === BLANK_PROJECT_XML;
|
||||
appState.global.justReset = xmlString === DEFAULT_PROJECT_XML;
|
||||
|
||||
const audioContainer = document.getElementById("audio-track-container");
|
||||
if (audioContainer) {
|
||||
@@ -305,10 +299,7 @@ export async function parseMmpContent(xmlString) {
|
||||
appState.pattern.activeTrackId = appState.pattern.tracks[0]?.id || null;
|
||||
appState.pattern.activePatternIndex = 0;
|
||||
|
||||
// --- SUBSTUIÇÃO DO BLOCO DE RESTAURAÇÃO ---
|
||||
// Em vez daquele bloco try/catch gigante, apenas chamamos a função:
|
||||
loadStateFromSession();
|
||||
// ------------------------------------------
|
||||
|
||||
await Promise.resolve();
|
||||
renderAll();
|
||||
@@ -324,17 +315,19 @@ export function generateMmpFile() {
|
||||
|
||||
function generateXmlFromState() {
|
||||
if (!appState.global.originalXmlDoc) {
|
||||
console.warn("Não há XML original. Retornando vazio.");
|
||||
return "";
|
||||
console.log("Gerando XML a partir do template em branco...");
|
||||
const parser = new DOMParser();
|
||||
appState.global.originalXmlDoc = parser.parseFromString(DEFAULT_PROJECT_XML, "application/xml");
|
||||
}
|
||||
|
||||
const xmlDoc = appState.global.originalXmlDoc.cloneNode(true);
|
||||
const head = xmlDoc.querySelector("head");
|
||||
|
||||
if (head) {
|
||||
head.setAttribute("bpm", document.getElementById("bpm-input").value);
|
||||
head.setAttribute("num_bars", document.getElementById("bars-input").value);
|
||||
head.setAttribute("timesig_numerator", document.getElementById("compasso-a-input").value);
|
||||
head.setAttribute("timesig_denominator", document.getElementById("compasso-b-input").value);
|
||||
head.setAttribute("bpm", document.getElementById("bpm-input").value || 140);
|
||||
head.setAttribute("num_bars", document.getElementById("bars-input").value || 1);
|
||||
head.setAttribute("timesig_numerator", document.getElementById("compasso-a-input").value || 4);
|
||||
head.setAttribute("timesig_denominator", document.getElementById("compasso-b-input").value || 4);
|
||||
}
|
||||
|
||||
const bbTrackContainer = xmlDoc.querySelector('track[type="1"] > bbtrack > trackcontainer');
|
||||
@@ -344,7 +337,6 @@ function generateXmlFromState() {
|
||||
.map((track) => createTrackXml(track))
|
||||
.join("");
|
||||
|
||||
// Gambiarra para inserir o XML gerado como nós DOM
|
||||
const tempDoc = new DOMParser().parseFromString(`<root>${tracksXml}</root>`, "application/xml");
|
||||
Array.from(tempDoc.documentElement.children).forEach((newTrackNode) => {
|
||||
bbTrackContainer.appendChild(newTrackNode);
|
||||
@@ -369,21 +361,22 @@ function createTrackXml(track) {
|
||||
const lmmsVolume = Math.round(track.volume * 100);
|
||||
const lmmsPan = Math.round(track.pan * 100);
|
||||
|
||||
// 🔥 PROTEÇÃO: Se não tiver instrumento definido, usa Kicker padrão
|
||||
const instrName = track.instrumentName || "kicker";
|
||||
const instrXml = track.instrumentXml || `<kicker><env amt="0" attack="0.01" hold="0.1" decay="0.1" release="0.1" sustain="0.5" sync_mode="0"/></kicker>`;
|
||||
|
||||
const patternsXml = track.patterns.map((pattern) => {
|
||||
let patternNotesXml = "";
|
||||
|
||||
// SE for plugin e tiver notas detalhadas, usa elas
|
||||
if (track.type === "plugin" && pattern.notes && pattern.notes.length > 0) {
|
||||
patternNotesXml = pattern.notes.map(note => {
|
||||
return `<note vol="${note.vol}" len="${note.len}" pos="${note.pos}" pan="${note.pan}" key="${note.key}"/>`;
|
||||
}).join("\n ");
|
||||
}
|
||||
// SE for sampler (ou plugin sem notas detalhadas), usa os steps convertidos em notas simples
|
||||
else {
|
||||
patternNotesXml = pattern.steps.map((isActive, index) => {
|
||||
if (isActive) {
|
||||
const notePos = Math.round(index * ticksPerStep);
|
||||
// Key 57 é o padrão do LMMS para samples (Lá)
|
||||
return `<note vol="100" len="${NOTE_LENGTH}" pos="${notePos}" pan="0" key="57"/>`;
|
||||
}
|
||||
return "";
|
||||
@@ -398,8 +391,8 @@ function createTrackXml(track) {
|
||||
return `
|
||||
<track type="0" solo="0" muted="0" name="${track.name}">
|
||||
<instrumenttrack vol="${lmmsVolume}" pitch="0" fxch="0" pitchrange="1" basenote="57" usemasterpitch="1" pan="${lmmsPan}">
|
||||
<instrument name="${track.instrumentName}">
|
||||
${track.instrumentXml}
|
||||
<instrument name="${instrName}">
|
||||
${instrXml}
|
||||
</instrument>
|
||||
<fxchain enabled="0" numofeffects="0"/>
|
||||
</instrumenttrack>
|
||||
@@ -461,5 +454,4 @@ function downloadFile(content, fileName) {
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
export { generateXmlFromState as generateXmlFromStateExported };
|
||||
export { BLANK_PROJECT_XML }; // Mantenha o que já estava
|
||||
export { generateXmlFromState as generateXmlFromStateExported };
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
restartAudioEditorIfPlaying,
|
||||
} from "./audio/audio_audio.js";
|
||||
import { initializeAudioContext } from "./audio.js";
|
||||
import { handleFileLoad, generateMmpFile, BLANK_PROJECT_XML } from "./file.js";
|
||||
import { handleFileLoad, generateMmpFile } from "./file.js";
|
||||
import {
|
||||
renderAll,
|
||||
loadAndRenderSampleBrowser,
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
closeOpenProjectModal,
|
||||
} from "./ui.js";
|
||||
import { renderAudioEditor } from "./audio/audio_ui.js";
|
||||
import { adjustValue, enforceNumericInput } from "./utils.js";
|
||||
import { adjustValue, enforceNumericInput, DEFAULT_PROJECT_XML } from "./utils.js";
|
||||
import { ZOOM_LEVELS } from "./config.js";
|
||||
import { loadProjectFromServer } from "./file.js";
|
||||
import { sendAction, joinRoom, setUserName } from "./socket.js";
|
||||
@@ -61,6 +61,11 @@ const PROJECT_NAME = new URLSearchParams(window.location.search).get("project");
|
||||
? PROJECT_NAME
|
||||
: `${PROJECT_NAME}.mmp`;
|
||||
loadProjectFromServer(filename);
|
||||
} else if (!restored && !PROJECT_NAME) {
|
||||
// CORREÇÃO: Se entrou numa sala vazia sem projeto na URL,
|
||||
// inicializa o XML base para não dar erro depois.
|
||||
const parser = new DOMParser();
|
||||
appState.global.originalXmlDoc = parser.parseFromString(DEFAULT_PROJECT_XML, "application/xml");
|
||||
}
|
||||
|
||||
})(); // Fim do initApp
|
||||
@@ -313,7 +318,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
)
|
||||
)
|
||||
return;
|
||||
sendAction({ type: "LOAD_PROJECT", xml: BLANK_PROJECT_XML });
|
||||
sendAction({ type: "LOAD_PROJECT", xml: DEFAULT_PROJECT_XML });
|
||||
});
|
||||
|
||||
addBarBtn?.addEventListener("click", () => {
|
||||
@@ -341,6 +346,29 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
initializeAudioContext();
|
||||
sendAction({ type: "REMOVE_LAST_TRACK" });
|
||||
});
|
||||
|
||||
// 👇 ATUALIZE ESTE LISTENER
|
||||
removeInstrumentBtn?.addEventListener("click", () => {
|
||||
initializeAudioContext();
|
||||
|
||||
// Pega o ID da faixa que está selecionada/ativa na interface
|
||||
const activeId = appState.pattern.activeTrackId;
|
||||
|
||||
if (activeId) {
|
||||
// Envia comando seguro por ID
|
||||
sendAction({
|
||||
type: "REMOVE_TRACK_BY_ID",
|
||||
trackId: activeId
|
||||
});
|
||||
} else {
|
||||
// Fallback: se não tiver ID ativo, tenta pegar a última (comportamento antigo, mas seguro localmente)
|
||||
const tracks = appState.pattern.tracks;
|
||||
if (tracks.length > 0) {
|
||||
const lastId = tracks[tracks.length - 1].id;
|
||||
sendAction({ type: "REMOVE_TRACK_BY_ID", trackId: lastId });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
playBtn?.addEventListener("click", () => {
|
||||
initializeAudioContext();
|
||||
@@ -496,6 +524,35 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
});
|
||||
}
|
||||
|
||||
const removeAudioTrackBtn = document.getElementById("remove-audio-track-btn");
|
||||
if (removeAudioTrackBtn) {
|
||||
removeAudioTrackBtn.addEventListener("click", () => {
|
||||
initializeAudioContext();
|
||||
|
||||
// Verifica quantas faixas existem
|
||||
const trackCount = appState.audio.tracks.length;
|
||||
|
||||
if (trackCount > 0) {
|
||||
// Pega a ÚLTIMA faixa da lista
|
||||
const lastTrack = appState.audio.tracks[trackCount - 1];
|
||||
|
||||
// Verifica se tem clips nela (segurança)
|
||||
const hasClips = appState.audio.clips.some(c => c.trackId === lastTrack.id);
|
||||
if (hasClips && !confirm("A última pista contém clips de áudio. Deseja removê-la mesmo assim?")) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 🔥 A MUDANÇA CRUCIAL: Envia o ID, e usa o NOME NOVO do comando
|
||||
sendAction({
|
||||
type: "REMOVE_AUDIO_LANE_BY_ID",
|
||||
trackId: lastTrack.id
|
||||
});
|
||||
} else {
|
||||
showToast("Não há pistas de áudio para remover.", "warning");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Navegador de Samples (local)
|
||||
loadAndRenderSampleBrowser();
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// js/pattern_state.js
|
||||
// js/pattern/pattern_state.js
|
||||
import * as Tone from "https://esm.sh/tone";
|
||||
import { TripleOscillator } from "../../audio/plugins/TripleOscillator.js";
|
||||
import { Kicker } from "../../audio/plugins/Kicker.js";
|
||||
@@ -11,6 +11,11 @@ import { DEFAULT_VOLUME, DEFAULT_PAN } from "../config.js";
|
||||
import { getMainGainNode } from "../audio.js";
|
||||
import { getTotalSteps } from "../utils.js";
|
||||
|
||||
// XML padrão para o instrumento Kicker (bateria simples)
|
||||
const DEFAULT_KICKER_XML = `<kicker>
|
||||
<env amt="0" attack="0.01" hold="0.1" decay="0.1" release="0.1" sustain="0.5" sync_mode="0"/>
|
||||
</kicker>`;
|
||||
|
||||
export function initializePatternState() {
|
||||
appState.pattern.tracks.forEach(track => {
|
||||
try { track.player?.dispose(); } catch {}
|
||||
@@ -55,20 +60,17 @@ export async function loadAudioForTrack(track) {
|
||||
|
||||
// --- DETECÇÃO DE TIPO DE ARQUIVO ---
|
||||
// Verifica se é um formato de áudio que o navegador suporta
|
||||
// Verifica tipo de arquivo
|
||||
const isStandardAudio = track.samplePath && /\.(wav|mp3|ogg|flac|m4a)$/i.test(track.samplePath);
|
||||
const isDrumSynth = track.samplePath && /\.ds$/i.test(track.samplePath);
|
||||
|
||||
// Se não for áudio (ou for .ds/.xpf), é Plugin
|
||||
// Se não for áudio padrão, assumimos Plugin (ou Kicker padrão)
|
||||
if (!track.samplePath || !isStandardAudio) {
|
||||
try {
|
||||
if (track.instrument) { try { track.instrument.dispose(); } catch {} }
|
||||
|
||||
let synth;
|
||||
const name = (track.instrumentName || "").toLowerCase();
|
||||
// Normaliza o nome do instrumento. Se vazio, assume kicker.
|
||||
const name = (track.instrumentName || "kicker").toLowerCase();
|
||||
|
||||
// DADOS DO PLUGIN (Tenta parsear XML se for string, ou usa vazio)
|
||||
// Dica: O ideal seria ter um parser real aqui, mas vamos passar {} por enquanto
|
||||
const pluginData = {};
|
||||
|
||||
// SELETOR DE PLUGINS
|
||||
@@ -87,64 +89,53 @@ export async function loadAudioForTrack(track) {
|
||||
break;
|
||||
|
||||
case "nes":
|
||||
case "freeboy": // Freeboy é parecido com NES
|
||||
case "papu": // Papu também é Gameboy
|
||||
case "sid": // SID é 8-bit também, usaremos NES como fallback por enquanto
|
||||
case "freeboy":
|
||||
case "papu":
|
||||
case "sid":
|
||||
synth = new Nes(Tone.getContext(), pluginData);
|
||||
break;
|
||||
|
||||
// --- PACOTE SUPER SAW ---
|
||||
case "zynaddsubfx": // O clássico
|
||||
case "watsyn": // Wavetable Synth
|
||||
case "monstro": // 3 Osciladores monstruosos
|
||||
case "vibedstrings": // Strings vibrantes (fatsaw funciona bem como base)
|
||||
case "SuperSaw":
|
||||
case "zynaddsubfx":
|
||||
case "watsyn":
|
||||
case "monstro":
|
||||
case "vibedstrings":
|
||||
case "supersaw":
|
||||
synth = new SuperSaw(Tone.getContext(), pluginData);
|
||||
break;
|
||||
|
||||
case "organic": // Fallback simples para Organic (Additive)
|
||||
case "organic":
|
||||
synth = new Tone.PolySynth(Tone.Synth, {
|
||||
oscillator: { type: "sine", count: 8, spread: 20 }
|
||||
});
|
||||
break;
|
||||
|
||||
case "zynaddsubfx": // O monstro!
|
||||
// Zyn é impossível de clonar rápido. Vamos usar um "SuperSaw" gordo como placeholder
|
||||
synth = new Tone.PolySynth(Tone.Synth, {
|
||||
oscillator: { type: "fatsawtooth", count: 3, spread: 30 },
|
||||
envelope: { attack: 0.1, decay: 0.3, sustain: 0.8, release: 1 }
|
||||
});
|
||||
break;
|
||||
|
||||
default:
|
||||
console.warn(`Plugin ${name} desconhecido, usando fallback.`);
|
||||
// Fallback genérico
|
||||
synth = new Tone.PolySynth(Tone.Synth, { oscillator: { type: "triangle" } });
|
||||
console.warn(`Plugin ${name} desconhecido, usando fallback (Kicker).`);
|
||||
// Fallback seguro: Kicker
|
||||
synth = new Kicker(Tone.getContext(), pluginData);
|
||||
}
|
||||
|
||||
// Se o plugin criou uma classe wrapper (nossas classes .js),
|
||||
// ele tem o método .connect. Se for Tone nativo (Organic/Zyn fallback), também tem.
|
||||
if (synth.output) {
|
||||
// Nossas classes customizadas
|
||||
synth.connect(track.volumeNode);
|
||||
} else {
|
||||
// Objetos Tone.js puros
|
||||
synth.connect(track.volumeNode);
|
||||
}
|
||||
|
||||
track.instrument = synth;
|
||||
track.player = null;
|
||||
track.type = 'plugin';
|
||||
// Atualiza o nome se ele estava vazio
|
||||
if (!track.instrumentName) track.instrumentName = name;
|
||||
|
||||
console.log(`[Audio] Plugin carregado: ${name}`);
|
||||
|
||||
} catch (e) {
|
||||
console.error("Erro ao carregar plugin:", name, e);
|
||||
console.error("Erro ao carregar plugin:", track.instrumentName, e);
|
||||
}
|
||||
return track;
|
||||
}
|
||||
|
||||
// 3. Lógica para SAMPLERS (Arquivos de Áudio Reais)
|
||||
// 3. Lógica para SAMPLERS
|
||||
try {
|
||||
try { track.player?.dispose(); } catch {}
|
||||
track.player = null;
|
||||
@@ -166,7 +157,7 @@ export async function loadAudioForTrack(track) {
|
||||
|
||||
track.player = player;
|
||||
track.buffer = buffer;
|
||||
track.type = 'sampler'; // Garante o tipo correto
|
||||
track.type = 'sampler';
|
||||
|
||||
} catch (error) {
|
||||
console.error('Erro ao carregar sample:', track.samplePath);
|
||||
@@ -181,21 +172,29 @@ export async function loadAudioForTrack(track) {
|
||||
export function addTrackToState() {
|
||||
const totalSteps = getTotalSteps();
|
||||
const referenceTrack = appState.pattern.tracks[0];
|
||||
const newId = Date.now() + Math.random();
|
||||
|
||||
const newTrack = {
|
||||
id: Date.now() + Math.random(),
|
||||
name: "novo instrumento",
|
||||
id: newId,
|
||||
name: `Novo Instrumento ${appState.pattern.tracks.length + 1}`,
|
||||
samplePath: null,
|
||||
type: 'plugin', // Padrão
|
||||
type: 'plugin',
|
||||
|
||||
// 🔥 CORREÇÃO: Definir instrumento padrão (Kicker) e XML
|
||||
instrumentName: "kicker",
|
||||
instrumentXml: DEFAULT_KICKER_XML,
|
||||
|
||||
player: null,
|
||||
buffer: null,
|
||||
patterns: referenceTrack
|
||||
? referenceTrack.patterns.map(p => ({
|
||||
name: p.name,
|
||||
steps: new Array(p.steps.length).fill(false),
|
||||
notes: [],
|
||||
pos: p.pos
|
||||
}))
|
||||
: [{ name: "Pattern 1", steps: new Array(totalSteps).fill(false), pos: 0 }],
|
||||
: [{ name: "Pattern 1", steps: new Array(totalSteps).fill(false), notes: [], pos: 0 }],
|
||||
|
||||
activePatternIndex: 0,
|
||||
volume: DEFAULT_VOLUME,
|
||||
pan: DEFAULT_PAN,
|
||||
@@ -206,10 +205,39 @@ export function addTrackToState() {
|
||||
newTrack.volumeNode.connect(newTrack.pannerNode);
|
||||
newTrack.pannerNode.connect(getMainGainNode());
|
||||
|
||||
// Carrega o áudio (vai cair no case "kicker" do loadAudioForTrack)
|
||||
loadAudioForTrack(newTrack);
|
||||
|
||||
appState.pattern.tracks.push(newTrack);
|
||||
appState.pattern.activeTrackId = newTrack.id;
|
||||
|
||||
console.log("Faixa adicionada ao estado com Kicker padrão.");
|
||||
}
|
||||
|
||||
export function removeTrackById(trackId) {
|
||||
const index = appState.pattern.tracks.findIndex(t => t.id === trackId);
|
||||
|
||||
if (index !== -1) {
|
||||
const trackToRemove = appState.pattern.tracks[index];
|
||||
|
||||
// Limpeza de memória
|
||||
try { trackToRemove.player?.dispose(); } catch {}
|
||||
try { trackToRemove.buffer?.dispose?.(); } catch {}
|
||||
try { trackToRemove.instrument?.dispose(); } catch {}
|
||||
try { trackToRemove.pannerNode?.disconnect(); } catch {}
|
||||
try { trackToRemove.volumeNode?.disconnect(); } catch {}
|
||||
|
||||
// Remove do array
|
||||
appState.pattern.tracks.splice(index, 1);
|
||||
|
||||
// Ajusta seleção se necessário
|
||||
if (appState.pattern.activeTrackId === trackId) {
|
||||
appState.pattern.activeTrackId = appState.pattern.tracks[0]?.id ?? null;
|
||||
}
|
||||
|
||||
return true; // Retorna sucesso
|
||||
}
|
||||
return false; // Não achou (o fantasma não existe aqui)
|
||||
}
|
||||
|
||||
export function removeLastTrackFromState() {
|
||||
|
||||
@@ -9,7 +9,87 @@ const { Server } = require("socket.io");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const pino = require("pino");
|
||||
//const { PORT_SOCK } = require("../config.js");
|
||||
|
||||
const DEFAULT_PROJECT_XML = `<?xml version="1.0"?>
|
||||
<!DOCTYPE lmms-project>
|
||||
<lmms-project version="1.0" type="song" creatorversion="1.2.2" creator="LMMS">
|
||||
<head mastervol="100" masterpitch="0" timesig_denominator="4" timesig_numerator="4" bpm="140"/>
|
||||
<song>
|
||||
<trackcontainer visible="1" y="5" minimized="0" height="300" type="song" x="5" maximized="0" width="600">
|
||||
<track muted="0" name="TripleOscillator" type="0" solo="0">
|
||||
<instrumenttrack pitchrange="1" fxch="0" usemasterpitch="1" vol="100" pitch="0" pan="0" basenote="57">
|
||||
<instrument name="tripleoscillator">
|
||||
<tripleoscillator wavetype1="0" pan2="0" finer1="0" finel1="0" userwavefile0="" wavetype0="0" userwavefile1="" finer0="0" stphdetun1="0" vol0="33" wavetype2="0" coarse0="0" finel2="0" finer2="0" modalgo2="2" finel0="0" coarse1="-12" phoffset1="0" pan0="0" modalgo3="2" coarse2="-24" phoffset2="0" pan1="0" vol1="33" phoffset0="0" stphdetun0="0" modalgo1="2" vol2="33" stphdetun2="0" userwavefile2=""/>
|
||||
</instrument>
|
||||
<eldata fres="0.5" fwet="0" ftype="0" fcut="14000">
|
||||
<elvol ctlenvamt="0" lspd_syncmode="0" x100="0" lpdel="0" latt="0" pdel="0" lspd="0.1" lshp="0" hold="0.5" lspd_numerator="4" userwavefile="" att="0" lamt="0" dec="0.5" lspd_denominator="4" sustain="0.5" rel="0.1" amt="0"/>
|
||||
<elcut ctlenvamt="0" lspd_syncmode="0" x100="0" lpdel="0" latt="0" pdel="0" lspd="0.1" lshp="0" hold="0.5" lspd_numerator="4" userwavefile="" att="0" lamt="0" dec="0.5" lspd_denominator="4" sustain="0.5" rel="0.1" amt="0"/>
|
||||
<elres ctlenvamt="0" lspd_syncmode="0" x100="0" lpdel="0" latt="0" pdel="0" lspd="0.1" lshp="0" hold="0.5" lspd_numerator="4" userwavefile="" att="0" lamt="0" dec="0.5" lspd_denominator="4" sustain="0.5" rel="0.1" amt="0"/>
|
||||
</eldata>
|
||||
<chordcreator chordrange="1" chord-enabled="0" chord="0"/>
|
||||
<arpeggiator arpcycle="0" arptime_denominator="4" arpgate="100" arpdir="0" arpmode="0" arptime_syncmode="0" arp="0" arpmiss="0" arp-enabled="0" arptime="100" arprange="1" arpskip="0" arptime_numerator="4"/>
|
||||
<midiport inputchannel="0" outputcontroller="0" fixedoutputvelocity="-1" outputchannel="1" fixedinputvelocity="-1" outputprogram="1" inputcontroller="0" readable="0" fixedoutputnote="-1" basevelocity="63" writable="0"/>
|
||||
<fxchain enabled="0" numofeffects="0"/>
|
||||
</instrumenttrack>
|
||||
</track>
|
||||
<track muted="0" name="Sample track" type="2" solo="0">
|
||||
<sampletrack vol="100" pan="0">
|
||||
<fxchain enabled="0" numofeffects="0"/>
|
||||
</sampletrack>
|
||||
</track>
|
||||
<track muted="0" name="Beat/Bassline 0" type="1" solo="0">
|
||||
<bbtrack>
|
||||
<trackcontainer visible="0" y="5" minimized="0" height="400" type="bbtrackcontainer" x="610" maximized="0" width="700">
|
||||
<track muted="0" name="Kicker" type="0" solo="0">
|
||||
<instrumenttrack pitchrange="1" fxch="0" usemasterpitch="1" vol="100" pitch="0" pan="0" basenote="57">
|
||||
<instrument name="kicker">
|
||||
<kicker decay="440" version="1" noise="0" endfreq="40" decay_syncmode="0" decay_denominator="4" startfreq="150" env="0.163" startnote="1" dist="0.8" slope="0.06" decay_numerator="4" click="0.4" distend="0.8" gain="1" endnote="0"/>
|
||||
</instrument>
|
||||
<eldata fres="0.5" fwet="0" ftype="0" fcut="14000">
|
||||
<elvol ctlenvamt="0" lspd_syncmode="0" x100="0" lpdel="0" latt="0" pdel="0" lspd="0.1" lshp="0" hold="0.5" lspd_numerator="4" userwavefile="" att="0" lamt="0" dec="0.5" lspd_denominator="4" sustain="0.5" rel="0.1" amt="0"/>
|
||||
<elcut ctlenvamt="0" lspd_syncmode="0" x100="0" lpdel="0" latt="0" pdel="0" lspd="0.1" lshp="0" hold="0.5" lspd_numerator="4" userwavefile="" att="0" lamt="0" dec="0.5" lspd_denominator="4" sustain="0.5" rel="0.1" amt="0"/>
|
||||
<elres ctlenvamt="0" lspd_syncmode="0" x100="0" lpdel="0" latt="0" pdel="0" lspd="0.1" lshp="0" hold="0.5" lspd_numerator="4" userwavefile="" att="0" lamt="0" dec="0.5" lspd_denominator="4" sustain="0.5" rel="0.1" amt="0"/>
|
||||
</eldata>
|
||||
<chordcreator chordrange="1" chord-enabled="0" chord="0"/>
|
||||
<arpeggiator arpcycle="0" arptime_denominator="4" arpgate="100" arpdir="0" arpmode="0" arptime_syncmode="0" arp="0" arpmiss="0" arp-enabled="0" arptime="100" arprange="1" arpskip="0" arptime_numerator="4"/>
|
||||
<midiport inputchannel="0" outputcontroller="0" fixedoutputvelocity="-1" outputchannel="1" fixedinputvelocity="-1" outputprogram="1" inputcontroller="0" readable="0" fixedoutputnote="-1" basevelocity="63" writable="0"/>
|
||||
<fxchain enabled="0" numofeffects="0"/>
|
||||
</instrumenttrack>
|
||||
<pattern pos="0" muted="0" name="Kicker" steps="16" type="0"/>
|
||||
</track>
|
||||
</trackcontainer>
|
||||
</bbtrack>
|
||||
</track>
|
||||
<track muted="0" name="Automation track" type="5" solo="0">
|
||||
<automationtrack/>
|
||||
</track>
|
||||
</trackcontainer>
|
||||
<track muted="0" name="Automation track" type="6" solo="0">
|
||||
<automationtrack/>
|
||||
<automationpattern pos="0" len="192" name="Numerator" prog="0" tens="1" mute="0"/>
|
||||
<automationpattern pos="0" len="192" name="Denominator" prog="0" tens="1" mute="0"/>
|
||||
<automationpattern pos="0" len="192" name="Tempo" prog="0" tens="1" mute="0"/>
|
||||
<automationpattern pos="0" len="192" name="Master volume" prog="0" tens="1" mute="0"/>
|
||||
<automationpattern pos="0" len="192" name="Master pitch" prog="0" tens="1" mute="0"/>
|
||||
</track>
|
||||
<fxmixer visible="1" y="310" minimized="0" height="333" x="5" maximized="0" width="543">
|
||||
<fxchannel volume="1" muted="0" num="0" name="Master" soloed="0">
|
||||
<fxchain enabled="0" numofeffects="0"/>
|
||||
</fxchannel>
|
||||
</fxmixer>
|
||||
<ControllerRackView visible="1" y="310" minimized="0" height="200" x="680" maximized="0" width="350"/>
|
||||
<pianoroll visible="0" y="5" minimized="0" height="480" x="5" maximized="0" width="860"/>
|
||||
<automationeditor visible="0" y="1" minimized="0" height="400" x="1" maximized="0" width="860"/>
|
||||
<projectnotes visible="0" y="10" minimized="0" height="400" x="700" maximized="0" width="679"><![CDATA[<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
|
||||
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
|
||||
p, li { white-space: pre-wrap; }
|
||||
</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:7.5pt; font-weight:400; font-style:normal;">
|
||||
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html>]]></projectnotes>
|
||||
<timeline lp1pos="192" lpstate="0" lp0pos="0"/>
|
||||
<controllers/>
|
||||
</song>
|
||||
</lmms-project>`;
|
||||
|
||||
|
||||
// -------------------------
|
||||
// LOGGER DINÂMICO (GERENCIADOR)
|
||||
@@ -111,7 +191,8 @@ function ensureRoom(roomName) {
|
||||
if (fs.existsSync(p)) {
|
||||
const j = JSON.parse(fs.readFileSync(p, "utf8"));
|
||||
roomStates[roomName] = {
|
||||
projectXml: j.projectXml || null,
|
||||
// 🔥 CORREÇÃO: Fallback para DEFAULT_PROJECT_XML se o JSON salvo estiver sem XML
|
||||
projectXml: j.projectXml || DEFAULT_PROJECT_XML,
|
||||
audio: j.audio || { tracks: [], clips: [] },
|
||||
seq: j.seq || 0,
|
||||
tokensSeen: new Set(),
|
||||
@@ -122,8 +203,10 @@ function ensureRoom(roomName) {
|
||||
console.warn(`[persist] falha ao carregar estado da sala ${roomName}:`, e);
|
||||
}
|
||||
|
||||
// Cria sala NOVA na memória
|
||||
roomStates[roomName] = {
|
||||
projectXml: null,
|
||||
// 🔥 CORREÇÃO: Inicia com o XML do LMMS completo, não mais null
|
||||
projectXml: DEFAULT_PROJECT_XML,
|
||||
audio: { tracks: [], clips: [] },
|
||||
seq: 0,
|
||||
tokensSeen: new Set(),
|
||||
@@ -245,9 +328,6 @@ function applyAuthoritativeAction(roomName, action) {
|
||||
}
|
||||
|
||||
case "UPDATE_AUDIO_CLIP": {
|
||||
// =================================================================
|
||||
// 👇 INÍCIO DA CORREÇÃO (Blindagem do Servidor para Bug 4)
|
||||
// =================================================================
|
||||
if (!action.clipId || !action.props) {
|
||||
console.warn(
|
||||
`[Server] Ação UPDATE_AUDIO_CLIP rejeitada (dados base inválidos):`,
|
||||
@@ -277,9 +357,6 @@ function applyAuthoritativeAction(roomName, action) {
|
||||
);
|
||||
return null;
|
||||
}
|
||||
// =================================================================
|
||||
// 👆 FIM DA CORREÇÃO
|
||||
// =================================================================
|
||||
|
||||
const c = state.clips.find((x) => String(x.id) === String(action.clipId));
|
||||
if (c && action.props && typeof action.props === "object") {
|
||||
@@ -300,6 +377,29 @@ function applyAuthoritativeAction(roomName, action) {
|
||||
break;
|
||||
}
|
||||
|
||||
case "REMOVE_AUDIO_LANE_BY_ID": {
|
||||
// 1. Acha e remove a Track
|
||||
const idx = state.tracks.findIndex((t) => t.id === action.trackId);
|
||||
if (idx !== -1) {
|
||||
state.tracks.splice(idx, 1);
|
||||
mutated = true;
|
||||
}
|
||||
|
||||
// 2. LIMPEZA PROFUNDA: Remove clips dessa track E clips órfãos (trackId null)
|
||||
// Se removemos a track, os clips dela têm que sumir.
|
||||
const prevCount = state.clips.length;
|
||||
state.clips = state.clips.filter((c) => {
|
||||
// Mantém apenas se tiver trackId E se esse trackId ainda existir na lista de tracks
|
||||
const parentTrackExists = state.tracks.some(t => t.id === c.trackId);
|
||||
return c.trackId && parentTrackExists;
|
||||
});
|
||||
|
||||
if (state.clips.length !== prevCount) {
|
||||
mutated = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
// outras ações não são da persistência do editor de áudio
|
||||
mutated = false;
|
||||
@@ -414,13 +514,19 @@ io.on("connection", (socket) => {
|
||||
// Carrega estado da sala
|
||||
const room = ensureRoom(roomName);
|
||||
|
||||
// Persiste estado se for LOAD_PROJECT (compat)
|
||||
if (action.type === "LOAD_PROJECT" && action.xml) {
|
||||
room.projectXml = action.xml;
|
||||
saveRoom(roomName);
|
||||
console.log(
|
||||
`[broadcast_action] Estado da sala ${roomName} atualizado (LOAD_PROJECT).`
|
||||
);
|
||||
// Persiste estado do Pattern (Notas/Sequenciador)
|
||||
// Aceita tanto carregamento total quanto atualização de notas
|
||||
if ((action.type === "LOAD_PROJECT" || action.type === "SYNC_PATTERN_STATE") && action.xml) {
|
||||
// Proteção: Não salva se o XML for vazio (evita corromper a sala com o bug antigo)
|
||||
if (action.xml.trim().length > 0) {
|
||||
room.projectXml = action.xml;
|
||||
saveRoom(roomName);
|
||||
console.log(
|
||||
`[broadcast_action] XML da sala ${roomName} atualizado via ${action.type}.`
|
||||
);
|
||||
} else {
|
||||
console.warn(`[Server] Ignorando XML vazio vindo de ${action.type}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Aplica ações autoritativas do editor de áudio
|
||||
|
||||
+164
-57
@@ -8,6 +8,7 @@ import {
|
||||
addTrackToState,
|
||||
removeLastTrackFromState,
|
||||
updateTrackSample,
|
||||
removeTrackById,
|
||||
} from "./pattern/pattern_state.js";
|
||||
import {
|
||||
addAudioTrackLane,
|
||||
@@ -37,13 +38,13 @@ import {
|
||||
parseMmpContent,
|
||||
handleLocalProjectReset,
|
||||
syncPatternStateToServer,
|
||||
BLANK_PROJECT_XML,
|
||||
generateXmlFromStateExported,
|
||||
} from "./file.js";
|
||||
|
||||
import { renderAll, showToast } from "./ui.js"; // showToast()
|
||||
import { updateStepUI, renderPatternEditor } from "./pattern/pattern_ui.js";
|
||||
import { PORT_SOCK } from "./config.js";
|
||||
import { DEFAULT_PROJECT_XML } from "./utils.js"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Gera um ID único otimista (ex: "track_1678886401000_abc123")
|
||||
@@ -200,17 +201,12 @@ socket.on("load_project_state", async (projectXml) => {
|
||||
// 🔥 CORREÇÃO: Força a restauração da sessão LOCAL logo após carregar o XML
|
||||
// Isso garante que suas alterações locais (BPM, steps) "ganhem" do servidor
|
||||
if (window.ROOM_NAME) {
|
||||
const raw = sessionStorage.getItem(`temp_state_${window.ROOM_NAME}`);
|
||||
const raw = sessionStorage.getItem(`temp_state_${window.ROOM_NAME}`);
|
||||
if (raw) {
|
||||
const parsed = JSON.parse(raw);
|
||||
const hasLocalAudio = parsed.audioSnapshot?.clips?.length > 0 ||
|
||||
parsed.audioSnapshot?.tracks?.length > 0;
|
||||
if (hasLocalAudio) {
|
||||
console.log("Re-aplicando sessão local sobre o XML do servidor...");
|
||||
// Se existe 'raw', confiamos que é o estado mais recente do usuário.
|
||||
console.log("Re-aplicando sessão local (mesmo se vazia)...");
|
||||
await loadStateFromSession();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
renderAll();
|
||||
@@ -513,6 +509,44 @@ async function handleActionBroadcast(action) {
|
||||
showToast(`▶ ${who} Play bases`, "info");
|
||||
break;
|
||||
}
|
||||
case "UPDATE_PATTERN_NOTES": {
|
||||
const {
|
||||
trackIndex: ti,
|
||||
patternIndex: pi,
|
||||
notes,
|
||||
steps: incomingSteps,
|
||||
} = action;
|
||||
|
||||
const t = appState.pattern.tracks[ti];
|
||||
if (!t) break;
|
||||
|
||||
if (!t.patterns[pi]) t.patterns[pi] = { steps: [], notes: [] };
|
||||
|
||||
t.patterns[pi].notes = Array.isArray(notes) ? notes : [];
|
||||
|
||||
if (Array.isArray(incomingSteps)) {
|
||||
t.patterns[pi].steps = incomingSteps;
|
||||
}
|
||||
|
||||
try {
|
||||
schedulePatternRerender();
|
||||
} catch (e) {
|
||||
console.warn("Erro no render UPDATE_PATTERN_NOTES", e);
|
||||
}
|
||||
|
||||
const isFromSelf = action.__senderId === socket.id;
|
||||
if (window.ROOM_NAME && isFromSelf) {
|
||||
const xml = generateXmlFromStateExported();
|
||||
sendAction({
|
||||
type: "SYNC_PATTERN_STATE",
|
||||
xml,
|
||||
});
|
||||
}
|
||||
|
||||
saveStateToSession();
|
||||
break;
|
||||
}
|
||||
|
||||
case "STOP_PLAYBACK": {
|
||||
setTimeout(stopPlayback, delayMs);
|
||||
const who = actorOf(action);
|
||||
@@ -551,9 +585,6 @@ async function handleActionBroadcast(action) {
|
||||
);
|
||||
break;
|
||||
|
||||
// =================================================================
|
||||
// 👇 INÍCIO DA CORREÇÃO (Handlers Sincronia de Loop/Seek/SyncMode)
|
||||
// =================================================================
|
||||
case "SET_LOOP_STATE": {
|
||||
const changed =
|
||||
appState.global.isLoopActive !== !!action.isLoopActive ||
|
||||
@@ -614,9 +645,6 @@ async function handleActionBroadcast(action) {
|
||||
saveStateToSession();
|
||||
break;
|
||||
}
|
||||
// =================================================================
|
||||
// 👆 FIM DA CORREÇÃO
|
||||
// =================================================================
|
||||
|
||||
// Estado Global
|
||||
case "LOAD_PROJECT": //
|
||||
@@ -647,17 +675,22 @@ async function handleActionBroadcast(action) {
|
||||
isLoadingProject = false;
|
||||
break;
|
||||
|
||||
case "SYNC_PATTERN_STATE": //
|
||||
// Esta ação agora só será recebida de *outros* usuários,
|
||||
// ou quando o servidor enviar, não de você mesmo.
|
||||
try {
|
||||
await parseMmpContent(action.xml); //
|
||||
renderAll();
|
||||
saveStateToSession(); //
|
||||
console.log("Socket: Pattern state sincronizado.");
|
||||
} catch (e) {
|
||||
console.error("Erro SYNC_PATTERN_STATE:", e);
|
||||
showToast("❌ Erro sync pattern", "error");
|
||||
case "SYNC_PATTERN_STATE":
|
||||
// 🔥 CORREÇÃO CRÍTICA:
|
||||
// Esta ação serve apenas para o SERVIDOR atualizar o arquivo .json no disco.
|
||||
// Os clientes online NÃO devem recarregar o XML, pois eles já atualizaram
|
||||
// o estado via ações atômicas (TOGGLE_NOTE, ADD_TRACK, etc).
|
||||
// Recarregar aqui causa o "resetProjectState" que mata o áudio.
|
||||
|
||||
console.log("Socket: XML salvo no servidor (Sync silencioso).");
|
||||
|
||||
// Se quiser garantir, salvamos apenas na sessão local do navegador
|
||||
// sem mexer na engine de áudio ou na tela.
|
||||
if (action.xml) {
|
||||
// Atualiza apenas a string do XML na memória global para referência futura
|
||||
const parser = new DOMParser();
|
||||
appState.global.originalXmlDoc = parser.parseFromString(action.xml, "application/xml");
|
||||
saveStateToSession();
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -713,17 +746,55 @@ async function handleActionBroadcast(action) {
|
||||
renderPatternEditor();
|
||||
const who = actorOf(action);
|
||||
showToast(`🥁 Faixa add por ${who}`, "info");
|
||||
// Salva o estado localmente também!
|
||||
|
||||
// 🔥 CORREÇÃO: Avisa o servidor que o XML mudou!
|
||||
const isFromSelf = action.__senderId === socket.id;
|
||||
if (window.ROOM_NAME && isFromSelf) {
|
||||
const xml = generateXmlFromStateExported();
|
||||
sendAction({ type: "SYNC_PATTERN_STATE", xml });
|
||||
}
|
||||
|
||||
saveStateToSession();
|
||||
break;
|
||||
}
|
||||
|
||||
case "REMOVE_TRACK_BY_ID": {
|
||||
const { trackId } = action;
|
||||
const removed = removeTrackById(trackId); // Tenta remover pelo ID seguro
|
||||
|
||||
if (removed) {
|
||||
renderPatternEditor();
|
||||
const who = actorOf(action);
|
||||
showToast(`❌ Faixa removida por ${who}`, "warning");
|
||||
|
||||
// Sincroniza o XML com o servidor para persistir a remoção
|
||||
const isFromSelf = action.__senderId === socket.id;
|
||||
if (window.ROOM_NAME && isFromSelf) {
|
||||
// Importante: gerar o XML atualizado (sem a faixa)
|
||||
const xml = generateXmlFromStateExported();
|
||||
sendAction({ type: "SYNC_PATTERN_STATE", xml });
|
||||
}
|
||||
saveStateToSession();
|
||||
} else {
|
||||
console.warn(`Tentativa de remover track inexistente/fantasma: ${trackId}`);
|
||||
// Não faz nada, protegendo as faixas locais!
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case "REMOVE_LAST_TRACK": {
|
||||
removeLastTrackFromState();
|
||||
renderPatternEditor();
|
||||
const who = actorOf(action);
|
||||
showToast(`❌ Faixa remov. por ${who}`, "warning");
|
||||
// Salva o estado localmente também!
|
||||
|
||||
// 🔥 CORREÇÃO: Avisa o servidor que o XML mudou (removeu o TripleOscillator, por exemplo)
|
||||
const isFromSelf = action.__senderId === socket.id;
|
||||
if (window.ROOM_NAME && isFromSelf) {
|
||||
const xml = generateXmlFromStateExported();
|
||||
sendAction({ type: "SYNC_PATTERN_STATE", xml });
|
||||
}
|
||||
|
||||
saveStateToSession();
|
||||
break;
|
||||
}
|
||||
@@ -751,6 +822,31 @@ async function handleActionBroadcast(action) {
|
||||
break;
|
||||
}
|
||||
|
||||
case "REMOVE_LAST_AUDIO_LANE": {
|
||||
// 1. Verifica se tem tracks
|
||||
if (appState.audio.tracks.length === 0) break;
|
||||
|
||||
// 2. Pega a última track
|
||||
const lastTrack = appState.audio.tracks.pop();
|
||||
|
||||
// 3. Remove os clips associados a essa track (Limpeza)
|
||||
if (lastTrack && lastTrack.id) {
|
||||
// Filtra mantendo apenas os clips que NÃO pertencem à track removida
|
||||
appState.audio.clips = appState.audio.clips.filter(clip => clip.trackId !== lastTrack.id);
|
||||
}
|
||||
|
||||
// 4. Atualiza a tela
|
||||
renderAll();
|
||||
|
||||
// 5. Notifica
|
||||
const who = actorOf(action);
|
||||
showToast(`🗑️ Pista de áudio removida por ${who}`, "error");
|
||||
|
||||
// 6. Salva sessão
|
||||
saveStateToSession();
|
||||
break;
|
||||
}
|
||||
|
||||
case "REMOVE_AUDIO_CLIP":
|
||||
if (removeAudioClip(action.clipId)) {
|
||||
appState.global.selectedClipId = null;
|
||||
@@ -887,47 +983,32 @@ async function handleActionBroadcast(action) {
|
||||
|
||||
// Snapshots
|
||||
case "AUDIO_SNAPSHOT_REQUEST": {
|
||||
const clips = appState.audio?.clips?.length || 0,
|
||||
tracks = appState.audio?.tracks?.length || 0;
|
||||
const iHave = clips > 0 || tracks > 0;
|
||||
if (!iHave || isFromSelf) break;
|
||||
if (!window.__lastSnapshotSentAt) window.__lastSnapshotSentAt = 0;
|
||||
const now = Date.now();
|
||||
if (now - window.__lastSnapshotSentAt < 1500) break;
|
||||
window.__lastSnapshotSentAt = now;
|
||||
try {
|
||||
const snap = getAudioSnapshot();
|
||||
sendAction({
|
||||
type: "AUDIO_SNAPSHOT",
|
||||
snapshot: snap,
|
||||
__target: action.__senderId,
|
||||
});
|
||||
} catch (e) {
|
||||
console.warn("Erro AUDIO_SNAPSHOT_REQUEST:", e);
|
||||
}
|
||||
// Salva o estado localmente também!
|
||||
saveStateToSession();
|
||||
// 🛑 BLOQUEADO: Agora o Servidor (backend) é quem manda o snapshot autoritativo.
|
||||
// Se deixarmos os clientes responderem, eles vão enviar estados antigos ("zumbis").
|
||||
console.log("Socket: Ignorando pedido de snapshot (Server Authoritative Mode).");
|
||||
break;
|
||||
}
|
||||
|
||||
case "AUDIO_SNAPSHOT": {
|
||||
if (action.__target && action.__target !== socket.id) break;
|
||||
if (appState.global.justReset) {
|
||||
console.warn("Socket: Snapshot de áudio ignorado (justReset=true).");
|
||||
break; // Ignora o snapshot
|
||||
}
|
||||
const hasClips = (appState.audio?.clips?.length || 0) > 0;
|
||||
if (hasClips) break;
|
||||
|
||||
// Removemos a verificação "if (hasClips) break" que impedia
|
||||
// de carregar um estado vazio se o local já tivesse algo (como as faixas zumbis).
|
||||
|
||||
try {
|
||||
console.log("Socket: Aplicando Snapshot de Áudio Autoritativo...");
|
||||
await applyAudioSnapshot(action.snapshot);
|
||||
renderAll();
|
||||
|
||||
const who = actorOf(action);
|
||||
showToast(`🔁 Sync áudio por ${who}`, "success");
|
||||
// Só mostra toast se realmente tiver conteúdo, para não spammar na entrada
|
||||
if ((action.snapshot?.tracks?.length || 0) > 0) {
|
||||
showToast(`🔁 Sync áudio por ${who}`, "success");
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Erro AUDIO_SNAPSHOT:", e);
|
||||
showToast("❌ Erro sync áudio", "error");
|
||||
}
|
||||
// Salva o estado localmente também!
|
||||
// Salva o estado localmente (mesmo que seja vazio!)
|
||||
saveStateToSession();
|
||||
break;
|
||||
}
|
||||
@@ -1016,6 +1097,32 @@ async function handleActionBroadcast(action) {
|
||||
break;
|
||||
}
|
||||
|
||||
case "REMOVE_AUDIO_LANE_BY_ID": { //
|
||||
const { trackId } = action;
|
||||
|
||||
// 1. Encontra o índice da faixa com esse ID específico
|
||||
const trackIndex = appState.audio.tracks.findIndex(t => t.id === trackId);
|
||||
|
||||
if (trackIndex !== -1) {
|
||||
// 2. Remove a faixa exata do array local
|
||||
appState.audio.tracks.splice(trackIndex, 1);
|
||||
|
||||
// 3. Remove os clips associados a este ID (Limpeza local)
|
||||
appState.audio.clips = appState.audio.clips.filter(c => c.trackId !== trackId);
|
||||
|
||||
renderAll(); // Atualiza a tela
|
||||
|
||||
const who = actorOf(action);
|
||||
// showToast(`🗑️ Pista removida por ${who}`, "warning");
|
||||
|
||||
// 4. Salva a sessão para garantir persistência no F5 local
|
||||
saveStateToSession();
|
||||
} else {
|
||||
console.warn(`Tentativa de remover track inexistente: ${trackId}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
console.warn("Ação desconhecida:", action.type);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,86 @@
|
||||
import { appState } from './state.js';
|
||||
import { PIXELS_PER_STEP, ZOOM_LEVELS } from './config.js';
|
||||
|
||||
export const DEFAULT_PROJECT_XML = `<?xml version="1.0"?>
|
||||
<!DOCTYPE lmms-project>
|
||||
<lmms-project version="1.0" type="song" creatorversion="1.2.2" creator="LMMS">
|
||||
<head mastervol="100" masterpitch="0" timesig_denominator="4" timesig_numerator="4" bpm="140"/>
|
||||
<song>
|
||||
<trackcontainer visible="1" y="5" minimized="0" height="300" type="song" x="5" maximized="0" width="600">
|
||||
<track muted="0" name="TripleOscillator" type="0" solo="0">
|
||||
<instrumenttrack pitchrange="1" fxch="0" usemasterpitch="1" vol="100" pitch="0" pan="0" basenote="57">
|
||||
<instrument name="tripleoscillator">
|
||||
<tripleoscillator wavetype1="0" pan2="0" finer1="0" finel1="0" userwavefile0="" wavetype0="0" userwavefile1="" finer0="0" stphdetun1="0" vol0="33" wavetype2="0" coarse0="0" finel2="0" finer2="0" modalgo2="2" finel0="0" coarse1="-12" phoffset1="0" pan0="0" modalgo3="2" coarse2="-24" phoffset2="0" pan1="0" vol1="33" phoffset0="0" stphdetun0="0" modalgo1="2" vol2="33" stphdetun2="0" userwavefile2=""/>
|
||||
</instrument>
|
||||
<eldata fres="0.5" fwet="0" ftype="0" fcut="14000">
|
||||
<elvol ctlenvamt="0" lspd_syncmode="0" x100="0" lpdel="0" latt="0" pdel="0" lspd="0.1" lshp="0" hold="0.5" lspd_numerator="4" userwavefile="" att="0" lamt="0" dec="0.5" lspd_denominator="4" sustain="0.5" rel="0.1" amt="0"/>
|
||||
<elcut ctlenvamt="0" lspd_syncmode="0" x100="0" lpdel="0" latt="0" pdel="0" lspd="0.1" lshp="0" hold="0.5" lspd_numerator="4" userwavefile="" att="0" lamt="0" dec="0.5" lspd_denominator="4" sustain="0.5" rel="0.1" amt="0"/>
|
||||
<elres ctlenvamt="0" lspd_syncmode="0" x100="0" lpdel="0" latt="0" pdel="0" lspd="0.1" lshp="0" hold="0.5" lspd_numerator="4" userwavefile="" att="0" lamt="0" dec="0.5" lspd_denominator="4" sustain="0.5" rel="0.1" amt="0"/>
|
||||
</eldata>
|
||||
<chordcreator chordrange="1" chord-enabled="0" chord="0"/>
|
||||
<arpeggiator arpcycle="0" arptime_denominator="4" arpgate="100" arpdir="0" arpmode="0" arptime_syncmode="0" arp="0" arpmiss="0" arp-enabled="0" arptime="100" arprange="1" arpskip="0" arptime_numerator="4"/>
|
||||
<midiport inputchannel="0" outputcontroller="0" fixedoutputvelocity="-1" outputchannel="1" fixedinputvelocity="-1" outputprogram="1" inputcontroller="0" readable="0" fixedoutputnote="-1" basevelocity="63" writable="0"/>
|
||||
<fxchain enabled="0" numofeffects="0"/>
|
||||
</instrumenttrack>
|
||||
</track>
|
||||
<track muted="0" name="Sample track" type="2" solo="0">
|
||||
<sampletrack vol="100" pan="0">
|
||||
<fxchain enabled="0" numofeffects="0"/>
|
||||
</sampletrack>
|
||||
</track>
|
||||
<track muted="0" name="Beat/Bassline 0" type="1" solo="0">
|
||||
<bbtrack>
|
||||
<trackcontainer visible="0" y="5" minimized="0" height="400" type="bbtrackcontainer" x="610" maximized="0" width="700">
|
||||
<track muted="0" name="Kicker" type="0" solo="0">
|
||||
<instrumenttrack pitchrange="1" fxch="0" usemasterpitch="1" vol="100" pitch="0" pan="0" basenote="57">
|
||||
<instrument name="kicker">
|
||||
<kicker decay="440" version="1" noise="0" endfreq="40" decay_syncmode="0" decay_denominator="4" startfreq="150" env="0.163" startnote="1" dist="0.8" slope="0.06" decay_numerator="4" click="0.4" distend="0.8" gain="1" endnote="0"/>
|
||||
</instrument>
|
||||
<eldata fres="0.5" fwet="0" ftype="0" fcut="14000">
|
||||
<elvol ctlenvamt="0" lspd_syncmode="0" x100="0" lpdel="0" latt="0" pdel="0" lspd="0.1" lshp="0" hold="0.5" lspd_numerator="4" userwavefile="" att="0" lamt="0" dec="0.5" lspd_denominator="4" sustain="0.5" rel="0.1" amt="0"/>
|
||||
<elcut ctlenvamt="0" lspd_syncmode="0" x100="0" lpdel="0" latt="0" pdel="0" lspd="0.1" lshp="0" hold="0.5" lspd_numerator="4" userwavefile="" att="0" lamt="0" dec="0.5" lspd_denominator="4" sustain="0.5" rel="0.1" amt="0"/>
|
||||
<elres ctlenvamt="0" lspd_syncmode="0" x100="0" lpdel="0" latt="0" pdel="0" lspd="0.1" lshp="0" hold="0.5" lspd_numerator="4" userwavefile="" att="0" lamt="0" dec="0.5" lspd_denominator="4" sustain="0.5" rel="0.1" amt="0"/>
|
||||
</eldata>
|
||||
<chordcreator chordrange="1" chord-enabled="0" chord="0"/>
|
||||
<arpeggiator arpcycle="0" arptime_denominator="4" arpgate="100" arpdir="0" arpmode="0" arptime_syncmode="0" arp="0" arpmiss="0" arp-enabled="0" arptime="100" arprange="1" arpskip="0" arptime_numerator="4"/>
|
||||
<midiport inputchannel="0" outputcontroller="0" fixedoutputvelocity="-1" outputchannel="1" fixedinputvelocity="-1" outputprogram="1" inputcontroller="0" readable="0" fixedoutputnote="-1" basevelocity="63" writable="0"/>
|
||||
<fxchain enabled="0" numofeffects="0"/>
|
||||
</instrumenttrack>
|
||||
<pattern pos="0" muted="0" name="Kicker" steps="16" type="0"/>
|
||||
</track>
|
||||
</trackcontainer>
|
||||
</bbtrack>
|
||||
</track>
|
||||
<track muted="0" name="Automation track" type="5" solo="0">
|
||||
<automationtrack/>
|
||||
</track>
|
||||
</trackcontainer>
|
||||
<track muted="0" name="Automation track" type="6" solo="0">
|
||||
<automationtrack/>
|
||||
<automationpattern pos="0" len="192" name="Numerator" prog="0" tens="1" mute="0"/>
|
||||
<automationpattern pos="0" len="192" name="Denominator" prog="0" tens="1" mute="0"/>
|
||||
<automationpattern pos="0" len="192" name="Tempo" prog="0" tens="1" mute="0"/>
|
||||
<automationpattern pos="0" len="192" name="Master volume" prog="0" tens="1" mute="0"/>
|
||||
<automationpattern pos="0" len="192" name="Master pitch" prog="0" tens="1" mute="0"/>
|
||||
</track>
|
||||
<fxmixer visible="1" y="310" minimized="0" height="333" x="5" maximized="0" width="543">
|
||||
<fxchannel volume="1" muted="0" num="0" name="Master" soloed="0">
|
||||
<fxchain enabled="0" numofeffects="0"/>
|
||||
</fxchannel>
|
||||
</fxmixer>
|
||||
<ControllerRackView visible="1" y="310" minimized="0" height="200" x="680" maximized="0" width="350"/>
|
||||
<pianoroll visible="0" y="5" minimized="0" height="480" x="5" maximized="0" width="860"/>
|
||||
<automationeditor visible="0" y="1" minimized="0" height="400" x="1" maximized="0" width="860"/>
|
||||
<projectnotes visible="0" y="10" minimized="0" height="400" x="700" maximized="0" width="679"><![CDATA[<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
|
||||
<html><head><meta name="qrichtext" content="1" /><style type="text/css">
|
||||
p, li { white-space: pre-wrap; }
|
||||
</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:7.5pt; font-weight:400; font-style:normal;">
|
||||
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html>]]></projectnotes>
|
||||
<timeline lp1pos="192" lpstate="0" lp0pos="0"/>
|
||||
<controllers/>
|
||||
</song>
|
||||
</lmms-project>`;
|
||||
|
||||
/**
|
||||
* Helper interna para ler o BPM do input.
|
||||
* @returns {number} O BPM atual.
|
||||
|
||||
Reference in new issue
Block a user