|
| 1 | +import { useCallback, useEffect, useState, useRef } from "react"; |
| 2 | +import AudioManager from "./AudioManager"; |
| 3 | +import { frequencies } from "./Equalizer"; |
| 4 | + |
| 5 | +export interface EqualizerControl { |
| 6 | + frequency: number; |
| 7 | + gain: number; |
| 8 | + type: BiquadFilterType; |
| 9 | +} |
| 10 | + |
| 11 | +interface DragState { |
| 12 | + isDragging: boolean; |
| 13 | + dragIndex: number; |
| 14 | + startY: number; |
| 15 | + startGain: number; |
| 16 | +} |
| 17 | + |
| 18 | +const minFreq = frequencies[0].frequency; |
| 19 | +const maxFreq = frequencies[frequencies.length - 1].frequency; |
| 20 | +const logMin = Math.log10(minFreq); |
| 21 | +const logMax = Math.log10(maxFreq); |
| 22 | + |
| 23 | +const initialValues: EqualizerControl[] = frequencies.map(freq => ({ |
| 24 | + frequency: freq.frequency, |
| 25 | + type: freq.type, |
| 26 | + gain: 0, |
| 27 | +})); |
| 28 | + |
| 29 | +export default function useEqualizerControls(canvasRef?: React.RefObject<HTMLCanvasElement>) { |
| 30 | + const [equalizerBands, setEqualizerBands] = useState<EqualizerControl[]>(initialValues); |
| 31 | + const dragState = useRef<DragState>({ |
| 32 | + isDragging: false, |
| 33 | + dragIndex: -1, |
| 34 | + startY: 0, |
| 35 | + startGain: 0, |
| 36 | + }); |
| 37 | + const canvasRect = useRef<DOMRect | null>(null); |
| 38 | + |
| 39 | + const updateGain = useCallback((index: number, gain: number) => { |
| 40 | + // Clamp gain between -12 and +12 dB |
| 41 | + const clampedGain = Math.max(-12, Math.min(12, gain)); |
| 42 | + |
| 43 | + setEqualizerBands(prev => { |
| 44 | + const newBands = [...prev]; |
| 45 | + newBands[index] = { ...newBands[index], gain: clampedGain }; |
| 46 | + return newBands; |
| 47 | + }); |
| 48 | + }, []); |
| 49 | + |
| 50 | + const getControlPointPosition = useCallback((frequency: number, gain: number, canvasRect: DOMRect, index: number) => { |
| 51 | + // Use the same drawing bounds logic as the drawing function |
| 52 | + const labelBoxSize = 32; |
| 53 | + const drawingWidth = canvasRect.width - labelBoxSize; |
| 54 | + const drawingHeight = canvasRect.height - labelBoxSize; |
| 55 | + const offsetX = 0; // drawing starts at x=0 |
| 56 | + const offsetY = 0; // drawing starts at y=0 |
| 57 | + |
| 58 | + const logFreq = Math.log10(frequency); |
| 59 | + const normalizedPos = (logFreq - logMin) / (logMax - logMin); |
| 60 | + let x = offsetX + (normalizedPos * drawingWidth); |
| 61 | + |
| 62 | + // Apply custom positioning - exactly like the drawing function: |
| 63 | + // Points 1,2,5 keep their current positions, point 3 goes to center + 96px, point 4 reflects point 2 |
| 64 | + if (index === 0) { |
| 65 | + x += 96; // Point 1 (index 0) - keep current position |
| 66 | + } else if (index === 1) { |
| 67 | + x += 91; // Point 2 (index 1) - keep current position |
| 68 | + } else if (index === 2) { |
| 69 | + x = offsetX + drawingWidth / 2 + 59; // Point 3 (index 2) - center + 59px to the right |
| 70 | + } else if (index === 3) { |
| 71 | + x -= 18; // Point 4 (index 3) - reflect point 2's position |
| 72 | + } else if (index === 4) { |
| 73 | + x -= 96; // Point 5 (index 4) - keep current position |
| 74 | + } |
| 75 | + |
| 76 | + const normalizedGain = gain / 12; // -1 to 1 range |
| 77 | + const y = offsetY + drawingHeight / 2 - (normalizedGain * drawingHeight / 2); |
| 78 | + |
| 79 | + return { x, y }; |
| 80 | + }, []); |
| 81 | + |
| 82 | + const findControlPointAtPosition = useCallback((x: number, y: number, rect: DOMRect) => { |
| 83 | + const controlPointRadius = 24; // 48px diameter = 24px radius |
| 84 | + |
| 85 | + for (let i = 0; i < equalizerBands.length; i++) { |
| 86 | + const band = equalizerBands[i]; |
| 87 | + const pos = getControlPointPosition(band.frequency, band.gain, rect, i); |
| 88 | + |
| 89 | + const distance = Math.sqrt((x - pos.x) ** 2 + (y - pos.y) ** 2); |
| 90 | + if (distance <= controlPointRadius) { |
| 91 | + return i; |
| 92 | + } |
| 93 | + } |
| 94 | + return -1; |
| 95 | + }, [equalizerBands, getControlPointPosition]); const handleStart = useCallback((clientX: number, clientY: number) => { |
| 96 | + if (!canvasRef?.current) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + const rect = canvasRef.current.getBoundingClientRect(); |
| 101 | + canvasRect.current = rect; |
| 102 | + |
| 103 | + const x = clientX - rect.left; |
| 104 | + const y = clientY - rect.top; |
| 105 | + |
| 106 | + const index = findControlPointAtPosition(x, y, rect); |
| 107 | + if (index !== -1) { |
| 108 | + dragState.current = { |
| 109 | + isDragging: true, |
| 110 | + dragIndex: index, |
| 111 | + startY: clientY, |
| 112 | + startGain: equalizerBands[index].gain, |
| 113 | + }; |
| 114 | + } |
| 115 | + }, [canvasRef, findControlPointAtPosition, equalizerBands]); |
| 116 | + |
| 117 | + const handleMove = useCallback((clientY: number) => { |
| 118 | + if (!dragState.current.isDragging || !canvasRect.current) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + const deltaY = dragState.current.startY - clientY; |
| 123 | + const labelBoxSize = 32; |
| 124 | + const drawingHeight = canvasRect.current.height - labelBoxSize; |
| 125 | + const gainChange = (deltaY / drawingHeight) * 24; // 24dB range using drawing height |
| 126 | + let newGain = dragState.current.startGain + gainChange; |
| 127 | + |
| 128 | + // Clamp gain to prevent control points from going outside the canvas boundaries |
| 129 | + // The control points have a 24px radius, so we need to account for that |
| 130 | + const controlPointRadius = 24; |
| 131 | + const maxGainForBounds = 12 * (1 - (2 * controlPointRadius) / drawingHeight); |
| 132 | + const minGainForBounds = -maxGainForBounds; |
| 133 | + |
| 134 | + // Apply stricter bounds to keep circles fully visible |
| 135 | + newGain = Math.max(minGainForBounds, Math.min(maxGainForBounds, newGain)); |
| 136 | + |
| 137 | + updateGain(dragState.current.dragIndex, newGain); |
| 138 | + }, [updateGain]); |
| 139 | + |
| 140 | + const handleEnd = useCallback(() => { |
| 141 | + dragState.current.isDragging = false; |
| 142 | + dragState.current.dragIndex = -1; |
| 143 | + canvasRect.current = null; |
| 144 | + }, []); |
| 145 | + |
| 146 | + // Mouse event handlers |
| 147 | + const handleMouseDown = useCallback((e: MouseEvent) => { |
| 148 | + handleStart(e.clientX, e.clientY); |
| 149 | + }, [handleStart]); |
| 150 | + |
| 151 | + const handleMouseMove = useCallback((e: MouseEvent) => { |
| 152 | + handleMove(e.clientY); |
| 153 | + }, [handleMove]); |
| 154 | + |
| 155 | + const handleMouseUp = useCallback(() => { |
| 156 | + handleEnd(); |
| 157 | + }, [handleEnd]); |
| 158 | + |
| 159 | + // Touch event handlers |
| 160 | + const handleTouchStart = useCallback((e: TouchEvent) => { |
| 161 | + if (e.touches.length === 1) { |
| 162 | + const touch = e.touches[0]; |
| 163 | + handleStart(touch.clientX, touch.clientY); |
| 164 | + } |
| 165 | + }, [handleStart]); |
| 166 | + |
| 167 | + const handleTouchMove = useCallback((e: TouchEvent) => { |
| 168 | + if (e.touches.length === 1 && dragState.current.isDragging) { |
| 169 | + e.preventDefault(); // Prevent scrolling |
| 170 | + const touch = e.touches[0]; |
| 171 | + handleMove(touch.clientY); |
| 172 | + } |
| 173 | + }, [handleMove]); |
| 174 | + |
| 175 | + const handleTouchEnd = useCallback(() => { |
| 176 | + handleEnd(); |
| 177 | + }, [handleEnd]); |
| 178 | + |
| 179 | + // Setup global event listeners |
| 180 | + useEffect(() => { |
| 181 | + if (!canvasRef?.current) { |
| 182 | + return; |
| 183 | + } |
| 184 | + |
| 185 | + const canvas = canvasRef.current; |
| 186 | + |
| 187 | + // Add canvas event listeners |
| 188 | + canvas.addEventListener('mousedown', handleMouseDown); |
| 189 | + canvas.addEventListener('touchstart', handleTouchStart, { passive: false }); |
| 190 | + |
| 191 | + // Add global event listeners |
| 192 | + document.addEventListener('mousemove', handleMouseMove); |
| 193 | + document.addEventListener('mouseup', handleMouseUp); |
| 194 | + document.addEventListener('touchmove', handleTouchMove, { passive: false }); |
| 195 | + document.addEventListener('touchend', handleTouchEnd); |
| 196 | + document.addEventListener('touchcancel', handleTouchEnd); |
| 197 | + |
| 198 | + return () => { |
| 199 | + // Remove canvas event listeners |
| 200 | + canvas.removeEventListener('mousedown', handleMouseDown); |
| 201 | + canvas.removeEventListener('touchstart', handleTouchStart); |
| 202 | + |
| 203 | + // Remove global event listeners |
| 204 | + document.removeEventListener('mousemove', handleMouseMove); |
| 205 | + document.removeEventListener('mouseup', handleMouseUp); |
| 206 | + document.removeEventListener('touchmove', handleTouchMove); |
| 207 | + document.removeEventListener('touchend', handleTouchEnd); |
| 208 | + document.removeEventListener('touchcancel', handleTouchEnd); |
| 209 | + }; |
| 210 | + }, [canvasRef, handleMouseDown, handleMouseMove, handleMouseUp, handleTouchStart, handleTouchMove, handleTouchEnd]); |
| 211 | + |
| 212 | + useEffect(() => { |
| 213 | + AudioManager.equalizer.setFrequencies(equalizerBands.map(band => band.frequency)); |
| 214 | + AudioManager.equalizer.setGains(equalizerBands.map(band => band.gain)); |
| 215 | + }, [equalizerBands]); |
| 216 | + |
| 217 | + return { |
| 218 | + equalizerBands, |
| 219 | + updateGain, |
| 220 | + }; |
| 221 | +} |
0 commit comments