refactor: removed redundant audioplayer and improved component coherence
This commit is contained in:
@@ -2,7 +2,7 @@ import Link from 'next/link';
|
|||||||
import db from '@/lib/db';
|
import db from '@/lib/db';
|
||||||
import { dataset_entry } from '@/lib/model/dataset_entry';
|
import { dataset_entry } from '@/lib/model/dataset_entry';
|
||||||
import { eq } from 'drizzle-orm';
|
import { eq } from 'drizzle-orm';
|
||||||
import AudioPlayer from '@/components/AudioPlayer';
|
import WaveformPlayer from '@/components/WaveformPlayer';
|
||||||
import { requireAdmin } from '@/lib/auth';
|
import { requireAdmin } from '@/lib/auth';
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
@@ -114,7 +114,11 @@ export default async function DatasetEntryPage({ params }: DatasetEntryPageProps
|
|||||||
|
|
||||||
<div className="col-span-2 bg-white border border-gray-200 p-4 rounded-lg">
|
<div className="col-span-2 bg-white border border-gray-200 p-4 rounded-lg">
|
||||||
<p className="text-sm text-gray-600 mb-3">Audio Player</p>
|
<p className="text-sm text-gray-600 mb-3">Audio Player</p>
|
||||||
<AudioPlayer datasetId={datasetIdNum} fileName={entry.fileName} externalId={entry.externalId} />
|
{(() => {
|
||||||
|
const fileExtension = entry.fileName.substring(entry.fileName.lastIndexOf('.'));
|
||||||
|
const src = `/public/datasets/${datasetIdNum}/${entry.externalId}${fileExtension}`;
|
||||||
|
return <WaveformPlayer src={src} showWaveform={false} />;
|
||||||
|
})()}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="bg-white border border-gray-200 p-4 rounded-lg">
|
<div className="bg-white border border-gray-200 p-4 rounded-lg">
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ export default function SingleChoiceEntryView({
|
|||||||
{/* Waveform player */}
|
{/* Waveform player */}
|
||||||
<WaveformPlayer
|
<WaveformPlayer
|
||||||
src={audioSrc}
|
src={audioSrc}
|
||||||
durationMs={entry.durationMs}
|
|
||||||
onFullyPlayed={() => setFullyPlayed(true)}
|
onFullyPlayed={() => setFullyPlayed(true)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|||||||
@@ -1,83 +0,0 @@
|
|||||||
'use client';
|
|
||||||
|
|
||||||
import { useState, useRef, useEffect } from 'react';
|
|
||||||
import { useAudio } from './AudioProvider';
|
|
||||||
|
|
||||||
interface AudioPlayerProps {
|
|
||||||
fileName: string;
|
|
||||||
datasetId: number;
|
|
||||||
externalId?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function AudioPlayer({ fileName, datasetId, externalId }: AudioPlayerProps) {
|
|
||||||
const [isPlaying, setIsPlaying] = useState(false);
|
|
||||||
const audioRef = useRef<HTMLAudioElement>(null);
|
|
||||||
const { currentAudioId, setCurrentAudio } = useAudio();
|
|
||||||
const audioId = `${datasetId}-${externalId}`;
|
|
||||||
|
|
||||||
// Extract file extension from original fileName and construct URL using externalId
|
|
||||||
const fileExtension = fileName.substring(fileName.lastIndexOf('.'));
|
|
||||||
const audioPath = `/public/datasets/${datasetId}/${externalId}${fileExtension}`;
|
|
||||||
|
|
||||||
// Stop playing if another audio started
|
|
||||||
useEffect(() => {
|
|
||||||
if (currentAudioId !== audioId && audioRef.current && isPlaying) {
|
|
||||||
audioRef.current.pause();
|
|
||||||
audioRef.current.currentTime = 0;
|
|
||||||
setIsPlaying(false);
|
|
||||||
}
|
|
||||||
}, [currentAudioId, audioId, isPlaying]);
|
|
||||||
|
|
||||||
const handlePlay = () => {
|
|
||||||
if (audioRef.current) {
|
|
||||||
setCurrentAudio(audioId);
|
|
||||||
audioRef.current.currentTime = 0;
|
|
||||||
audioRef.current.play();
|
|
||||||
setIsPlaying(true);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleStop = () => {
|
|
||||||
if (audioRef.current) {
|
|
||||||
audioRef.current.pause();
|
|
||||||
audioRef.current.currentTime = 0;
|
|
||||||
setCurrentAudio(null);
|
|
||||||
setIsPlaying(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handlePlaybackEnd = () => {
|
|
||||||
setIsPlaying(false);
|
|
||||||
setCurrentAudio(null);
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<audio
|
|
||||||
ref={audioRef}
|
|
||||||
src={audioPath}
|
|
||||||
onEnded={handlePlaybackEnd}
|
|
||||||
/>
|
|
||||||
<button
|
|
||||||
onClick={isPlaying ? handleStop : handlePlay}
|
|
||||||
className={`inline-flex items-center justify-center w-8 h-8 rounded-full transition-colors ${
|
|
||||||
isPlaying
|
|
||||||
? 'bg-red-500 hover:bg-red-600 text-white'
|
|
||||||
: 'bg-blue-500 hover:bg-blue-600 text-white'
|
|
||||||
}`}
|
|
||||||
title={isPlaying ? 'Stop' : 'Play'}
|
|
||||||
>
|
|
||||||
{isPlaying ? (
|
|
||||||
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
|
|
||||||
<rect x="6" y="4" width="2" height="12" />
|
|
||||||
<rect x="12" y="4" width="2" height="12" />
|
|
||||||
</svg>
|
|
||||||
) : (
|
|
||||||
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
|
|
||||||
<path d="M6.3 2.841A1.5 1.5 0 004 4.11v11.78a1.5 1.5 0 002.3 1.269l9.344-5.89a1.5 1.5 0 000-2.538L6.3 2.84z" />
|
|
||||||
</svg>
|
|
||||||
)}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -82,7 +82,6 @@ export default function CalibrationEntryView({
|
|||||||
{/* Waveform player */}
|
{/* Waveform player */}
|
||||||
<WaveformPlayer
|
<WaveformPlayer
|
||||||
src={audioSrc}
|
src={audioSrc}
|
||||||
durationMs={null}
|
|
||||||
onFullyPlayed={() => setFullyPlayed(true)}
|
onFullyPlayed={() => setFullyPlayed(true)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import AudioPlayer from './AudioPlayer';
|
import WaveformPlayer from './WaveformPlayer';
|
||||||
import { getDatasetEntries } from '@/app/actions/get-dataset-entries';
|
import { getDatasetEntries } from '@/app/actions/get-dataset-entries';
|
||||||
import { getFilterOptions } from '@/app/actions/get-filter-options';
|
import { getFilterOptions } from '@/app/actions/get-filter-options';
|
||||||
import { downloadFilteredEntriesAsZip, downloadFilteredEntriesAsCsv } from '@/lib/download-utils';
|
import { downloadFilteredEntriesAsZip, downloadFilteredEntriesAsCsv } from '@/lib/download-utils';
|
||||||
@@ -514,7 +514,11 @@ export default function DatasetEntriesList({
|
|||||||
{entries.map((entry) => (
|
{entries.map((entry) => (
|
||||||
<tr key={entry.id} className="hover:bg-blue-50 transition-colors border-b border-gray-200">
|
<tr key={entry.id} className="hover:bg-blue-50 transition-colors border-b border-gray-200">
|
||||||
<td className="py-3 px-4">
|
<td className="py-3 px-4">
|
||||||
<AudioPlayer datasetId={datasetId} fileName={entry.fileName} externalId={entry.externalId} />
|
{(() => {
|
||||||
|
const fileExtension = entry.fileName.substring(entry.fileName.lastIndexOf('.'));
|
||||||
|
const src = `/public/datasets/${datasetId}/${entry.externalId}${fileExtension}`;
|
||||||
|
return <WaveformPlayer src={src} showWaveform={false} />;
|
||||||
|
})()}
|
||||||
</td>
|
</td>
|
||||||
<td className="py-3 px-4">
|
<td className="py-3 px-4">
|
||||||
<Link
|
<Link
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
import { ParticipantDetail, getParticipantCalibrationScores } from '@/app/actions/participants';
|
import { ParticipantDetail, getParticipantCalibrationScores } from '@/app/actions/participants';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import CalibrationScoresDisplay from './CalibrationScoresDisplay';
|
import CalibrationScoresDisplay from './CalibrationScoresDisplay';
|
||||||
import AudioPlayer from './AudioPlayer';
|
import WaveformPlayer from './WaveformPlayer';
|
||||||
import ExportParticipantDataButton from './ExportParticipantDataButton';
|
import ExportParticipantDataButton from './ExportParticipantDataButton';
|
||||||
|
|
||||||
interface ParticipantDetailViewProps {
|
interface ParticipantDetailViewProps {
|
||||||
@@ -165,11 +165,11 @@ export default function ParticipantDetailView({ participant, experimentId }: Par
|
|||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<div className="text-sm text-gray-600 mb-1">Audio</div>
|
<div className="text-sm text-gray-600 mb-1">Audio</div>
|
||||||
<AudioPlayer
|
{(() => {
|
||||||
fileName={selectedAnnotation.fileName}
|
const fileExtension = selectedAnnotation.fileName.substring(selectedAnnotation.fileName.lastIndexOf('.'));
|
||||||
datasetId={selectedAnnotation.datasetId}
|
const src = `/public/datasets/${selectedAnnotation.datasetId}/${selectedAnnotation.externalId}${fileExtension}`;
|
||||||
externalId={selectedAnnotation.externalId}
|
return <WaveformPlayer src={src} showWaveform={false} />;
|
||||||
/>
|
})()}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,38 +1,43 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useEffect, useRef, useState, useCallback, useMemo } from 'react';
|
import { useEffect, useRef, useState, useCallback } from 'react';
|
||||||
import { useAudio } from './AudioProvider';
|
import { useAudio } from './AudioProvider';
|
||||||
|
|
||||||
interface WaveformPlayerProps {
|
interface WaveformPlayerProps {
|
||||||
|
// Audio source URL (required)
|
||||||
src: string;
|
src: string;
|
||||||
durationMs?: number | null;
|
|
||||||
|
// Audio playback options
|
||||||
onPlay?: () => void;
|
onPlay?: () => void;
|
||||||
onFullyPlayed: () => void;
|
onFullyPlayed?: () => void;
|
||||||
|
|
||||||
|
// Display options
|
||||||
|
showWaveform?: boolean; // defaults to true; if false, shows simple player button
|
||||||
}
|
}
|
||||||
|
|
||||||
const BAR_COUNT = 120;
|
const BAR_COUNT = 120;
|
||||||
|
|
||||||
export default function WaveformPlayer({
|
export default function WaveformPlayer({
|
||||||
src,
|
src,
|
||||||
durationMs,
|
|
||||||
onPlay,
|
onPlay,
|
||||||
onFullyPlayed,
|
onFullyPlayed,
|
||||||
|
showWaveform = true,
|
||||||
}: WaveformPlayerProps) {
|
}: WaveformPlayerProps) {
|
||||||
const { currentAudioId, setCurrentAudio } = useAudio();
|
const { currentAudioId, setCurrentAudio } = useAudio();
|
||||||
|
|
||||||
// Generate unique ID for this player instance
|
// Generate unique ID for this player instance
|
||||||
const playerId = useMemo(() => Math.random().toString(36).slice(2), []);
|
const playerId = useRef(Math.random().toString(36).slice(2)).current;
|
||||||
const isActive = currentAudioId === playerId;
|
const isActive = currentAudioId === playerId;
|
||||||
|
|
||||||
const audioRef = useRef<HTMLAudioElement>(null);
|
const audioRef = useRef<HTMLAudioElement>(null);
|
||||||
const canvasRef = useRef<HTMLCanvasElement>(null);
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
||||||
const rafRef = useRef<number>(0);
|
const rafRef = useRef<number>(0);
|
||||||
|
const fullyPlayedRef = useRef(false);
|
||||||
|
|
||||||
const [isPlaying, setIsPlaying] = useState(false);
|
const [isPlaying, setIsPlaying] = useState(false);
|
||||||
const [currentTime, setCurrentTime] = useState(0);
|
const [currentTime, setCurrentTime] = useState(0);
|
||||||
const [duration, setDuration] = useState<number>(durationMs ? durationMs / 1000 : 0);
|
const [duration, setDuration] = useState(0);
|
||||||
const [peaks, setPeaks] = useState<number[]>([]);
|
const [peaks, setPeaks] = useState<number[]>([]);
|
||||||
const fullyPlayedRef = useRef(false);
|
|
||||||
|
|
||||||
// Stop if another player became active
|
// Stop if another player became active
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -43,12 +48,11 @@ export default function WaveformPlayer({
|
|||||||
}
|
}
|
||||||
}, [isActive, isPlaying]);
|
}, [isActive, isPlaying]);
|
||||||
|
|
||||||
// Reset state when src changes and cancel playback
|
// Reset state when src changes
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setPeaks([]);
|
setPeaks([]);
|
||||||
setCurrentTime(0);
|
setCurrentTime(0);
|
||||||
setDuration(durationMs ? durationMs / 1000 : 0);
|
setDuration(0);
|
||||||
fullyPlayedRef.current = false;
|
fullyPlayedRef.current = false;
|
||||||
if (audioRef.current) {
|
if (audioRef.current) {
|
||||||
audioRef.current.pause();
|
audioRef.current.pause();
|
||||||
@@ -56,10 +60,61 @@ export default function WaveformPlayer({
|
|||||||
}
|
}
|
||||||
cancelAnimationFrame(rafRef.current);
|
cancelAnimationFrame(rafRef.current);
|
||||||
setIsPlaying(false);
|
setIsPlaying(false);
|
||||||
}, [src, durationMs]);
|
}, [src]);
|
||||||
|
|
||||||
// Decode audio and generate waveform peaks
|
const tick = useCallback(() => {
|
||||||
|
const audio = audioRef.current;
|
||||||
|
if (!audio) return;
|
||||||
|
setCurrentTime(audio.currentTime);
|
||||||
|
rafRef.current = requestAnimationFrame(tick);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handlePlay = useCallback(() => {
|
||||||
|
const audio = audioRef.current;
|
||||||
|
if (!audio) return;
|
||||||
|
setCurrentAudio(playerId);
|
||||||
|
onPlay?.();
|
||||||
|
audio.currentTime = 0;
|
||||||
|
audio.play();
|
||||||
|
setIsPlaying(true);
|
||||||
|
rafRef.current = requestAnimationFrame(tick);
|
||||||
|
}, [playerId, onPlay, tick]);
|
||||||
|
|
||||||
|
const handlePause = useCallback(() => {
|
||||||
|
const audio = audioRef.current;
|
||||||
|
if (!audio) return;
|
||||||
|
audio.pause();
|
||||||
|
setIsPlaying(false);
|
||||||
|
cancelAnimationFrame(rafRef.current);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleStop = useCallback(() => {
|
||||||
|
const audio = audioRef.current;
|
||||||
|
if (!audio) return;
|
||||||
|
audio.pause();
|
||||||
|
audio.currentTime = 0;
|
||||||
|
setCurrentAudio(null);
|
||||||
|
setIsPlaying(false);
|
||||||
|
cancelAnimationFrame(rafRef.current);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleEnded = useCallback(() => {
|
||||||
|
setIsPlaying(false);
|
||||||
|
cancelAnimationFrame(rafRef.current);
|
||||||
|
if (!fullyPlayedRef.current) {
|
||||||
|
fullyPlayedRef.current = true;
|
||||||
|
onFullyPlayed?.();
|
||||||
|
}
|
||||||
|
}, [onFullyPlayed]);
|
||||||
|
|
||||||
|
const handleLoadedMetadata = useCallback(() => {
|
||||||
|
if (audioRef.current) setDuration(audioRef.current.duration);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Decode audio and generate waveform peaks (only if showWaveform is true)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!showWaveform) return;
|
||||||
|
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
@@ -87,10 +142,17 @@ export default function WaveformPlayer({
|
|||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
return () => { cancelled = true; };
|
return () => { cancelled = true; };
|
||||||
|
}, [src, showWaveform]);
|
||||||
|
|
||||||
|
// Reset peaks when src changes
|
||||||
|
useEffect(() => {
|
||||||
|
setPeaks([]);
|
||||||
}, [src]);
|
}, [src]);
|
||||||
|
|
||||||
// Draw waveform on canvas whenever peaks or playback position change
|
// Draw waveform on canvas whenever peaks or playback position change
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!showWaveform) return;
|
||||||
|
|
||||||
const canvas = canvasRef.current;
|
const canvas = canvasRef.current;
|
||||||
if (!canvas || peaks.length === 0) return;
|
if (!canvas || peaks.length === 0) return;
|
||||||
const ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
@@ -110,45 +172,7 @@ export default function WaveformPlayer({
|
|||||||
ctx.roundRect(x, y, barW, barH, 2);
|
ctx.roundRect(x, y, barW, barH, 2);
|
||||||
ctx.fill();
|
ctx.fill();
|
||||||
});
|
});
|
||||||
}, [peaks, currentTime, duration]);
|
}, [peaks, currentTime, duration, showWaveform]);
|
||||||
|
|
||||||
const tick = useCallback(() => {
|
|
||||||
const audio = audioRef.current;
|
|
||||||
if (!audio) return;
|
|
||||||
setCurrentTime(audio.currentTime);
|
|
||||||
rafRef.current = requestAnimationFrame(tick);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const handlePlay = () => {
|
|
||||||
const audio = audioRef.current;
|
|
||||||
if (!audio) return;
|
|
||||||
setCurrentAudio(playerId);
|
|
||||||
onPlay?.();
|
|
||||||
audio.play();
|
|
||||||
setIsPlaying(true);
|
|
||||||
rafRef.current = requestAnimationFrame(tick);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handlePause = () => {
|
|
||||||
const audio = audioRef.current;
|
|
||||||
if (!audio) return;
|
|
||||||
audio.pause();
|
|
||||||
setIsPlaying(false);
|
|
||||||
cancelAnimationFrame(rafRef.current);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleEnded = () => {
|
|
||||||
setIsPlaying(false);
|
|
||||||
cancelAnimationFrame(rafRef.current);
|
|
||||||
if (!fullyPlayedRef.current) {
|
|
||||||
fullyPlayedRef.current = true;
|
|
||||||
onFullyPlayed?.();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleLoadedMetadata = () => {
|
|
||||||
if (audioRef.current) setDuration(audioRef.current.duration);
|
|
||||||
};
|
|
||||||
|
|
||||||
const fmt = (s: number) => {
|
const fmt = (s: number) => {
|
||||||
const m = Math.floor(s / 60);
|
const m = Math.floor(s / 60);
|
||||||
@@ -156,6 +180,40 @@ export default function WaveformPlayer({
|
|||||||
return `${m}:${sec.toString().padStart(2, '0')}`;
|
return `${m}:${sec.toString().padStart(2, '0')}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Simple player button (no waveform)
|
||||||
|
if (!showWaveform) {
|
||||||
|
return (
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<audio
|
||||||
|
ref={audioRef}
|
||||||
|
src={src}
|
||||||
|
onEnded={handleEnded}
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
onClick={isPlaying ? handleStop : handlePlay}
|
||||||
|
className={`inline-flex items-center justify-center w-8 h-8 rounded-full transition-colors ${
|
||||||
|
isPlaying
|
||||||
|
? 'bg-red-500 hover:bg-red-600 text-white'
|
||||||
|
: 'bg-blue-500 hover:bg-blue-600 text-white'
|
||||||
|
}`}
|
||||||
|
title={isPlaying ? 'Stop' : 'Play'}
|
||||||
|
>
|
||||||
|
{isPlaying ? (
|
||||||
|
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
|
||||||
|
<rect x="6" y="4" width="2" height="12" />
|
||||||
|
<rect x="12" y="4" width="2" height="12" />
|
||||||
|
</svg>
|
||||||
|
) : (
|
||||||
|
<svg className="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
|
||||||
|
<path d="M6.3 2.841A1.5 1.5 0 004 4.11v11.78a1.5 1.5 0 002.3 1.269l9.344-5.89a1.5 1.5 0 000-2.538L6.3 2.84z" />
|
||||||
|
</svg>
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Waveform player with visualization
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col gap-2">
|
<div className="flex flex-col gap-2">
|
||||||
<audio
|
<audio
|
||||||
|
|||||||
Reference in New Issue
Block a user