-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransport.tsx
More file actions
474 lines (447 loc) · 18.1 KB
/
Transport.tsx
File metadata and controls
474 lines (447 loc) · 18.1 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
import { useState, useCallback } from 'react';
import type { EffectsState, ScaleState } from '../types';
import { DEFAULT_EFFECTS_STATE } from '../audio/toneEffects';
import { DELAY_TIME_OPTIONS } from '../audio/delay-constants';
import { audioEngine } from '../audio/engine';
import { applyEffectToEngine } from '../audio/effects-util';
import { XYPad } from './XYPad';
import { ScaleSelector } from './ScaleSelector';
import { DEFAULT_SCALE_STATE } from '../state/grid';
import { useSyncExternalState, useSyncExternalStateWithSideEffect } from '../hooks/useSyncExternalState';
import './Transport.css';
interface TransportProps {
isPlaying: boolean;
tempo: number;
swing: number;
onPlayPause: () => void;
onTempoChange: (tempo: number) => void;
onSwingChange: (swing: number) => void;
// Effects props for integrated FX panel
effectsState?: EffectsState;
onEffectsChange?: (effects: EffectsState) => void;
effectsDisabled?: boolean;
// Scale props for Key Assistant (Phase 29E)
scaleState?: ScaleState;
onScaleChange?: (scale: ScaleState) => void;
// Phase 31A: Beat pulse for metronome visual
beatPulse?: boolean;
beatPulseDuration?: number; // Duration in ms, proportional to tempo
// Phase 31D: Unmute all
onUnmuteAll?: () => void;
mutedTrackCount?: number;
// Phase 31I: Mixer panel toggle
onToggleMixer?: () => void;
isMixerOpen?: boolean;
// Phase 31 TCG: Badge indicator when any track volume is adjusted
hasAdjustedVolumes?: boolean;
// Phase 31: Primary Action Button Pattern - Play is primary when stopped with tracks
hasTracks?: boolean;
// Phase 31H: Pitch overview panel toggle
onTogglePitch?: () => void;
isPitchOpen?: boolean;
hasMelodicTracks?: boolean;
}
export function Transport({
isPlaying,
tempo,
swing,
onPlayPause,
onTempoChange,
onSwingChange,
effectsState,
onEffectsChange,
effectsDisabled = false,
scaleState,
onScaleChange,
beatPulse = false,
beatPulseDuration = 100,
onUnmuteAll,
mutedTrackCount = 0,
onToggleMixer,
isMixerOpen = false,
hasAdjustedVolumes = false,
hasTracks = false,
onTogglePitch,
isPitchOpen = false,
hasMelodicTracks = false,
}: TransportProps) {
const [fxExpanded, setFxExpanded] = useState(false);
// Sync effects state from props with side effect to apply to audio engine
const [effects, setEffects] = useSyncExternalStateWithSideEffect<EffectsState>(
effectsState,
{ ...DEFAULT_EFFECTS_STATE },
(state) => {
// Apply to audio engine if Tone.js is initialized
if (audioEngine.isToneInitialized()) {
audioEngine.applyEffectsState(state);
}
}
);
// Sync scale state from external sources (multiplayer, session load)
const [scale, setScale] = useSyncExternalState<ScaleState>(
scaleState,
{ ...DEFAULT_SCALE_STATE }
);
// Handle scale change - syncs to server
const handleScaleChange = useCallback((newScale: ScaleState) => {
setScale(newScale);
onScaleChange?.(newScale);
}, [onScaleChange, setScale]);
// Check if any effects are active
const hasActiveEffects =
effects.reverb.wet > 0 ||
effects.delay.wet > 0 ||
effects.chorus.wet > 0 ||
effects.distortion.wet > 0;
// Update a single effect parameter - syncs to server immediately
// Excludes 'bypass' which is boolean, not an object with params
const updateEffect = useCallback(<K extends Exclude<keyof EffectsState, 'bypass'>>(
effectName: K,
param: keyof EffectsState[K],
value: number | string
) => {
// Compute new effects state
const currentEffect = effects[effectName] as Record<string, unknown>;
const newEffects = {
...effects,
[effectName]: {
...currentEffect,
[param]: value,
},
};
setEffects(newEffects);
applyEffectToEngine(effectName, param, value);
onEffectsChange?.(newEffects); // Sync to server immediately (like toggleBypass)
}, [effects, onEffectsChange, setEffects]);
// Toggle effects bypass (mutes all effects without losing settings)
// Bypass is synced across multiplayer - everyone hears the same music
const toggleBypass = useCallback(() => {
const newBypassed = !(effects.bypass ?? false);
const newEffects = { ...effects, bypass: newBypassed };
setEffects(newEffects);
audioEngine.setEffectsEnabled(!newBypassed);
onEffectsChange?.(newEffects); // Sync to server
}, [effects, onEffectsChange, setEffects]);
// XY Pad handler for reverb (X = wet, Y = decay normalized)
// Batches both updates into single state change to avoid stale closure issue
// (calling updateEffect twice would cause second call to overwrite first)
const handleReverbXY = useCallback((x: number, y: number) => {
// X = wet (0-1)
// Y = decay (0.1-10, mapped from 0-1)
const decay = 0.1 + y * 9.9; // 0.1 to 10
// Build complete new state with both values
const newEffects = {
...effects,
reverb: {
...effects.reverb,
wet: x,
decay: decay,
},
};
// Single state update, single server sync
setEffects(newEffects);
applyEffectToEngine('reverb', 'wet', x);
applyEffectToEngine('reverb', 'decay', decay);
onEffectsChange?.(newEffects);
}, [effects, onEffectsChange, setEffects]);
return (
<div className={`transport ${fxExpanded ? 'fx-expanded' : ''}`}>
{/* Top row: playback controls and FX toggle */}
<div className="transport-controls">
<button
className={`play-button ${isPlaying ? 'playing' : ''} ${beatPulse ? 'beat-pulse' : ''} ${hasTracks && !isPlaying ? 'primary-action' : ''}`}
onClick={onPlayPause}
data-testid="play-button"
title={isPlaying ? 'Stop (Space)' : 'Play (Space)'}
aria-label={isPlaying ? 'Stop' : 'Play'}
style={{ '--beat-pulse-duration': `${beatPulseDuration}ms` } as React.CSSProperties}
>
{isPlaying ? '■' : '▶'}
</button>
<div className="tempo-control" title="Tempo in beats per minute">
<label htmlFor="tempo">BPM</label>
<input
id="tempo"
type="range"
min="60"
max="180"
value={tempo}
onChange={(e) => onTempoChange(Number(e.target.value))}
/>
<span className="tempo-value">{tempo}</span>
</div>
<div className="swing-control" title="Swing feel: 0% = straight, higher = shuffle">
<label htmlFor="swing">Swing</label>
<input
id="swing"
type="range"
min="0"
max="100"
value={swing}
onChange={(e) => onSwingChange(Number(e.target.value))}
/>
<span className="swing-value">{swing}%</span>
</div>
{/* Scale selector - Phase 29E Key Assistant */}
<ScaleSelector
scale={scale}
onScaleChange={handleScaleChange}
disabled={effectsDisabled}
/>
{/* Transport control group: Unmute, FX, Mixer - unified styling */}
<div className="transport-control-group">
{/* Unmute All button - always visible, enabled when tracks muted */}
{onUnmuteAll && (
<button
className={`control-group-btn unmute-btn ${mutedTrackCount > 0 ? 'has-muted' : ''}`}
onClick={onUnmuteAll}
disabled={mutedTrackCount === 0}
title={mutedTrackCount > 0 ? `Unmute all tracks (⌘⇧M)` : 'No tracks muted'}
aria-label={mutedTrackCount > 0 ? `Unmute all ${mutedTrackCount} muted tracks` : 'Unmute all (no tracks muted)'}
>
<span className="btn-label">Unmute all</span>
{mutedTrackCount > 0 && <span className="btn-badge">{mutedTrackCount}</span>}
</button>
)}
{/* FX button - simple panel toggle (bypass control moved inside panel) */}
<button
className={`control-group-btn fx-btn ${hasActiveEffects ? 'has-effects' : ''} ${effects.bypass ? 'bypassed' : ''} ${fxExpanded ? 'expanded' : ''}`}
onClick={() => setFxExpanded(!fxExpanded)}
disabled={effectsDisabled}
title={fxExpanded ? 'Close effects panel' : 'Open effects panel'}
aria-label={fxExpanded ? 'Close effects panel' : 'Open effects panel'}
aria-expanded={fxExpanded}
>
<span className="btn-label">FX</span>
{hasActiveEffects && (
<span className={`btn-badge ${effects.bypass ? 'bypassed' : ''}`}>
{effects.bypass ? '⊗' : '●'}
</span>
)}
</button>
{/* Mixer panel toggle */}
{onToggleMixer && (
<button
className={`control-group-btn mixer-btn ${isMixerOpen ? 'active' : ''} ${hasAdjustedVolumes ? 'has-adjustments' : ''}`}
onClick={onToggleMixer}
title={isMixerOpen ? 'Close mixer (return to pattern view)' : 'Open mixer (all volumes)'}
aria-label={isMixerOpen ? 'Close mixer' : 'Open mixer'}
aria-pressed={isMixerOpen}
>
<span className="btn-label">Mixer</span>
</button>
)}
{/* Phase 31H: Pitch overview panel toggle */}
{onTogglePitch && hasMelodicTracks && (
<button
className={`control-group-btn pitch-btn ${isPitchOpen ? 'active' : ''}`}
onClick={onTogglePitch}
title={isPitchOpen ? 'Close pitch overview' : 'Open pitch overview (chord detection, pitch range)'}
aria-label={isPitchOpen ? 'Close pitch overview' : 'Open pitch overview'}
aria-pressed={isPitchOpen}
>
<span className="btn-label">Pitch</span>
</button>
)}
</div>
</div>
{/* Effects panel - expands below controls, pushes content down */}
<div className={`transport-fx-panel ${fxExpanded ? 'expanded' : ''}`}>
<div className="fx-panel-content">
{/* Header row with title and Master control - matches Mixer/Pitch Overview */}
<div className="fx-header">
<h2 className="fx-title">FX</h2>
<button
className={`fx-master-toggle ${effects.bypass ? 'bypassed' : ''}`}
onClick={toggleBypass}
disabled={effectsDisabled || !hasActiveEffects}
title={effects.bypass ? 'Enable all effects' : 'Bypass all effects'}
aria-pressed={!effects.bypass}
>
<span className="master-indicator">{effects.bypass ? '⊗' : '●'}</span>
<span className="master-label">{effects.bypass ? 'Bypassed' : 'Active'}</span>
</button>
</div>
{/* Effect groups in a 4-column grid */}
<div className="fx-groups">
{/* Reverb */}
<div className="fx-group" title="Reverb adds space and depth to your sound">
<span className="fx-label">Reverb</span>
<div className="fx-controls fx-controls-with-xy">
<XYPad
x={effects.reverb.wet}
y={(effects.reverb.decay - 0.1) / 9.9}
onChange={handleReverbXY}
xLabel="Mix"
yLabel="Decay"
size={120}
disabled={effectsDisabled}
color="#9c27b0"
/>
<div className="fx-sliders">
<div className="fx-param">
<label htmlFor="transport-reverb-mix">Mix</label>
<input
id="transport-reverb-mix"
type="range"
min="0"
max="1"
step="0.01"
value={effects.reverb.wet}
onChange={(e) => updateEffect('reverb', 'wet', parseFloat(e.target.value))}
disabled={effectsDisabled}
/>
<span className="fx-value">{Math.round(effects.reverb.wet * 100)}%</span>
</div>
<div className="fx-param">
<label htmlFor="transport-reverb-decay">Decay</label>
<input
id="transport-reverb-decay"
type="range"
min="0.1"
max="10"
step="0.1"
value={effects.reverb.decay}
onChange={(e) => updateEffect('reverb', 'decay', parseFloat(e.target.value))}
disabled={effectsDisabled}
/>
<span className="fx-value">{effects.reverb.decay.toFixed(1)}s</span>
</div>
</div>
</div>
</div>
{/* Delay */}
<div className="fx-group" title="Delay creates echoes synced to the tempo">
<span className="fx-label">Delay</span>
<div className="fx-controls">
<div className="fx-param">
<label htmlFor="transport-delay-mix">Mix</label>
<input
id="transport-delay-mix"
type="range"
min="0"
max="1"
step="0.01"
value={effects.delay.wet}
onChange={(e) => updateEffect('delay', 'wet', parseFloat(e.target.value))}
disabled={effectsDisabled}
/>
<span className="fx-value">{Math.round(effects.delay.wet * 100)}%</span>
</div>
<div className="fx-param">
<label htmlFor="transport-delay-time">Time</label>
<select
id="transport-delay-time"
value={effects.delay.time}
onChange={(e) => updateEffect('delay', 'time', e.target.value)}
disabled={effectsDisabled}
>
{DELAY_TIME_OPTIONS.map(opt => (
<option key={opt.value} value={opt.value}>{opt.label}</option>
))}
</select>
</div>
<div className="fx-param">
<label htmlFor="transport-delay-feedback">Feedback</label>
<input
id="transport-delay-feedback"
type="range"
min="0"
max="0.95"
step="0.01"
value={effects.delay.feedback}
onChange={(e) => updateEffect('delay', 'feedback', parseFloat(e.target.value))}
disabled={effectsDisabled}
/>
<span className="fx-value">{Math.round(effects.delay.feedback * 100)}%</span>
</div>
</div>
</div>
{/* Chorus */}
<div className="fx-group" title="Chorus adds width and movement">
<span className="fx-label">Chorus</span>
<div className="fx-controls">
<div className="fx-param">
<label htmlFor="transport-chorus-mix">Mix</label>
<input
id="transport-chorus-mix"
type="range"
min="0"
max="1"
step="0.01"
value={effects.chorus.wet}
onChange={(e) => updateEffect('chorus', 'wet', parseFloat(e.target.value))}
disabled={effectsDisabled}
/>
<span className="fx-value">{Math.round(effects.chorus.wet * 100)}%</span>
</div>
<div className="fx-param">
<label htmlFor="transport-chorus-rate">Rate</label>
<input
id="transport-chorus-rate"
type="range"
min="0.1"
max="10"
step="0.1"
value={effects.chorus.frequency}
onChange={(e) => updateEffect('chorus', 'frequency', parseFloat(e.target.value))}
disabled={effectsDisabled}
/>
<span className="fx-value">{effects.chorus.frequency.toFixed(1)}Hz</span>
</div>
<div className="fx-param">
<label htmlFor="transport-chorus-depth">Depth</label>
<input
id="transport-chorus-depth"
type="range"
min="0"
max="1"
step="0.01"
value={effects.chorus.depth}
onChange={(e) => updateEffect('chorus', 'depth', parseFloat(e.target.value))}
disabled={effectsDisabled}
/>
<span className="fx-value">{Math.round(effects.chorus.depth * 100)}%</span>
</div>
</div>
</div>
{/* Distortion */}
<div className="fx-group" title="Distortion adds grit and edge">
<span className="fx-label">Distortion</span>
<div className="fx-controls">
<div className="fx-param">
<label htmlFor="transport-distortion-mix">Mix</label>
<input
id="transport-distortion-mix"
type="range"
min="0"
max="1"
step="0.01"
value={effects.distortion.wet}
onChange={(e) => updateEffect('distortion', 'wet', parseFloat(e.target.value))}
disabled={effectsDisabled}
/>
<span className="fx-value">{Math.round(effects.distortion.wet * 100)}%</span>
</div>
<div className="fx-param">
<label htmlFor="transport-distortion-drive">Drive</label>
<input
id="transport-distortion-drive"
type="range"
min="0"
max="1"
step="0.01"
value={effects.distortion.amount}
onChange={(e) => updateEffect('distortion', 'amount', parseFloat(e.target.value))}
disabled={effectsDisabled}
/>
<span className="fx-value">{Math.round(effects.distortion.amount * 100)}%</span>
</div>
</div>
</div>
</div>{/* Close fx-groups */}
</div>
</div>
</div>
);
}