mmpSearch/assets/js/creations/audio/audio_audio.js

304 lines
8.8 KiB
JavaScript

// js/audio/audio_audio.js
import { appState } from "../state.js";
import { updateAudioEditorUI, updatePlayheadVisual, resetPlayheadVisual } from "./audio_ui.js";
import { initializeAudioContext, getAudioContext } from "../audio.js";
import { getPixelsPerSecond } from "../utils.js";
// --- Configurações do Scheduler ---
const LOOKAHEAD_INTERVAL_MS = 25.0;
const SCHEDULE_AHEAD_TIME_SEC = 0.5; // 500ms
// --- Estado Interno do Engine ---
let audioCtx = null;
let isPlaying = false;
let schedulerIntervalId = null;
let animationFrameId = null;
// Sincronização de Tempo
let startTime = 0;
let seekTime = 0;
let logicalPlaybackTime = 0;
// Configurações de Loop
let isLoopActive = false;
let loopStartTimeSec = 0;
let loopEndTimeSec = 8;
const runtimeClipState = new Map();
const scheduledNodes = new Map();
let nextEventId = 0;
const callbacks = {
onClipScheduled: null,
onClipPlayed: null,
};
// --- Funções Auxiliares de Tempo (sem alterações) ---
function _getBpm() {
const bpmInput = document.getElementById("bpm-input");
return parseFloat(bpmInput.value) || 120;
}
function _getSecondsPerBeat() { return 60.0 / _getBpm(); }
function _convertBeatToSeconds(beat) { return beat * _getSecondsPerBeat(); }
function _convertSecondsToBeat(seconds) { return seconds / _getSecondsPerBeat(); }
function _initContext() {
if (!audioCtx) {
initializeAudioContext();
audioCtx = getAudioContext();
}
}
// --- Lógica Principal do Scheduler (sem alterações) ---
function _scheduleClip(clip, absolutePlayTime, durationSec) {
if (!clip.buffer) {
console.warn(`Clip ${clip.id} não possui áudio buffer carregado.`);
return;
}
if (!clip.gainNode || !clip.pannerNode) {
console.warn(`Clip ${clip.id} não possui gainNode ou pannerNode.`);
return;
}
const source = new Tone.BufferSource(clip.buffer);
source.connect(clip.gainNode);
// --- CORREÇÃO: Aplica o pitch (que pode ser de stretch ou wheel) ---
if (clip.pitch && clip.pitch !== 0) {
source.playbackRate.value = Math.pow(2, clip.pitch / 12);
} else {
source.playbackRate.value = 1.0; // Garante que o modo 'trim' toque normal
}
// --- FIM DA CORREÇÃO ---
const eventId = nextEventId++;
const clipOffset = clip.offsetInSeconds || clip.offset || 0;
source.start(absolutePlayTime, clipOffset, durationSec);
scheduledNodes.set(eventId, { sourceNode: source, clipId: clip.id });
if (callbacks.onClipScheduled) {
callbacks.onClipScheduled(clip);
}
source.onended = () => {
_handleClipEnd(eventId, clip.id);
source.dispose();
};
}
function _handleClipEnd(eventId, clipId) {
scheduledNodes.delete(eventId);
runtimeClipState.delete(clipId);
if (callbacks.onClipPlayed) {
const clip = appState.audio.clips.find(c => c.id == clipId);
if(clip) callbacks.onClipPlayed(clip);
}
}
function _schedulerTick() {
if (!isPlaying || !audioCtx) return;
const now = audioCtx.currentTime;
const logicalTime = (now - startTime) + seekTime;
const scheduleWindowStartSec = logicalTime;
const scheduleWindowEndSec = logicalTime + SCHEDULE_AHEAD_TIME_SEC;
for (const clip of appState.audio.clips) {
const clipRuntime = runtimeClipState.get(clip.id) || { isScheduled: false };
if (clipRuntime.isScheduled) {
continue;
}
if (!clip.buffer) {
continue;
}
const clipStartTimeSec = clip.startTimeInSeconds;
const clipDurationSec = clip.durationInSeconds;
if (typeof clipStartTimeSec === 'undefined' || typeof clipDurationSec === 'undefined') {
continue;
}
let occurrenceStartTimeSec = clipStartTimeSec;
if (isLoopActive) {
const loopDuration = loopEndTimeSec - loopStartTimeSec;
if (loopDuration <= 0) continue;
if (occurrenceStartTimeSec < loopStartTimeSec && logicalTime >= loopStartTimeSec) {
const offsetFromLoopStart = (occurrenceStartTimeSec - loopStartTimeSec) % loopDuration;
occurrenceStartTimeSec = loopStartTimeSec + (offsetFromLoopStart < 0 ? offsetFromLoopStart + loopDuration : offsetFromLoopStart);
}
if (occurrenceStartTimeSec < logicalTime) {
const loopsMissed = Math.floor((logicalTime - occurrenceStartTimeSec) / loopDuration) + 1;
occurrenceStartTimeSec += loopsMissed * loopDuration;
}
}
if (
occurrenceStartTimeSec >= scheduleWindowStartSec &&
occurrenceStartTimeSec < scheduleWindowEndSec
) {
const absolutePlayTime = startTime + (occurrenceStartTimeSec - seekTime);
_scheduleClip(clip, absolutePlayTime, clipDurationSec);
clipRuntime.isScheduled = true;
runtimeClipState.set(clip.id, clipRuntime);
}
}
}
// --- Loop de Animação (sem alterações) ---
function _animationLoop() {
if (!isPlaying) {
animationFrameId = null;
return;
}
const now = audioCtx.currentTime;
let newLogicalTime = (now - startTime) + seekTime;
if (isLoopActive) {
if (newLogicalTime >= loopEndTimeSec) {
const loopDuration = loopEndTimeSec - loopStartTimeSec;
newLogicalTime = loopStartTimeSec + ((newLogicalTime - loopStartTimeSec) % loopDuration);
startTime = now;
seekTime = newLogicalTime;
}
}
logicalPlaybackTime = newLogicalTime;
if (!isLoopActive) {
let maxTime = 0;
appState.audio.clips.forEach(clip => {
const clipStartTime = clip.startTimeInSeconds || 0;
const clipDuration = clip.durationInSeconds || 0;
const endTime = clipStartTime + clipDuration;
if (endTime > maxTime) maxTime = endTime;
});
if (maxTime > 0 && logicalPlaybackTime >= maxTime) {
stopAudioEditorPlayback(true); // Rebobina no fim
resetPlayheadVisual();
return;
}
}
const pixelsPerSecond = getPixelsPerSecond();
const newPositionPx = logicalPlaybackTime * pixelsPerSecond;
updatePlayheadVisual(newPositionPx);
animationFrameId = requestAnimationFrame(_animationLoop);
}
// --- API Pública ---
export function updateTransportLoop() {
isLoopActive = appState.global.isLoopActive;
loopStartTimeSec = appState.global.loopStartTime;
loopEndTimeSec = appState.global.loopEndTime;
runtimeClipState.clear();
scheduledNodes.forEach(nodeData => {
// --- CORREÇÃO BUG 1: Remove a linha 'onended = null' ---
nodeData.sourceNode.stop(0);
nodeData.sourceNode.dispose();
});
scheduledNodes.clear();
}
export function startAudioEditorPlayback() {
if (isPlaying) return;
_initContext();
if (audioCtx.state === 'suspended') {
audioCtx.resume();
}
isPlaying = true;
// --- CORREÇÃO BUG 2: Atualiza o estado global ---
appState.global.isAudioEditorPlaying = true;
startTime = audioCtx.currentTime;
updateTransportLoop();
console.log("%cIniciando Playback...", "color: #3498db;");
_schedulerTick();
schedulerIntervalId = setInterval(_schedulerTick, LOOKAHEAD_INTERVAL_MS);
animationFrameId = requestAnimationFrame(_animationLoop);
updateAudioEditorUI();
const playBtn = document.getElementById("audio-editor-play-btn");
if (playBtn) {
playBtn.className = 'fa-solid fa-pause';
}
}
export function stopAudioEditorPlayback(rewind = false) {
if (!isPlaying) return;
isPlaying = false;
// --- CORREÇÃO BUG 2: Atualiza o estado global ---
appState.global.isAudioEditorPlaying = false;
console.log(`%cParando Playback... (Rewind: ${rewind})`, "color: #d9534f;");
clearInterval(schedulerIntervalId);
schedulerIntervalId = null;
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
seekTime = logicalPlaybackTime;
logicalPlaybackTime = 0;
if (rewind) {
seekTime = 0;
}
scheduledNodes.forEach(nodeData => {
// --- CORREÇÃO BUG 1: Remove a linha 'onended = null' ---
nodeData.sourceNode.stop(0);
nodeData.sourceNode.dispose();
});
scheduledNodes.clear();
runtimeClipState.clear();
updateAudioEditorUI();
const playBtn = document.getElementById("audio-editor-play-btn");
if (playBtn) {
playBtn.className = 'fa-solid fa-play';
}
if (rewind) {
resetPlayheadVisual();
}
}
export function restartAudioEditorIfPlaying() {
if (isPlaying) {
stopAudioEditorPlayback(false); // Pausa
startAudioEditorPlayback();
}
}
export function seekAudioEditor(newTime) {
const wasPlaying = isPlaying;
if (wasPlaying) {
stopAudioEditorPlayback(false); // Pausa
}
seekTime = newTime;
logicalPlaybackTime = newTime;
const pixelsPerSecond = getPixelsPerSecond();
const newPositionPx = newTime * pixelsPerSecond;
updatePlayheadVisual(newPositionPx);
if (wasPlaying) {
startAudioEditorPlayback();
}
}
export function registerCallbacks(newCallbacks) {
if (newCallbacks.onClipScheduled) {
callbacks.onClipScheduled = newCallbacks.onClipScheduled;
}
if (newCallbacks.onClipPlayed) {
callbacks.onClipPlayed = newCallbacks.onClipPlayed;
}
}