Add category rings and beat labels

This commit is contained in:
Ninosaurier committed 2026-11-15 14:35:36 +01:00
1 parent 9d8f277971
commit 4f221ea122
21 files changed
+752 -339

No files matched your search

+21 -6
View File
@@ -136,6 +136,7 @@ export type InstrumentCategoryId = InstrumentDefinition['categoryId'];
export interface InstrumentCategory {
id: InstrumentCategoryId;
label: string;
letter: string;
instrumentIds: readonly InstrumentId[];
}
@@ -148,19 +149,33 @@ export function isInstrumentId(value: unknown): value is InstrumentId {
}
export const INSTRUMENT_CATEGORIES = [
{ id: 'drums', label: 'Drums', instrumentIds: ['kick', 'snare', 'hi-hat', 'toms', 'cymbals'] },
{
id: 'drums',
label: 'Drums',
letter: 'A',
instrumentIds: ['kick', 'snare', 'hi-hat', 'toms', 'cymbals'],
},
{
id: 'percussion',
label: 'Percussion',
letter: 'B',
instrumentIds: ['clap', 'shaker', 'cowbell', 'woodblock', 'bongos'],
},
{ id: 'bass', label: 'Bass', instrumentIds: ['bass'] },
{ id: 'keys', label: 'Keys', instrumentIds: ['piano', 'organ'] },
{ id: 'strings', label: 'Strings', instrumentIds: ['strings'] },
{ id: 'winds', label: 'Winds', instrumentIds: ['winds'] },
{ id: 'synth', label: 'Synth', instrumentIds: ['synth'] },
{ id: 'bass', label: 'Bass', letter: 'C', instrumentIds: ['bass'] },
{ id: 'keys', label: 'Keys', letter: 'D', instrumentIds: ['piano', 'organ'] },
{ id: 'strings', label: 'Strings', letter: 'E', instrumentIds: ['strings'] },
{ id: 'winds', label: 'Winds', letter: 'F', instrumentIds: ['winds'] },
{ id: 'synth', label: 'Synth', letter: 'G', instrumentIds: ['synth'] },
] as const satisfies readonly InstrumentCategory[];
export const INSTRUMENT_CATEGORY_BY_ID = INSTRUMENT_CATEGORIES.reduce(
(categories, category) => {
categories[category.id] = category;
return categories;
},
{} as Record<InstrumentCategoryId, InstrumentCategory>,
);
export const DEFAULT_PITCH_SEMITONES = 0;
export const MIN_PITCH_SEMITONES = -24;
export const MAX_PITCH_SEMITONES = 24;
+36 -1
View File
@@ -1,4 +1,5 @@
import type { InstrumentId } from './instruments';
import { INSTRUMENT_BY_ID } from './instruments';
import type { InstrumentCategoryId, InstrumentId } from './instruments';
export const STEPS_PER_BEAT = 4;
export const PLAYBACK_SUBSTEPS_PER_STEP = 4;
@@ -25,12 +26,46 @@ export function getPlaybackTickDurationMs(tempoBpm: number): number {
export interface BeatPoint {
id: string;
soundId: InstrumentId;
/** Short unique label within its instrument category, such as a, b, or c. */
letter: string;
/** Position in sixteenth-note steps; fractional values represent microtiming. */
step: number;
/** Pitch offset from the instrument's base note in semitones. */
pitchSemitones?: number;
}
export function getAlphabeticLabel(index: number): string {
let value = Math.floor(index) + 1;
if (!Number.isSafeInteger(value) || value < 1) return '';
let label = '';
while (value > 0) {
value -= 1;
label = String.fromCharCode(65 + (value % 26)) + label;
value = Math.floor(value / 26);
}
return label;
}
export function getNextBeatLetter(
beats: readonly BeatPoint[],
categoryId: InstrumentCategoryId,
): string {
const usedLetters = new Set(
beats
.filter((beat) => INSTRUMENT_BY_ID[beat.soundId].categoryId === categoryId)
.map((beat) => beat.letter),
);
let index = 0;
let letter = getAlphabeticLabel(index).toLowerCase();
while (usedLetters.has(letter)) {
index += 1;
letter = getAlphabeticLabel(index).toLowerCase();
}
return letter;
}
export function getNextAvailableStep(
beats: readonly BeatPoint[],
totalSteps = TOTAL_STEPS,
+34 -23
View File
@@ -5,13 +5,13 @@ import {
TEMPO_BPM,
} from './sequencer';
import type { BeatsPerBar, BeatPoint } from './sequencer';
import { clampPitchSemitones } from './instruments';
import type { InstrumentId } from './instruments';
import { clampPitchSemitones, INSTRUMENT_BY_ID } from './instruments';
import type { InstrumentCategoryId } from './instruments';
export interface SequencerState {
beats: BeatPoint[];
selectedBeatId: string | null;
focusedSoundId: InstrumentId | null;
focusedCategoryId: InstrumentCategoryId | null;
tempoBpm: number;
beatsPerBar: BeatsPerBar;
}
@@ -21,9 +21,9 @@ export type SequencerAction =
| { type: 'beat-moved'; beatId: string; step: number }
| { type: 'beat-pitch-changed'; beatId: string; pitchSemitones: number }
| { type: 'beat-selected'; beatId: string }
| { type: 'sound-selected'; soundId: InstrumentId }
| { type: 'category-selected'; categoryId: InstrumentCategoryId }
| { type: 'selected-beat-removed' }
| { type: 'focused-sound-removed' }
| { type: 'focused-category-removed' }
| { type: 'tempo-changed'; tempoBpm: number }
| { type: 'time-signature-changed'; beatsPerBar: BeatsPerBar }
| { type: 'template-loaded'; beats: BeatPoint[]; tempoBpm: number; beatsPerBar: BeatsPerBar };
@@ -32,7 +32,7 @@ export function createInitialSequencerState(): SequencerState {
return {
beats: [],
selectedBeatId: null,
focusedSoundId: null,
focusedCategoryId: null,
tempoBpm: TEMPO_BPM,
beatsPerBar: DEFAULT_BEATS_PER_BAR,
};
@@ -49,7 +49,7 @@ export function sequencerReducer(state: SequencerState, action: SequencerAction)
...state,
beats: [...state.beats, action.beat],
selectedBeatId: action.beat.id,
focusedSoundId: action.beat.soundId,
focusedCategoryId: INSTRUMENT_BY_ID[action.beat.soundId].categoryId,
};
case 'beat-moved':
return {
@@ -70,34 +70,43 @@ export function sequencerReducer(state: SequencerState, action: SequencerAction)
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 };
return {
...state,
selectedBeatId: beat.id,
focusedCategoryId: INSTRUMENT_BY_ID[beat.soundId].categoryId,
};
}
case 'sound-selected':
return { ...state, focusedSoundId: action.soundId, selectedBeatId: null };
case 'category-selected':
return { ...state, focusedCategoryId: action.categoryId, 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);
const removedCategoryId = INSTRUMENT_BY_ID[removedBeat.soundId].categoryId;
const categoryRemains = beats.some(
(beat) => INSTRUMENT_BY_ID[beat.soundId].categoryId === removedCategoryId,
);
return {
...state,
beats,
selectedBeatId: null,
focusedSoundId: state.focusedSoundId === removedBeat.soundId && !soundRemains
? beats[0]?.soundId ?? null
: state.focusedSoundId,
focusedCategoryId: state.focusedCategoryId === removedCategoryId && !categoryRemains
? beats[0] ? INSTRUMENT_BY_ID[beats[0].soundId].categoryId : null
: state.focusedCategoryId,
};
}
case 'focused-sound-removed': {
if (!state.focusedSoundId) return state;
const beats = state.beats.filter((beat) => beat.soundId !== state.focusedSoundId);
case 'focused-category-removed': {
if (!state.focusedCategoryId) return state;
const beats = state.beats.filter(
(beat) => INSTRUMENT_BY_ID[beat.soundId].categoryId !== state.focusedCategoryId,
);
return {
...state,
beats,
selectedBeatId: null,
focusedSoundId: beats[0]?.soundId ?? null,
focusedCategoryId: beats[0] ? INSTRUMENT_BY_ID[beats[0].soundId].categoryId : null,
};
}
case 'tempo-changed':
@@ -106,14 +115,16 @@ export function sequencerReducer(state: SequencerState, action: SequencerAction)
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);
const focusedCategoryRemains = visibleBeats.some(
(beat) => INSTRUMENT_BY_ID[beat.soundId].categoryId === state.focusedCategoryId,
);
return {
...state,
beatsPerBar: action.beatsPerBar,
selectedBeatId: selectedBeat?.id ?? null,
focusedSoundId: focusedSoundRemains
? state.focusedSoundId
: visibleBeats[0]?.soundId ?? null,
focusedCategoryId: focusedCategoryRemains
? state.focusedCategoryId
: visibleBeats[0] ? INSTRUMENT_BY_ID[visibleBeats[0].soundId].categoryId : null,
};
}
case 'template-loaded': {
@@ -122,7 +133,7 @@ export function sequencerReducer(state: SequencerState, action: SequencerAction)
...state,
beats: action.beats,
selectedBeatId: firstBeat?.id ?? null,
focusedSoundId: firstBeat?.soundId ?? null,
focusedCategoryId: firstBeat ? INSTRUMENT_BY_ID[firstBeat.soundId].categoryId : null,
tempoBpm: clampTempoBpm(action.tempoBpm),
beatsPerBar: action.beatsPerBar,
};
+7 -3
View File
@@ -1,4 +1,5 @@
import type { BeatPoint } from './sequencer';
import { INSTRUMENT_BY_ID } from './instruments';
import {
PLAYBACK_SUBSTEPS_PER_STEP,
TOTAL_STEPS,
@@ -47,10 +48,12 @@ 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 getCategoryOrbitRadius(categoryIndex: number, categoryCount: number): number {
return ORBIT_RADIUS * (categoryCount + 1 - categoryIndex) / (categoryCount + 1);
}
export const getSoundOrbitRadius = getCategoryOrbitRadius;
export function getPlaybackTick(step: number, totalSteps: number): number {
const totalPlaybackTicks = totalSteps * PLAYBACK_SUBSTEPS_PER_STEP;
return Math.round(step * PLAYBACK_SUBSTEPS_PER_STEP) % totalPlaybackTicks;
@@ -64,7 +67,8 @@ export function getStackedPosition(
): CirclePoint {
const siblings = beats
.filter((candidate) => (
candidate.soundId === beat.soundId
INSTRUMENT_BY_ID[candidate.soundId].categoryId
=== INSTRUMENT_BY_ID[beat.soundId].categoryId
&& getPlaybackTick(candidate.step, totalSteps) === getPlaybackTick(beat.step, totalSteps)
))
.sort((first, second) => first.id.localeCompare(second.id));