11import { useEffect , useMemo , useRef } from "react" ;
22import { usePlayerStore } from "../player/store/playerStore" ;
3- import { isMusicTrack } from "../utils/timelineInspector" ;
3+ import { resolveBeatSourceTrack } from "../utils/timelineInspector" ;
44import { analyzeMusicFromUrl } from "@hyperframes/core/beats" ;
55import { useFileManagerContextOptional } from "../contexts/FileManagerContext" ;
66import { mergeUserBeats } from "../utils/beatEditing" ;
@@ -52,6 +52,44 @@ async function resolveBeats(
5252 return { ...detected , hasFile : false } ;
5353}
5454
55+ /** True when the beats file for a track exists and holds at least one beat. */
56+ async function readHasSavedBeats ( io : ProjectIo , beatPath : string ) : Promise < boolean > {
57+ try {
58+ const content = await io . readOptionalProjectFile ( beatPath ) ;
59+ const parsed = content ? parseBeats ( content ) : null ;
60+ return ! ! ( parsed && parsed . times . length > 0 ) ;
61+ } catch {
62+ return false ;
63+ }
64+ }
65+
66+ type MusicAnalysis = Awaited < ReturnType < typeof analyzeMusicFromUrl > > ;
67+
68+ /**
69+ * Analyze a track (memoized per URL) and fold in any saved beat edits. Null on
70+ * decode/analysis failure — the caller then clears state and drops the cache
71+ * entry (only when the effect is still live).
72+ */
73+ async function loadBeatAnalysis (
74+ musicSrc : string ,
75+ beatPath : string ,
76+ io : ProjectIo ,
77+ ) : Promise < { analysis : MusicAnalysis ; times : number [ ] ; strengths : number [ ] } | null > {
78+ let promise = analysisCache . get ( musicSrc ) ;
79+ if ( ! promise ) {
80+ promise = analyzeMusicFromUrl ( musicSrc ) ;
81+ cacheAnalysis ( musicSrc , promise ) ;
82+ }
83+ try {
84+ const analysis = await promise ;
85+ const detected = { times : analysis . beatTimes , strengths : analysis . beatStrengths } ;
86+ const { times, strengths } = await resolveBeats ( beatPath , detected , io ) ;
87+ return { analysis, times, strengths } ;
88+ } catch {
89+ return null ;
90+ }
91+ }
92+
5593export function useMusicBeatAnalysis ( ) : void {
5694 const elements = usePlayerStore ( ( s ) => s . elements ) ;
5795 const setBeatAnalysis = usePlayerStore ( ( s ) => s . setBeatAnalysis ) ;
@@ -71,9 +109,12 @@ export function useMusicBeatAnalysis(): void {
71109 ? { readOptionalProjectFile, writeProjectFile }
72110 : null ;
73111
74- const musicSrc = useMemo ( ( ) => {
75- const el = elements . find ( ( e ) => isMusicTrack ( e ) ) ;
76- return el ?. src ?? null ;
112+ const { musicSrc, isFallbackTrack } = useMemo ( ( ) => {
113+ const resolved = resolveBeatSourceTrack ( elements ) ;
114+ return {
115+ musicSrc : resolved ?. element . src ?? null ,
116+ isFallbackTrack : resolved ?. isFallback ?? false ,
117+ } ;
77118 } , [ elements ] ) ;
78119
79120 // ── Load: decode for strength data, then use the saved beat file if present,
@@ -95,50 +136,45 @@ export function useMusicBeatAnalysis(): void {
95136 const beatPath = beatFilePathForSrc ( musicSrc ) ;
96137 const io = ioRef . current ;
97138
98- // Only run expensive audio decode + beat analysis when the user has an
99- // explicit beats file saved. Without one, skip entirely — no surprise
100- // green lines on the timeline after dragging unrelated assets.
139+ // For explicitly tagged/named music tracks: only run expensive audio decode
140+ // + beat analysis when the user has an explicit beats file saved. Without
141+ // one, skip entirely — no surprise green lines on the timeline after
142+ // dragging unrelated assets.
143+ //
144+ // For fallback tracks (audio dropped from Finder with no role/music-id):
145+ // always run analysis so the Beat tool becomes usable immediately.
101146 ( async ( ) => {
102147 if ( ! beatPath || ! io ) return ;
103- let hasSavedBeats = false ;
104- try {
105- const content = await io . readOptionalProjectFile ( beatPath ) ;
106- const parsed = content ? parseBeats ( content ) : null ;
107- hasSavedBeats = ! ! ( parsed && parsed . times . length > 0 ) ;
108- } catch {
109- /* no file */
148+ if ( ! isFallbackTrack ) {
149+ const hasSavedBeats = await readHasSavedBeats ( io , beatPath ) ;
150+ if ( cancelled ) return ;
151+ if ( ! hasSavedBeats ) {
152+ setBeatAnalysis ( null ) ;
153+ return ;
154+ }
110155 }
111156 if ( cancelled ) return ;
112- if ( ! hasSavedBeats ) {
157+
158+ const result = await loadBeatAnalysis ( musicSrc , beatPath , io ) ;
159+ if ( cancelled ) return ;
160+ if ( ! result ) {
113161 setBeatAnalysis ( null ) ;
162+ analysisCache . delete ( musicSrc ) ;
114163 return ;
115164 }
116-
117- let promise = analysisCache . get ( musicSrc ) ;
118- if ( ! promise ) {
119- promise = analyzeMusicFromUrl ( musicSrc ) ;
120- cacheAnalysis ( musicSrc , promise ) ;
121- }
122- try {
123- const analysis = await promise ;
124- const detected = { times : analysis . beatTimes , strengths : analysis . beatStrengths } ;
125- const { times, strengths } = await resolveBeats ( beatPath , detected , io ) ;
126- if ( cancelled ) return ;
127- setBeatEdits ( null ) ;
128- resetBeatHistory ( ) ;
129- setBeatAnalysis ( { ...analysis , beatTimes : times , beatStrengths : strengths } ) ;
130- } catch {
131- if ( ! cancelled ) {
132- setBeatAnalysis ( null ) ;
133- analysisCache . delete ( musicSrc ) ;
134- }
135- }
165+ setBeatEdits ( null ) ;
166+ resetBeatHistory ( ) ;
167+ setBeatAnalysis ( {
168+ ...result . analysis ,
169+ beatTimes : result . times ,
170+ beatStrengths : result . strengths ,
171+ } ) ;
136172 } ) ( ) ;
137173
138174 return ( ) => {
139175 cancelled = true ;
140176 } ;
141- } , [ musicSrc , setBeatAnalysis , setBeatEdits , resetBeatHistory ] ) ;
177+ } , [ musicSrc , isFallbackTrack , setBeatAnalysis , setBeatEdits , resetBeatHistory ] ) ;
142178
143179 // ── Persist: register a debounced writer fired by every beat edit/undo/redo.
144180 // Flushes any pending write on cleanup so the last edit is never lost. ──
0 commit comments