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 -13
View File
@@ -1,13 +1,15 @@
import { useCallback, useMemo, useReducer, useRef } from 'react';
import type { BeatTemplate } from '../domain/beatTemplates';
import {
getAlphabeticLabel,
getNextAvailableStep,
getNextBeatLetter,
getTotalSteps,
} from '../domain/sequencer';
import type { BeatsPerBar } from '../domain/sequencer';
import type { BeatPoint } from '../domain/sequencer';
import { DEFAULT_PITCH_SEMITONES } from '../domain/instruments';
import type { InstrumentId } from '../domain/instruments';
import { DEFAULT_PITCH_SEMITONES, INSTRUMENT_BY_ID } from '../domain/instruments';
import type { InstrumentCategoryId, InstrumentId } from '../domain/instruments';
import { createInitialSequencerState, sequencerReducer } from '../domain/sequencerReducer';
import { useBeatPlayback } from './useBeatPlayback';
@@ -20,15 +22,15 @@ export interface BeatSequencer {
tempoBpm: number;
beatsPerBar: BeatsPerBar;
totalSteps: number;
focusedSoundId: InstrumentId | null;
focusedCategoryId: InstrumentCategoryId | null;
selectedBeat: BeatPoint | undefined;
addBeat: (soundId: InstrumentId) => void;
moveBeat: (beatId: string, step: number) => void;
changeBeatPitch: (beatId: string, pitchSemitones: number) => void;
selectBeat: (beatId: string) => void;
selectSound: (soundId: InstrumentId) => void;
selectCategory: (categoryId: InstrumentCategoryId) => void;
removeSelectedBeat: () => void;
removeFocusedSound: () => void;
removeFocusedCategory: () => void;
setTempoBpm: (tempoBpm: number) => void;
setBeatsPerBar: (beatsPerBar: BeatsPerBar) => void;
loadTemplate: (template: BeatTemplate) => void;
@@ -41,7 +43,7 @@ export function useBeatSequencer(): BeatSequencer {
undefined,
createInitialSequencerState,
);
const { beats, beatsPerBar, focusedSoundId, selectedBeatId, tempoBpm } = state;
const { beats, beatsPerBar, focusedCategoryId, selectedBeatId, tempoBpm } = state;
const totalSteps = getTotalSteps(beatsPerBar);
const nextBeatIdRef = useRef(1);
@@ -61,6 +63,7 @@ export function useBeatSequencer(): BeatSequencer {
const beat: BeatPoint = {
id: `beat-${nextBeatIdRef.current}`,
soundId,
letter: getNextBeatLetter(beats, INSTRUMENT_BY_ID[soundId].categoryId),
step: getNextAvailableStep(beats, totalSteps),
pitchSemitones: DEFAULT_PITCH_SEMITONES,
};
@@ -80,16 +83,16 @@ export function useBeatSequencer(): BeatSequencer {
dispatch({ type: 'beat-selected', beatId });
}, []);
const selectSound = useCallback((soundId: InstrumentId): void => {
dispatch({ type: 'sound-selected', soundId });
const selectCategory = useCallback((categoryId: InstrumentCategoryId): void => {
dispatch({ type: 'category-selected', categoryId });
}, []);
const removeSelectedBeat = useCallback((): void => {
dispatch({ type: 'selected-beat-removed' });
}, []);
const removeFocusedSound = useCallback((): void => {
dispatch({ type: 'focused-sound-removed' });
const removeFocusedCategory = useCallback((): void => {
dispatch({ type: 'focused-category-removed' });
}, []);
const setTempoBpm = useCallback((tempo: number): void => {
@@ -101,10 +104,15 @@ export function useBeatSequencer(): BeatSequencer {
}, []);
const loadTemplate = useCallback((template: BeatTemplate): void => {
const nextLetterByCategory = new Map<InstrumentCategoryId, number>();
const templateBeats: BeatPoint[] = template.beats.map((templateBeat) => {
const categoryId = INSTRUMENT_BY_ID[templateBeat.soundId].categoryId;
const letterIndex = nextLetterByCategory.get(categoryId) ?? 0;
nextLetterByCategory.set(categoryId, letterIndex + 1);
const beat: BeatPoint = {
id: `beat-${nextBeatIdRef.current}`,
...templateBeat,
letter: getAlphabeticLabel(letterIndex).toLowerCase(),
pitchSemitones: templateBeat.pitchSemitones ?? DEFAULT_PITCH_SEMITONES,
};
nextBeatIdRef.current += 1;
@@ -129,15 +137,15 @@ export function useBeatSequencer(): BeatSequencer {
tempoBpm,
beatsPerBar,
totalSteps,
focusedSoundId,
focusedCategoryId,
selectedBeat,
addBeat,
moveBeat,
changeBeatPitch,
selectBeat,
selectSound,
selectCategory,
removeSelectedBeat,
removeFocusedSound,
removeFocusedCategory,
setTempoBpm,
setBeatsPerBar,
loadTemplate,
+5 -1
View File
@@ -5,10 +5,13 @@ import type { BeatSequencer } from './useBeatSequencer';
import { useBeatSequencer } from './useBeatSequencer';
import type { SoundPalette } from './useSoundPalette';
import { useSoundPalette } from './useSoundPalette';
import type { CategoryLabels } from './useCategoryLabels';
import { useCategoryLabels } from './useCategoryLabels';
export interface BeatStudio {
sequencer: BeatSequencer;
soundPalette: SoundPalette;
categoryLabels: CategoryLabels;
addSound: (soundId: InstrumentId) => void;
applyTemplate: (template: BeatTemplate) => void;
}
@@ -16,6 +19,7 @@ export interface BeatStudio {
export function useBeatStudio(): BeatStudio {
const sequencer = useBeatSequencer();
const soundPalette = useSoundPalette();
const categoryLabels = useCategoryLabels();
const { addBeat, loadTemplate } = sequencer;
const { activateSounds } = soundPalette;
@@ -29,5 +33,5 @@ export function useBeatStudio(): BeatStudio {
activateSounds(template.beats.map((beat) => beat.soundId));
}, [activateSounds, loadTemplate]);
return { sequencer, soundPalette, addSound, applyTemplate };
return { sequencer, soundPalette, categoryLabels, addSound, applyTemplate };
}
+25
View File
@@ -0,0 +1,25 @@
import { useCallback, useState } from 'react';
import type { InstrumentCategoryId } from '../domain/instruments';
export type CategoryLabelOverrides = Partial<Record<InstrumentCategoryId, string>>;
export interface CategoryLabels {
labelByCategoryId: CategoryLabelOverrides;
renameCategory: (categoryId: InstrumentCategoryId, label: string) => void;
}
export function useCategoryLabels(): CategoryLabels {
const [labelByCategoryId, setLabelByCategoryId] = useState<CategoryLabelOverrides>({});
const renameCategory = useCallback((categoryId: InstrumentCategoryId, label: string): void => {
const trimmedLabel = label.trim();
if (!trimmedLabel) return;
setLabelByCategoryId((currentLabels) => ({
...currentLabels,
[categoryId]: trimmedLabel,
}));
}, []);
return { labelByCategoryId, renameCategory };
}
+30 -24
View File
@@ -1,44 +1,50 @@
import { useCallback, useMemo, useState } from 'react';
import type { InstrumentId } from '../domain/instruments';
import { INSTRUMENT_BY_ID, INSTRUMENT_CATEGORIES } from '../domain/instruments';
import type { InstrumentCategoryId, InstrumentId } from '../domain/instruments';
import { generateUniqueSoundColor } from '../domain/soundColors';
export interface PaletteSound {
id: InstrumentId;
export interface PaletteCategory {
id: InstrumentCategoryId;
color: string;
}
export interface SoundPalette {
activeSounds: PaletteSound[];
colorBySoundId: Partial<Record<InstrumentId, string>>;
activateSounds: (soundIds: readonly InstrumentId[]) => void;
activeCategories: PaletteCategory[];
colorByCategoryId: Partial<Record<InstrumentCategoryId, string>>;
activateSounds: (instrumentIds: readonly InstrumentId[]) => void;
}
export function useSoundPalette(): SoundPalette {
const [activeSounds, setActiveSounds] = useState<PaletteSound[]>([]);
const [activeCategories, setActiveCategories] = useState<PaletteCategory[]>([]);
const activateSounds = useCallback((soundIds: readonly InstrumentId[]): void => {
setActiveSounds((currentSounds) => {
const nextSounds = [...currentSounds];
const currentCount = nextSounds.length;
soundIds.forEach((soundId) => {
if (nextSounds.some((sound) => sound.id === soundId)) return;
const activateSounds = useCallback((instrumentIds: readonly InstrumentId[]): void => {
setActiveCategories((currentCategories) => {
const nextCategories = [...currentCategories];
const currentCount = nextCategories.length;
const categoryIds = instrumentIds.map(
(instrumentId) => INSTRUMENT_BY_ID[instrumentId].categoryId,
);
INSTRUMENT_CATEGORIES
.filter((category) => categoryIds.includes(category.id))
.forEach((category) => {
if (nextCategories.some((active) => active.id === category.id)) return;
nextSounds.push({
id: soundId,
color: generateUniqueSoundColor(nextSounds.map((sound) => sound.color)),
nextCategories.push({
id: category.id,
color: generateUniqueSoundColor(nextCategories.map((active) => active.color)),
});
});
});
return nextSounds.length === currentCount ? currentSounds : nextSounds;
return nextCategories.length === currentCount ? currentCategories : nextCategories;
});
}, []);
const colorBySoundId = useMemo(() => {
const colors: Partial<Record<InstrumentId, string>> = {};
activeSounds.forEach((sound) => {
colors[sound.id] = sound.color;
const colorByCategoryId = useMemo(() => {
const colors: Partial<Record<InstrumentCategoryId, string>> = {};
activeCategories.forEach((category) => {
colors[category.id] = category.color;
});
return colors;
}, [activeSounds]);
}, [activeCategories]);
return { activeSounds, colorBySoundId, activateSounds };
return { activeCategories, colorByCategoryId, activateSounds };
}