import React, { useState, useRef, useEffect, useCallback } from 'react';
import { Play, Square, Volume2, Info, Activity, Sparkles, ShieldCheck } from 'lucide-react';
import { Visualizer } from './components/Visualizer';
const App: React.FC = () => {
const [isPlaying, setIsPlaying] = useState(false);
const [volume, setVolume] = useState(0.5);
const TARGET_HZ = 432;
const audioCtxRef = useRef
(null);
const oscillatorRef = useRef(null);
const gainNodeRef = useRef(null);
const analyzerRef = useRef(null);
const initAudio = useCallback(() => {
if (!audioCtxRef.current) {
audioCtxRef.current = new (window.AudioContext || (window as any).webkitAudioContext)();
gainNodeRef.current = audioCtxRef.current.createGain();
analyzerRef.current = audioCtxRef.current.createAnalyser();
analyzerRef.current.fftSize = 2048;
gainNodeRef.current.connect(analyzerRef.current);
analyzerRef.current.connect(audioCtxRef.current.destination);
}
}, []);
const togglePlayback = () => {
initAudio();
if (isPlaying) {
if (gainNodeRef.current && audioCtxRef.current) {
// Precise linear ramp down to prevent popping
gainNodeRef.current.gain.setTargetAtTime(0, audioCtxRef.current.currentTime, 0.03);
setTimeout(() => {
oscillatorRef.current?.stop();
oscillatorRef.current?.disconnect();
oscillatorRef.current = null;
setIsPlaying(false);
}, 100);
}
} else {
if (audioCtxRef.current?.state === 'suspended') {
audioCtxRef.current.resume();
}
const osc = audioCtxRef.current!.createOscillator();
osc.type = 'sine';
osc.frequency.setValueAtTime(TARGET_HZ, audioCtxRef.current!.currentTime);
osc.connect(gainNodeRef.current!);
gainNodeRef.current!.gain.setValueAtTime(0, audioCtxRef.current!.currentTime);
gainNodeRef.current!.gain.setTargetAtTime(volume, audioCtxRef.current!.currentTime, 0.1);
osc.start();
oscillatorRef.current = osc;
setIsPlaying(true);
}
};
useEffect(() => {
if (isPlaying && audioCtxRef.current && gainNodeRef.current) {
gainNodeRef.current.gain.setTargetAtTime(volume, audioCtxRef.current.currentTime, 0.05);
}
}, [volume, isPlaying]);
return (
{/* Therapeutic Aura Background */}
Frequência Pura
432
Hertz
{isPlaying ? 'Ressonância Infinita Ativa' : 'Pronto para Sintonização'}
);
};