317 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			317 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
// js/file.js
 | 
						|
import { appState, resetProjectState } from "./state.js";
 | 
						|
import { loadAudioForTrack } from "./pattern/pattern_state.js";
 | 
						|
import { renderAll, getSamplePathMap } from "./ui.js";
 | 
						|
import { DEFAULT_PAN, DEFAULT_VOLUME, NOTE_LENGTH } 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 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);
 | 
						|
    console.error(error); 
 | 
						|
    alert(`Erro ao carregar projeto: ${error.message}`);
 | 
						|
    return false;
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
export async function parseMmpContent(xmlString) {
 | 
						|
  resetProjectState();
 | 
						|
  initializeAudioContext();
 | 
						|
 | 
						|
  const parser = new DOMParser();
 | 
						|
  const xmlDoc = parser.parseFromString(xmlString, "application/xml");
 | 
						|
 | 
						|
  appState.global.originalXmlDoc = xmlDoc;
 | 
						|
  let 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 allBBTrackNodes = Array.from(xmlDoc.querySelectorAll('song > trackcontainer[type="song"] > track[type="1"]'));
 | 
						|
  if (allBBTrackNodes.length === 0) {
 | 
						|
    appState.pattern.tracks = []; 
 | 
						|
    renderAll(); 
 | 
						|
    return;
 | 
						|
  }
 | 
						|
  
 | 
						|
  const sortedBBTrackNameNodes = [...allBBTrackNodes].sort((a, b) => {
 | 
						|
    const bbtcoA = a.querySelector('bbtco');
 | 
						|
    const bbtcoB = b.querySelector('bbtco');
 | 
						|
    const posA = bbtcoA ? parseInt(bbtcoA.getAttribute('pos'), 10) : Infinity;
 | 
						|
    const posB = bbtcoB ? parseInt(bbtcoB.getAttribute('pos'), 10) : Infinity;
 | 
						|
    return posA - posB;
 | 
						|
  });
 | 
						|
  
 | 
						|
  const dataSourceTrack = allBBTrackNodes[0];
 | 
						|
  appState.global.currentBeatBasslineName = dataSourceTrack.getAttribute("name") || "Beat/Bassline";
 | 
						|
 | 
						|
  const bbTrackContainer = dataSourceTrack.querySelector('bbtrack > trackcontainer');
 | 
						|
  if (!bbTrackContainer) {
 | 
						|
    appState.pattern.tracks = []; 
 | 
						|
    renderAll(); 
 | 
						|
    return;
 | 
						|
  }
 | 
						|
 | 
						|
  const instrumentTracks = bbTrackContainer.querySelectorAll('track[type="0"]');
 | 
						|
  const pathMap = getSamplePathMap();
 | 
						|
  
 | 
						|
  newTracks = Array.from(instrumentTracks).map(trackNode => {
 | 
						|
    const instrumentNode = trackNode.querySelector("instrument");
 | 
						|
    const instrumentTrackNode = trackNode.querySelector("instrumenttrack");
 | 
						|
    if (!instrumentNode || !instrumentTrackNode) return null;
 | 
						|
    
 | 
						|
    const trackName = trackNode.getAttribute("name");
 | 
						|
    
 | 
						|
    if (instrumentNode.getAttribute("name") === 'tripleoscillator') {
 | 
						|
        return null; 
 | 
						|
    }
 | 
						|
 | 
						|
    const allPatternsNodeList = trackNode.querySelectorAll("pattern");
 | 
						|
    const allPatternsArray = Array.from(allPatternsNodeList).sort((a, b) => {
 | 
						|
        const posA = parseInt(a.getAttribute('pos'), 10) || 0;
 | 
						|
        const posB = parseInt(b.getAttribute('pos'), 10) || 0;
 | 
						|
        return posB - posA;
 | 
						|
    });
 | 
						|
    
 | 
						|
    const patterns = sortedBBTrackNameNodes.map((bbTrack, index) => {
 | 
						|
        const patternNode = allPatternsArray[index];
 | 
						|
        const bbTrackName = bbTrack.getAttribute("name") || `Pattern ${index + 1}`;
 | 
						|
 | 
						|
        if (!patternNode) {
 | 
						|
            const firstPattern = allPatternsArray[0];
 | 
						|
            const stepsLength = firstPattern ? parseInt(firstPattern.getAttribute("steps"), 10) || 16 : 16;
 | 
						|
            return { name: bbTrackName, steps: new Array(stepsLength).fill(false), pos: 0 };
 | 
						|
        }
 | 
						|
 | 
						|
        const patternSteps = parseInt(patternNode.getAttribute("steps"), 10) || 16;
 | 
						|
        const steps = new Array(patternSteps).fill(false);
 | 
						|
        const ticksPerStep = 12;
 | 
						|
 | 
						|
        patternNode.querySelectorAll("note").forEach((noteNode) => {
 | 
						|
            const noteLocalPos = parseInt(noteNode.getAttribute("pos"), 10);
 | 
						|
            const stepIndex = Math.round(noteLocalPos / ticksPerStep);
 | 
						|
            if (stepIndex < patternSteps) {
 | 
						|
                steps[stepIndex] = true;
 | 
						|
            }
 | 
						|
        });
 | 
						|
 | 
						|
        return {
 | 
						|
            name: bbTrackName,
 | 
						|
            steps: steps,
 | 
						|
            pos: parseInt(patternNode.getAttribute("pos"), 10) || 0
 | 
						|
        };
 | 
						|
    });
 | 
						|
 | 
						|
    const hasNotes = patterns.some(p => p.steps.includes(true));
 | 
						|
    if (!hasNotes) return null;
 | 
						|
 | 
						|
    const afpNode = instrumentNode.querySelector("audiofileprocessor");
 | 
						|
    const sampleSrc = afpNode ? afpNode.getAttribute("src") : null;
 | 
						|
    let finalSamplePath = null;
 | 
						|
    if (sampleSrc) {
 | 
						|
        const filename = sampleSrc.split("/").pop();
 | 
						|
        if (pathMap[filename]) {
 | 
						|
            finalSamplePath = pathMap[filename];
 | 
						|
        } else {
 | 
						|
            let cleanSrc = sampleSrc;
 | 
						|
            if (cleanSrc.startsWith('samples/')) {
 | 
						|
                cleanSrc = cleanSrc.substring('samples/'.length);
 | 
						|
            }
 | 
						|
            finalSamplePath = `src/samples/${cleanSrc}`;
 | 
						|
        }
 | 
						|
    }
 | 
						|
    
 | 
						|
    const volFromFile = parseFloat(instrumentTrackNode.getAttribute("vol"));
 | 
						|
    const panFromFile = parseFloat(instrumentTrackNode.getAttribute("pan"));
 | 
						|
    const firstPatternWithNotesIndex = patterns.findIndex(p => p.steps.includes(true));
 | 
						|
 | 
						|
    return {
 | 
						|
      id: Date.now() + Math.random(),
 | 
						|
      name: trackName,
 | 
						|
      samplePath: finalSamplePath,
 | 
						|
      patterns: patterns,
 | 
						|
      activePatternIndex: firstPatternWithNotesIndex !== -1 ? firstPatternWithNotesIndex : 0,
 | 
						|
      volume: !isNaN(volFromFile) ? volFromFile / 100 : DEFAULT_VOLUME,
 | 
						|
      pan: !isNaN(panFromFile) ? panFromFile / 100 : DEFAULT_PAN,
 | 
						|
      instrumentName: instrumentNode.getAttribute("name"),
 | 
						|
      instrumentXml: instrumentNode.innerHTML,
 | 
						|
    };
 | 
						|
  }).filter(track => track !== null);
 | 
						|
 | 
						|
  let isFirstTrackWithNotes = true;
 | 
						|
  newTracks.forEach(track => {
 | 
						|
    // --- INÍCIO DA CORREÇÃO ---
 | 
						|
    // Cria os nós de áudio usando os construtores do Tone.js
 | 
						|
    track.gainNode = new Tone.Gain(Tone.gainToDb(track.volume));
 | 
						|
    track.pannerNode = new Tone.Panner(track.pan);
 | 
						|
 | 
						|
    // Conecta a cadeia de áudio: Gain -> Panner -> Saída Principal (Destination)
 | 
						|
    track.gainNode.connect(track.pannerNode);
 | 
						|
    track.pannerNode.connect(getMainGainNode());
 | 
						|
    // --- FIM DA CORREÇÃO ---
 | 
						|
 | 
						|
    if (isFirstTrackWithNotes) {
 | 
						|
      const activeIdx = track.activePatternIndex || 0;
 | 
						|
      const activePattern = track.patterns[activeIdx];
 | 
						|
      if (activePattern) {
 | 
						|
        const firstPatternSteps = activePattern.steps.length;
 | 
						|
        const stepsPerBar = 16;
 | 
						|
        const requiredBars = Math.ceil(firstPatternSteps / stepsPerBar);
 | 
						|
        document.getElementById("bars-input").value = requiredBars > 0 ? requiredBars : 1;
 | 
						|
        isFirstTrackWithNotes = false;
 | 
						|
      }
 | 
						|
    }
 | 
						|
  });
 | 
						|
 | 
						|
  try {
 | 
						|
    const trackLoadPromises = newTracks.map(track => loadAudioForTrack(track));
 | 
						|
    await Promise.all(trackLoadPromises);
 | 
						|
  } catch (error) {
 | 
						|
    console.error("Ocorreu um erro ao carregar os áudios do projeto:", error);
 | 
						|
  }
 | 
						|
 | 
						|
  appState.pattern.tracks = newTracks;
 | 
						|
  appState.pattern.activeTrackId = appState.pattern.tracks[0]?.id || null;
 | 
						|
  renderAll();
 | 
						|
}
 | 
						|
 | 
						|
export function generateMmpFile() {
 | 
						|
  if (appState.global.originalXmlDoc) {
 | 
						|
    modifyAndSaveExistingMmp();
 | 
						|
  } else {
 | 
						|
    generateNewMmp();
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
function createTrackXml(track) {
 | 
						|
  if (track.patterns.length === 0) return "";
 | 
						|
  const ticksPerStep = 12; 
 | 
						|
  const lmmsVolume = Math.round(track.volume * 100);
 | 
						|
  const lmmsPan = Math.round(track.pan * 100);
 | 
						|
  const patternsXml = track.patterns.map(pattern => {
 | 
						|
    const patternNotes = pattern.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 `<pattern type="0" pos="${pattern.pos}" muted="0" steps="${pattern.steps.length}" name="${pattern.name}">
 | 
						|
        ${patternNotes}
 | 
						|
      </pattern>`;
 | 
						|
  }).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="${track.instrumentName}">
 | 
						|
          ${track.instrumentXml}
 | 
						|
        </instrument>
 | 
						|
        <fxchain enabled="0" numofeffects="0"/>
 | 
						|
      </instrumenttrack>
 | 
						|
      ${patternsXml}
 | 
						|
    </track>`;
 | 
						|
}
 | 
						|
 | 
						|
function modifyAndSaveExistingMmp() {
 | 
						|
  console.log("Modificando arquivo .mmp existente...");
 | 
						|
  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);
 | 
						|
  }
 | 
						|
  const bbTrackContainer = xmlDoc.querySelector('track[type="1"] > bbtrack > trackcontainer');
 | 
						|
  if (bbTrackContainer) {
 | 
						|
    bbTrackContainer.querySelectorAll('track[type="0"]').forEach(node => node.remove());
 | 
						|
    const tracksXml = appState.pattern.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 generateNewMmp() {
 | 
						|
  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.pattern.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</p>
 | 
						|
</body></html>]]></projectnotes>
 | 
						|
</song>
 | 
						|
</lmms-project>`;
 | 
						|
  downloadFile(mmpContent, "novo_projeto.mmp");
 | 
						|
}
 | 
						|
 | 
						|
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);
 | 
						|
} |