Skip to content

OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.

TypeScript

fix: update audio mixer levels and loading

CodeChef7bc271413f94
diff --git a/apps/web/src/components/audio-mixer/AudioMixer.tsx b/apps/web/src/components/audio-mixer/AudioMixer.tsx
index dc228ac5..6bae947a 100644
--- a/apps/web/src/components/audio-mixer/AudioMixer.tsx
+++ b/apps/web/src/components/audio-mixer/AudioMixer.tsx
@@ -9,10 +9,7 @@ import { useProjectStore } from "../../stores/project-store";
import { ChannelStrip } from "./ChannelStrip";
import type { ChannelStripState } from "./types";
import { volumeToDb, formatDb } from "./types";
-import {
- getRealtimeAudioProcessor,
- type RealtimeAudioProcessor,
-} from "@openreel/core";
+import { getRealtimeAudioGraph } from "@openreel/core";
export interface AudioMixerProps {
/** Whether the mixer panel is visible */
@@ -127,8 +124,8 @@ export const AudioMixer: React.FC<AudioMixerProps> = ({
const muteTrack = useProjectStore((state) => state.muteTrack);
const soloTrack = useProjectStore((state) => state.soloTrack);
- // Reference to the audio processor
- const audioProcessorRef = useRef<RealtimeAudioProcessor | null>(null);
+ // Use the same graph as playback so mixer volume affects preview/playback
+ const audioGraphRef = useRef<ReturnType<typeof getRealtimeAudioGraph> | null>(null);
// Local state for master volume and levels
const [masterVolume, setMasterVolume] = useState(1);
@@ -142,17 +139,51 @@ export const AudioMixer: React.FC<AudioMixerProps> = ({
Record<string, { peak: number; rms: number }>
>({});
- // Initialize audio processor reference
- useEffect(() => {
- audioProcessorRef.current = getRealtimeAudioProcessor();
- }, []);
-
- // Get audio tracks from the timeline (Requirement 20.1)
+ // Get audio tracks from the timeline (Requirement 20.1) – safe if project/timeline not ready
const audioTracks = useMemo(() => {
- return project.timeline.tracks.filter(
+ const tracks = project?.timeline?.tracks ?? [];
+ return tracks.filter(
(track) => track.type === "audio" || track.type === "video",
);
- }, [project.timeline.tracks]);
+ }, [project?.timeline?.tracks]);
+
+ useEffect(() => {
+ try {
+ audioGraphRef.current = getRealtimeAudioGraph();
+ } catch (err) {
+ console.warn("[AudioMixer] Realtime audio graph not available:", err);
+ audioGraphRef.current = null;
+ }
+ }, []);
+
+ // Sync initial volume/pan/master from graph when mixer opens (e.g. after playback)
+ useEffect(() => {
+ if (!visible || !audioGraphRef.current) return;
+ const graph = audioGraphRef.current;
+ try {
+ if (typeof graph.getMasterVolume === "function") {
+ setMasterVolume(graph.getMasterVolume());
+ }
+ if (typeof graph.getTrackVolume === "function" && typeof graph.getTrackPan === "function") {
+ setTrackVolumes((prev) => {
+ const next = { ...prev };
+ audioTracks.forEach((t) => {
+ next[t.id] = graph.getTrackVolume(t.id);
+ });
+ return next;
+ });
+ setTrackPans((prev) => {
+ const next = { ...prev };
+ audioTracks.forEach((t) => {
+ next[t.id] = graph.getTrackPan(t.id);
+ });
+ return next;
+ });
+ }
+ } catch (err) {
+ console.warn("[AudioMixer] Failed to sync levels from graph:", err);
+ }
+ }, [visible, audioTracks]);
// Check if any track has solo enabled (for Requirement 20.4)
const hasSoloedTracks = useMemo(() => {
@@ -174,14 +205,13 @@ export const AudioMixer: React.FC<AudioMixerProps> = ({
}));
}, [audioTracks, trackVolumes, trackPans, trackLevels]);
- // Handle volume change (Requirement 20.2)
+ // Handle volume change (Requirement 20.2) – applies to same graph used for playback
const handleVolumeChange = useCallback((trackId: string, volume: number) => {
setTrackVolumes((prev) => ({
...prev,
[trackId]: volume,
}));
- // Apply volume change to audio processor
- audioProcessorRef.current?.setTrackVolume(trackId, volume);
+ audioGraphRef.current?.updateTrackVolume(trackId, volume);
}, []);
// Handle pan change (Requirement 20.3)
@@ -190,8 +220,7 @@ export const AudioMixer: React.FC<AudioMixerProps> = ({
...prev,
[trackId]: pan,
}));
- // Apply pan change to audio processor
- audioProcessorRef.current?.setTrackPan(trackId, pan);
+ audioGraphRef.current?.updateTrackPan(trackId, pan);
}, []);
// Handle mute toggle (Requirement 20.5)
@@ -219,26 +248,19 @@ export const AudioMixer: React.FC<AudioMixerProps> = ({
// Handle master volume change
const handleMasterVolumeChange = useCallback((volume: number) => {
setMasterVolume(volume);
- // Apply master volume to audio processor
- audioProcessorRef.current?.setMasterVolume(volume);
+ audioGraphRef.current?.setMasterVolume(volume);
}, []);
- // Level metering - updates based on track audibility from audio processor
+ // Level metering – based on track volume and mute/solo
useEffect(() => {
if (!visible) return;
const interval = setInterval(() => {
const newLevels: Record<string, { peak: number; rms: number }> = {};
- const processor = audioProcessorRef.current;
-
- // Get effective audibility from audio processor if available
- const audibilityMap = processor?.getEffectiveAudibility();
audioTracks.forEach((track) => {
- // Use processor's audibility calculation or fall back to local state
- const isAudible = audibilityMap
- ? (audibilityMap.get(track.id) ?? false)
- : !track.muted && (!hasSoloedTracks || track.solo);
+ const isAudible =
+ !track.muted && (!hasSoloedTracks || track.solo);
const trackVolume = trackVolumes[track.id] ?? 1;
if (isAudible && trackVolume > 0) {
diff --git a/apps/web/src/components/editor/EditorInterface.tsx b/apps/web/src/components/editor/EditorInterface.tsx
index c9f31f83..9a6c98e2 100644
--- a/apps/web/src/components/editor/EditorInterface.tsx
+++ b/apps/web/src/components/editor/EditorInterface.tsx
@@ -6,6 +6,7 @@ import { Preview } from "./Preview";
import { InspectorPanel } from "./InspectorPanel";
import { Timeline } from "./Timeline";
import { KeyframeEditorPanel } from "./KeyframeEditorPanel";
+import { AudioMixer } from "../audio-mixer";
import { KeyboardShortcutsOverlay } from "./KeyboardShortcutsOverlay";
import { PanelErrorBoundary } from "../ErrorBoundary";
import { SpotlightTour, MoGraphTour } from "./tour";
@@ -165,7 +166,12 @@ export const EditorInterface: React.FC = () => {
useKeyboardShortcuts();
useAutoSave();
- const { keyframeEditorOpen, setKeyframeEditorOpen, getSelectedClipIds } = useUIStore();
+ const {
+ keyframeEditorOpen,
+ getSelectedClipIds,
+ panels,
+ setPanelVisible,
+ } = useUIStore();
const { project, updateClipKeyframes } = useProjectStore();
const tracks = project.timeline.tracks;
@@ -335,6 +341,16 @@ export const EditorInterface: React.FC = () => {
<div className="absolute inset-x-0 -top-1 -bottom-1 bg-transparent" />
</div>
+ {/* Audio Mixer (when open) */}
+ {panels.audioMixer?.visible && (
+ <PanelErrorBoundary name="Audio Mixer">
+ <AudioMixer
+ visible
+ onClose={() => setPanelVisible("audioMixer", false)}
+ />
+ </PanelErrorBoundary>
+ )}
+
{/* BOTTOM PANEL: Timeline */}
<div
style={{ height: timelineHeight }}
diff --git a/apps/web/src/components/editor/Preview.tsx b/apps/web/src/components/editor/Preview.tsx
index 12ad30de..328aa5b0 100644
--- a/apps/web/src/components/editor/Preview.tsx
+++ b/apps/web/src/components/editor/Preview.tsx
@@ -702,7 +702,7 @@ export const Preview: React.FC = () => {
gainNodeRef.current.gain.value = isMuted ? 0 : 1;
}
if (audioGraphRef.current) {
- audioGraphRef.current.setMasterVolume(isMuted ? 0 : 1);
+ audioGraphRef.current.setPreviewMuted(isMuted);
}
}, [isMuted]);
@@ -818,7 +818,7 @@ export const Preview: React.FC = () => {
audioGraphRef.current = getRealtimeAudioGraph();
}
const audioGraph = audioGraphRef.current;
- audioGraph.setMasterVolume(isMuted ? 0 : 1);
+ audioGraph.setPreviewMuted(isMuted);
const projectStore = useProjectStore.getState();
const speedEngine = getSpeedEngine();
@@ -1881,7 +1881,7 @@ export const Preview: React.FC = () => {
audioGraphRef.current = getRealtimeAudioGraph();
}
const audioGraph = audioGraphRef.current;
- audioGraph.setMasterVolume(isMuted ? 0 : 1);
+ audioGraph.setPreviewMuted(isMuted);
const tracksWithAudio = timelineTracks.filter(
(t) => (t.type === "audio" || t.type === "video") && !t.hidden,
@@ -2813,7 +2813,7 @@ export const Preview: React.FC = () => {
audioGraphRef.current = getRealtimeAudioGraph();
}
const audioGraph = audioGraphRef.current;
- audioGraph.setMasterVolume(isMuted ? 0 : 1);
+ audioGraph.setPreviewMuted(isMuted);
const tracksWithAudio = timelineTracksRef.current.filter(
(t) => (t.type === "audio" || t.type === "video") && !t.hidden,
diff --git a/apps/web/src/components/editor/Toolbar.tsx b/apps/web/src/components/editor/Toolbar.tsx
index 644e643b..aad80633 100644
--- a/apps/web/src/components/editor/Toolbar.tsx
+++ b/apps/web/src/components/editor/Toolbar.tsx
@@ -77,8 +77,15 @@ interface ExportState {
export const Toolbar: React.FC = () => {
const { project } = useProjectStore();
- const { openModal, selectedItems, setExportState: setGlobalExportState, keyframeEditorOpen, toggleKeyframeEditor } =
- useUIStore();
+ const {
+ openModal,
+ selectedItems,
+ setExportState: setGlobalExportState,
+ keyframeEditorOpen,
+ toggleKeyframeEditor,
+ panels,
+ togglePanel,
+ } = useUIStore();
const { mode: themeMode, toggleTheme } = useThemeStore();
const [isExportOpen, setIsExportOpen] = useState(false);
const [isExportDialogOpen, setIsExportDialogOpen] = useState(false);
@@ -951,6 +958,24 @@ export const Toolbar: React.FC = () => {
</TooltipContent>
</Tooltip>
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <button
+ onClick={() => togglePanel("audioMixer")}
+ className={`p-2 rounded-lg transition-colors ${
+ panels.audioMixer?.visible
+ ? "bg-primary/20 text-primary"
+ : "hover:bg-background-elevated text-text-secondary hover:text-text-primary"
+ }`}
+ >
+ <Music size={16} />
+ </button>
+ </TooltipTrigger>
+ <TooltipContent>
+ <p>Audio Mixer – track volume and master level</p>
+ </TooltipContent>
+ </Tooltip>
+
<Tooltip>
<TooltipTrigger asChild>
<button
diff --git a/packages/core/src/audio/realtime-audio-graph.ts b/packages/core/src/audio/realtime-audio-graph.ts
index 08a9db76..ec34c48f 100644
--- a/packages/core/src/audio/realtime-audio-graph.ts
+++ b/packages/core/src/audio/realtime-audio-graph.ts
@@ -76,6 +76,11 @@ export class RealtimeAudioGraph {
private seekPending = false;
private scheduleAheadTime = 0.2;
private schedulerIntervalId: number | null = null;
+ /** Persist mixer volume/pan so they survive track recreate (e.g. on seek). */
+ private trackVolumeOverrides: Map<string, number> = new Map();
+ private trackPanOverrides: Map<string, number> = new Map();
+ private masterVolumeOverride = 1;
+ private previewMuted = false;
constructor(masterClock?: MasterTimelineClock) {
this.masterClock = masterClock || getMasterClock();
@@ -92,15 +97,27 @@ export class RealtimeAudioGraph {
return this.masterGain;
}
+ /** Set master volume from the mixer (1 = 0 dB, 4 = +12 dB). Persists across preview mute. */
setMasterVolume(volume: number): void {
- this.masterGain.gain.setValueAtTime(
- Math.max(0, Math.min(4, volume)),
- this.audioContext.currentTime,
- );
+ this.masterVolumeOverride = Math.max(0, Math.min(4, volume));
+ this.applyMasterGain();
+ }
+
+ /** Mute/unmute preview without changing mixer master volume. */
+ setPreviewMuted(muted: boolean): void {
+ this.previewMuted = muted;
+ this.applyMasterGain();
+ }
+
+ private applyMasterGain(): void {
+ const v = this.previewMuted ? 0 : this.masterVolumeOverride;
+ this.masterGain.gain.setValueAtTime(v, this.audioContext.currentTime);
}
createTrack(config: TrackConfig): void {
this.removeTrack(config.trackId);
+ config.volume = this.trackVolumeOverrides.get(config.trackId) ?? config.volume;
+ config.pan = this.trackPanOverrides.get(config.trackId) ?? config.pan;
this.trackConfigs.set(config.trackId, config);
const inputGain = this.audioContext.createGain();
@@ -384,28 +401,46 @@ export class RealtimeAudioGraph {
}
updateTrackVolume(trackId: string, volume: number): void {
+ const clamped = Math.max(0, Math.min(4, volume));
+ this.trackVolumeOverrides.set(trackId, clamped);
const nodes = this.trackNodes.get(trackId);
const config = this.trackConfigs.get(trackId);
if (nodes && config) {
- config.volume = Math.max(0, Math.min(4, volume));
+ config.volume = clamped;
this.updateTrackAudibility(trackId);
}
}
updateTrackPan(trackId: string, pan: number): void {
+ const clamped = Math.max(-1, Math.min(1, pan));
+ this.trackPanOverrides.set(trackId, clamped);
const nodes = this.trackNodes.get(trackId);
if (nodes) {
nodes.panNode.pan.setValueAtTime(
- Math.max(-1, Math.min(1, pan)),
+ clamped,
this.audioContext.currentTime,
);
}
const config = this.trackConfigs.get(trackId);
if (config) {
- config.pan = pan;
+ config.pan = clamped;
}
}
+ getTrackVolume(trackId: string): number {
+ const config = this.trackConfigs.get(trackId);
+ return this.trackVolumeOverrides.get(trackId) ?? config?.volume ?? 1;
+ }
+
+ getTrackPan(trackId: string): number {
+ const config = this.trackConfigs.get(trackId);
+ return this.trackPanOverrides.get(trackId) ?? config?.pan ?? 0;
+ }
+
+ getMasterVolume(): number {
+ return this.masterVolumeOverride;
+ }
+
setTrackMuted(trackId: string, muted: boolean): void {
const config = this.trackConfigs.get(trackId);
if (config) {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index f5dd178b..16b6fa61 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -18,22 +18,6 @@ importers:
specifier: ^1.25.3
version: 1.25.3
- apps/cloud:
- dependencies:
- hono:
- specifier: ^4.11.4
- version: 4.11.4
- devDependencies:
- '@cloudflare/workers-types':
- specifier: ^4.20260117.0
- version: 4.20260117.0
- typescript:
- specifier: ^5.5.2
- version: 5.9.3
- wrangler:
- specifier: ^3.101.0
- version: 3.114.17(@cloudflare/[email protected])
-
apps/image:
dependencies:
'@imgly/background-removal':
@@ -2662,10 +2646,6 @@ packages:
resolution: {integrity: sha512-PDEfEF102G23vHmPhLyPboFCD+BkMGu+GuJe2d9/eH4FsCwvgBpnc9n0pGE+ffKdph38s6foEZiEjdgHdzp+IA==}
- resolution: {integrity: sha512-U7tt8JsyrxSRKspfhtLET79pU8K+tInj5QZXs1jSugO1Vq5dFj3kmZsRldo29mTBfcjDRVRXrEZ6LS63Cog9ZA==}
- engines: {node: '>=16.9.0'}
-
resolution: {integrity: sha512-Y22oTqIU4uuPgEemfz7NDJz6OeKf12Lsu+QC+s3BVpda64lTiMYCyGwg5ki4vFxkMwQdeZDl2adZoqUgdFuTgQ==}
engines: {node: '>=18'}
@@ -3872,7 +3852,8 @@ snapshots:
'@cloudflare/[email protected]':
optional: true
- '@cloudflare/[email protected]': {}
+ '@cloudflare/[email protected]':
+ optional: true
'@cspotcode/[email protected]':
dependencies:
@@ -5840,8 +5821,6 @@ snapshots:
-
dependencies:
whatwg-encoding: 3.1.1