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,54 @@
|
||||
import { DEFAULT_BEATS_PER_BAR, TEMPO_BPM } from './sequencer';
|
||||
import type { BeatsPerBar } from './sequencer';
|
||||
import type { SoundId } from './sounds';
|
||||
|
||||
export interface TemplateBeat {
|
||||
soundId: SoundId;
|
||||
step: number;
|
||||
}
|
||||
|
||||
export type BeatTemplateId = 'empty' | 'bachata' | 'salsa' | 'lindy-hop';
|
||||
|
||||
export interface BeatTemplate {
|
||||
id: BeatTemplateId;
|
||||
label: string;
|
||||
description: string;
|
||||
beatsPerBar: BeatsPerBar;
|
||||
tempoBpm: number;
|
||||
beats: readonly TemplateBeat[];
|
||||
}
|
||||
|
||||
export const BEAT_TEMPLATES = [
|
||||
{
|
||||
id: 'empty',
|
||||
label: 'Empty',
|
||||
description: 'Clear the current loop.',
|
||||
beatsPerBar: DEFAULT_BEATS_PER_BAR,
|
||||
tempoBpm: TEMPO_BPM,
|
||||
beats: [],
|
||||
},
|
||||
{
|
||||
id: 'bachata',
|
||||
label: 'Bachata',
|
||||
description: 'Kick on beats 1, 2, 3, and 4.',
|
||||
beatsPerBar: DEFAULT_BEATS_PER_BAR,
|
||||
tempoBpm: 120,
|
||||
beats: [0, 4, 8, 12].map((step) => ({ soundId: 'kick', step })),
|
||||
},
|
||||
{
|
||||
id: 'salsa',
|
||||
label: 'Salsa',
|
||||
description: 'Kick on beats 1, 2, and 3.',
|
||||
beatsPerBar: DEFAULT_BEATS_PER_BAR,
|
||||
tempoBpm: 180,
|
||||
beats: [0, 4, 8].map((step) => ({ soundId: 'kick', step })),
|
||||
},
|
||||
{
|
||||
id: 'lindy-hop',
|
||||
label: 'Lindy Hop',
|
||||
description: 'Kick on beats 1–8.',
|
||||
beatsPerBar: 8,
|
||||
tempoBpm: 120,
|
||||
beats: [0, 4, 8, 12, 16, 20, 24, 28].map((step) => ({ soundId: 'kick', step })),
|
||||
},
|
||||
] as const satisfies readonly BeatTemplate[];
|
||||
@@ -0,0 +1,40 @@
|
||||
import type { SoundId } from './sounds';
|
||||
|
||||
export const STEPS_PER_BEAT = 4;
|
||||
export const PLAYBACK_SUBSTEPS_PER_STEP = 4;
|
||||
export const DEFAULT_BEATS_PER_BAR = 4;
|
||||
export const BEATS_PER_BAR_OPTIONS = [2, 3, 4, 5, 6, 7, 8] as const;
|
||||
export type BeatsPerBar = (typeof BEATS_PER_BAR_OPTIONS)[number];
|
||||
export const TOTAL_STEPS = DEFAULT_BEATS_PER_BAR * STEPS_PER_BEAT;
|
||||
export const TEMPO_BPM = 112;
|
||||
export const MIN_TEMPO_BPM = 60;
|
||||
export const MAX_TEMPO_BPM = 180;
|
||||
|
||||
export function getTotalSteps(beatsPerBar: BeatsPerBar): number {
|
||||
return beatsPerBar * STEPS_PER_BEAT;
|
||||
}
|
||||
|
||||
export function clampTempoBpm(tempoBpm: number): number {
|
||||
return Math.min(MAX_TEMPO_BPM, Math.max(MIN_TEMPO_BPM, Math.round(tempoBpm)));
|
||||
}
|
||||
|
||||
export function getPlaybackTickDurationMs(tempoBpm: number): number {
|
||||
return 60_000 / tempoBpm / STEPS_PER_BEAT / PLAYBACK_SUBSTEPS_PER_STEP;
|
||||
}
|
||||
|
||||
export interface BeatPoint {
|
||||
id: string;
|
||||
soundId: SoundId;
|
||||
/** Position in sixteenth-note steps; fractional values represent microtiming. */
|
||||
step: number;
|
||||
}
|
||||
|
||||
export function getNextAvailableStep(
|
||||
beats: readonly BeatPoint[],
|
||||
totalSteps = TOTAL_STEPS,
|
||||
): number {
|
||||
const occupiedSteps = new Set(beats.map((beat) => beat.step));
|
||||
const nextStep = Array.from({ length: totalSteps }, (_, step) => step)
|
||||
.find((step) => !occupiedSteps.has(step));
|
||||
return nextStep ?? 0;
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import { clampTempoBpm, DEFAULT_BEATS_PER_BAR, getTotalSteps, TEMPO_BPM } from './sequencer';
|
||||
import type { BeatsPerBar, BeatPoint } from './sequencer';
|
||||
import type { SoundId } from './sounds';
|
||||
|
||||
export interface SequencerState {
|
||||
beats: BeatPoint[];
|
||||
selectedBeatId: string | null;
|
||||
focusedSoundId: SoundId | null;
|
||||
tempoBpm: number;
|
||||
beatsPerBar: BeatsPerBar;
|
||||
}
|
||||
|
||||
export type SequencerAction =
|
||||
| { type: 'beat-added'; beat: BeatPoint }
|
||||
| { type: 'beat-moved'; beatId: string; step: number }
|
||||
| { type: 'beat-selected'; beatId: string }
|
||||
| { type: 'sound-selected'; soundId: SoundId }
|
||||
| { type: 'selected-beat-removed' }
|
||||
| { type: 'focused-sound-removed' }
|
||||
| { type: 'tempo-changed'; tempoBpm: number }
|
||||
| { type: 'time-signature-changed'; beatsPerBar: BeatsPerBar }
|
||||
| { type: 'template-loaded'; beats: BeatPoint[]; tempoBpm: number; beatsPerBar: BeatsPerBar };
|
||||
|
||||
export function createInitialSequencerState(): SequencerState {
|
||||
return {
|
||||
beats: [],
|
||||
selectedBeatId: null,
|
||||
focusedSoundId: null,
|
||||
tempoBpm: TEMPO_BPM,
|
||||
beatsPerBar: DEFAULT_BEATS_PER_BAR,
|
||||
};
|
||||
}
|
||||
|
||||
function assertNever(action: never): never {
|
||||
throw new Error(`Unhandled sequencer action: ${action}`);
|
||||
}
|
||||
|
||||
export function sequencerReducer(state: SequencerState, action: SequencerAction): SequencerState {
|
||||
switch (action.type) {
|
||||
case 'beat-added':
|
||||
return {
|
||||
...state,
|
||||
beats: [...state.beats, action.beat],
|
||||
selectedBeatId: action.beat.id,
|
||||
focusedSoundId: action.beat.soundId,
|
||||
};
|
||||
case 'beat-moved':
|
||||
return {
|
||||
...state,
|
||||
beats: state.beats.map((beat) => (
|
||||
beat.id === action.beatId ? { ...beat, step: action.step } : beat
|
||||
)),
|
||||
};
|
||||
case 'beat-selected': {
|
||||
const beat = state.beats.find((candidate) => candidate.id === action.beatId);
|
||||
if (!beat) return state;
|
||||
return { ...state, selectedBeatId: beat.id, focusedSoundId: beat.soundId };
|
||||
}
|
||||
case 'sound-selected':
|
||||
return { ...state, focusedSoundId: action.soundId, selectedBeatId: null };
|
||||
case 'selected-beat-removed': {
|
||||
if (!state.selectedBeatId) return state;
|
||||
const removedBeat = state.beats.find((beat) => beat.id === state.selectedBeatId);
|
||||
if (!removedBeat) return { ...state, selectedBeatId: null };
|
||||
|
||||
const beats = state.beats.filter((beat) => beat.id !== removedBeat.id);
|
||||
const soundRemains = beats.some((beat) => beat.soundId === removedBeat.soundId);
|
||||
return {
|
||||
...state,
|
||||
beats,
|
||||
selectedBeatId: null,
|
||||
focusedSoundId: state.focusedSoundId === removedBeat.soundId && !soundRemains
|
||||
? beats[0]?.soundId ?? null
|
||||
: state.focusedSoundId,
|
||||
};
|
||||
}
|
||||
case 'focused-sound-removed': {
|
||||
if (!state.focusedSoundId) return state;
|
||||
const beats = state.beats.filter((beat) => beat.soundId !== state.focusedSoundId);
|
||||
return {
|
||||
...state,
|
||||
beats,
|
||||
selectedBeatId: null,
|
||||
focusedSoundId: beats[0]?.soundId ?? null,
|
||||
};
|
||||
}
|
||||
case 'tempo-changed':
|
||||
return { ...state, tempoBpm: clampTempoBpm(action.tempoBpm) };
|
||||
case 'time-signature-changed': {
|
||||
const totalSteps = getTotalSteps(action.beatsPerBar);
|
||||
const visibleBeats = state.beats.filter((beat) => beat.step < totalSteps);
|
||||
const selectedBeat = visibleBeats.find((beat) => beat.id === state.selectedBeatId);
|
||||
const focusedSoundRemains = visibleBeats.some((beat) => beat.soundId === state.focusedSoundId);
|
||||
return {
|
||||
...state,
|
||||
beatsPerBar: action.beatsPerBar,
|
||||
selectedBeatId: selectedBeat?.id ?? null,
|
||||
focusedSoundId: focusedSoundRemains
|
||||
? state.focusedSoundId
|
||||
: visibleBeats[0]?.soundId ?? null,
|
||||
};
|
||||
}
|
||||
case 'template-loaded': {
|
||||
const firstBeat = action.beats[0];
|
||||
return {
|
||||
...state,
|
||||
beats: action.beats,
|
||||
selectedBeatId: firstBeat?.id ?? null,
|
||||
focusedSoundId: firstBeat?.soundId ?? null,
|
||||
tempoBpm: clampTempoBpm(action.tempoBpm),
|
||||
beatsPerBar: action.beatsPerBar,
|
||||
};
|
||||
}
|
||||
default:
|
||||
return assertNever(action);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
const FIRST_HUE = 18;
|
||||
const GOLDEN_ANGLE = 137.508;
|
||||
const HUE_CYCLE = 360;
|
||||
|
||||
export function generateUniqueSoundColor(existingColors: readonly string[]): string {
|
||||
const existing = new Set(existingColors);
|
||||
|
||||
for (let index = existingColors.length; index < existingColors.length + HUE_CYCLE; index += 1) {
|
||||
const hue = ((FIRST_HUE + index * GOLDEN_ANGLE) % HUE_CYCLE).toFixed(2);
|
||||
const color = `hsl(${hue} 76% 65%)`;
|
||||
if (!existing.has(color)) return color;
|
||||
}
|
||||
|
||||
throw new Error('Unable to generate a unique sound color.');
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
const SOUND_DEFINITIONS = {
|
||||
'hi-hat': { id: 'hi-hat', label: 'Hi-hat', description: 'Bright tick' },
|
||||
kick: { id: 'kick', label: 'Kick', description: 'Low pulse' },
|
||||
} as const;
|
||||
|
||||
export type SoundId = keyof typeof SOUND_DEFINITIONS;
|
||||
export type SoundDefinition = (typeof SOUND_DEFINITIONS)[SoundId];
|
||||
|
||||
export const SOUND_BY_ID: Record<SoundId, SoundDefinition> = SOUND_DEFINITIONS;
|
||||
export const SOUND_CATALOG: readonly SoundDefinition[] = Object.values(SOUND_DEFINITIONS);
|
||||
@@ -0,0 +1,74 @@
|
||||
import type { BeatPoint } from './sequencer';
|
||||
import {
|
||||
PLAYBACK_SUBSTEPS_PER_STEP,
|
||||
TOTAL_STEPS,
|
||||
} from './sequencer';
|
||||
|
||||
export const SVG_SIZE = 600;
|
||||
export const SVG_CENTER = SVG_SIZE / 2;
|
||||
export const ORBIT_RADIUS = 216;
|
||||
|
||||
const MARKER_STEP_OFFSET = 0.3;
|
||||
|
||||
export interface CirclePoint {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
export function getStepFromPosition(
|
||||
x: number,
|
||||
y: number,
|
||||
center = SVG_CENTER,
|
||||
totalSteps = TOTAL_STEPS,
|
||||
): number {
|
||||
const angle = Math.atan2(y - center, x - center) + Math.PI / 2;
|
||||
const normalizedAngle = (angle + Math.PI * 2) % (Math.PI * 2);
|
||||
return (normalizedAngle / (Math.PI * 2)) * totalSteps;
|
||||
}
|
||||
|
||||
export function getPointForStep(
|
||||
step: number,
|
||||
radius: number,
|
||||
center = SVG_CENTER,
|
||||
totalSteps = TOTAL_STEPS,
|
||||
): CirclePoint {
|
||||
const angle = (step / totalSteps) * Math.PI * 2 - Math.PI / 2;
|
||||
return {
|
||||
x: center + Math.cos(angle) * radius,
|
||||
y: center + Math.sin(angle) * radius,
|
||||
};
|
||||
}
|
||||
|
||||
export function getLoopPositionPercent(step: number, totalSteps: number): number {
|
||||
return Math.round((step / totalSteps) * 100);
|
||||
}
|
||||
|
||||
export function wrapStepPosition(step: number, totalSteps: number): number {
|
||||
return ((step % totalSteps) + totalSteps) % totalSteps;
|
||||
}
|
||||
|
||||
export function getSoundOrbitRadius(soundIndex: number, soundCount: number): number {
|
||||
return ORBIT_RADIUS * (soundCount + 1 - soundIndex) / (soundCount + 1);
|
||||
}
|
||||
|
||||
export function getPlaybackTick(step: number, totalSteps: number): number {
|
||||
const totalPlaybackTicks = totalSteps * PLAYBACK_SUBSTEPS_PER_STEP;
|
||||
return Math.round(step * PLAYBACK_SUBSTEPS_PER_STEP) % totalPlaybackTicks;
|
||||
}
|
||||
|
||||
export function getStackedPosition(
|
||||
beat: BeatPoint,
|
||||
beats: readonly BeatPoint[],
|
||||
totalSteps: number,
|
||||
orbitRadius: number,
|
||||
): CirclePoint {
|
||||
const siblings = beats
|
||||
.filter((candidate) => (
|
||||
candidate.soundId === beat.soundId
|
||||
&& getPlaybackTick(candidate.step, totalSteps) === getPlaybackTick(beat.step, totalSteps)
|
||||
))
|
||||
.sort((first, second) => first.id.localeCompare(second.id));
|
||||
const index = siblings.findIndex((candidate) => candidate.id === beat.id);
|
||||
const offset = (index - (siblings.length - 1) / 2) * MARKER_STEP_OFFSET;
|
||||
return getPointForStep(beat.step + offset, orbitRadius, SVG_CENTER, totalSteps);
|
||||
}
|
||||
Reference in new issue
Block a user