forked from Lab-Lab-Lab/CPR-Music
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultitrackContext.js
More file actions
1370 lines (1218 loc) · 42.9 KB
/
MultitrackContext.js
File metadata and controls
1370 lines (1218 loc) · 42.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// contexts/MultitrackContext.js
'use client';
import {
createContext,
useContext,
useState,
useCallback,
useRef,
useEffect,
} from 'react';
import audioContextManager from '../components/audio/DAW/Multitrack/AudioContextManager';
import { createTransport } from '../components/audio/DAW/Multitrack/AudioEngine';
import RecordingManager from '../components/audio/DAW/Multitrack/recording/RecordingManager';
import { getDAWActivityLogger } from '../lib/activity/DAWActivityLogger';
const MultitrackContext = createContext();
export const useMultitrack = () => {
const context = useContext(MultitrackContext);
if (!context) {
throw new Error('useMultitrack must be used within MultitrackProvider');
}
return context;
};
export const MultitrackProvider = ({ children, initialTracks = [] }) => {
// Get activity logger instance
const activityLogger = getDAWActivityLogger();
// Track state - Initialize with provided tracks or empty array
const [tracks, setTracks] = useState(initialTracks);
const [selectedTrackId, setSelectedTrackId] = useState(null);
const [soloTrackId, setSoloTrackId] = useState(null);
// Multitrack playback state
const [isPlaying, setIsPlaying] = useState(false);
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0);
// Editor state
const [activeRegion, setActiveRegion] = useState(null);
const [editorTool, setEditorTool] = useState('select'); // 'select' | 'clip' | 'cut'
const [selectedClipId, setSelectedClipId] = useState(null);
const [selectedClipIds, setSelectedClipIds] = useState([]); // For multi-selection
const [snapEnabled, setSnapEnabled] = useState(true);
const [gridSizeSec, setGridSizeSec] = useState(0.1);
// Effects modal state
const [showEffectSelectionModal, setShowEffectSelectionModal] = useState(false);
const [showEffectParametersModal, setShowEffectParametersModal] = useState(false);
const [selectedEffectType, setSelectedEffectType] = useState(null);
const [effectTargetTrackId, setEffectTargetTrackId] = useState(null);
// Playback timer
const playbackTimerRef = useRef(null);
// Recording timer for MIDI recording
const recordingTimerRef = useRef(null);
const recordingStartTimeRef = useRef(0);
const recordingInitialTimeRef = useRef(0);
const currentTimeRef = useRef(currentTime);
// MIDI-specific refs
const trackInstrumentsRef = useRef({}); // Store instrument references for each track
const midiSchedulerRef = useRef(null); // For future MIDI scheduling
// Unified transport (single timebase for audio + MIDI)
const transportRef = useRef(null);
const trackPlayersRef = useRef({}); // trackId -> ClipPlayer instance
useEffect(() => {
// create a fresh transport when duration changes (so it clamps correctly)
transportRef.current = createTransport({
onTick: (t) => setCurrentTime(t),
getProjectDurationSec: () => duration || 0,
});
return () => {
try {
transportRef.current?.stop?.();
} catch {}
};
}, [duration]);
// Get live transport time directly from AudioContext
// This ensures components can sync with the exact same timing
const getTransportTime = useCallback(() => {
if (!transportRef.current) return 0;
// Access the transport's currentTime getter directly
return transportRef.current.currentTime || 0;
}, []);
// Check if any track is recording (using RecordingManager)
const isAnyTrackRecording = useCallback(() => {
return RecordingManager.hasActiveRecordings();
}, []);
// Check if specific track is recording
const isTrackRecording = useCallback((trackId) => {
return RecordingManager.isTrackRecording(trackId);
}, []);
// (removed tick and useEffect for old transport)
// Clear any stuck mixdown state on component mount and initialize RecordingManager
useEffect(() => {
if (typeof window !== 'undefined' && window.__MIXDOWN_ACTIVE__) {
window.__MIXDOWN_ACTIVE__ = false;
console.log('🔧 Startup: Cleared stuck mixdown state from previous session');
}
// Initialize RecordingManager with audio context
const audioContext = audioContextManager.getContext();
RecordingManager.initialize(audioContext);
console.log('🎙️ MultitrackContext: RecordingManager initialized');
// Subscribe to recording events to update track state
const handleRecordingComplete = (data) => {
console.log('🎙️ MultitrackContext: Recording complete', data);
// This will be handled by the individual track components
};
RecordingManager.on('audio-recording-complete', handleRecordingComplete);
RecordingManager.on('midi-recording-complete', handleRecordingComplete);
return () => {
RecordingManager.off('audio-recording-complete', handleRecordingComplete);
RecordingManager.off('midi-recording-complete', handleRecordingComplete);
RecordingManager.destroy();
};
}, []); // Run once on mount
// Debug: Log when instruments change (only in development and when non-empty)
useEffect(() => {
const instrumentIds = Object.keys(trackInstrumentsRef.current);
if (process.env.NODE_ENV === 'development' && instrumentIds.length > 0) {
console.log(
'🎻 Registered instruments:',
instrumentIds,
trackInstrumentsRef.current
);
}
}, [tracks]); // Re-log when tracks change
// (removed interval-based timer effect for updating current time)
// Update duration when tracks change (clip-aware)
useEffect(() => {
let maxDuration = 0;
// Seed per-track clip initializations when needed
const initClipsById = new Map();
// Pass 1: audio tracks — prefer clips to compute end; otherwise fallback to WS duration
tracks.forEach((track) => {
// If clips exist, compute end from clips
if (Array.isArray(track.clips) && track.clips.length > 0) {
const end = track.clips.reduce(
(m, c) => Math.max(m, (c.start || 0) + (c.duration || 0)),
0,
);
maxDuration = Math.max(maxDuration, end);
return;
}
// No clips yet: fallback to the raw audio duration from wavesurfer
if (track.wavesurferInstance) {
try {
const d = track.wavesurferInstance.getDuration() || 0;
maxDuration = Math.max(maxDuration, d);
if (d > 0 && track.type !== 'midi' && track.audioURL) {
// Schedule an initial single clip so we can render/manipulate non-destructively later
initClipsById.set(track.id, [
{
id: `clip-${track.id}`,
start: 0,
duration: d,
sourceDuration: d, // total buffer length for trim clamping
color: track.color || '#7bafd4',
src: track.audioURL,
offset: 0,
},
]);
}
} catch (e) {
// ignore
}
}
});
// Pass 2: MIDI tracks — notes are in seconds
tracks.forEach((track) => {
if (track.type === 'midi' && track.midiData?.notes?.length > 0) {
const lastSecond = track.midiData.notes.reduce((latest, note) => {
const noteEnd = note.startTime + note.duration; // seconds
return noteEnd > latest ? noteEnd : latest;
}, 0);
maxDuration = Math.max(maxDuration, lastSecond);
}
});
// Apply any clip initializations in one state update
if (initClipsById.size > 0) {
setTracks((prev) =>
prev.map((t) =>
initClipsById.has(t.id) &&
(!Array.isArray(t.clips) || t.clips.length === 0)
? { ...t, clips: initClipsById.get(t.id) }
: t,
),
);
}
console.log(`📐 MultitrackContext: Updating duration`, {
maxDuration,
trackCount: tracks.length,
clipsPerTrack: tracks.map(t => ({
id: t.id,
type: t.type,
clipCount: t.clips?.length || 0,
clipEnds: t.clips?.map(c => (c.start || 0) + (c.duration || 0)) || []
}))
});
setDuration(maxDuration);
}, [tracks]);
const registerTrackPlayer = useCallback((trackId, player) => {
trackPlayersRef.current[trackId] = player;
}, []);
const unregisterTrackPlayer = useCallback((trackId) => {
delete trackPlayersRef.current[trackId];
}, []);
// Track management
const addTrack = useCallback(
(trackData = {}) => {
const newTrack = {
id: Date.now(),
name: trackData.name || `Track ${tracks.length + 1}`,
type: trackData.type || 'audio', // 'audio' or 'midi'
audioURL: trackData.audioURL || null,
volume: trackData.volume || 1,
pan: trackData.pan || 0,
muted: trackData.muted || false,
solo: trackData.solo || false,
color: trackData.color || '#7bafd4',
wavesurferInstance: null, // Will be set by Track component
clips: trackData.clips || [], // Always initialize clips array
isRecording: false, // Explicitly ensure recording is false
armed: false, // Explicitly ensure not armed
// Preserve any additional properties passed in (except isRecording/armed)
...trackData,
isRecording: false, // Override any passed isRecording
armed: false, // Override any passed armed
};
console.log('MultitrackContext: Adding track:', newTrack);
setTracks((prev) => [...prev, newTrack]);
// Log track addition
try {
if (activityLogger?.isActive) {
activityLogger.logTrackAdded(newTrack.id, newTrack.type, newTrack.name);
}
} catch (error) {
console.error('📊 Error logging track addition:', error);
}
return newTrack;
},
[tracks.length],
);
const removeTrack = useCallback(
(trackId) => {
// Clean up instrument reference if it's a MIDI track
if (trackInstrumentsRef.current[trackId]) {
console.log('🗑️ Removing instrument for track:', trackId);
const instrument = trackInstrumentsRef.current[trackId];
instrument.stopAllNotes?.();
instrument.dispose?.();
delete trackInstrumentsRef.current[trackId];
}
// Get track type before deletion for logging
const trackToDelete = tracks.find(t => t.id === trackId);
setTracks((prev) => prev.filter((track) => track.id !== trackId));
if (selectedTrackId === trackId) {
setSelectedTrackId(null);
}
if (soloTrackId === trackId) {
setSoloTrackId(null);
}
// Log track deletion
try {
if (activityLogger?.isActive && trackToDelete) {
activityLogger.logTrackDeleted(trackId, trackToDelete.type);
}
} catch (error) {
console.error('📊 Error logging track deletion:', error);
}
},
[selectedTrackId, soloTrackId, tracks],
);
/**
* Update a single track by id.
*
* Usage:
* updateTrack(id, { muted: true });
* updateTrack(id, (t) => ({ muted: !t.muted }));
* updateTrack(id, { midiData: { tempo: 140 } }); // merges tempo without clobbering notes
* updateTrack(id, { midiData: newMidi }, { mergeMidiData: false }); // replace midiData wholesale
*
* - Accepts either a plain updates object or a function (track) => updates.
* - By default, performs a shallow merge AND a safe nested merge for `midiData`
* so you don't accidentally clobber notes/tempo when touching only one field.
* - Pass { mergeMidiData: false } to replace midiData wholesale.
*/
const updateTrack = useCallback((trackId, updatesOrFn, options = {}) => {
const { mergeMidiData = true } = options;
let wavesurferChanged = false;
console.log('🔄 updateTrack called:', {
trackId,
updates: typeof updatesOrFn === 'function' ? 'function' : updatesOrFn,
options
});
setTracks((prev) => {
const beforeTrack = prev.find(t => t.id === trackId);
console.log('🔄 Track before update:', {
id: beforeTrack?.id,
name: beforeTrack?.name,
clipCount: beforeTrack?.clips?.length,
clipIds: beforeTrack?.clips?.map(c => c.id)
});
const updated = prev.map((track) => {
if (track.id !== trackId) return track;
const updates =
typeof updatesOrFn === 'function'
? updatesOrFn(track)
: updatesOrFn || {};
console.log('🔄 Applying updates to track:', {
trackId: track.id,
updates,
oldClipCount: track.clips?.length,
newClipCount: updates.clips?.length
});
// Start with a shallow merge
let next = { ...track, ...updates };
// Optionally deep-merge midiData to avoid clobbering nested fields
if (
mergeMidiData &&
(updates?.midiData !== undefined || track.midiData !== undefined)
) {
next.midiData = {
...(track.midiData || {}),
...(updates?.midiData || {}),
};
}
if (
updates &&
Object.prototype.hasOwnProperty.call(updates, 'wavesurferInstance')
) {
wavesurferChanged = true;
}
console.log('🔄 Track after update:', {
id: next.id,
name: next.name,
clipCount: next.clips?.length,
clipIds: next.clips?.map(c => c.id)
});
return next;
});
return updated;
});
if (wavesurferChanged) {
// Force a re-render to update duration after wavesurfer instance changes
setTimeout(() => {
setTracks((prev) => [...prev]);
}, 100);
}
}, []);
const clearAllTracks = useCallback(() => {
// Stop any playing tracks first
tracks.forEach((track) => {
if (track.wavesurferInstance) {
try {
track.wavesurferInstance.pause();
track.wavesurferInstance.destroy();
} catch (e) {
console.warn('Error cleaning up track:', e);
}
}
});
// Clean up instruments properly
Object.values(trackInstrumentsRef.current).forEach((instrument) => {
if (instrument) {
instrument.stopAllNotes?.();
instrument.dispose?.();
}
});
// Clear instrument references
trackInstrumentsRef.current = {};
setTracks([]);
setSelectedTrackId(null);
setSoloTrackId(null);
setCurrentTime(0);
setIsPlaying(false);
setActiveRegion(null);
}, [tracks]);
// Playback control
const seek = useCallback(
(progress) => {
// Clamp progress
const p = Math.max(
0,
Math.min(1, typeof progress === 'number' ? progress : 0),
);
const projectDuration = duration || 0;
const targetSec = projectDuration > 0 ? p * projectDuration : 0;
// Seek all track players
Object.values(trackPlayersRef.current).forEach((player) => {
if (player) {
player.seek(targetSec);
}
});
// Update transport
try {
transportRef.current?.seek?.(targetSec);
} catch {}
// Update current time
setCurrentTime(targetSec);
},
[duration],
);
// --- Local pure helpers for clip editing (non-destructive) ---
function splitClipLocal(clip, at) {
const end = (clip.start || 0) + (clip.duration || 0);
if (at <= (clip.start || 0) || at >= end) return [clip, null];
const left = { ...clip, duration: at - (clip.start || 0) };
const right = {
...clip,
id: `${clip.id}-r-${Math.random().toString(36).slice(2, 7)}`,
start: at,
duration: end - at,
offset: (clip.offset || 0) + (at - (clip.start || 0)),
};
return [left, right];
}
function trimClipLocal(clip, newStart, newEnd) {
const s = Math.max(clip.start || 0, newStart);
const e = Math.min((clip.start || 0) + (clip.duration || 0), newEnd);
if (e <= s) return null;
const delta = s - (clip.start || 0);
return {
...clip,
start: s,
duration: e - s,
offset: (clip.offset || 0) + delta,
};
}
function rippleClipsLocal(clips, start, end) {
const cut = Math.max(0, end - start);
if (cut === 0) return clips.slice();
const out = [];
for (const c of clips) {
const cEnd = (c.start || 0) + (c.duration || 0);
if (cEnd <= start) {
out.push({ ...c });
continue;
}
if ((c.start || 0) >= end) {
out.push({ ...c, start: (c.start || 0) - cut });
continue;
}
const [left, right] = splitClipLocal(c, start);
if (left && left !== c) out.push(left);
else if ((c.start || 0) < start) {
const trimmed = trimClipLocal(c, c.start || 0, start);
if (trimmed) out.push(trimmed);
}
if (right) out.push({ ...right, start });
else if (cEnd > end) {
const trimmedRight = trimClipLocal(c, end, cEnd);
if (trimmedRight) out.push({ ...trimmedRight, start });
}
}
return out.sort((a, b) => (a.start || 0) - (b.start || 0));
}
// --- Playback control methods (play, pause, stop) ---
const play = useCallback(() => {
// Start transport from current time
try {
transportRef.current?.play?.(currentTime || 0);
} catch {}
// Start all track players
Object.entries(trackPlayersRef.current).forEach(([trackId, player]) => {
const track = tracks.find((t) => t.id === trackId);
if (!track) return;
// Handle solo/mute logic
const shouldPlay = soloTrackId ? track.id === soloTrackId : !track.muted;
if (shouldPlay && player) {
player.play(currentTime || 0);
}
});
setIsPlaying(true);
// Log play action for multitrack
try {
if (activityLogger?.isActive) {
activityLogger.logEvent('multitrack_play', {
trackCount: tracks.length,
currentTime
});
}
} catch (error) {
console.error('📊 Error logging multitrack play:', error);
}
}, [currentTime, tracks, soloTrackId]);
const pause = useCallback(() => {
// Pause transport
try {
transportRef.current?.pause?.();
} catch {}
// Pause all track players and get current position
let maxPosition = 0;
Object.values(trackPlayersRef.current).forEach((player) => {
if (player) {
const position = player.pause();
maxPosition = Math.max(maxPosition, position);
}
});
setIsPlaying(false);
setCurrentTime(maxPosition);
// Log pause action for multitrack
try {
if (activityLogger?.isActive) {
activityLogger.logEvent('multitrack_pause', {
trackCount: tracks.length,
currentTime: maxPosition
});
}
} catch (error) {
console.error('📊 Error logging multitrack pause:', error);
}
}, [tracks]);
// Ref for scroll reset callback (set by MultitrackEditor)
const scrollResetCallbackRef = useRef(null);
const stop = useCallback(() => {
// Stop transport
try {
transportRef.current?.stop?.();
} catch {}
// Stop all track players
Object.values(trackPlayersRef.current).forEach((player) => {
if (player) {
player.stop();
}
});
setIsPlaying(false);
setCurrentTime(0);
// Reset scroll position to beginning
if (scrollResetCallbackRef.current) {
scrollResetCallbackRef.current();
}
// Log stop action for multitrack
try {
if (activityLogger?.isActive) {
activityLogger.logEvent('multitrack_stop', {
trackCount: tracks.length
});
}
} catch (error) {
console.error('📊 Error logging multitrack stop:', error);
}
}, [tracks]);
// --- Non-destructive CLIP actions (audio + MIDI) ---
const splitAtPlayhead = useCallback(
(scope = 'selected') => {
const tSec = currentTime || 0;
setTracks((prev) =>
prev.map((track) => {
const inScope = scope === 'all' ? true : track.id === selectedTrackId;
if (
!inScope ||
!Array.isArray(track.clips) ||
track.clips.length === 0
)
return track;
let changed = false;
const out = [];
for (const c of track.clips) {
const cEnd = (c.start || 0) + (c.duration || 0);
if (tSec > (c.start || 0) && tSec < cEnd) {
const [left, right] = splitClipLocal(c, tSec);
if (left) out.push(left);
if (right) out.push(right);
changed = true;
} else {
out.push(c);
}
}
return changed ? { ...track, clips: out } : track;
}),
);
},
[currentTime, selectedTrackId],
);
const rippleDeleteSelection = useCallback(() => {
const region = activeRegion;
if (!region) return;
const rStart = Math.max(
0,
Math.min(
region.start ?? region.startTime ?? 0,
region.end ?? region.endTime ?? 0,
),
);
const rEnd = Math.max(
rStart,
region.end ?? region.endTime ?? region.start ?? region.startTime ?? 0,
);
const cut = Math.max(0, rEnd - rStart);
if (cut === 0) return;
try {
pause();
} catch {}
setTracks((prev) =>
prev.map((track) => {
if (track.type !== 'midi' && Array.isArray(track.clips)) {
const nextClips = rippleClipsLocal(track.clips, rStart, rEnd);
return { ...track, clips: nextClips };
}
if (track.type === 'midi' && track.midiData) {
const tempo = track.midiData.tempo || 120;
const spb = 60 / tempo;
const rs = rStart / spb;
const re = rEnd / spb;
const delta = re - rs;
const src = Array.isArray(track.midiData.notes)
? track.midiData.notes
: [];
const out = [];
for (const n of src) {
const s = n.startTime;
const e = n.startTime + n.duration;
if (e <= rs) {
out.push(n);
} else if (s >= re) {
out.push({ ...n, startTime: s - delta });
} else if (s < rs && e > re) {
const leftDur = Math.max(0, rs - s);
const rightDur = Math.max(0, e - re);
if (leftDur > 1e-6) out.push({ ...n, duration: leftDur });
if (rightDur > 1e-6)
out.push({ ...n, startTime: re - delta, duration: rightDur });
} else if (s < rs && e > rs && e <= re) {
const leftDur = Math.max(0, rs - s);
if (leftDur > 1e-6) out.push({ ...n, duration: leftDur });
} else if (s >= rs && s < re && e > re) {
const rightDur = Math.max(0, e - re);
if (rightDur > 1e-6)
out.push({ ...n, startTime: re - delta, duration: rightDur });
}
// fully inside region → dropped
}
return { ...track, midiData: { ...track.midiData, notes: out } };
}
return track;
}),
);
setActiveRegion(null);
setCurrentTime(rStart);
}, [activeRegion, pause, setTracks, setActiveRegion, setCurrentTime]);
// MIDI-specific methods
const registerTrackInstrument = useCallback((trackId, instrument) => {
console.log('📝 Context: registerTrackInstrument called', {
trackId,
hasInstrument: !!instrument,
});
if (instrument) {
trackInstrumentsRef.current[trackId] = instrument;
console.log('✅ Instrument registered for track:', trackId);
console.log(
'📊 Total registered instruments:',
Object.keys(trackInstrumentsRef.current).length,
);
} else {
delete trackInstrumentsRef.current[trackId];
console.log('❌ Instrument unregistered for track:', trackId);
}
}, []);
const playNoteOnSelectedTrack = useCallback(
(note, velocity = 0.8) => {
console.log('🎵 Context: playNoteOnSelectedTrack called', {
note,
velocity,
selectedTrackId,
availableInstruments: Object.keys(trackInstrumentsRef.current),
hasInstrumentForTrack: !!trackInstrumentsRef.current[selectedTrackId],
});
const selectedTrack = tracks.find(
(t) => t.id === selectedTrackId && t.type === 'midi',
);
console.log('🎵 Context: Selected track:', selectedTrack);
if (selectedTrack && trackInstrumentsRef.current[selectedTrackId]) {
console.log('🎵 Context: Found instrument, checking mute/solo state');
// Check if track is muted or if we're in solo mode
if (selectedTrack.muted) {
console.log('❌ Track is muted');
return null;
}
if (soloTrackId && selectedTrack.id !== soloTrackId) {
console.log('❌ Track is not soloed');
return null;
}
try {
console.log('🎵 Context: Calling instrument.playNote()');
const audioContext = audioContextManager.getContext();
const handle = trackInstrumentsRef.current[selectedTrackId].playNote(
note,
velocity,
audioContext.currentTime,
);
console.log('✅ Note played successfully', { handle });
return handle ?? null;
} catch (error) {
console.error('❌ Error playing note:', error);
return null;
}
} else {
console.log('❌ No instrument found for track', {
hasTrack: !!selectedTrack,
hasInstrument: !!trackInstrumentsRef.current[selectedTrackId],
trackType: selectedTrack?.type,
});
return null;
}
},
[selectedTrackId, tracks, soloTrackId],
);
// Get real-time current time for precise note placement
// Keep currentTime ref updated
useEffect(() => {
currentTimeRef.current = currentTime;
}, [currentTime]);
const getPreciseCurrentTime = useCallback(() => {
if (recordingTimerRef.current && recordingStartTimeRef.current) {
// If recording timer is active, use the same calculation as the recording timer
const now = performance.now() / 1000;
const elapsed = now - recordingStartTimeRef.current;
// Use the stored initial time from when recording started + precise elapsed time
return recordingInitialTimeRef.current + elapsed;
}
return currentTimeRef.current; // Use ref to avoid callback recreation
}, []); // No dependencies = stable callback
// Recording timer functions for MIDI recording
const startRecordingTimer = useCallback(() => {
if (recordingTimerRef.current) return; // Already running
recordingStartTimeRef.current = performance.now() / 1000;
const initialTime = currentTime;
recordingInitialTimeRef.current = initialTime; // Store for precise timing
const startTime = recordingStartTimeRef.current;
recordingTimerRef.current = requestAnimationFrame(function updateRecordingTime() {
// Check if timer was cancelled during frame
if (!recordingTimerRef.current || recordingStartTimeRef.current !== startTime) {
return; // Timer was cancelled, don't continue
}
try {
const now = performance.now() / 1000;
const elapsed = now - recordingStartTimeRef.current;
const newTime = initialTime + elapsed;
// Only log in development
if (process.env.NODE_ENV === 'development') {
console.log(`⏱️ RECORDING TIMER:`, {
elapsed: elapsed.toFixed(3),
initialTime: initialTime.toFixed(3),
newTime: newTime.toFixed(3),
performanceNow: now.toFixed(3),
});
}
setCurrentTime(newTime);
// Schedule next frame if still active
if (recordingTimerRef.current) {
recordingTimerRef.current = requestAnimationFrame(updateRecordingTime);
}
} catch (error) {
console.error('Recording timer error:', error);
// Clean up on error
if (recordingTimerRef.current) {
cancelAnimationFrame(recordingTimerRef.current);
recordingTimerRef.current = null;
}
}
});
}, [currentTime]);
const stopRecordingTimer = useCallback(() => {
if (recordingTimerRef.current) {
cancelAnimationFrame(recordingTimerRef.current);
recordingTimerRef.current = null;
}
}, []);
// Emergency reset function to clear stuck mixdown state
const resetMixdownState = useCallback(() => {
if (typeof window !== 'undefined') {
window.__MIXDOWN_ACTIVE__ = false;
console.log('🔧 Emergency: Cleared stuck mixdown state');
}
}, []);
// Note batching for smoother recording performance
const noteBufferRef = useRef([]);
const noteBufferTimerRef = useRef(null);
const addNoteToSelectedTrack = useCallback(
(note, velocity01, startBeat, durationBeats) => {
if (!selectedTrackId) return;
const newNote = {
note,
velocity: Math.max(
1,
Math.min(127, Math.round((velocity01 ?? 0.8) * 127)),
),
startTime: startBeat,
duration: durationBeats,
};
// Add to buffer instead of immediate state update
noteBufferRef.current.push(newNote);
// Debounce: flush buffer after 150ms of inactivity
if (noteBufferTimerRef.current) {
clearTimeout(noteBufferTimerRef.current);
}
noteBufferTimerRef.current = setTimeout(() => {
const notesToAdd = [...noteBufferRef.current];
noteBufferRef.current = [];
if (notesToAdd.length > 0) {
setTracks((prev) =>
prev.map((t) => {
if (t.id !== selectedTrackId || t.type !== 'midi') return t;
const notes = Array.isArray(t.midiData?.notes)
? t.midiData.notes
: [];
return {
...t,
midiData: {
...(t.midiData || {}),
notes: [...notes, ...notesToAdd] // Batch add all buffered notes
},
};
}),
);
}
}, 150);
},
[selectedTrackId, setTracks],
);
// Flush buffer immediately when stopping recording
const flushNoteBuffer = useCallback(() => {
if (noteBufferTimerRef.current) {
clearTimeout(noteBufferTimerRef.current);
}
const notesToAdd = [...noteBufferRef.current];
noteBufferRef.current = [];
if (notesToAdd.length > 0 && selectedTrackId) {
setTracks((prev) =>
prev.map((t) => {
if (t.id !== selectedTrackId || t.type !== 'midi') return t;
const notes = Array.isArray(t.midiData?.notes)
? t.midiData.notes
: [];
return {
...t,
midiData: {
...(t.midiData || {}),
notes: [...notes, ...notesToAdd]
},
};
}),
);
}
}, [selectedTrackId, setTracks]);
const stopNoteOnSelectedTrack = useCallback(
(note, token = null) => {
console.log('🎵 Context: stopNoteOnSelectedTrack called', {
note,
token,
selectedTrackId,
});
const selectedTrack = tracks.find(
(t) => t.id === selectedTrackId && t.type === 'midi',
);
if (selectedTrack && trackInstrumentsRef.current[selectedTrackId]) {
try {
const instrument = trackInstrumentsRef.current[selectedTrackId];
if (!instrument || typeof instrument.stopNote !== 'function') return;
const audioContext = audioContextManager.getContext();
// Always provide a second arg: token if available, otherwise current time.
// Extra args are safe in JS; this guarantees preview handles are used when supported.
const secondArg = token ?? audioContext.currentTime;
instrument.stopNote(note, secondArg);
console.log('✅ Note stopped successfully', { token });
} catch (error) {
console.error('❌ Error stopping note:', error);
}
}
},
[selectedTrackId, tracks],
);
// --- Region editing helpers (audio + MIDI) ---
function audioBufferToWav(buffer) {
const length = buffer.length * buffer.numberOfChannels * 2 + 44;
const arrayBuffer = new ArrayBuffer(length);
const view = new DataView(arrayBuffer);
let pos = 0;
const setUint16 = (d) => {
view.setUint16(pos, d, true);
pos += 2;
};
const setUint32 = (d) => {
view.setUint32(pos, d, true);
pos += 4;
};