|
|
|
|
@@ -16,6 +16,9 @@ export default function WaveformEditor({ audioUrl, startTime, duration, unlockSt
|
|
|
|
|
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
|
|
|
|
|
@@ -133,6 +136,24 @@ export default function WaveformEditor({ audioUrl, startTime, duration, unlockSt
|
|
|
|
|
|
|
|
|
|
cumulativeTime = step;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Draw end marker for the last segment (at startTime + duration)
|
|
|
|
|
const endTime = startTime + duration;
|
|
|
|
|
const endPx = ((endTime - visibleStart) / visibleDuration) * width;
|
|
|
|
|
if (endPx >= 0 && endPx <= width) {
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.moveTo(endPx, 0);
|
|
|
|
|
ctx.lineTo(endPx, height);
|
|
|
|
|
ctx.stroke();
|
|
|
|
|
|
|
|
|
|
// Draw "End" label
|
|
|
|
|
ctx.setLineDash([]);
|
|
|
|
|
ctx.fillStyle = '#ef4444';
|
|
|
|
|
ctx.font = 'bold 12px sans-serif';
|
|
|
|
|
ctx.fillText('End', endPx + 3, 15);
|
|
|
|
|
ctx.setLineDash([5, 5]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.setLineDash([]);
|
|
|
|
|
|
|
|
|
|
// Draw hover preview (semi-transparent)
|
|
|
|
|
@@ -215,11 +236,22 @@ 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);
|
|
|
|
|
} else {
|
|
|
|
|
// Clear paused position if stopping completely
|
|
|
|
|
setPausedPosition(null);
|
|
|
|
|
setPausedType(null);
|
|
|
|
|
}
|
|
|
|
|
sourceRef.current?.stop();
|
|
|
|
|
setIsPlaying(false);
|
|
|
|
|
setPlayingSegment(null);
|
|
|
|
|
setPlaybackPosition(null);
|
|
|
|
|
setIsPlayingFullTitle(false);
|
|
|
|
|
if (!savePosition) {
|
|
|
|
|
setPlaybackPosition(null);
|
|
|
|
|
}
|
|
|
|
|
if (animationFrameRef.current) {
|
|
|
|
|
cancelAnimationFrame(animationFrameRef.current);
|
|
|
|
|
animationFrameRef.current = null;
|
|
|
|
|
@@ -287,30 +319,119 @@ export default function WaveformEditor({ audioUrl, startTime, duration, unlockSt
|
|
|
|
|
const handlePlayFull = () => {
|
|
|
|
|
if (!audioBuffer || !audioContextRef.current) return;
|
|
|
|
|
|
|
|
|
|
if (isPlaying) {
|
|
|
|
|
stopPlayback();
|
|
|
|
|
} else {
|
|
|
|
|
const source = audioContextRef.current.createBufferSource();
|
|
|
|
|
source.buffer = audioBuffer;
|
|
|
|
|
source.connect(audioContextRef.current.destination);
|
|
|
|
|
|
|
|
|
|
playbackStartTimeRef.current = audioContextRef.current.currentTime;
|
|
|
|
|
playbackOffsetRef.current = startTime;
|
|
|
|
|
|
|
|
|
|
source.start(0, startTime, duration);
|
|
|
|
|
sourceRef.current = source;
|
|
|
|
|
setIsPlaying(true);
|
|
|
|
|
setPlaybackPosition(startTime);
|
|
|
|
|
|
|
|
|
|
source.onended = () => {
|
|
|
|
|
setIsPlaying(false);
|
|
|
|
|
setPlaybackPosition(null);
|
|
|
|
|
if (animationFrameRef.current) {
|
|
|
|
|
cancelAnimationFrame(animationFrameRef.current);
|
|
|
|
|
animationFrameRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// 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, full selection, or full title)
|
|
|
|
|
stopPlayback();
|
|
|
|
|
|
|
|
|
|
// 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 = resumePosition;
|
|
|
|
|
|
|
|
|
|
source.start(0, resumePosition, remainingDuration);
|
|
|
|
|
sourceRef.current = source;
|
|
|
|
|
setIsPlaying(true);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleZoomIn = () => setZoom(prev => Math.min(prev * 1.5, 10));
|
|
|
|
|
@@ -401,7 +522,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' }}>
|
|
|
|
|
|