Spaces:
Sleeping
Sleeping
File size: 7,659 Bytes
f0a176a b5c16f8 f0a176a 1646c97 f0a176a 1646c97 f0a176a 1646c97 f0a176a 1646c97 f0a176a 1646c97 f0a176a 1646c97 f0a176a 1646c97 f0a176a adf3d85 f0a176a adf3d85 f0a176a adf3d85 f0a176a adf3d85 f0a176a b5c16f8 f0a176a 1646c97 f0a176a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | import { useState, useEffect, useRef } from 'react';
import OctopusLogo from './OctopusLogo';
function formatTime(s) {
const m = Math.floor(s / 60);
const sec = Math.floor(s % 60);
return `${m}:${sec.toString().padStart(2, '0')}`;
}
export default function Controls({
isPlaying,
togglePlayPause,
tempo,
setTempo,
currentTimeRef,
totalDuration,
seekTo,
fileName,
onNewSong,
loopStart,
loopEnd,
isLooping,
onSetLoopA,
onSetLoopB,
onClearLoop,
originalAudioOn,
setOriginalAudioOn,
originalVolume,
setOriginalVolume,
}) {
const [displayTime, setDisplayTime] = useState(0);
const intervalRef = useRef(null);
useEffect(() => {
intervalRef.current = setInterval(() => {
setDisplayTime(currentTimeRef.current);
}, 50);
return () => clearInterval(intervalRef.current);
}, [currentTimeRef]);
const progress = totalDuration > 0 ? (displayTime / totalDuration) * 100 : 0;
// Loop region markers for the timeline
const loopStartPct = loopStart !== null && totalDuration > 0
? (loopStart / totalDuration) * 100 : null;
const loopEndPct = loopEnd !== null && totalDuration > 0
? (loopEnd / totalDuration) * 100 : null;
// Build timeline background with loop region
let timelineBg;
if (loopStartPct !== null && loopEndPct !== null) {
timelineBg = `linear-gradient(to right,
var(--border) ${loopStartPct}%,
rgba(139, 92, 246, 0.3) ${loopStartPct}%,
var(--primary) ${Math.min(progress, loopEndPct)}%,
rgba(139, 92, 246, 0.3) ${Math.min(progress, loopEndPct)}%,
rgba(139, 92, 246, 0.3) ${loopEndPct}%,
var(--border) ${loopEndPct}%)`;
} else {
timelineBg = `linear-gradient(to right, var(--primary) ${progress}%, var(--border) ${progress}%)`;
}
return (
<div className="controls">
{/* Main controls row */}
<div className="controls-main">
<div className="controls-left">
<div className="brand-mark">
<OctopusLogo size={32} />
<span className="brand-name">Mr. Octopus</span>
</div>
{fileName && (
<span className="file-name">{fileName.replace(/\.[^.]+$/, '')}</span>
)}
</div>
<div className="controls-center">
<button
className="transport-btn"
onClick={() => seekTo(Math.max(0, displayTime - 5))}
title="Back 5s"
>
<svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor">
<path d="M11 18V6l-8.5 6 8.5 6zm.5-6l8.5 6V6l-8.5 6z" />
</svg>
<span className="transport-label">5s</span>
</button>
<button className="play-btn" onClick={togglePlayPause}>
{isPlaying ? (
<svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
<rect x="6" y="4" width="4" height="16" rx="1" />
<rect x="14" y="4" width="4" height="16" rx="1" />
</svg>
) : (
<svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
<path d="M8 5v14l11-7z" />
</svg>
)}
</button>
<button
className="transport-btn"
onClick={() => seekTo(Math.min(totalDuration, displayTime + 5))}
title="Forward 5s"
>
<svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor">
<path d="M4 18l8.5-6L4 6v12zm9-12v12l8.5-6L13 6z" />
</svg>
<span className="transport-label">5s</span>
</button>
</div>
<div className="controls-right">
{/* Loop controls */}
<div className="loop-controls">
{isLooping ? (
<>
<span className="loop-badge">
Looping {formatTime(loopStart)} — {formatTime(loopEnd)}
</span>
<button className="btn btn-loop-clear" onClick={onClearLoop} title="Clear loop">
×
</button>
</>
) : loopStart !== null ? (
<>
<span className="loop-step">Start: {formatTime(loopStart)}</span>
<span className="loop-arrow">→</span>
<button className="btn btn-loop-action" onClick={onSetLoopB} title="Set loop end to current position">
Set End
</button>
</>
) : (
<button className="btn btn-loop-action" onClick={onSetLoopA} title="Set loop start to current position">
Loop from here
</button>
)}
</div>
{/* Original audio toggle + volume */}
{setOriginalAudioOn && (
<div className="original-audio-control">
<button
className={`btn btn-original ${originalAudioOn ? 'active' : ''}`}
onClick={() => setOriginalAudioOn(!originalAudioOn)}
title={originalAudioOn ? 'Mute original audio' : 'Play original audio'}
>
{originalAudioOn ? (
<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
<path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z"/>
</svg>
) : (
<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
<path d="M16.5 12c0-1.77-1.02-3.29-2.5-4.03v2.21l2.45 2.45c.03-.2.05-.41.05-.63zm2.5 0c0 .94-.2 1.82-.54 2.64l1.51 1.51C20.63 14.91 21 13.5 21 12c0-4.28-2.99-7.86-7-8.77v2.06c2.89.86 5 3.54 5 6.71zM4.27 3L3 4.27 7.73 9H3v6h4l5 5v-6.73l4.25 4.25c-.67.52-1.42.93-2.25 1.18v2.06c1.38-.31 2.63-.95 3.69-1.81L19.73 21 21 19.73l-9-9L4.27 3zM12 4L9.91 6.09 12 8.18V4z"/>
</svg>
)}
<span>Original</span>
</button>
{originalAudioOn && (
<input
type="range"
className="original-volume"
min={0}
max={100}
value={originalVolume}
onChange={(e) => setOriginalVolume(Number(e.target.value))}
title={`Original volume: ${originalVolume}%`}
/>
)}
</div>
)}
<div className="tempo-control">
<span className="tempo-label">Speed</span>
<input
type="range"
min={50}
max={200}
value={tempo}
onChange={(e) => setTempo(Number(e.target.value))}
/>
<span className="tempo-value">{tempo}%</span>
</div>
{onNewSong && (
<button className="btn btn-new" onClick={onNewSong}>
+ New Song
</button>
)}
</div>
</div>
{/* Timeline row */}
<div className="timeline">
<span className="timeline-time">{formatTime(displayTime)}</span>
<div className="timeline-track">
<input
type="range"
min={0}
max={totalDuration || 1}
step={0.1}
value={displayTime}
onChange={(e) => seekTo(Number(e.target.value))}
style={{ background: timelineBg }}
/>
</div>
<span className="timeline-time">{formatTime(totalDuration)}</span>
</div>
</div>
);
}
|