/* ============================================================
EFFECTS PAGE — Sound Effect Settings
Design: Reference luxsin_x8_音效.jpg
Layout:
- Page header with global Effect toggle
- Glass cards: Style, 声场宽度, 交叉反馈, 音调, 响度
All dropdowns → navigate to /select page
============================================================ */
import { useDevice } from "@/contexts/DeviceContext";
import { ChevronLeft, ChevronRight } from "lucide-react";
import { useLocation } from "wouter";
import { useState, useEffect, useRef, useMemo } from "react";
import BottomNav from "@/components/BottomNav";
import { FeatureGate } from "@/components/FeatureGate";
import { navigateToSelect } from "./SelectPage";
import { cn } from "@/lib/utils";
import localeZh from "@/locales/data-zh.json";
import localeEn from "@/locales/data-en.json";
import { resolveLocalePack } from "@/locales/resolveLocale";
import { usePageTitleWithConnection } from "@/hooks/usePageTitleWithConnection";
type EffectLocale = NonNullable<(typeof localeZh)["effect"]>;
function clampPickIndex(i: number, len: number) {
if (len <= 0) return 0;
return Math.min(Math.max(i, 0), len - 1);
}
function IOSToggle({ checked, onChange }: { checked: boolean; onChange: (v: boolean) => void }) {
return (
);
}
function CyanSlider({
value,
min,
max,
step = 0.1,
onChange,
onRelease,
disabled = false,
}: {
value: number;
min: number;
max: number;
step?: number;
onChange: (v: number) => void;
/** Fires once when the user releases the thumb (pointer/mouse/touch). Read value from DOM to avoid stale React state. */
onRelease?: (v: number) => void;
disabled?: boolean;
}) {
const fillPct = ((value - min) / (max - min)) * 100;
const emitRelease = (el: EventTarget | null) => {
if (disabled) return;
if (!(el instanceof HTMLInputElement)) return;
onRelease?.(Number(el.value));
};
return (
);
}
export default function EffectsPage() {
const [, setLocation] = useLocation();
const { deviceState, updateSetting } = useDevice();
const effectText = useMemo((): EffectLocale => {
const pack = resolveLocalePack(deviceState?.language);
return (pack.effect ?? localeEn.effect) as EffectLocale;
}, [deviceState?.language]);
const pageTitle = usePageTitleWithConnection(effectText.pageTitle ?? "Effects");
const sceneLabels = useMemo(() => {
const opts = effectText.style?.options ?? [];
return [...opts].sort((a, b) => a.index - b.index).map((o) => o.label);
}, [effectText]);
const crossfeedLabels = useMemo(
() => effectText.crossfeed?.options ?? [],
[effectText],
);
const ds = deviceState;
const bypassOn = (ds?.dsp_enable ?? 0) === 0;
const audioOn = (ds?.audio_enable ?? 0) === 1;
const effectOn = audioOn && !bypassOn;
const widthOn = (ds?.width_enable ?? 0) === 1;
const widthVal = ds?.width_value ?? 50;
const crossfeedOn = (ds?.crossfeed_enable ?? 0) === 1;
const crossfeedVal = ds?.crossfeed_value ?? 0;
const sceneOn = (ds?.effect_enable ?? 0) === 1;
const sceneVal = ds?.effect_value ?? 0;
const colorOn = (ds?.color_enable ?? 0) === 1;
const colorBassGain = (ds?.color_bass_gain ?? 0) / 10;
const colorMidGain = (ds?.color_mid_gain ?? 0) / 10;
const colorTrebleGain = (ds?.color_treble_gain ?? 0) / 10;
const loudnessOn = (ds?.loudness_enable ?? 0) === 1;
const loudnessThreshold = ds?.loudness_threshold_gain ?? 0;
const loudnessBassGain = (ds?.loudness_bass_gain ?? 0) / 10;
const loudnessTrebleGain = (ds?.loudness_treble_gain ?? 0) / 10;
const [localBass, setLocalBass] = useState(colorBassGain);
const [localMid, setLocalMid] = useState(colorMidGain);
const [localTreble, setLocalTreble] = useState(colorTrebleGain);
const isDraggingBass = useRef(false);
const isDraggingMid = useRef(false);
const isDraggingTreble = useRef(false);
const [localLoudnessThreshold, setLocalLoudnessThreshold] = useState(loudnessThreshold);
const [localLoudnessBass, setLocalLoudnessBass] = useState(loudnessBassGain);
const [localLoudnessTreble, setLocalLoudnessTreble] = useState(loudnessTrebleGain);
const isDraggingLoudnessThreshold = useRef(false);
const isDraggingLoudnessBass = useRef(false);
const isDraggingLoudnessTreble = useRef(false);
useEffect(() => {
if (!isDraggingBass.current) setLocalBass(colorBassGain);
if (!isDraggingMid.current) setLocalMid(colorMidGain);
if (!isDraggingTreble.current) setLocalTreble(colorTrebleGain);
if (!isDraggingLoudnessThreshold.current) setLocalLoudnessThreshold(loudnessThreshold);
if (!isDraggingLoudnessBass.current) setLocalLoudnessBass(loudnessBassGain);
if (!isDraggingLoudnessTreble.current) setLocalLoudnessTreble(loudnessTrebleGain);
}, [colorBassGain, colorMidGain, colorTrebleGain, loudnessThreshold, loudnessBassGain, loudnessTrebleGain]);
const [localWidth, setLocalWidth] = useState(() => Math.round(widthVal));
const isDragging = useRef(false);
useEffect(() => {
if (!isDragging.current) setLocalWidth(Math.round(widthVal));
}, [widthVal]);
const goSelect = (title: string, options: string[], selected: number, key: string) => {
navigateToSelect(title, options, selected, "/effects", key);
setLocation("/select");
};
const sceneIdx = clampPickIndex(sceneVal, sceneLabels.length);
const sceneDisplay = sceneLabels[sceneIdx] ?? "";
const crossIdx = clampPickIndex(crossfeedVal, crossfeedLabels.length);
const crossfeedDisplay = crossfeedLabels[crossIdx] ?? "";
return (
{pageTitle}
{
if (v) {
void updateSetting(
bypassOn ? { audio_enable: 1, dsp_enable: 1 } : { audio_enable: 1 },
);
} else {
void updateSetting({ audio_enable: 0 });
}
}}
/>
{
const bypass = (ds?.dsp_enable ?? 0) === 0;
return updateSetting(bypass ? { audio_enable: 1, dsp_enable: 1 } : { audio_enable: 1 });
}}
>
{/* ── Style ── */}
{effectText.style.label}
updateSetting({ effect_enable: v ? 1 : 0 })} />
{/* ── 声场宽度 ── */}
{effectText.stereoWidth.label}
{Math.round(localWidth)}
updateSetting({ width_enable: v ? 1 : 0 })} />
{
if (!widthOn) return;
isDragging.current = true;
setLocalWidth(Math.round(v));
}}
onRelease={(v) => {
if (!widthOn) return;
isDragging.current = false;
const w = Math.round(v);
setLocalWidth(w);
updateSetting({ width_value: w });
}}
/>
{/* ── 交叉反馈 ── */}
{effectText.crossfeed.label}
updateSetting({ crossfeed_enable: v ? 1 : 0 })} />
{/* ── 音调 ── */}
{effectText.tone}
updateSetting({ color_enable: v ? 1 : 0 })} />
{colorOn && (
{/* 低音 */}
{effectText.lowBass}
{localBass.toFixed(1)}
{
isDraggingBass.current = true;
setLocalBass(v);
}}
onRelease={(v) => {
isDraggingBass.current = false;
updateSetting({ color_bass_gain: Math.round(v * 10) });
}}
/>
{/* 中音 */}
{effectText.enterBass}
{localMid.toFixed(1)}
{
isDraggingMid.current = true;
setLocalMid(v);
}}
onRelease={(v) => {
isDraggingMid.current = false;
updateSetting({ color_mid_gain: Math.round(v * 10) });
}}
/>
{/* 高音 */}
{effectText.highBass}
{localTreble.toFixed(1)}
{
isDraggingTreble.current = true;
setLocalTreble(v);
}}
onRelease={(v) => {
isDraggingTreble.current = false;
updateSetting({ color_treble_gain: Math.round(v * 10) });
}}
/>
)}
{/* ── 响度 ── */}
{effectText.loudness}
updateSetting({ loudness_enable: v ? 1 : 0 })} />
{loudnessOn && (
{/* 阈值 */}
{effectText.threshold}
{localLoudnessThreshold}
{
isDraggingLoudnessThreshold.current = true;
setLocalLoudnessThreshold(v);
}}
onRelease={(v) => {
isDraggingLoudnessThreshold.current = false;
updateSetting({ loudness_threshold_gain: Math.round(v) });
}}
/>
{/* Loudness bass */}
{effectText.lowBass}
{localLoudnessBass.toFixed(1)}
{
isDraggingLoudnessBass.current = true;
setLocalLoudnessBass(v);
}}
onRelease={(v) => {
isDraggingLoudnessBass.current = false;
updateSetting({ loudness_bass_gain: Math.round(v * 10) });
}}
/>
{/* Loudness treble */}
{effectText.highBass}
{localLoudnessTreble.toFixed(1)}
{
isDraggingLoudnessTreble.current = true;
setLocalLoudnessTreble(v);
}}
onRelease={(v) => {
isDraggingLoudnessTreble.current = false;
updateSetting({ loudness_treble_gain: Math.round(v * 10) });
}}
/>
)}
);
}