Files
stemengine_old/frontend/src/components/PerformanceSidebar.tsx
T
2026-04-09 08:30:51 +09:00

446 lines
20 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useCallback, useEffect, useRef, useState } from 'react'
import type { LyricsVisualStyle } from './LyricsLivePreview'
interface Stem {
name: string
label: string
url: string
}
interface SidebarStyleState {
fontFamily: string
fontSize: number
fontBold: boolean
textAlign: 'left' | 'center' | 'right'
activeLineVerticalAlign: 'top' | 'center' | 'bottom'
lineSpacing: number
textColor: string
highlightColor: string
bgColor: string
videoDisplayMode: 'scroll' | 'line' | 'word' | 'syllable'
layoutOffsetXScroll: number
layoutOffsetYScroll: number
layoutOffsetXLine: number
layoutOffsetYLine: number
layoutOffsetXWord: number
layoutOffsetYWord: number
layoutOffsetXSyllable: number
layoutOffsetYSyllable: number
}
type DeepPartial<T> = {
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K]
}
interface VizSettings {
quality: 'low' | 'balanced' | 'high'
fps_cap: 30 | 60 | 120
muon: {
radius_multiplier: number
aperture: number
spacing: number
}
fluid: {
spawn_density: number
splat_force: number
decay: number
}
}
const VIZ_DEFAULTS: VizSettings = {
quality: 'balanced',
fps_cap: 60,
muon: { radius_multiplier: 1, aperture: 1, spacing: 1 },
fluid: { spawn_density: 1, splat_force: 1, decay: 1 },
}
const FONT_OPTIONS = [
{ value: 'system-ui, sans-serif', label: 'System' },
{ value: '"Inter", sans-serif', label: 'Inter' },
{ value: '"Work Sans", sans-serif', label: 'Work Sans' },
{ value: '"Archivo", sans-serif', label: 'Archivo' },
{ value: '"Poppins", sans-serif', label: 'Poppins' },
{ value: '"Roboto", sans-serif', label: 'Roboto' },
{ value: '"Oswald", sans-serif', label: 'Oswald' },
{ value: '"EB Garamond", serif', label: 'EB Garamond' },
{ value: '"Libre Baskerville", serif', label: 'Libre Baskerville' },
{ value: '"Young Serif", serif', label: 'Young Serif' },
]
interface PerformanceSidebarProps {
isNarrow: boolean
visualStyle: LyricsVisualStyle
setVisualStyle: (s: LyricsVisualStyle) => void
hasVideo: boolean
stems: Stem[] | undefined
mutedStems: Record<string, boolean>
soloedStems: Record<string, boolean>
gains: Record<string, number>
toggleStemMute: (name: string) => void
toggleStemSolo: (name: string) => void
setStemGain: (name: string, value: number) => void
style: SidebarStyleState
patchStyle: (patch: Partial<SidebarStyleState>) => void
vizSettings: VizSettings
manifestVizSettings: VizSettings | null
patchVizSettings: (patch: DeepPartial<VizSettings>) => void
onClose: () => void
}
function formatMultiplier(value: number): string {
return `${value.toFixed(2)}×`
}
function getLayoutOffset(style: SidebarStyleState): { x: number; y: number } {
switch (style.videoDisplayMode) {
case 'line':
return { x: style.layoutOffsetXLine, y: style.layoutOffsetYLine }
case 'word':
return { x: style.layoutOffsetXWord, y: style.layoutOffsetYWord }
case 'syllable':
return { x: style.layoutOffsetXSyllable, y: style.layoutOffsetYSyllable }
case 'scroll':
default:
return { x: style.layoutOffsetXScroll, y: style.layoutOffsetYScroll }
}
}
export default function PerformanceSidebar({
isNarrow,
visualStyle,
setVisualStyle,
hasVideo,
stems,
mutedStems,
soloedStems,
gains,
toggleStemMute,
toggleStemSolo,
setStemGain,
style,
patchStyle,
vizSettings,
manifestVizSettings,
patchVizSettings,
onClose,
}: PerformanceSidebarProps) {
const [open, setOpen] = useState({
background: true,
stems: true,
lyrics: true,
visualizer: true,
})
const autoHideTimerRef = useRef<number | null>(null)
const scheduleAutoHide = useCallback(() => {
if (autoHideTimerRef.current !== null) {
window.clearTimeout(autoHideTimerRef.current)
}
autoHideTimerRef.current = window.setTimeout(() => {
onClose()
}, 4000)
}, [onClose])
useEffect(() => {
scheduleAutoHide()
return () => {
if (autoHideTimerRef.current !== null) {
window.clearTimeout(autoHideTimerRef.current)
}
}
}, [scheduleAutoHide])
const activeOffset = getLayoutOffset(style)
const isVisualizerMode = visualStyle === 'muon' || visualStyle === 'fluid'
const isStaticMode = visualStyle !== 'muon' && visualStyle !== 'fluid' && visualStyle !== 'video'
const patchOffset = (axis: 'x' | 'y', value: number) => {
if (style.videoDisplayMode === 'line') {
patchStyle(axis === 'x' ? { layoutOffsetXLine: value } : { layoutOffsetYLine: value })
return
}
if (style.videoDisplayMode === 'word') {
patchStyle(axis === 'x' ? { layoutOffsetXWord: value } : { layoutOffsetYWord: value })
return
}
if (style.videoDisplayMode === 'syllable') {
patchStyle(axis === 'x' ? { layoutOffsetXSyllable: value } : { layoutOffsetYSyllable: value })
return
}
patchStyle(axis === 'x' ? { layoutOffsetXScroll: value } : { layoutOffsetYScroll: value })
}
const sectionHeader = (key: keyof typeof open, label: string, hidden = false) => {
if (hidden) return null
return (
<button
type="button"
onClick={() => setOpen((prev) => ({ ...prev, [key]: !prev[key] }))}
className="w-full flex items-center justify-between text-xs font-semibold tracking-wide text-slate-200"
>
<span>{label}</span>
<span className="text-slate-400">{open[key] ? '▾' : '▸'}</span>
</button>
)
}
const panelClass = isNarrow
? 'absolute inset-x-0 bottom-0 h-[62vh] rounded-t-2xl border-t border-white/15'
: 'absolute right-0 top-0 h-full border-l border-white/15'
return (
<div
className={`${panelClass} z-[9] bg-black/70 backdrop-blur-md text-slate-100 overflow-y-auto`}
style={isNarrow ? undefined : { width: 280 }}
onMouseMove={scheduleAutoHide}
onPointerMove={scheduleAutoHide}
>
<div className="sticky top-0 z-10 px-3 py-2 border-b border-white/10 bg-black/75 backdrop-blur-md flex items-center justify-between">
<span className="text-xs font-semibold uppercase tracking-[0.08em] text-slate-300">Controls</span>
<button
type="button"
onClick={onClose}
className="w-7 h-7 rounded bg-white/10 hover:bg-white/20 text-slate-200 text-sm"
title="Close"
>
✕
</button>
</div>
<div className="px-3 py-3 space-y-4">
<section className="space-y-2">
{sectionHeader('background', 'BACKGROUND')}
{open.background && (
<div className={isNarrow ? 'flex flex-wrap gap-2' : 'grid grid-cols-2 gap-2'}>
{[
{ key: 'static', label: 'Static', value: 'karaoke' as LyricsVisualStyle, active: isStaticMode },
{ key: 'muon', label: 'Muon', value: 'muon' as LyricsVisualStyle, active: visualStyle === 'muon' },
{ key: 'fluid', label: 'Fluid', value: 'fluid' as LyricsVisualStyle, active: visualStyle === 'fluid' },
...(hasVideo ? [{ key: 'video', label: 'Video', value: 'video' as LyricsVisualStyle, active: visualStyle === 'video' }] : []),
].map((mode) => (
<button
key={mode.key}
type="button"
onClick={() => setVisualStyle(mode.value)}
className={`rounded-md px-2 py-2 text-xs border transition-colors ${isNarrow ? 'min-w-[70px] flex-1' : ''} ${mode.active ? 'border-violet-400 bg-violet-500/20 text-white' : 'border-white/15 bg-white/5 text-slate-300 hover:bg-white/10'}`}
>
{mode.label}
</button>
))}
</div>
)}
</section>
<section className="space-y-2">
{sectionHeader('stems', 'STEMS')}
{open.stems && (
<div className="space-y-2">
{stems?.length ? stems.map((stem) => (
<div key={stem.name} className="space-y-1.5">
<div className="flex items-center gap-2">
<span className="text-xs font-medium text-slate-300 flex-1 truncate">{stem.label}</span>
<button
type="button"
onClick={() => toggleStemMute(stem.name)}
className={`w-6 h-6 text-[10px] rounded ${mutedStems[stem.name] ? 'bg-red-500 text-white' : 'bg-white/10 text-slate-300'}`}
>
M
</button>
<button
type="button"
onClick={() => toggleStemSolo(stem.name)}
className={`w-6 h-6 text-[10px] rounded ${soloedStems[stem.name] ? 'bg-yellow-300 text-black' : 'bg-white/10 text-slate-300'}`}
>
S
</button>
</div>
<div className="flex items-center gap-2">
<input
type="range"
min={0}
max={2}
step={0.05}
value={gains[stem.name] ?? 1}
onChange={(event) => setStemGain(stem.name, parseFloat(event.target.value))}
className="w-full h-1.5 accent-brand-500"
/>
<span className="text-[10px] tabular-nums text-slate-400 w-11 text-right">{Math.round((gains[stem.name] ?? 1) * 100)}%</span>
</div>
</div>
)) : (
<p className="text-[11px] text-slate-400">No stems — single track mode</p>
)}
</div>
)}
</section>
<section className="space-y-2">
{sectionHeader('lyrics', 'LYRICS')}
{open.lyrics && (
<div className="space-y-2.5">
<div className="flex items-center gap-2">
<select
value={style.fontFamily}
onChange={(event) => patchStyle({ fontFamily: event.target.value })}
className="flex-1 text-xs bg-white/10 border border-white/15 rounded px-2 py-1"
>
{FONT_OPTIONS.map((font) => (
<option key={font.value} value={font.value} style={{ background: '#0f172a', color: '#e2e8f0', fontFamily: font.value }}>
{font.label}
</option>
))}
</select>
<button
type="button"
onClick={() => patchStyle({ fontBold: !style.fontBold })}
className={`px-2 py-1 rounded text-xs font-semibold ${style.fontBold ? 'bg-white/25 text-white' : 'bg-white/10 text-slate-300'}`}
>
B
</button>
</div>
<div className="space-y-1">
<div className="flex items-center justify-between text-[10px] text-slate-400"><span>Size</span><span>{style.fontSize}</span></div>
<input type="range" min={12} max={56} step={1} value={style.fontSize} onChange={(event) => patchStyle({ fontSize: parseInt(event.target.value, 10) })} className="w-full h-1.5 accent-brand-500" />
</div>
<div className="flex items-center gap-2">
{(['left', 'center', 'right'] as const).map((align) => (
<button
key={align}
type="button"
onClick={() => patchStyle({ textAlign: align })}
className={`flex-1 rounded px-2 py-1 text-xs ${style.textAlign === align ? 'bg-white/20 text-white' : 'bg-white/10 text-slate-300'}`}
>
{align === 'left' ? 'L' : align === 'center' ? 'C' : 'R'}
</button>
))}
</div>
<div className="flex items-center gap-2">
{(['top', 'center', 'bottom'] as const).map((pos) => (
<button
key={pos}
type="button"
onClick={() => patchStyle({ activeLineVerticalAlign: pos })}
className={`flex-1 rounded px-2 py-1 text-xs ${style.activeLineVerticalAlign === pos ? 'bg-white/20 text-white' : 'bg-white/10 text-slate-300'}`}
>
{pos === 'top' ? 'T' : pos === 'center' ? 'C' : 'B'}
</button>
))}
</div>
<div className="space-y-1">
<div className="flex items-center justify-between text-[10px] text-slate-400"><span>Line spacing</span><span>{style.lineSpacing.toFixed(2)}</span></div>
<input type="range" min={1} max={2.5} step={0.05} value={style.lineSpacing} onChange={(event) => patchStyle({ lineSpacing: parseFloat(event.target.value) })} className="w-full h-1.5 accent-brand-500" />
</div>
<div className="grid grid-cols-3 gap-2 text-[10px] text-slate-300">
<label className="space-y-1"><span className="block">Text</span><input type="color" value={style.textColor} onChange={(event) => patchStyle({ textColor: event.target.value })} className="w-full h-7 rounded border border-white/15 bg-transparent" /></label>
<label className="space-y-1"><span className="block">Highlight</span><input type="color" value={style.highlightColor} onChange={(event) => patchStyle({ highlightColor: event.target.value })} className="w-full h-7 rounded border border-white/15 bg-transparent" /></label>
<label className="space-y-1"><span className="block">BG</span><input type="color" value={style.bgColor} onChange={(event) => patchStyle({ bgColor: event.target.value })} className="w-full h-7 rounded border border-white/15 bg-transparent" /></label>
</div>
<div className="space-y-1">
<div className="flex items-center justify-between text-[10px] text-slate-400"><span>X offset</span><span>{activeOffset.x}</span></div>
<input type="range" min={-50} max={50} step={1} value={activeOffset.x} onChange={(event) => patchOffset('x', parseInt(event.target.value, 10))} className="w-full h-1.5 accent-brand-500" />
</div>
<div className="space-y-1">
<div className="flex items-center justify-between text-[10px] text-slate-400"><span>Y offset</span><span>{activeOffset.y}</span></div>
<input type="range" min={-50} max={50} step={1} value={activeOffset.y} onChange={(event) => patchOffset('y', parseInt(event.target.value, 10))} className="w-full h-1.5 accent-brand-500" />
</div>
</div>
)}
</section>
<section className="space-y-2">
{sectionHeader('visualizer', 'VISUALIZER')}
{open.visualizer && (
<div className="space-y-2.5">
<div className="space-y-1.5">
<div className="text-[10px] text-slate-400">Quality</div>
<div className="grid grid-cols-3 gap-1.5">
{(['low', 'balanced', 'high'] as const).map((quality) => (
<button
key={quality}
type="button"
onClick={() => patchVizSettings({ quality })}
className={`rounded px-2 py-1 text-[11px] border ${vizSettings.quality === quality ? 'border-violet-400 bg-violet-500/20 text-white' : 'border-white/15 bg-white/5 text-slate-300 hover:bg-white/10'}`}
>
{quality === 'balanced' ? 'Balanced' : quality === 'low' ? 'Low' : 'High'}
</button>
))}
</div>
</div>
<div className="space-y-1.5">
<div className="text-[10px] text-slate-400">FPS cap</div>
<div className="grid grid-cols-3 gap-1.5">
{([30, 60, 120] as const).map((fps) => (
<button
key={fps}
type="button"
onClick={() => patchVizSettings({ fps_cap: fps })}
className={`rounded px-2 py-1 text-[11px] border ${vizSettings.fps_cap === fps ? 'border-violet-400 bg-violet-500/20 text-white' : 'border-white/15 bg-white/5 text-slate-300 hover:bg-white/10'}`}
>
{fps}
</button>
))}
</div>
</div>
{!isVisualizerMode && (
<p className="text-[11px] text-slate-400">
Switch background to Muon or Fluid to access mode-specific controls.
</p>
)}
{visualStyle === 'muon' && (
<>
<div className="space-y-1">
<div className="flex items-center justify-between text-[10px] text-slate-400"><span>Radius</span><span>{formatMultiplier(vizSettings.muon.radius_multiplier)}</span></div>
<input type="range" min={0.25} max={2} step={0.05} value={vizSettings.muon.radius_multiplier} onChange={(event) => patchVizSettings({ muon: { radius_multiplier: parseFloat(event.target.value) } })} className="w-full h-1.5 accent-brand-500" />
</div>
<div className="space-y-1">
<div className="flex items-center justify-between text-[10px] text-slate-400"><span>Aperture</span><span>{formatMultiplier(vizSettings.muon.aperture)}</span></div>
<input type="range" min={0.25} max={2} step={0.05} value={vizSettings.muon.aperture} onChange={(event) => patchVizSettings({ muon: { aperture: parseFloat(event.target.value) } })} className="w-full h-1.5 accent-brand-500" />
</div>
<div className="space-y-1">
<div className="flex items-center justify-between text-[10px] text-slate-400"><span>Spacing</span><span>{formatMultiplier(vizSettings.muon.spacing)}</span></div>
<input type="range" min={0.25} max={2} step={0.05} value={vizSettings.muon.spacing} onChange={(event) => patchVizSettings({ muon: { spacing: parseFloat(event.target.value) } })} className="w-full h-1.5 accent-brand-500" />
</div>
</>
)}
{visualStyle === 'fluid' && (
<>
<div className="space-y-1">
<div className="flex items-center justify-between text-[10px] text-slate-400"><span>Density</span><span>{formatMultiplier(vizSettings.fluid.spawn_density)}</span></div>
<input type="range" min={0.25} max={2} step={0.05} value={vizSettings.fluid.spawn_density} onChange={(event) => patchVizSettings({ fluid: { spawn_density: parseFloat(event.target.value) } })} className="w-full h-1.5 accent-brand-500" />
</div>
<div className="space-y-1">
<div className="flex items-center justify-between text-[10px] text-slate-400"><span>Force</span><span>{formatMultiplier(vizSettings.fluid.splat_force)}</span></div>
<input type="range" min={0.25} max={2} step={0.05} value={vizSettings.fluid.splat_force} onChange={(event) => patchVizSettings({ fluid: { splat_force: parseFloat(event.target.value) } })} className="w-full h-1.5 accent-brand-500" />
</div>
<div className="space-y-1">
<div className="flex items-center justify-between text-[10px] text-slate-400"><span>Trail decay</span><span>{formatMultiplier(vizSettings.fluid.decay)}</span></div>
<input type="range" min={0.25} max={2} step={0.05} value={vizSettings.fluid.decay} onChange={(event) => patchVizSettings({ fluid: { decay: parseFloat(event.target.value) } })} className="w-full h-1.5 accent-brand-500" />
</div>
</>
)}
<button
type="button"
onClick={() => patchVizSettings(manifestVizSettings ?? VIZ_DEFAULTS)}
className="w-full rounded-md px-2 py-1.5 text-xs bg-white/10 hover:bg-white/20 text-slate-200"
>
{manifestVizSettings ? 'Reset to song defaults' : 'Reset to defaults'}
</button>
</div>
)}
</section>
</div>
</div>
)
}