|
|
|
|
@@ -12,10 +12,14 @@ interface WaveformEditorProps {
|
|
|
|
|
|
|
|
|
|
export default function WaveformEditor({ audioUrl, startTime, duration, unlockSteps, onStartTimeChange }: WaveformEditorProps) {
|
|
|
|
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
|
|
|
const timelineRef = useRef<HTMLCanvasElement>(null);
|
|
|
|
|
const [audioBuffer, setAudioBuffer] = useState<AudioBuffer | null>(null);
|
|
|
|
|
const [audioDuration, setAudioDuration] = useState(0);
|
|
|
|
|
const [isPlaying, setIsPlaying] = useState(false);
|
|
|
|
|
const [playingSegment, setPlayingSegment] = useState<number | null>(null);
|
|
|
|
|
const [isPlayingFullTitle, setIsPlayingFullTitle] = useState(false);
|
|
|
|
|
const [pausedPosition, setPausedPosition] = useState<number | null>(null); // Position when paused
|
|
|
|
|
const [pausedType, setPausedType] = useState<'selection' | 'title' | null>(null); // Type of playback that was paused
|
|
|
|
|
const [zoom, setZoom] = useState(1); // 1 = full view, higher = zoomed in
|
|
|
|
|
const [viewOffset, setViewOffset] = useState(0); // Offset in seconds for panning
|
|
|
|
|
const [playbackPosition, setPlaybackPosition] = useState<number | null>(null); // Current playback position in seconds
|
|
|
|
|
@@ -55,6 +59,80 @@ export default function WaveformEditor({ audioUrl, startTime, duration, unlockSt
|
|
|
|
|
};
|
|
|
|
|
}, [audioUrl]);
|
|
|
|
|
|
|
|
|
|
// Draw timeline
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!audioDuration || !timelineRef.current) return;
|
|
|
|
|
|
|
|
|
|
const timeline = timelineRef.current;
|
|
|
|
|
const ctx = timeline.getContext('2d');
|
|
|
|
|
if (!ctx) return;
|
|
|
|
|
|
|
|
|
|
const width = timeline.width;
|
|
|
|
|
const height = timeline.height;
|
|
|
|
|
|
|
|
|
|
// Calculate visible range based on zoom and offset (same as waveform)
|
|
|
|
|
const visibleDuration = audioDuration / zoom;
|
|
|
|
|
const visibleStart = Math.max(0, Math.min(viewOffset, audioDuration - visibleDuration));
|
|
|
|
|
const visibleEnd = Math.min(audioDuration, visibleStart + visibleDuration);
|
|
|
|
|
|
|
|
|
|
// Clear timeline
|
|
|
|
|
ctx.fillStyle = '#ffffff';
|
|
|
|
|
ctx.fillRect(0, 0, width, height);
|
|
|
|
|
|
|
|
|
|
// Draw border
|
|
|
|
|
ctx.strokeStyle = '#e5e7eb';
|
|
|
|
|
ctx.lineWidth = 1;
|
|
|
|
|
ctx.strokeRect(0, 0, width, height);
|
|
|
|
|
|
|
|
|
|
// Calculate appropriate time interval based on visible duration
|
|
|
|
|
let timeInterval = 1; // Start with 1 second
|
|
|
|
|
if (visibleDuration > 60) timeInterval = 10;
|
|
|
|
|
else if (visibleDuration > 30) timeInterval = 5;
|
|
|
|
|
else if (visibleDuration > 10) timeInterval = 2;
|
|
|
|
|
else if (visibleDuration > 5) timeInterval = 1;
|
|
|
|
|
else if (visibleDuration > 1) timeInterval = 0.5;
|
|
|
|
|
else timeInterval = 0.1;
|
|
|
|
|
|
|
|
|
|
// Draw time markers
|
|
|
|
|
ctx.strokeStyle = '#9ca3af';
|
|
|
|
|
ctx.lineWidth = 1;
|
|
|
|
|
ctx.fillStyle = '#374151';
|
|
|
|
|
ctx.font = '10px sans-serif';
|
|
|
|
|
ctx.textAlign = 'center';
|
|
|
|
|
ctx.textBaseline = 'top';
|
|
|
|
|
|
|
|
|
|
const startTimeMarker = Math.floor(visibleStart / timeInterval) * timeInterval;
|
|
|
|
|
for (let time = startTimeMarker; time <= visibleEnd; time += timeInterval) {
|
|
|
|
|
const timePx = ((time - visibleStart) / visibleDuration) * width;
|
|
|
|
|
|
|
|
|
|
if (timePx >= 0 && timePx <= width) {
|
|
|
|
|
// Draw tick mark
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.moveTo(timePx, 0);
|
|
|
|
|
ctx.lineTo(timePx, height);
|
|
|
|
|
ctx.stroke();
|
|
|
|
|
|
|
|
|
|
// Draw time label
|
|
|
|
|
const timeLabel = time.toFixed(timeInterval < 1 ? 1 : 0);
|
|
|
|
|
ctx.fillText(`${timeLabel}s`, timePx, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Draw current playback position if playing
|
|
|
|
|
if (playbackPosition !== null) {
|
|
|
|
|
const playbackPx = ((playbackPosition - visibleStart) / visibleDuration) * width;
|
|
|
|
|
if (playbackPx >= 0 && playbackPx <= width) {
|
|
|
|
|
ctx.strokeStyle = '#10b981';
|
|
|
|
|
ctx.lineWidth = 2;
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.moveTo(playbackPx, 0);
|
|
|
|
|
ctx.lineTo(playbackPx, height);
|
|
|
|
|
ctx.stroke();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, [audioDuration, zoom, viewOffset, playbackPosition]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!audioBuffer || !canvasRef.current) return;
|
|
|
|
|
|
|
|
|
|
@@ -233,11 +311,21 @@ export default function WaveformEditor({ audioUrl, startTime, duration, unlockSt
|
|
|
|
|
setHoverPreviewTime(null);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const stopPlayback = () => {
|
|
|
|
|
const stopPlayback = (savePosition = false) => {
|
|
|
|
|
if (savePosition && playbackPosition !== null) {
|
|
|
|
|
// Save current position for resume
|
|
|
|
|
setPausedPosition(playbackPosition);
|
|
|
|
|
// Keep playbackPosition visible (don't set to null) so cursor stays visible
|
|
|
|
|
} else {
|
|
|
|
|
// Clear paused position if stopping completely
|
|
|
|
|
setPausedPosition(null);
|
|
|
|
|
setPausedType(null);
|
|
|
|
|
setPlaybackPosition(null);
|
|
|
|
|
}
|
|
|
|
|
sourceRef.current?.stop();
|
|
|
|
|
setIsPlaying(false);
|
|
|
|
|
setPlayingSegment(null);
|
|
|
|
|
setPlaybackPosition(null);
|
|
|
|
|
setIsPlayingFullTitle(false);
|
|
|
|
|
if (animationFrameRef.current) {
|
|
|
|
|
cancelAnimationFrame(animationFrameRef.current);
|
|
|
|
|
animationFrameRef.current = null;
|
|
|
|
|
@@ -305,33 +393,114 @@ export default function WaveformEditor({ audioUrl, startTime, duration, unlockSt
|
|
|
|
|
const handlePlayFull = () => {
|
|
|
|
|
if (!audioBuffer || !audioContextRef.current) return;
|
|
|
|
|
|
|
|
|
|
// If full playback is already playing, stop it
|
|
|
|
|
if (isPlaying && playingSegment === null) {
|
|
|
|
|
stopPlayback();
|
|
|
|
|
// If full selection playback is already playing, pause it
|
|
|
|
|
if (isPlaying && playingSegment === null && !isPlayingFullTitle) {
|
|
|
|
|
stopPlayback(true); // Save position
|
|
|
|
|
setPausedType('selection');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop any current playback (segment or full)
|
|
|
|
|
// Stop any current playback (segment, full selection, or full title)
|
|
|
|
|
stopPlayback();
|
|
|
|
|
|
|
|
|
|
// Start full playback
|
|
|
|
|
// Determine start position (resume from pause or start from beginning)
|
|
|
|
|
const resumePosition = pausedType === 'selection' && pausedPosition !== null
|
|
|
|
|
? pausedPosition
|
|
|
|
|
: startTime;
|
|
|
|
|
const remainingDuration = resumePosition >= startTime + duration
|
|
|
|
|
? 0
|
|
|
|
|
: (startTime + duration) - resumePosition;
|
|
|
|
|
|
|
|
|
|
if (remainingDuration <= 0) {
|
|
|
|
|
// Already finished, reset
|
|
|
|
|
setPausedPosition(null);
|
|
|
|
|
setPausedType(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start full selection playback
|
|
|
|
|
const source = audioContextRef.current.createBufferSource();
|
|
|
|
|
source.buffer = audioBuffer;
|
|
|
|
|
source.connect(audioContextRef.current.destination);
|
|
|
|
|
|
|
|
|
|
playbackStartTimeRef.current = audioContextRef.current.currentTime;
|
|
|
|
|
playbackOffsetRef.current = startTime;
|
|
|
|
|
playbackOffsetRef.current = resumePosition;
|
|
|
|
|
|
|
|
|
|
source.start(0, startTime, duration);
|
|
|
|
|
source.start(0, resumePosition, remainingDuration);
|
|
|
|
|
sourceRef.current = source;
|
|
|
|
|
setIsPlaying(true);
|
|
|
|
|
setPlayingSegment(null); // Ensure playingSegment is null for full playback
|
|
|
|
|
setPlaybackPosition(startTime);
|
|
|
|
|
setPlayingSegment(null);
|
|
|
|
|
setIsPlayingFullTitle(false);
|
|
|
|
|
setPausedPosition(null);
|
|
|
|
|
setPausedType(null);
|
|
|
|
|
setPlaybackPosition(resumePosition);
|
|
|
|
|
|
|
|
|
|
source.onended = () => {
|
|
|
|
|
setIsPlaying(false);
|
|
|
|
|
setPlayingSegment(null);
|
|
|
|
|
setIsPlayingFullTitle(false);
|
|
|
|
|
setPlaybackPosition(null);
|
|
|
|
|
setPausedPosition(null);
|
|
|
|
|
setPausedType(null);
|
|
|
|
|
if (animationFrameRef.current) {
|
|
|
|
|
cancelAnimationFrame(animationFrameRef.current);
|
|
|
|
|
animationFrameRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handlePlayFullTitle = () => {
|
|
|
|
|
if (!audioBuffer || !audioContextRef.current) return;
|
|
|
|
|
|
|
|
|
|
// If full title playback is already playing, pause it
|
|
|
|
|
if (isPlaying && isPlayingFullTitle) {
|
|
|
|
|
stopPlayback(true); // Save position
|
|
|
|
|
setPausedType('title');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop any current playback (segment, full selection, or full title)
|
|
|
|
|
stopPlayback();
|
|
|
|
|
|
|
|
|
|
// Determine start position (resume from pause or start from beginning)
|
|
|
|
|
const resumePosition = pausedType === 'title' && pausedPosition !== null
|
|
|
|
|
? pausedPosition
|
|
|
|
|
: 0;
|
|
|
|
|
const remainingDuration = resumePosition >= audioDuration
|
|
|
|
|
? 0
|
|
|
|
|
: audioDuration - resumePosition;
|
|
|
|
|
|
|
|
|
|
if (remainingDuration <= 0) {
|
|
|
|
|
// Already finished, reset
|
|
|
|
|
setPausedPosition(null);
|
|
|
|
|
setPausedType(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start full title playback (from resumePosition to audioDuration)
|
|
|
|
|
const source = audioContextRef.current.createBufferSource();
|
|
|
|
|
source.buffer = audioBuffer;
|
|
|
|
|
source.connect(audioContextRef.current.destination);
|
|
|
|
|
|
|
|
|
|
playbackStartTimeRef.current = audioContextRef.current.currentTime;
|
|
|
|
|
playbackOffsetRef.current = resumePosition;
|
|
|
|
|
|
|
|
|
|
source.start(0, resumePosition, remainingDuration);
|
|
|
|
|
sourceRef.current = source;
|
|
|
|
|
setIsPlaying(true);
|
|
|
|
|
setPlayingSegment(null);
|
|
|
|
|
setIsPlayingFullTitle(true);
|
|
|
|
|
setPausedPosition(null);
|
|
|
|
|
setPausedType(null);
|
|
|
|
|
setPlaybackPosition(resumePosition);
|
|
|
|
|
|
|
|
|
|
source.onended = () => {
|
|
|
|
|
setIsPlaying(false);
|
|
|
|
|
setPlayingSegment(null);
|
|
|
|
|
setIsPlayingFullTitle(false);
|
|
|
|
|
setPlaybackPosition(null);
|
|
|
|
|
setPausedPosition(null);
|
|
|
|
|
setPausedType(null);
|
|
|
|
|
if (animationFrameRef.current) {
|
|
|
|
|
cancelAnimationFrame(animationFrameRef.current);
|
|
|
|
|
animationFrameRef.current = null;
|
|
|
|
|
@@ -397,21 +566,38 @@ export default function WaveformEditor({ audioUrl, startTime, duration, unlockSt
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<canvas
|
|
|
|
|
ref={canvasRef}
|
|
|
|
|
width={800}
|
|
|
|
|
height={150}
|
|
|
|
|
onClick={handleCanvasClick}
|
|
|
|
|
onMouseMove={handleCanvasMouseMove}
|
|
|
|
|
onMouseLeave={handleCanvasMouseLeave}
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
height: 'auto',
|
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
border: '1px solid #e5e7eb',
|
|
|
|
|
borderRadius: '0.5rem'
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<div style={{ position: 'relative' }}>
|
|
|
|
|
<canvas
|
|
|
|
|
ref={canvasRef}
|
|
|
|
|
width={800}
|
|
|
|
|
height={150}
|
|
|
|
|
onClick={handleCanvasClick}
|
|
|
|
|
onMouseMove={handleCanvasMouseMove}
|
|
|
|
|
onMouseLeave={handleCanvasMouseLeave}
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
height: 'auto',
|
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
border: '1px solid #e5e7eb',
|
|
|
|
|
borderRadius: '0.5rem 0.5rem 0 0',
|
|
|
|
|
display: 'block'
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<canvas
|
|
|
|
|
ref={timelineRef}
|
|
|
|
|
width={800}
|
|
|
|
|
height={30}
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
height: '30px',
|
|
|
|
|
border: '1px solid #e5e7eb',
|
|
|
|
|
borderTop: 'none',
|
|
|
|
|
borderRadius: '0 0 0.5rem 0.5rem',
|
|
|
|
|
display: 'block',
|
|
|
|
|
background: '#ffffff'
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Playback Controls */}
|
|
|
|
|
<div style={{ marginTop: '1rem', display: 'flex', gap: '1rem', alignItems: 'center', flexWrap: 'wrap' }}>
|
|
|
|
|
@@ -427,7 +613,29 @@ export default function WaveformEditor({ audioUrl, startTime, duration, unlockSt
|
|
|
|
|
fontWeight: 'bold'
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{isPlaying && playingSegment === null ? '⏸ Pause' : '▶ Play Full Selection'}
|
|
|
|
|
{isPlaying && playingSegment === null && !isPlayingFullTitle
|
|
|
|
|
? '⏸ Pause'
|
|
|
|
|
: (pausedType === 'selection' && pausedPosition !== null
|
|
|
|
|
? '▶ Resume'
|
|
|
|
|
: '▶ Play Full Selection')}
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
onClick={handlePlayFullTitle}
|
|
|
|
|
style={{
|
|
|
|
|
padding: '0.5rem 1rem',
|
|
|
|
|
background: '#10b981',
|
|
|
|
|
color: 'white',
|
|
|
|
|
border: 'none',
|
|
|
|
|
borderRadius: '0.5rem',
|
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
fontWeight: 'bold'
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{isPlaying && isPlayingFullTitle
|
|
|
|
|
? '⏸ Pause'
|
|
|
|
|
: (pausedType === 'title' && pausedPosition !== null
|
|
|
|
|
? '▶ Resume'
|
|
|
|
|
: '▶ Play Full Title')}
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
<div style={{ fontSize: '0.875rem', color: '#666' }}>
|
|
|
|
|
|