|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team |
| 3 | +/** |
| 4 | + * @file AudioMixerPanel.tsx |
| 5 | + * @brief The project mixer — one strip per bus (volume fader, mute, insert |
| 6 | + * chain, duck rule) editing `project.esproject` features.audio. Every |
| 7 | + * edit persists through ProjectStore.setAudio AND live-applies to the |
| 8 | + * edit realm's Audio resource, so tweaks are audible immediately and |
| 9 | + * Play/exports boot the same mix (the config rides the physics-config |
| 10 | + * pipeline). |
| 11 | + */ |
| 12 | + |
| 13 | +import { useSyncExternalStore } from 'react'; |
| 14 | +import { Plus, Trash2, VolumeX, Volume2 } from 'lucide-react'; |
| 15 | +import { |
| 16 | + Audio, applyAudioProjectConfig, |
| 17 | + type AudioProjectConfig, type AudioBusDecl, type BusEffectDef, |
| 18 | +} from 'esengine'; |
| 19 | +import { ProjectStore } from '@/project/ProjectStore'; |
| 20 | +import { EngineHost } from '@/engine/EngineHost'; |
| 21 | +import { Select } from '@/components/Select'; |
| 22 | +import { t } from '@/i18n'; |
| 23 | + |
| 24 | +/** The always-present mixer tree; custom buses append after these. */ |
| 25 | +const DEFAULT_BUSES = ['master', 'music', 'sfx', 'ui', 'voice']; |
| 26 | +const DEFAULT_VOLUME: Record<string, number> = { master: 1, music: 0.8, sfx: 1, ui: 1, voice: 1 }; |
| 27 | + |
| 28 | +interface StripModel extends AudioBusDecl { |
| 29 | + builtin: boolean; |
| 30 | +} |
| 31 | + |
| 32 | +/** Default buses merged with the project declarations, declaration order kept. */ |
| 33 | +function stripsOf(config: AudioProjectConfig): StripModel[] { |
| 34 | + const byName = new Map((config.buses ?? []).map((b) => [b.name, b])); |
| 35 | + const strips: StripModel[] = DEFAULT_BUSES.map((name) => ({ |
| 36 | + name, volume: DEFAULT_VOLUME[name], ...byName.get(name), builtin: true, |
| 37 | + })); |
| 38 | + for (const b of config.buses ?? []) { |
| 39 | + if (!DEFAULT_BUSES.includes(b.name)) strips.push({ ...b, builtin: false }); |
| 40 | + } |
| 41 | + return strips; |
| 42 | +} |
| 43 | + |
| 44 | +/** Persist + live-apply one new config state. */ |
| 45 | +function commit(next: AudioProjectConfig): void { |
| 46 | + void ProjectStore.setAudio(next); |
| 47 | + const audio = EngineHost.getResource(Audio); |
| 48 | + if (audio) applyAudioProjectConfig(audio, next); |
| 49 | +} |
| 50 | + |
| 51 | +/** Replace/patch one bus's declaration inside the config. */ |
| 52 | +function patchBus(config: AudioProjectConfig, name: string, patch: Partial<AudioBusDecl> | null): AudioProjectConfig { |
| 53 | + const buses = [...(config.buses ?? [])]; |
| 54 | + const i = buses.findIndex((b) => b.name === name); |
| 55 | + if (patch === null) { |
| 56 | + if (i >= 0) buses.splice(i, 1); |
| 57 | + } else if (i >= 0) { |
| 58 | + buses[i] = { ...buses[i], ...patch }; |
| 59 | + } else { |
| 60 | + buses.push({ name, ...patch }); |
| 61 | + } |
| 62 | + return { buses }; |
| 63 | +} |
| 64 | + |
| 65 | +const FX_KINDS: Array<{ value: BusEffectDef['type']; label: () => string }> = [ |
| 66 | + { value: 'filter', label: () => t('mix.fx.filter') }, |
| 67 | + { value: 'reverb', label: () => t('mix.fx.reverb') }, |
| 68 | + { value: 'compressor', label: () => t('mix.fx.compressor') }, |
| 69 | +]; |
| 70 | + |
| 71 | +function defaultEffect(type: BusEffectDef['type']): BusEffectDef { |
| 72 | + switch (type) { |
| 73 | + case 'filter': return { type: 'filter', filter: 'lowpass', frequency: 1200, q: 1 }; |
| 74 | + case 'reverb': return { type: 'reverb', seconds: 1.5, wet: 0.35 }; |
| 75 | + case 'compressor': return { type: 'compressor', thresholdDb: -24, ratio: 4 }; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +function NumCell(props: { label: string; value: number; step?: number; onCommit: (n: number) => void }) { |
| 80 | + return ( |
| 81 | + <label className="mix-num"> |
| 82 | + <span>{props.label}</span> |
| 83 | + <input |
| 84 | + type="number" defaultValue={props.value} key={props.value} step={props.step ?? 1} |
| 85 | + onBlur={(e) => { |
| 86 | + const n = Number(e.target.value); |
| 87 | + if (Number.isFinite(n) && n !== props.value) props.onCommit(n); |
| 88 | + }} |
| 89 | + onKeyDown={(e) => { if (e.key === 'Enter') (e.target as HTMLInputElement).blur(); }} |
| 90 | + /> |
| 91 | + </label> |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +function EffectRow(props: { fx: BusEffectDef; onChange: (fx: BusEffectDef) => void; onRemove: () => void }) { |
| 96 | + const { fx, onChange, onRemove } = props; |
| 97 | + return ( |
| 98 | + <div className="mix-fx"> |
| 99 | + <span className="mix-fx-kind">{FX_KINDS.find((k) => k.value === fx.type)?.label()}</span> |
| 100 | + {fx.type === 'filter' && ( |
| 101 | + <> |
| 102 | + <Select |
| 103 | + ariaLabel={t('mix.fx.filterKind')} |
| 104 | + value={fx.filter} |
| 105 | + options={(['lowpass', 'highpass', 'bandpass', 'lowshelf', 'highshelf', 'peaking', 'notch'] as const) |
| 106 | + .map((v) => ({ value: v, label: v }))} |
| 107 | + onChange={(v) => onChange({ ...fx, filter: v })} |
| 108 | + /> |
| 109 | + <NumCell label="Hz" value={fx.frequency} step={50} onCommit={(n) => onChange({ ...fx, frequency: Math.max(10, n) })} /> |
| 110 | + <NumCell label="Q" value={fx.q ?? 1} step={0.1} onCommit={(n) => onChange({ ...fx, q: n })} /> |
| 111 | + </> |
| 112 | + )} |
| 113 | + {fx.type === 'reverb' && ( |
| 114 | + <> |
| 115 | + <NumCell label={t('mix.fx.seconds')} value={fx.seconds ?? 1.5} step={0.1} onCommit={(n) => onChange({ ...fx, seconds: Math.max(0.05, n) })} /> |
| 116 | + <NumCell label={t('mix.fx.wet')} value={fx.wet ?? 0.35} step={0.05} onCommit={(n) => onChange({ ...fx, wet: Math.max(0, Math.min(1, n)) })} /> |
| 117 | + </> |
| 118 | + )} |
| 119 | + {fx.type === 'compressor' && ( |
| 120 | + <> |
| 121 | + <NumCell label="dB" value={fx.thresholdDb ?? -24} onCommit={(n) => onChange({ ...fx, thresholdDb: n })} /> |
| 122 | + <NumCell label={t('mix.fx.ratio')} value={fx.ratio ?? 4} step={0.5} onCommit={(n) => onChange({ ...fx, ratio: Math.max(1, n) })} /> |
| 123 | + </> |
| 124 | + )} |
| 125 | + <button type="button" className="mix-rm" title={t('mix.fx.remove')} onClick={onRemove}> |
| 126 | + <Trash2 size={12} /> |
| 127 | + </button> |
| 128 | + </div> |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +function BusStrip(props: { strip: StripModel; all: StripModel[]; config: AudioProjectConfig }) { |
| 133 | + const { strip, all, config } = props; |
| 134 | + const volume = strip.volume ?? 1; |
| 135 | + const patch = (p: Partial<AudioBusDecl> | null) => commit(patchBus(config, strip.name, p)); |
| 136 | + const effects = strip.effects ?? []; |
| 137 | + const duckTargets = all.filter((s) => s.name !== strip.name).map((s) => s.name); |
| 138 | + |
| 139 | + return ( |
| 140 | + <div className="mix-strip"> |
| 141 | + <div className="mix-head"> |
| 142 | + <span className="mix-name">{strip.name}</span> |
| 143 | + {!strip.builtin && ( |
| 144 | + <button type="button" className="mix-rm" title={t('mix.removeBus')} onClick={() => patch(null)}> |
| 145 | + <Trash2 size={12} /> |
| 146 | + </button> |
| 147 | + )} |
| 148 | + </div> |
| 149 | + |
| 150 | + <div className="mix-vol"> |
| 151 | + <input |
| 152 | + type="range" min={0} max={1} step={0.01} value={volume} |
| 153 | + className="mix-fader" |
| 154 | + onChange={(e) => patch({ volume: Number(e.target.value) })} |
| 155 | + /> |
| 156 | + <span className="mix-vol-num">{Math.round(volume * 100)}</span> |
| 157 | + <button |
| 158 | + type="button" |
| 159 | + className={'mix-mute' + (strip.muted ? ' is-on' : '')} |
| 160 | + title={t('mix.mute')} |
| 161 | + onClick={() => patch({ muted: !strip.muted })} |
| 162 | + > |
| 163 | + {strip.muted ? <VolumeX size={13} /> : <Volume2 size={13} />} |
| 164 | + </button> |
| 165 | + </div> |
| 166 | + |
| 167 | + <div className="mix-fxs"> |
| 168 | + {effects.map((fx, i) => ( |
| 169 | + <EffectRow |
| 170 | + key={`${i}-${fx.type}`} |
| 171 | + fx={fx} |
| 172 | + onChange={(nf) => patch({ effects: effects.map((e, j) => (j === i ? nf : e)) })} |
| 173 | + onRemove={() => patch({ effects: effects.filter((_, j) => j !== i) })} |
| 174 | + /> |
| 175 | + ))} |
| 176 | + <Select |
| 177 | + ariaLabel={t('mix.fx.add')} |
| 178 | + value={'' as string} |
| 179 | + options={[ |
| 180 | + { value: '', label: t('mix.fx.add') }, |
| 181 | + ...FX_KINDS.map((k) => ({ value: k.value as string, label: k.label() })), |
| 182 | + ]} |
| 183 | + onChange={(v) => { if (v) patch({ effects: [...effects, defaultEffect(v as BusEffectDef['type'])] }); }} |
| 184 | + /> |
| 185 | + </div> |
| 186 | + |
| 187 | + {strip.name !== 'master' && ( |
| 188 | + <div className="mix-duck"> |
| 189 | + <span className="mix-lbl">{t('mix.duckBy')}</span> |
| 190 | + <Select |
| 191 | + ariaLabel={t('mix.duckBy')} |
| 192 | + value={strip.duck?.trigger ?? ''} |
| 193 | + options={[{ value: '', label: t('mix.duckNone') }, ...duckTargets.map((n) => ({ value: n, label: n }))]} |
| 194 | + onChange={(v) => patch({ duck: v ? { trigger: v, amount: strip.duck?.amount ?? 0.3 } : undefined })} |
| 195 | + /> |
| 196 | + {strip.duck && ( |
| 197 | + <input |
| 198 | + type="range" min={0} max={1} step={0.05} value={strip.duck.amount} |
| 199 | + className="mix-duck-amt" title={t('mix.duckAmount')} |
| 200 | + onChange={(e) => patch({ duck: { ...strip.duck!, amount: Number(e.target.value) } })} |
| 201 | + /> |
| 202 | + )} |
| 203 | + </div> |
| 204 | + )} |
| 205 | + </div> |
| 206 | + ); |
| 207 | +} |
| 208 | + |
| 209 | +export function AudioMixerPanel() { |
| 210 | + const project = useSyncExternalStore(ProjectStore.subscribe, ProjectStore.getSnapshot); |
| 211 | + if (!project) { |
| 212 | + return <div className="mix-empty">{t('mix.noProject')}</div>; |
| 213 | + } |
| 214 | + const config = ProjectStore.audioFeature(); |
| 215 | + const strips = stripsOf(config); |
| 216 | + |
| 217 | + const addBus = () => { |
| 218 | + let n = 1; |
| 219 | + let name = 'bus'; |
| 220 | + while (strips.some((s) => s.name === name)) name = `bus-${n++}`; |
| 221 | + commit(patchBus(config, name, { parent: 'master', volume: 1 })); |
| 222 | + }; |
| 223 | + |
| 224 | + return ( |
| 225 | + <div className="mix-panel"> |
| 226 | + <div className="mix-strips"> |
| 227 | + {strips.map((s) => ( |
| 228 | + <BusStrip key={s.name} strip={s} all={strips} config={config} /> |
| 229 | + ))} |
| 230 | + <button type="button" className="mix-add" title={t('mix.addBus')} onClick={addBus}> |
| 231 | + <Plus size={14} /> {t('mix.addBus')} |
| 232 | + </button> |
| 233 | + </div> |
| 234 | + </div> |
| 235 | + ); |
| 236 | +} |
0 commit comments