@@ -20,7 +20,7 @@ import type { StereoLevel } from "@hyperframes/core/runtime/levelTap";
2020import { usePlayerStore } from "../../player" ;
2121import { clampNumber } from "../../utils/studioHelpers" ;
2222import { useAudioMetersVisible } from "../../utils/audioMeterVisibility" ;
23- import { useStudioShellContext } from "../../contexts/StudioContext" ;
23+ import { useStudioShellContextOptional } from "../../contexts/StudioContext" ;
2424import { useTimelineEditContextOptional } from "../../contexts/TimelineEditContext" ;
2525import {
2626 METER_DB_MARKS ,
@@ -107,7 +107,7 @@ function useVolumeHandlers(): {
107107
108108type PreviewWindow = ( Window & { __hf ?: { audioMeter ?: AudioMeterHook } } ) | null | undefined ;
109109
110- function readHook ( iframe : HTMLIFrameElement | null ) : AudioMeterHook | null {
110+ function readHook ( iframe : HTMLIFrameElement | null | undefined ) : AudioMeterHook | null {
111111 try {
112112 return ( iframe ?. contentWindow as PreviewWindow ) ?. __hf ?. audioMeter ?? null ;
113113 } catch {
@@ -123,9 +123,8 @@ function paintPeak(el: HTMLElement | null, peak: number): void {
123123 el . style . setProperty ( "transform" , peak >= 1 ? "translateY(1px)" : "none" ) ;
124124}
125125
126- /** The fill is a fixed green/amber/red backdrop; painting only moves the dark
127- * mask that covers the unlit top portion, so a loud peak lights the real red
128- * band instead of tinting a flat colour brighter. */
126+ /** The fill is a fixed green/amber/red backdrop; painting only moves the dark mask that covers the unlit top
127+ * portion, so a loud peak lights the real red band instead of tinting a flat colour brighter. */
129128function paint ( bars : StripBars | undefined , channels : Pair ) : void {
130129 channels . forEach ( ( ch , i ) => {
131130 bars ?. [ i ] ?. mask ?. style . setProperty ( "height" , `${ ( 1 - ch . level ) * 100 } %` ) ;
@@ -179,8 +178,13 @@ export function stepAndPaintStrips(
179178}
180179
181180/** One rAF loop re-reads the hook off the live preview window, so a reloaded iframe is followed. */
182- function useMeterLoop ( strips : Strip [ ] , bars : RefObject < Map < string | null , StripBars > > ) {
183- const { previewIframeRef } = useStudioShellContext ( ) ;
181+ function useMeterLoop (
182+ strips : Strip [ ] ,
183+ bars : RefObject < Map < string | null , StripBars > > ,
184+ iframeRef : RefObject < HTMLIFrameElement | null > | undefined ,
185+ ) {
186+ const shell = useStudioShellContextOptional ( ) ;
187+ const previewIframeRef = iframeRef ?? shell ?. previewIframeRef ;
184188 const stripsRef = useRef ( strips ) ;
185189 stripsRef . current = strips ;
186190 useEffect ( ( ) => {
@@ -190,7 +194,7 @@ function useMeterLoop(strips: Strip[], bars: RefObject<Map<string | null, StripB
190194 const state = new Map < string | null , Pair > ( ) ;
191195 const tick = ( now : number ) => {
192196 raf = requestAnimationFrame ( tick ) ;
193- active = followMeterHook ( active , readHook ( previewIframeRef . current ) ) ;
197+ active = followMeterHook ( active , readHook ( previewIframeRef ? .current ) ) ;
194198 const levels = active ?. read ( ) ;
195199 const dt = now - last ;
196200 last = now ;
@@ -395,22 +399,29 @@ function MeterStrip({
395399 ) ;
396400}
397401
398- export const AudioMeterStrip = memo ( function AudioMeterStrip ( ) {
402+ export interface AudioMeterStripProps {
403+ /** Pass a stable ref (useRef): a new object each render restarts the meter loop. */
404+ previewIframeRef ?: RefObject < HTMLIFrameElement | null > ;
405+ }
406+
407+ export const AudioMeterStrip = memo ( function AudioMeterStrip ( {
408+ previewIframeRef,
409+ } : AudioMeterStripProps ) {
399410 const visible = useAudioMetersVisible ( ( s ) => s . visible ) ;
400411 const projectHasAudio = useProjectHasAudio ( ) ;
401412 if ( ! visible || ! projectHasAudio ) return null ;
402- return < MeterStripBody /> ;
413+ return < MeterStripBody previewIframeRef = { previewIframeRef } /> ;
403414} ) ;
404415
405- function MeterStripBody ( ) {
416+ function MeterStripBody ( { previewIframeRef } : AudioMeterStripProps ) {
406417 const strips = useStrips ( ) ;
407418 const bars = useRef ( new Map < string | null , StripBars > ( ) ) ;
408419 const register = useRef ( ( id : string | null , b : StripBars | null ) => {
409420 if ( b ) bars . current . set ( id , b ) ;
410421 else bars . current . delete ( id ) ;
411422 } ) . current ;
412423 const { onLive, onCommit } = useVolumeHandlers ( ) ;
413- useMeterLoop ( strips , bars ) ;
424+ useMeterLoop ( strips , bars , previewIframeRef ) ;
414425 return (
415426 < div
416427 data-testid = "audio-meter-strip"
0 commit comments