Spaces:
Sleeping
Sleeping
File size: 11,093 Bytes
6678fa1 |
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
import { useRef, useState, useEffect, useMemo } from "react";
import { Play, Pause, Volume2, VolumeX } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Slider } from "@/components/ui/slider";
import { Card, CardContent } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
interface ChunkMatch {
score: number;
title: string;
artist: string;
stemType?: string;
matchedStartTime?: number;
matchedEndTime?: number;
matchType?: "exact" | "similar";
}
interface ChunkAttribution {
chunkIndex: number;
startTime: number;
endTime: number;
matches: ChunkMatch[];
}
interface AudioPlayerWithMatchesProps {
src: string;
title: string;
stemType?: string;
attributions: Array<{
id: number;
score: number;
metadata: {
chunkIndex?: number;
startTime?: number;
endTime?: number;
matchedTitle?: string;
matchedArtist?: string;
matchedStemType?: string;
matchedStartTime?: number;
matchedEndTime?: number;
matchType?: "exact" | "similar";
} | null;
}>;
}
export function AudioPlayerWithMatches({
src,
title,
stemType,
attributions
}: AudioPlayerWithMatchesProps) {
const audioRef = useRef<HTMLAudioElement>(null);
const waveformRef = useRef<HTMLDivElement>(null);
const [isPlaying, setIsPlaying] = useState(false);
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0);
const [isMuted, setIsMuted] = useState(false);
const [hoverTime, setHoverTime] = useState<number | null>(null);
const [hoverX, setHoverX] = useState<number>(0);
// Group attributions by chunk
const chunkAttributions = useMemo(() => {
const chunks: Map<number, ChunkAttribution> = new Map();
for (const attr of attributions) {
const meta = attr.metadata;
if (!meta || meta.chunkIndex === undefined) continue;
const chunkIndex = meta.chunkIndex;
const startTime = meta.startTime || 0;
// For first 10 seconds, only show exact matches (skip style matches)
const isFirstChunk = startTime < 1;
if (isFirstChunk && meta.matchType !== "exact") {
continue; // Skip style matches in first chunk
}
if (!chunks.has(chunkIndex)) {
chunks.set(chunkIndex, {
chunkIndex,
startTime,
endTime: meta.endTime || 0,
matches: [],
});
}
const chunk = chunks.get(chunkIndex)!;
chunk.matches.push({
score: attr.score,
title: meta.matchedTitle || "Unknown",
artist: meta.matchedArtist || "Unknown",
stemType: meta.matchedStemType,
matchedStartTime: meta.matchedStartTime,
matchedEndTime: meta.matchedEndTime,
matchType: meta.matchType,
});
}
// Sort matches by score within each chunk
for (const chunk of chunks.values()) {
chunk.matches.sort((a, b) => b.score - a.score);
}
return Array.from(chunks.values()).sort((a, b) => a.startTime - b.startTime);
}, [attributions]);
// Get matches for current hover position
const hoveredChunk = useMemo(() => {
if (hoverTime === null) return null;
return chunkAttributions.find(
c => hoverTime >= c.startTime && hoverTime < c.endTime
);
}, [hoverTime, chunkAttributions]);
useEffect(() => {
const audio = audioRef.current;
if (!audio) return;
const handleTimeUpdate = () => setCurrentTime(audio.currentTime);
const handleDurationChange = () => setDuration(audio.duration);
const handleEnded = () => setIsPlaying(false);
audio.addEventListener("timeupdate", handleTimeUpdate);
audio.addEventListener("durationchange", handleDurationChange);
audio.addEventListener("ended", handleEnded);
return () => {
audio.removeEventListener("timeupdate", handleTimeUpdate);
audio.removeEventListener("durationchange", handleDurationChange);
audio.removeEventListener("ended", handleEnded);
};
}, []);
const togglePlay = () => {
const audio = audioRef.current;
if (!audio) return;
if (isPlaying) {
audio.pause();
} else {
audio.play();
}
setIsPlaying(!isPlaying);
};
const toggleMute = () => {
const audio = audioRef.current;
if (!audio) return;
audio.muted = !isMuted;
setIsMuted(!isMuted);
};
const handleSeek = (value: number[]) => {
const audio = audioRef.current;
if (!audio || !value[0]) return;
audio.currentTime = value[0];
setCurrentTime(value[0]);
};
const handleWaveformHover = (e: React.MouseEvent<HTMLDivElement>) => {
const rect = e.currentTarget.getBoundingClientRect();
const x = e.clientX - rect.left;
const percentage = x / rect.width;
const time = percentage * duration;
setHoverTime(time);
setHoverX(x);
};
const handleWaveformLeave = () => {
setHoverTime(null);
};
const handleWaveformClick = (e: React.MouseEvent<HTMLDivElement>) => {
const rect = e.currentTarget.getBoundingClientRect();
const x = e.clientX - rect.left;
const percentage = x / rect.width;
const time = percentage * duration;
handleSeek([time]);
};
const formatTime = (seconds: number) => {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins}:${secs.toString().padStart(2, "0")}`;
};
// Get color based on match type: green for exact, orange for style
const getChunkColor = (chunk: ChunkAttribution) => {
const topMatch = chunk.matches[0];
if (!topMatch) return "bg-gray-500/20";
// Check if any match is exact
const hasExact = chunk.matches.some(m => m.matchType === "exact");
if (hasExact) {
// Green shades for exact matches
if (topMatch.score > 0.9) return "bg-green-500/70";
if (topMatch.score > 0.8) return "bg-green-500/50";
return "bg-green-500/40";
} else {
// Orange shades for style matches
if (topMatch.score > 0.9) return "bg-orange-500/60";
if (topMatch.score > 0.8) return "bg-orange-500/50";
return "bg-orange-500/40";
}
};
return (
<Card className="relative">
<CardContent className="p-4 overflow-visible">
<div className="flex items-center gap-4 mb-3">
<Button
variant="outline"
size="icon"
onClick={togglePlay}
className="shrink-0"
>
{isPlaying ? <Pause className="h-4 w-4" /> : <Play className="h-4 w-4" />}
</Button>
<div className="flex-1 min-w-0">
<h4 className="font-medium truncate">{title}</h4>
{stemType && (
<Badge variant="secondary" className="text-xs">
{stemType}
</Badge>
)}
</div>
<Button
variant="ghost"
size="icon"
onClick={toggleMute}
className="shrink-0"
>
{isMuted ? <VolumeX className="h-4 w-4" /> : <Volume2 className="h-4 w-4" />}
</Button>
</div>
{/* Waveform */}
<div className="relative">
<div
ref={waveformRef}
className="relative h-16 bg-secondary/30 rounded-lg cursor-pointer overflow-hidden"
onMouseMove={handleWaveformHover}
onMouseLeave={handleWaveformLeave}
onClick={handleWaveformClick}
>
{/* Chunk regions with match intensity coloring */}
{duration > 0 && chunkAttributions.map((chunk) => {
const left = (chunk.startTime / duration) * 100;
const width = ((chunk.endTime - chunk.startTime) / duration) * 100;
return (
<div
key={chunk.chunkIndex}
className={`absolute top-0 h-full ${getChunkColor(chunk)} border-r border-background/20`}
style={{ left: `${left}%`, width: `${width}%` }}
/>
);
})}
{/* Playhead */}
{duration > 0 && (
<div
className="absolute top-0 h-full w-0.5 bg-primary z-10"
style={{ left: `${(currentTime / duration) * 100}%` }}
/>
)}
{/* Hover indicator */}
{hoverTime !== null && duration > 0 && (
<div
className="absolute top-0 h-full w-0.5 bg-foreground/50 z-10"
style={{ left: `${(hoverTime / duration) * 100}%` }}
/>
)}
{/* Waveform placeholder bars */}
<div className="absolute inset-0 flex items-center justify-around px-1">
{Array.from({ length: 60 }).map((_, i) => (
<div
key={i}
className="w-1 bg-foreground/20 rounded-full"
style={{
height: `${20 + Math.sin(i * 0.5) * 15 + Math.random() * 20}%`,
}}
/>
))}
</div>
</div>
</div>
{/* Time display */}
<div className="flex justify-between text-xs text-muted-foreground mt-2">
<span>{formatTime(currentTime)}</span>
<span>{formatTime(duration)}</span>
</div>
{/* Matches panel - shown on hover */}
<div className={`mt-3 rounded-lg border bg-muted/50 transition-all duration-200 overflow-hidden ${hoveredChunk ? 'max-h-40 p-3' : 'max-h-0 p-0 border-transparent'}`}>
{hoveredChunk && (
<>
<div className="flex items-center justify-between mb-2">
<span className="text-xs font-medium">
Matches for {formatTime(hoveredChunk.startTime)} - {formatTime(hoveredChunk.endTime)}
</span>
<Badge variant="outline" className="text-[10px]">
{hoveredChunk.matches.length} matches
</Badge>
</div>
<div className="flex flex-wrap gap-2">
{hoveredChunk.matches.slice(0, 5).map((match, i) => (
<div key={i} className="flex items-center gap-1.5 bg-background rounded-md px-2 py-1 border">
<Badge
variant={match.score > 0.7 ? "destructive" : "secondary"}
className="text-[10px] px-1"
>
{(match.score * 100).toFixed(0)}%
</Badge>
<span className="text-xs truncate max-w-[120px]">{match.title}</span>
</div>
))}
{hoveredChunk.matches.length === 0 && (
<span className="text-xs text-muted-foreground">No matching tracks found</span>
)}
</div>
</>
)}
</div>
{/* Hidden audio element */}
<audio ref={audioRef} src={src} preload="metadata" />
</CardContent>
</Card>
);
}
|