mmpSearch/assets/js/creations/file.js

278 lines
10 KiB
JavaScript

// js/file.js
import { appState, loadAudioForTrack } from "./state.js";
import { getTotalSteps } from "./utils.js";
import { renderApp, getSamplePathMap } from "./ui.js";
import { NOTE_LENGTH, TICKS_PER_BAR } from "./config.js";
import {
initializeAudioContext,
getAudioContext,
getMainGainNode,
} from "./audio.js";
export async function handleFileLoad(file) {
let xmlContent = "";
try {
if (file.name.toLowerCase().endsWith(".mmpz")) {
const jszip = new JSZip();
const zip = await jszip.loadAsync(file);
const projectFile = Object.keys(zip.files).find((name) =>
name.toLowerCase().endsWith(".mmp")
);
if (!projectFile)
throw new Error(
"Não foi possível encontrar um arquivo .mmp dentro do .mmpz"
);
xmlContent = await zip.files[projectFile].async("string");
} else {
xmlContent = await file.text();
}
await parseMmpContent(xmlContent);
} catch (error) {
console.error("Erro ao carregar o projeto:", error);
alert(`Erro ao carregar projeto: ${error.message}`);
}
}
export async function parseMmpContent(xmlString) {
initializeAudioContext();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "application/xml");
appState.originalXmlDoc = xmlDoc;
const newTracks = [];
const head = xmlDoc.querySelector("head");
if (head) {
document.getElementById("bpm-input").value = head.getAttribute("bpm") || 140;
document.getElementById("bars-input").value = head.getAttribute("num_bars") || 1;
document.getElementById("compasso-a-input").value = head.getAttribute("timesig_numerator") || 4;
document.getElementById("compasso-b-input").value = head.getAttribute("timesig_denominator") || 4;
}
const sampleTrackElements = xmlDoc.querySelectorAll(
'instrument[name="audiofileprocessor"]'
);
const pathMap = getSamplePathMap();
sampleTrackElements.forEach((instrumentNode) => {
const afpNode = instrumentNode.querySelector("audiofileprocessor");
const instrumentTrackNode = instrumentNode.parentElement;
const trackNode = instrumentTrackNode.parentElement;
if (!afpNode || !instrumentTrackNode || !trackNode) return;
const audioContext = getAudioContext();
const mainGainNode = getMainGainNode();
const totalSteps = getTotalSteps();
const newSteps = new Array(totalSteps).fill(false);
// ==================================================================
// (CORREÇÃO DEFINITIVA)
// 1. Simplificamos o cálculo: cada step de 1/16 vale 12 ticks.
const ticksPerStep = 12;
// 2. Buscamos TODAS as notas da trilha, não importa em qual pattern elas estejam.
trackNode.querySelectorAll("note").forEach((noteNode) => {
// ==================================================================
const pos = parseInt(noteNode.getAttribute("pos"), 10);
const stepIndex = Math.round(pos / ticksPerStep);
if (stepIndex < totalSteps) {
newSteps[stepIndex] = true;
}
});
const srcAttribute = afpNode.getAttribute("src");
const filename = srcAttribute.split("/").pop();
const finalSamplePath = pathMap[filename] || `src/samples/${srcAttribute}`;
const newTrack = {
id: Date.now() + Math.random(),
name: filename || trackNode.getAttribute("name"),
samplePath: finalSamplePath,
audioBuffer: null,
steps: newSteps,
volume: parseFloat(instrumentTrackNode.getAttribute("vol")) / 100,
pan: parseFloat(instrumentTrackNode.getAttribute("pan")) / 100,
gainNode: audioContext.createGain(),
pannerNode: audioContext.createStereoPanner(),
};
newTrack.gainNode.connect(newTrack.pannerNode);
newTrack.pannerNode.connect(mainGainNode);
newTrack.gainNode.gain.value = newTrack.volume;
newTrack.pannerNode.pan.value = newTrack.pan;
newTracks.push(newTrack);
});
try {
const trackLoadPromises = newTracks.map(track => loadAudioForTrack(track));
await Promise.all(trackLoadPromises);
console.log("Todos os áudios do projeto foram carregados.");
} catch (error) {
console.error("Ocorreu um erro ao carregar os áudios do projeto:", error);
}
appState.tracks = newTracks;
renderApp();
console.log("Projeto carregado com sucesso!", appState);
}
export function generateMmpFile() {
if (appState.originalXmlDoc) {
modifyAndSaveExistingMmp();
} else {
generateNewMmp();
}
}
function generateNewMmp() {
console.log("Gerando novo arquivo .mmp do zero...");
const bpm = document.getElementById("bpm-input").value;
const sig_num = document.getElementById("compasso-a-input").value;
const sig_den = document.getElementById("compasso-b-input").value;
const num_bars = document.getElementById("bars-input").value;
const tracksXml = appState.tracks
.map((track) => createTrackXml(track))
.join("");
const mmpContent = `<?xml version="1.0"?>
<!DOCTYPE lmms-project>
<lmms-project version="1.0" type="song" creator="MMPCreator" creatorversion="1.0">
<head mastervol="100" timesig_denominator="${sig_den}" bpm="${bpm}" timesig_numerator="${sig_num}" masterpitch="0" num_bars="${num_bars}"/>
<song>
<trackcontainer type="song">
<track type="1" solo="0" muted="0" name="Beat/Bassline 0">
<bbtrack>
<trackcontainer type="bbtrackcontainer">
${tracksXml}
</trackcontainer>
</bbtrack>
<bbtco color="4286611584" len="192" usestyle="1" pos="0" muted="0" name="Pattern 1"/>
</track>
</trackcontainer>
<timeline lp1pos="192" lp0pos="0" lpstate="0"/>
<controllers/>
<projectnotes width="686" y="10" minimized="0" visible="0" x="700" maximized="0" height="400"><![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>
<p>Feito com MMPCreator no https://alice.ufsj.edu.br/MMPSearch/creator</p>
</body></html>]]></projectnotes>
</song>
</lmms-project>`;
downloadFile(mmpContent, "novo_projeto.mmp");
}
function modifyAndSaveExistingMmp() {
console.log("Modificando arquivo .mmp existente...");
const xmlDoc = appState.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
);
}
const bbTrackContainer = xmlDoc.querySelector("bbtrack > trackcontainer");
if (bbTrackContainer) {
const oldSampleTracks = bbTrackContainer.querySelectorAll(
'instrument[name="audiofileprocessor"]'
);
oldSampleTracks.forEach((node) => node.closest("track").remove());
const tracksXml = appState.tracks
.map((track) => createTrackXml(track))
.join("");
const tempDoc = new DOMParser().parseFromString(
`<root>${tracksXml}</root>`,
"application/xml"
);
Array.from(tempDoc.documentElement.children).forEach((newTrackNode) => {
bbTrackContainer.appendChild(newTrackNode);
});
}
const serializer = new XMLSerializer();
const mmpContent = serializer.serializeToString(xmlDoc);
downloadFile(mmpContent, "projeto_editado.mmp");
}
function createTrackXml(track) {
if (!track.samplePath) return "";
const totalSteps = track.steps.length || getTotalSteps();
const ticksPerStep = 12; // Valor fixo de 12 ticks por step de 1/16
const lmmsVolume = Math.round(track.volume * 100);
const lmmsPan = Math.round(track.pan * 100);
const sampleSrc = track.samplePath.replace("src/samples/", "");
const notesXml = track.steps
.map((isActive, index) => {
if (isActive) {
const notePos = Math.round(index * ticksPerStep);
return `<note vol="100" len="${NOTE_LENGTH}" pos="${notePos}" pan="0" key="57"/>`;
}
return "";
})
.join("\n ");
// Cria um pattern para cada compasso (16 steps)
let patternsXml = '';
const stepsPerBar = 16;
const numBars = Math.ceil(totalSteps / stepsPerBar);
for (let i = 0; i < numBars; i++) {
const patternNotes = track.steps.slice(i * stepsPerBar, (i + 1) * stepsPerBar).map((isActive, index) => {
if (isActive) {
const notePos = Math.round(index * ticksPerStep);
return `<note vol="100" len="${NOTE_LENGTH}" pos="${notePos}" pan="0" key="57"/>`;
}
return "";
}).join("\n ");
patternsXml += `<pattern type="0" pos="${i * TICKS_PER_BAR}" muted="0" steps="${stepsPerBar}" name="Pattern ${i+1}">
${patternNotes}
</pattern>`
}
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="audiofileprocessor">
<audiofileprocessor eframe="1" stutter="0" looped="0" interp="1" reversed="0" lframe="0" src="${sampleSrc}" amp="100" sframe="0"/>
</instrument>
<fxchain enabled="0" numofeffects="0"/>
</instrumenttrack>
${patternsXml}
</track>`;
}
function downloadFile(content, fileName) {
const blob = new Blob([content], { type: "application/xml;charset=utf-8" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
export async function loadProjectFromServer(fileName) {
try {
const response = await fetch(`mmp/${fileName}`);
if (!response.ok)
throw new Error(`Não foi possível carregar o arquivo ${fileName}`);
const xmlContent = await response.text();
await parseMmpContent(xmlContent);
return true;
} catch (error) {
console.error("Erro ao carregar projeto do servidor:", error);
alert(`Erro ao carregar projeto: ${error.message}`);
return false;
}
}