145 lines
3.9 KiB
JavaScript
145 lines
3.9 KiB
JavaScript
// js/state.js
|
|
import { initializePatternState } from "./pattern/pattern_state.js";
|
|
import { audioState, initializeAudioState, getAudioSnapshot, applyAudioSnapshot } from "./audio/audio_state.js";
|
|
import { DEFAULT_VOLUME, DEFAULT_PAN } from "./config.js";
|
|
import { generateMmpFile } from "./file.js"; // Vamos usar uma adaptação disto ou criar um helper
|
|
import { parseMmpContent } from "./file.js"; // Importante para restaurar o XML
|
|
|
|
// Estado global da aplicação
|
|
const globalState = {
|
|
sliceToolActive: false,
|
|
isPlaying: false,
|
|
isAudioEditorPlaying: false,
|
|
playbackIntervalId: null,
|
|
currentStep: 0,
|
|
metronomeEnabled: false,
|
|
originalXmlDoc: null,
|
|
currentBeatBasslineName: "Novo Projeto",
|
|
masterVolume: DEFAULT_VOLUME,
|
|
masterPan: DEFAULT_PAN,
|
|
zoomLevelIndex: 2,
|
|
isLoopActive: false,
|
|
loopStartTime: 0,
|
|
loopEndTime: 8,
|
|
resizeMode: "trim",
|
|
selectedClipId: null,
|
|
isRecording: false,
|
|
clipboard: null,
|
|
lastRulerClickTime: 0,
|
|
justReset: false,
|
|
// Configs Globais que devem persistir
|
|
bpm: 140,
|
|
compassoA: 4,
|
|
compassoB: 4,
|
|
bars: 1,
|
|
syncMode: "global" // Adicionado para persistir a escolha
|
|
};
|
|
|
|
const patternState = {
|
|
tracks: [],
|
|
activeTrackId: null,
|
|
activePatternIndex: 0,
|
|
};
|
|
|
|
// Combina todos os estados em um único objeto namespaced
|
|
export let appState = {
|
|
global: globalState,
|
|
pattern: patternState,
|
|
audio: audioState,
|
|
};
|
|
window.appState = appState;
|
|
|
|
export function resetProjectState() {
|
|
console.log("Executando resetProjectState completo...");
|
|
|
|
// 1. Reseta o estado global
|
|
Object.assign(appState.global, {
|
|
sliceToolActive: false,
|
|
isPlaying: false,
|
|
isAudioEditorPlaying: false,
|
|
playbackIntervalId: null,
|
|
currentStep: 0,
|
|
metronomeEnabled: false,
|
|
originalXmlDoc: null,
|
|
currentBeatBasslineName: "Novo Projeto",
|
|
masterVolume: DEFAULT_VOLUME,
|
|
masterPan: DEFAULT_PAN,
|
|
zoomLevelIndex: 2,
|
|
isLoopActive: false,
|
|
loopStartTime: 0,
|
|
loopEndTime: 8,
|
|
resizeMode: "trim",
|
|
selectedClipId: null,
|
|
isRecording: false,
|
|
clipboard: null,
|
|
lastRulerClickTime: 0,
|
|
justReset: false,
|
|
});
|
|
|
|
// 2. Reseta o estado do pattern
|
|
Object.assign(appState.pattern, {
|
|
tracks: [],
|
|
activeTrackId: null,
|
|
activePatternIndex: 0,
|
|
});
|
|
|
|
// 3. 🔥 CRÍTICO: Reseta o estado de áudio na força bruta
|
|
// Isso garante que os arrays fiquem vazios ANTES de qualquer salvamento de sessão
|
|
if (appState.audio) {
|
|
appState.audio.tracks = [];
|
|
appState.audio.clips = [];
|
|
appState.audio.audioEditorSeekTime = 0;
|
|
}
|
|
|
|
// Chama a função original para garantir reinicialização de contextos se houver
|
|
initializeAudioState();
|
|
}
|
|
|
|
export function saveStateToSession() {
|
|
if (!window.ROOM_NAME) return;
|
|
|
|
// 1. Limpa Tracks do Pattern
|
|
const cleanTracks = appState.pattern.tracks.map((track) => {
|
|
return {
|
|
id: track.id,
|
|
name: track.name,
|
|
samplePath: track.samplePath,
|
|
patterns: track.patterns,
|
|
activePatternIndex: track.activePatternIndex,
|
|
volume: track.volume,
|
|
pan: track.pan,
|
|
instrumentName: track.instrumentName,
|
|
instrumentXml: track.instrumentXml,
|
|
};
|
|
});
|
|
|
|
// 2. Constrói o objeto. 🔥 AQUI ADICIONAMOS O ÁUDIO
|
|
const stateToSave = {
|
|
pattern: {
|
|
...appState.pattern,
|
|
tracks: cleanTracks,
|
|
},
|
|
// Salva o estado de Áudio para o F5 funcionar
|
|
audio: {
|
|
tracks: appState.audio.tracks || [],
|
|
clips: appState.audio.clips || [],
|
|
},
|
|
global: {
|
|
bpm: document.getElementById("bpm-input").value,
|
|
compassoA: document.getElementById("compasso-a-input").value,
|
|
compassoB: document.getElementById("compasso-b-input").value,
|
|
bars: document.getElementById("bars-input").value,
|
|
syncMode: appState.global.syncMode,
|
|
},
|
|
};
|
|
|
|
try {
|
|
const roomName = window.ROOM_NAME || "default_room";
|
|
sessionStorage.setItem(
|
|
`temp_state_${roomName}`,
|
|
JSON.stringify(stateToSave)
|
|
);
|
|
} catch (e) {
|
|
console.error("Falha ao salvar estado na sessão:", e);
|
|
}
|
|
} |