Corrigindo samples sobrecarregados. Agora está carregando e reaproveitando, ao invés de sempre carregar um novo sample ao tocá-los nas patterns.

This commit is contained in:
JotaChina committed 2025-08-29 17:03:48 -03:00
1 parent 5332e426c0
commit e776a830c8
2 files changed
+54 -13

No files matched your search

+29 -10
View File
@@ -1,5 +1,3 @@
//TODO ver Tone.js
// js/audio.js
import { appState } from "./state.js";
import { highlightStep } from "./ui.js";
@@ -44,23 +42,44 @@ export function playMetronomeSound(isDownbeat) {
oscillator.stop(audioContext.currentTime + 0.05);
}
// (ALTERADO) Função reescrita para usar AudioBuffers pré-carregados
export function playSample(filePath, trackId) {
initializeAudioContext();
if (!filePath) return;
const track = trackId ? appState.tracks.find((t) => t.id == trackId) : null;
const audio = new Audio(filePath);
const source = audioContext.createMediaElementSource(audio);
if (track && track.gainNode) {
source.connect(track.gainNode);
} else {
source.connect(mainGainNode);
// Se a função for chamada sem um ID de track (ex: clique no browser de samples),
// usa o método antigo como fallback para uma prévia rápida.
if (!track) {
const audio = new Audio(filePath);
audio.play();
return;
}
audio.play();
// Se a track não tiver o buffer de áudio pronto, avisa no console e não toca.
if (!track.audioBuffer) {
console.warn(`Buffer para a track ${track.name} ainda não carregado.`);
return;
}
// 1. Cria uma fonte de áudio leve (BufferSource)
const source = audioContext.createBufferSource();
// 2. Conecta o buffer de áudio já decodificado
source.buffer = track.audioBuffer;
// 3. Conecta na cadeia de áudio da track (respeitando volume e pan)
if (track.gainNode) {
source.connect(track.gainNode);
} else {
source.connect(mainGainNode); // Fallback
}
// 4. Toca o som imediatamente
source.start(0);
}
function tick() {
const totalSteps = getTotalSteps();
if (totalSteps === 0) {
@@ -131,4 +150,4 @@ export function togglePlayback() {
} else {
startPlayback();
}
}
}