Restore sound-specific rings and refine studio UI

This commit is contained in:
Ninosaurier committed 2026-11-17 17:04:02 +01:00
1 parent 02eb8911e8
commit e7b6c0bb3e
16 files changed
+318 -524

No files matched your search

+11 -11
View File
@@ -22,15 +22,15 @@ export interface BeatSequencer {
tempoBpm: number;
beatsPerBar: BeatsPerBar;
totalSteps: number;
focusedCategoryId: InstrumentCategoryId | null;
focusedSoundId: InstrumentId | null;
selectedBeat: BeatPoint | undefined;
addBeat: (soundId: InstrumentId) => void;
moveBeat: (beatId: string, step: number) => void;
changeBeatPitch: (beatId: string, pitchSemitones: number) => void;
selectBeat: (beatId: string) => void;
selectCategory: (categoryId: InstrumentCategoryId) => void;
selectSound: (soundId: InstrumentId) => void;
removeSelectedBeat: () => void;
removeFocusedCategory: () => void;
removeFocusedSound: () => void;
setTempoBpm: (tempoBpm: number) => void;
setBeatsPerBar: (beatsPerBar: BeatsPerBar) => void;
loadTemplate: (template: BeatTemplate) => void;
@@ -43,7 +43,7 @@ export function useBeatSequencer(): BeatSequencer {
undefined,
createInitialSequencerState,
);
const { beats, beatsPerBar, focusedCategoryId, selectedBeatId, tempoBpm } = state;
const { beats, beatsPerBar, focusedSoundId, selectedBeatId, tempoBpm } = state;
const totalSteps = getTotalSteps(beatsPerBar);
const nextBeatIdRef = useRef(1);
@@ -83,16 +83,16 @@ export function useBeatSequencer(): BeatSequencer {
dispatch({ type: 'beat-selected', beatId });
}, []);
const selectCategory = useCallback((categoryId: InstrumentCategoryId): void => {
dispatch({ type: 'category-selected', categoryId });
const selectSound = useCallback((soundId: InstrumentId): void => {
dispatch({ type: 'sound-selected', soundId });
}, []);
const removeSelectedBeat = useCallback((): void => {
dispatch({ type: 'selected-beat-removed' });
}, []);
const removeFocusedCategory = useCallback((): void => {
dispatch({ type: 'focused-category-removed' });
const removeFocusedSound = useCallback((): void => {
dispatch({ type: 'focused-sound-removed' });
}, []);
const setTempoBpm = useCallback((tempo: number): void => {
@@ -137,15 +137,15 @@ export function useBeatSequencer(): BeatSequencer {
tempoBpm,
beatsPerBar,
totalSteps,
focusedCategoryId,
focusedSoundId,
selectedBeat,
addBeat,
moveBeat,
changeBeatPitch,
selectBeat,
selectCategory,
selectSound,
removeSelectedBeat,
removeFocusedCategory,
removeFocusedSound,
setTempoBpm,
setBeatsPerBar,
loadTemplate,
+1 -5
View File
@@ -5,13 +5,10 @@ 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;
}
@@ -19,7 +16,6 @@ export interface BeatStudio {
export function useBeatStudio(): BeatStudio {
const sequencer = useBeatSequencer();
const soundPalette = useSoundPalette();
const categoryLabels = useCategoryLabels();
const { addBeat, loadTemplate } = sequencer;
const { activateSounds } = soundPalette;
@@ -33,5 +29,5 @@ export function useBeatStudio(): BeatStudio {
activateSounds(template.beats.map((beat) => beat.soundId));
}, [activateSounds, loadTemplate]);
return { sequencer, soundPalette, categoryLabels, addSound, applyTemplate };
return { sequencer, soundPalette, addSound, applyTemplate };
}
+30 -26
View File
@@ -1,50 +1,54 @@
import { useCallback, useMemo, useState } from 'react';
import { INSTRUMENT_BY_ID, INSTRUMENT_CATEGORIES } from '../domain/instruments';
import { INSTRUMENT_BY_ID } from '../domain/instruments';
import type { InstrumentCategoryId, InstrumentId } from '../domain/instruments';
import { generateUniqueSoundColor } from '../domain/soundColors';
export interface PaletteCategory {
id: InstrumentCategoryId;
export interface PaletteSound {
id: InstrumentId;
color: string;
}
export interface SoundPalette {
activeCategories: PaletteCategory[];
activeSounds: PaletteSound[];
colorBySoundId: Partial<Record<InstrumentId, string>>;
colorByCategoryId: Partial<Record<InstrumentCategoryId, string>>;
activateSounds: (instrumentIds: readonly InstrumentId[]) => void;
}
export function useSoundPalette(): SoundPalette {
const [activeCategories, setActiveCategories] = useState<PaletteCategory[]>([]);
const [activeSounds, setActiveSounds] = useState<PaletteSound[]>([]);
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;
setActiveSounds((currentSounds) => {
const nextSounds = [...currentSounds];
instrumentIds.forEach((instrumentId) => {
if (nextSounds.some((sound) => sound.id === instrumentId)) return;
nextCategories.push({
id: category.id,
color: generateUniqueSoundColor(nextCategories.map((active) => active.color)),
});
nextSounds.push({
id: instrumentId,
color: generateUniqueSoundColor(nextSounds.map((sound) => sound.color)),
});
return nextCategories.length === currentCount ? currentCategories : nextCategories;
});
return nextSounds.length === currentSounds.length ? currentSounds : nextSounds;
});
}, []);
const colorByCategoryId = useMemo(() => {
const colors: Partial<Record<InstrumentCategoryId, string>> = {};
activeCategories.forEach((category) => {
colors[category.id] = category.color;
const colorBySoundId = useMemo(() => {
const colors: Partial<Record<InstrumentId, string>> = {};
activeSounds.forEach((sound) => {
colors[sound.id] = sound.color;
});
return colors;
}, [activeCategories]);
}, [activeSounds]);
return { activeCategories, colorByCategoryId, activateSounds };
const colorByCategoryId = useMemo(() => {
const colors: Partial<Record<InstrumentCategoryId, string>> = {};
activeSounds.forEach((sound) => {
const categoryId = INSTRUMENT_BY_ID[sound.id].categoryId;
if (colors[categoryId] === undefined) colors[categoryId] = sound.color;
});
return colors;
}, [activeSounds]);
return { activeSounds, colorBySoundId, colorByCategoryId, activateSounds };
}