244 lines
8.6 KiB
JavaScript
244 lines
8.6 KiB
JavaScript
// js/file.js
|
|
import { appState } from "./state.js";
|
|
import { getTotalSteps } from "./utils.js";
|
|
import { renderApp } 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();
|
|
}
|
|
parseMmpContent(xmlContent);
|
|
} catch (error) {
|
|
console.error("Erro ao carregar o projeto:", error);
|
|
alert(`Erro ao carregar projeto: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
export 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("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"]'
|
|
);
|
|
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 = parseInt(
|
|
trackNode.querySelector("pattern")?.getAttribute("steps") ||
|
|
getTotalSteps(),
|
|
10
|
|
);
|
|
const newSteps = new Array(totalSteps).fill(false);
|
|
const ticksPerStep = totalSteps > 0 ? TICKS_PER_BAR / totalSteps : 0;
|
|
|
|
trackNode.querySelectorAll("pattern note").forEach((noteNode) => {
|
|
const pos = parseInt(noteNode.getAttribute("pos"), 10);
|
|
const stepIndex = Math.round(pos / ticksPerStep);
|
|
if (stepIndex < totalSteps) {
|
|
newSteps[stepIndex] = true;
|
|
}
|
|
});
|
|
const newTrack = {
|
|
id: Date.now() + Math.random(),
|
|
name:
|
|
afpNode.getAttribute("src").split("/").pop() ||
|
|
trackNode.getAttribute("name"),
|
|
samplePath: `samples/${afpNode.getAttribute("src")}`,
|
|
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);
|
|
});
|
|
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 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"/>
|
|
<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(
|
|
"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 = totalSteps > 0 ? TICKS_PER_BAR / totalSteps : 0;
|
|
const lmmsVolume = Math.round(track.volume * 100);
|
|
const lmmsPan = Math.round(track.pan * 100);
|
|
const sampleSrc = track.samplePath.replace("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 ");
|
|
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>
|
|
<pattern type="0" pos="0" muted="0" steps="${totalSteps}" name="${track.name}">
|
|
${notesXml}
|
|
</pattern>
|
|
</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);
|
|
}
|
|
|
|
// (ALTERADO) Adiciona export na frente da função
|
|
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();
|
|
parseMmpContent(xmlContent);
|
|
// Retorna true em caso de sucesso
|
|
return true;
|
|
} catch (error) {
|
|
console.error("Erro ao carregar projeto do servidor:", error);
|
|
alert(`Erro ao carregar projeto: ${error.message}`);
|
|
// Retorna false em caso de falha
|
|
return false;
|
|
}
|
|
}
|