Build BeatHound sequencer app
This commit is contained in:
1 parent
226ababcf1
commit
e23825cdb1
49 files changed
+6455
-2
No files matched your search
@@ -0,0 +1,218 @@
|
||||
import { useRef } from 'react';
|
||||
import type { KeyboardEvent as ReactKeyboardEvent, PointerEvent as ReactPointerEvent } from 'react';
|
||||
import type { BeatPoint } from '../domain/sequencer';
|
||||
import {
|
||||
PLAYBACK_SUBSTEPS_PER_STEP,
|
||||
STEPS_PER_BEAT,
|
||||
} from '../domain/sequencer';
|
||||
import {
|
||||
getPointForStep,
|
||||
getLoopPositionPercent,
|
||||
getSoundOrbitRadius,
|
||||
getStepFromPosition,
|
||||
ORBIT_RADIUS,
|
||||
SVG_CENTER,
|
||||
SVG_SIZE,
|
||||
getStackedPosition,
|
||||
wrapStepPosition,
|
||||
} from '../domain/timeline';
|
||||
import { SOUND_BY_ID } from '../domain/sounds';
|
||||
import type { SoundId } from '../domain/sounds';
|
||||
|
||||
interface CircularTimelineProps {
|
||||
beats: BeatPoint[];
|
||||
soundIds: readonly SoundId[];
|
||||
colorBySoundId: Partial<Record<SoundId, string>>;
|
||||
currentStep: number;
|
||||
totalSteps: number;
|
||||
focusedSoundId: SoundId | null;
|
||||
selectedBeatId: string | null;
|
||||
onMoveBeat: (beatId: string, step: number) => void;
|
||||
onSelectBeat: (beatId: string) => void;
|
||||
onSelectSound: (soundId: SoundId) => void;
|
||||
}
|
||||
|
||||
function getPointerCoordinates(event: ReactPointerEvent<SVGSVGElement>) {
|
||||
const bounds = event.currentTarget.getBoundingClientRect();
|
||||
return {
|
||||
x: ((event.clientX - bounds.left) / bounds.width) * SVG_SIZE,
|
||||
y: ((event.clientY - bounds.top) / bounds.height) * SVG_SIZE,
|
||||
};
|
||||
}
|
||||
|
||||
export function CircularTimeline({
|
||||
beats,
|
||||
soundIds,
|
||||
colorBySoundId,
|
||||
currentStep,
|
||||
totalSteps,
|
||||
focusedSoundId,
|
||||
selectedBeatId,
|
||||
onMoveBeat,
|
||||
onSelectBeat,
|
||||
onSelectSound,
|
||||
}: CircularTimelineProps) {
|
||||
const draggingBeatIdRef = useRef<string | null>(null);
|
||||
const selectedBeatSoundId = beats.find((beat) => beat.id === selectedBeatId)?.soundId;
|
||||
const activeSoundId = selectedBeatSoundId ?? focusedSoundId;
|
||||
const orderedSoundIds = activeSoundId
|
||||
? [
|
||||
...soundIds.filter((soundId) => soundId === activeSoundId),
|
||||
...soundIds.filter((soundId) => soundId !== activeSoundId),
|
||||
]
|
||||
: soundIds;
|
||||
|
||||
function handlePointerDown(event: ReactPointerEvent<SVGGElement>, beatId: string): void {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
draggingBeatIdRef.current = beatId;
|
||||
onSelectBeat(beatId);
|
||||
event.currentTarget.setPointerCapture(event.pointerId);
|
||||
}
|
||||
|
||||
function handlePointerMove(event: ReactPointerEvent<SVGSVGElement>): void {
|
||||
const beatId = draggingBeatIdRef.current;
|
||||
if (!beatId) return;
|
||||
|
||||
const { x, y } = getPointerCoordinates(event);
|
||||
onMoveBeat(beatId, getStepFromPosition(x, y, SVG_CENTER, totalSteps));
|
||||
}
|
||||
|
||||
function handleMarkerKeyDown(event: ReactKeyboardEvent<SVGGElement>, beat: BeatPoint): void {
|
||||
let nextStep: number | null = null;
|
||||
const stepSize = event.shiftKey ? 1 : 1 / PLAYBACK_SUBSTEPS_PER_STEP;
|
||||
if (event.key === 'ArrowRight' || event.key === 'ArrowDown') {
|
||||
nextStep = wrapStepPosition(beat.step + stepSize, totalSteps);
|
||||
}
|
||||
if (event.key === 'ArrowLeft' || event.key === 'ArrowUp') {
|
||||
nextStep = wrapStepPosition(beat.step - stepSize, totalSteps);
|
||||
}
|
||||
if (nextStep === null) return;
|
||||
|
||||
event.preventDefault();
|
||||
onSelectBeat(beat.id);
|
||||
onMoveBeat(beat.id, nextStep);
|
||||
}
|
||||
|
||||
function handleOrbitKeyDown(event: ReactKeyboardEvent<SVGCircleElement>, soundId: SoundId): void {
|
||||
if (event.key !== 'Enter' && event.key !== ' ') return;
|
||||
event.preventDefault();
|
||||
onSelectSound(soundId);
|
||||
}
|
||||
|
||||
return (
|
||||
<svg
|
||||
className="circular-timeline"
|
||||
viewBox={`0 0 ${SVG_SIZE} ${SVG_SIZE}`}
|
||||
role="group"
|
||||
aria-label={`${totalSteps}-step circular beat timeline with a separate orbit per sound. Drag sound points to move them.`}
|
||||
onPointerMove={handlePointerMove}
|
||||
onPointerUp={() => { draggingBeatIdRef.current = null; }}
|
||||
onPointerCancel={() => { draggingBeatIdRef.current = null; }}
|
||||
>
|
||||
{orderedSoundIds.map((soundId, soundIndex) => (
|
||||
<circle
|
||||
key={soundId}
|
||||
cx={SVG_CENTER}
|
||||
cy={SVG_CENTER}
|
||||
r={getSoundOrbitRadius(soundIndex, orderedSoundIds.length)}
|
||||
className={`timeline-orbit${soundId === activeSoundId ? ' is-focused' : ''}`}
|
||||
style={{
|
||||
stroke: colorBySoundId[soundId] ?? '#b8b4e0',
|
||||
opacity: soundId === activeSoundId ? 0.55 : 0.32,
|
||||
strokeWidth: soundId === activeSoundId ? 2.2 : 1.5,
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
|
||||
{Array.from({ length: totalSteps * PLAYBACK_SUBSTEPS_PER_STEP }, (_, tick) => {
|
||||
const step = tick / PLAYBACK_SUBSTEPS_PER_STEP;
|
||||
const major = tick % (STEPS_PER_BEAT * PLAYBACK_SUBSTEPS_PER_STEP) === 0;
|
||||
const fullStep = tick % PLAYBACK_SUBSTEPS_PER_STEP === 0;
|
||||
const inner = getPointForStep(step, ORBIT_RADIUS + 11, SVG_CENTER, totalSteps);
|
||||
const outer = getPointForStep(
|
||||
step,
|
||||
ORBIT_RADIUS + (major ? 24 : fullStep ? 18 : 14),
|
||||
SVG_CENTER,
|
||||
totalSteps,
|
||||
);
|
||||
const label = getPointForStep(step, ORBIT_RADIUS + 53, SVG_CENTER, totalSteps);
|
||||
return (
|
||||
<g
|
||||
className={`timeline-tick${major ? ' is-major' : fullStep ? '' : ' is-substep'}`}
|
||||
key={tick}
|
||||
>
|
||||
<line x1={inner.x} y1={inner.y} x2={outer.x} y2={outer.y} />
|
||||
{major && (
|
||||
<text x={label.x} y={label.y} textAnchor="middle" dominantBaseline="middle">
|
||||
{String(Math.floor(step + 1)).padStart(2, '0')}
|
||||
</text>
|
||||
)}
|
||||
</g>
|
||||
);
|
||||
})}
|
||||
|
||||
{orderedSoundIds.map((soundId, soundIndex) => (
|
||||
<circle
|
||||
key={soundId}
|
||||
cx={SVG_CENTER}
|
||||
cy={SVG_CENTER}
|
||||
r={getSoundOrbitRadius(soundIndex, orderedSoundIds.length)}
|
||||
className="timeline-orbit-hit-area"
|
||||
fill="none"
|
||||
stroke="transparent"
|
||||
strokeWidth="24"
|
||||
pointerEvents="stroke"
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
aria-label={`Select ${SOUND_BY_ID[soundId].label} ring`}
|
||||
aria-pressed={soundId === activeSoundId}
|
||||
onClick={() => onSelectSound(soundId)}
|
||||
onKeyDown={(event) => handleOrbitKeyDown(event, soundId)}
|
||||
onFocus={() => onSelectSound(soundId)}
|
||||
/>
|
||||
))}
|
||||
|
||||
{currentStep >= 0 && (() => {
|
||||
const cursor = getPointForStep(currentStep, ORBIT_RADIUS + 1, SVG_CENTER, totalSteps);
|
||||
return (
|
||||
<g className="timeline-playhead" aria-hidden="true">
|
||||
<line x1={SVG_CENTER} y1={SVG_CENTER} x2={cursor.x} y2={cursor.y} />
|
||||
<circle cx={cursor.x} cy={cursor.y} r="4" />
|
||||
</g>
|
||||
);
|
||||
})()}
|
||||
|
||||
{beats.map((beat) => {
|
||||
const sound = SOUND_BY_ID[beat.soundId];
|
||||
const soundIndex = orderedSoundIds.indexOf(beat.soundId);
|
||||
const point = getStackedPosition(
|
||||
beat,
|
||||
beats,
|
||||
totalSteps,
|
||||
getSoundOrbitRadius(soundIndex, orderedSoundIds.length),
|
||||
);
|
||||
const selected = selectedBeatId === beat.id;
|
||||
return (
|
||||
<g
|
||||
key={beat.id}
|
||||
className={`sound-point${selected ? ' is-selected' : ''}`}
|
||||
style={{ color: colorBySoundId[beat.soundId] ?? '#f4f1ed' }}
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
aria-label={`${sound.label}, ${getLoopPositionPercent(beat.step, totalSteps)}% around the loop. Drag to move freely, or use arrow keys for small steps.`}
|
||||
aria-pressed={selected}
|
||||
onPointerDown={(event) => handlePointerDown(event, beat.id)}
|
||||
onClick={() => onSelectBeat(beat.id)}
|
||||
onKeyDown={(event) => handleMarkerKeyDown(event, beat)}
|
||||
onFocus={() => onSelectBeat(beat.id)}
|
||||
>
|
||||
<circle cx={point.x} cy={point.y} r="19" className="sound-point-hit-area" />
|
||||
<circle cx={point.x} cy={point.y} r="10" className="sound-point-face" />
|
||||
</g>
|
||||
);
|
||||
})}
|
||||
|
||||
</svg>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user