From e7b6c0bb3e567775e37154fc513bf0a8ef07b7a6 Mon Sep 17 00:00:00 2001 From: Ninosaurier Date: Tue, 17 Nov 2026 17:04:02 +0100 Subject: [PATCH] Restore sound-specific rings and refine studio UI --- src/components/BeatLoopPanel.tsx | 72 +++++------- src/components/BeatSelectionHint.tsx | 29 ++--- src/components/CategoryLegend.tsx | 139 ++++-------------------- src/components/CategoryRingSelector.tsx | 97 +---------------- src/components/CircularTimeline.tsx | 113 ++++++++----------- src/components/SoundRingSelector.tsx | 83 +++++++++++++- src/domain/sequencerReducer.ts | 53 ++++----- src/domain/timeline.ts | 10 +- src/hooks/useBeatSequencer.ts | 22 ++-- src/hooks/useBeatStudio.ts | 6 +- src/hooks/useSoundPalette.ts | 56 +++++----- src/ionicons.d.ts | 1 + src/sequencer.test.ts | 34 +++--- src/styles.css | 106 +++++------------- src/views/AboutView.tsx | 20 +++- src/views/StudioView.tsx | 1 - 16 files changed, 318 insertions(+), 524 deletions(-) diff --git a/src/components/BeatLoopPanel.tsx b/src/components/BeatLoopPanel.tsx index b25502a..71b272c 100644 --- a/src/components/BeatLoopPanel.tsx +++ b/src/components/BeatLoopPanel.tsx @@ -2,45 +2,39 @@ import { useMemo } from 'react'; import { IonButton, IonIcon } from '@ionic/react'; import { trash } from 'ionicons/icons'; import { BeatSelectionHint } from './BeatSelectionHint'; -import { CategoryLegend } from './CategoryLegend'; -import { CategoryRingSelector } from './CategoryRingSelector'; +import { SampleLegend } from './CategoryLegend'; import { CircularTimeline } from './CircularTimeline'; +import { SoundRingSelector } from './SoundRingSelector'; import { TempoControl } from './TempoControl'; import { TimeSignatureSelector } from './TimeSignatureSelector'; import { INSTRUMENT_BY_ID, - INSTRUMENT_CATEGORIES, - INSTRUMENT_CATEGORY_BY_ID, + INSTRUMENT_CATALOG, } from '../domain/instruments'; -import type { InstrumentCategoryId } from '../domain/instruments'; import type { BeatSequencer } from '../hooks/useBeatSequencer'; import type { SoundPalette } from '../hooks/useSoundPalette'; -import type { CategoryLabels } from '../hooks/useCategoryLabels'; interface BeatLoopPanelProps { sequencer: BeatSequencer; soundPalette: SoundPalette; - categoryLabels: CategoryLabels; } -export function BeatLoopPanel({ sequencer, soundPalette, categoryLabels }: BeatLoopPanelProps) { - const categoryIdsInLoop = useMemo(() => { - const categoryIds = new Set( - sequencer.beats.map((beat) => INSTRUMENT_BY_ID[beat.soundId].categoryId), - ); - return INSTRUMENT_CATEGORIES - .filter((category) => categoryIds.has(category.id)) - .map((category) => category.id); +export function BeatLoopPanel({ sequencer, soundPalette }: BeatLoopPanelProps) { + const soundIdsInLoop = useMemo(() => { + const soundIds = new Set(sequencer.beats.map((beat) => beat.soundId)); + return INSTRUMENT_CATALOG + .filter((instrument) => soundIds.has(instrument.id)) + .map((instrument) => instrument.id); }, [sequencer.beats]); - const focusedRingCategoryId = sequencer.focusedCategoryId - && categoryIdsInLoop.includes(sequencer.focusedCategoryId) - ? sequencer.focusedCategoryId + const focusedRingSoundId = sequencer.focusedSoundId + && soundIdsInLoop.includes(sequencer.focusedSoundId) + ? sequencer.focusedSoundId : null; - const hasDeleteTarget = Boolean(sequencer.selectedBeat || focusedRingCategoryId); + const hasDeleteTarget = Boolean(sequencer.selectedBeat || focusedRingSoundId); const deleteLabel = sequencer.selectedBeat ? `Remove ${INSTRUMENT_BY_ID[sequencer.selectedBeat.soundId].label}` - : focusedRingCategoryId - ? `Remove ${categoryLabels.labelByCategoryId[focusedRingCategoryId] ?? INSTRUMENT_CATEGORY_BY_ID[focusedRingCategoryId].label} category` + : focusedRingSoundId + ? `Remove ${INSTRUMENT_BY_ID[focusedRingSoundId].label} ring` : 'Remove sound'; return ( @@ -51,28 +45,26 @@ export function BeatLoopPanel({ sequencer, soundPalette, categoryLabels }: BeatL beatsPerBar={sequencer.beatsPerBar} onChange={sequencer.setBeatsPerBar} /> -
@@ -85,9 +77,8 @@ export function BeatLoopPanel({ sequencer, soundPalette, categoryLabels }: BeatL
@@ -98,7 +89,7 @@ export function BeatLoopPanel({ sequencer, soundPalette, categoryLabels }: BeatL aria-label={deleteLabel} onClick={sequencer.selectedBeat ? sequencer.removeSelectedBeat - : sequencer.removeFocusedCategory} + : sequencer.removeFocusedSound} > @@ -109,12 +100,9 @@ export function BeatLoopPanel({ sequencer, soundPalette, categoryLabels }: BeatL
- ); diff --git a/src/components/BeatSelectionHint.tsx b/src/components/BeatSelectionHint.tsx index b332b11..3e38ed3 100644 --- a/src/components/BeatSelectionHint.tsx +++ b/src/components/BeatSelectionHint.tsx @@ -1,43 +1,36 @@ import { memo } from 'react'; import { getLoopPositionPercent } from '../domain/timeline'; -import { INSTRUMENT_BY_ID, INSTRUMENT_CATEGORY_BY_ID } from '../domain/instruments'; +import { INSTRUMENT_BY_ID } from '../domain/instruments'; import type { BeatPoint } from '../domain/sequencer'; -import type { InstrumentCategoryId } from '../domain/instruments'; -import type { CategoryLabelOverrides } from '../hooks/useCategoryLabels'; +import type { InstrumentId } from '../domain/instruments'; interface BeatSelectionHintProps { selectedBeat: BeatPoint | undefined; - focusedCategoryId: InstrumentCategoryId | null; - categoryLabelById: CategoryLabelOverrides; - colorByCategoryId: Partial>; + focusedSoundId: InstrumentId | null; + colorBySoundId: Partial>; totalSteps: number; } export const BeatSelectionHint = memo(function BeatSelectionHint({ selectedBeat, - focusedCategoryId, - categoryLabelById, - colorByCategoryId, + focusedSoundId, + colorBySoundId, totalSteps, }: BeatSelectionHintProps) { - const selectedInstrument = selectedBeat ? INSTRUMENT_BY_ID[selectedBeat.soundId] : undefined; - const categoryId = selectedInstrument?.categoryId ?? focusedCategoryId; - if (!categoryId) return

; + const soundId = selectedBeat?.soundId ?? focusedSoundId; + if (!soundId) return

; - const category = INSTRUMENT_CATEGORY_BY_ID[categoryId]; - const categoryLabel = categoryLabelById[categoryId] ?? category.label; + const sound = INSTRUMENT_BY_ID[soundId]; return (

- {category.letter} · {categoryLabel} + {sound.label} {selectedBeat && ( <> - · - {selectedBeat.letter} · {selectedInstrument?.label} · Step {getLoopPositionPercent(selectedBeat.step, totalSteps)}% selected diff --git a/src/components/CategoryLegend.tsx b/src/components/CategoryLegend.tsx index 26fb388..fa2a901 100644 --- a/src/components/CategoryLegend.tsx +++ b/src/components/CategoryLegend.tsx @@ -1,124 +1,27 @@ -import { useEffect, useRef, useState } from 'react'; -import { IonAlert, IonButton } from '@ionic/react'; -import { INSTRUMENT_BY_ID, INSTRUMENT_CATEGORY_BY_ID } from '../domain/instruments'; -import type { InstrumentCategoryId } from '../domain/instruments'; -import type { BeatPoint } from '../domain/sequencer'; -import type { CategoryLabelOverrides } from '../hooks/useCategoryLabels'; +import { INSTRUMENT_BY_ID } from '../domain/instruments'; +import type { InstrumentId } from '../domain/instruments'; -interface CategoryLegendProps { - beats: readonly BeatPoint[]; - categoryIds: readonly InstrumentCategoryId[]; - colorByCategoryId: Partial>; - labelByCategoryId: CategoryLabelOverrides; - onRenameCategory: (categoryId: InstrumentCategoryId, label: string) => void; +interface SampleLegendProps { + soundIds: readonly InstrumentId[]; + colorBySoundId: Partial>; } -export function CategoryLegend({ - beats, - categoryIds, - colorByCategoryId, - labelByCategoryId, - onRenameCategory, -}: CategoryLegendProps) { - const [editingCategoryId, setEditingCategoryId] = useState(null); - const longPressTimerRef = useRef(null); - const editingCategory = editingCategoryId - ? INSTRUMENT_CATEGORY_BY_ID[editingCategoryId] - : undefined; - const editingLabel = editingCategory - ? labelByCategoryId[editingCategory.id] ?? editingCategory.label - : ''; - - function clearLongPressTimer(): void { - if (longPressTimerRef.current === null) return; - window.clearTimeout(longPressTimerRef.current); - longPressTimerRef.current = null; - } - - function beginRename(categoryId: InstrumentCategoryId): void { - clearLongPressTimer(); - setEditingCategoryId(categoryId); - } - - function saveRename(value: unknown): void { - if (!editingCategoryId || typeof value !== 'object' || value === null) return; - const label = (value as Record).categoryName; - if (typeof label === 'string') onRenameCategory(editingCategoryId, label); - } - - useEffect(() => () => { - if (longPressTimerRef.current !== null) window.clearTimeout(longPressTimerRef.current); - }, []); - +export function SampleLegend({ soundIds, colorBySoundId }: SampleLegendProps) { return ( - <> -

- {categoryIds.length > 0 ? ( - categoryIds.map((categoryId) => { - const category = INSTRUMENT_CATEGORY_BY_ID[categoryId]; - const label = labelByCategoryId[categoryId] ?? category.label; - const categoryBeats = beats.filter( - (beat) => INSTRUMENT_BY_ID[beat.soundId].categoryId === categoryId, - ); - return ( -
- { - clearLongPressTimer(); - longPressTimerRef.current = window.setTimeout( - () => beginRename(categoryId), - 550, - ); - }} - onPointerUp={clearLongPressTimer} - onPointerLeave={clearLongPressTimer} - onPointerCancel={clearLongPressTimer} - onContextMenu={(event) => { - event.preventDefault(); - beginRename(categoryId); - }} - onKeyDown={(event) => { - if (event.key !== 'Enter' && event.key !== ' ') return; - event.preventDefault(); - beginRename(categoryId); - }} - > - - {category.letter} - {label} - - - {categoryBeats.map((beat) => ( - - {beat.letter} {INSTRUMENT_BY_ID[beat.soundId].label} - - ))} - -
- ); - }) - ) : ( - Added instrument categories will appear here. - )} -
- - saveRename(value) }, - ]} - onDidDismiss={() => setEditingCategoryId(null)} - /> - +
+ {soundIds.length > 0 ? ( + soundIds.map((soundId) => ( + + + {INSTRUMENT_BY_ID[soundId].label} + + )) + ) : ( + Added samples will appear here. + )} +
); } diff --git a/src/components/CategoryRingSelector.tsx b/src/components/CategoryRingSelector.tsx index 64947ec..b96f285 100644 --- a/src/components/CategoryRingSelector.tsx +++ b/src/components/CategoryRingSelector.tsx @@ -1,96 +1 @@ -import { memo } from 'react'; -import { IonButton, IonIcon, IonPopover } from '@ionic/react'; -import { chevronDown } from 'ionicons/icons'; -import { INSTRUMENT_CATEGORY_BY_ID } from '../domain/instruments'; -import type { InstrumentCategoryId } from '../domain/instruments'; -import type { CategoryLabelOverrides } from '../hooks/useCategoryLabels'; -import { usePopoverDisclosure } from '../hooks/usePopoverDisclosure'; - -interface CategoryRingSelectorProps { - categoryIds: readonly InstrumentCategoryId[]; - focusedCategoryId: InstrumentCategoryId | null; - colorByCategoryId: Partial>; - labelByCategoryId: CategoryLabelOverrides; - onSelectCategory: (categoryId: InstrumentCategoryId) => void; -} - -export const CategoryRingSelector = memo(function CategoryRingSelector({ - categoryIds, - focusedCategoryId, - colorByCategoryId, - labelByCategoryId, - onSelectCategory, -}: CategoryRingSelectorProps) { - const popover = usePopoverDisclosure(); - const focusedCategory = focusedCategoryId - ? INSTRUMENT_CATEGORY_BY_ID[focusedCategoryId] - : undefined; - const focusedLabel = focusedCategory - ? labelByCategoryId[focusedCategory.id] ?? focusedCategory.label - : undefined; - - return ( - <> - - - {focusedCategory && ( - <> - - {focusedCategory.letter} - - )} - {focusedLabel ? `${focusedLabel} ring` : 'Category rings'} - - - - - -
- {categoryIds.map((categoryId) => { - const category = INSTRUMENT_CATEGORY_BY_ID[categoryId]; - const label = labelByCategoryId[categoryId] ?? category.label; - const selected = categoryId === focusedCategoryId; - return ( - onSelectCategory(categoryId)} - > - - - {category.letter} - {label} ring - - - ); - })} -
-
- - ); -}); +export { SoundRingSelector as CategoryRingSelector } from './SoundRingSelector'; diff --git a/src/components/CircularTimeline.tsx b/src/components/CircularTimeline.tsx index 1db190a..6616f9d 100644 --- a/src/components/CircularTimeline.tsx +++ b/src/components/CircularTimeline.tsx @@ -8,7 +8,7 @@ import { import { getPointForStep, getLoopPositionPercent, - getCategoryOrbitRadius, + getSoundOrbitRadius, getStepFromPosition, ORBIT_RADIUS, SVG_CENTER, @@ -16,22 +16,20 @@ import { getStackedPosition, wrapStepPosition, } from '../domain/timeline'; -import { INSTRUMENT_BY_ID, INSTRUMENT_CATEGORY_BY_ID } from '../domain/instruments'; -import type { InstrumentCategoryId } from '../domain/instruments'; -import type { CategoryLabelOverrides } from '../hooks/useCategoryLabels'; +import { INSTRUMENT_BY_ID } from '../domain/instruments'; +import type { InstrumentId } from '../domain/instruments'; interface CircularTimelineProps { beats: BeatPoint[]; - categoryIds: readonly InstrumentCategoryId[]; - colorByCategoryId: Partial>; - labelByCategoryId: CategoryLabelOverrides; + soundIds: readonly InstrumentId[]; + colorBySoundId: Partial>; currentStep: number; totalSteps: number; - focusedCategoryId: InstrumentCategoryId | null; + focusedSoundId: InstrumentId | null; selectedBeatId: string | null; onMoveBeat: (beatId: string, step: number) => void; onSelectBeat: (beatId: string) => void; - onSelectCategory: (categoryId: InstrumentCategoryId) => void; + onSelectSound: (soundId: InstrumentId) => void; } function getPointerCoordinates(event: ReactPointerEvent) { @@ -44,29 +42,25 @@ function getPointerCoordinates(event: ReactPointerEvent) { export function CircularTimeline({ beats, - categoryIds, - colorByCategoryId, - labelByCategoryId, + soundIds, + colorBySoundId, currentStep, totalSteps, - focusedCategoryId, + focusedSoundId, selectedBeatId, onMoveBeat, onSelectBeat, - onSelectCategory, + onSelectSound, }: CircularTimelineProps) { const draggingBeatIdRef = useRef(null); const selectedBeatSoundId = beats.find((beat) => beat.id === selectedBeatId)?.soundId; - const selectedBeatCategoryId = selectedBeatSoundId - ? INSTRUMENT_BY_ID[selectedBeatSoundId].categoryId - : null; - const activeCategoryId = selectedBeatCategoryId ?? focusedCategoryId; - const orderedCategoryIds = activeCategoryId + const activeSoundId = selectedBeatSoundId ?? focusedSoundId; + const orderedSoundIds = activeSoundId ? [ - ...categoryIds.filter((categoryId) => categoryId === activeCategoryId), - ...categoryIds.filter((categoryId) => categoryId !== activeCategoryId), + ...soundIds.filter((soundId) => soundId === activeSoundId), + ...soundIds.filter((soundId) => soundId !== activeSoundId), ] - : categoryIds; + : soundIds; function handlePointerDown(event: ReactPointerEvent, beatId: string): void { event.preventDefault(); @@ -102,11 +96,11 @@ export function CircularTimeline({ function handleOrbitKeyDown( event: ReactKeyboardEvent, - categoryId: InstrumentCategoryId, + soundId: InstrumentId, ): void { if (event.key !== 'Enter' && event.key !== ' ') return; event.preventDefault(); - onSelectCategory(categoryId); + onSelectSound(soundId); } return ( @@ -114,42 +108,26 @@ export function CircularTimeline({ className="circular-timeline" viewBox={`0 0 ${SVG_SIZE} ${SVG_SIZE}`} role="group" - aria-label={`${totalSteps}-step circular beat timeline with one orbit per instrument category. Drag beat points to move them.`} + aria-label={`${totalSteps}-step circular beat timeline with one orbit per sound. Drag beat points to move them.`} onPointerMove={handlePointerMove} onPointerUp={() => { draggingBeatIdRef.current = null; }} onPointerCancel={() => { draggingBeatIdRef.current = null; }} > - {orderedCategoryIds.map((categoryId, categoryIndex) => { - const category = INSTRUMENT_CATEGORY_BY_ID[categoryId]; - const color = colorByCategoryId[categoryId] ?? '#b8b4e0'; - const radius = getCategoryOrbitRadius(categoryIndex, orderedCategoryIds.length); - const letterPosition = getPointForStep(totalSteps * 0.12, radius, SVG_CENTER, totalSteps); - const focused = categoryId === activeCategoryId; + {orderedSoundIds.map((soundId, soundIndex) => { + const focused = soundId === activeSoundId; return ( - - - - + ); })} @@ -180,12 +158,12 @@ export function CircularTimeline({ ); })} - {orderedCategoryIds.map((categoryId, categoryIndex) => ( + {orderedSoundIds.map((soundId, soundIndex) => ( onSelectCategory(categoryId)} - onKeyDown={(event) => handleOrbitKeyDown(event, categoryId)} - onFocus={() => onSelectCategory(categoryId)} + aria-label={`Select ${INSTRUMENT_BY_ID[soundId].label} ring`} + aria-pressed={soundId === activeSoundId} + onClick={() => onSelectSound(soundId)} + onKeyDown={(event) => handleOrbitKeyDown(event, soundId)} + onFocus={() => onSelectSound(soundId)} /> ))} @@ -213,23 +191,22 @@ export function CircularTimeline({ {beats.map((beat) => { const instrument = INSTRUMENT_BY_ID[beat.soundId]; - const category = INSTRUMENT_CATEGORY_BY_ID[instrument.categoryId]; - const categoryIndex = orderedCategoryIds.indexOf(instrument.categoryId); + const soundIndex = orderedSoundIds.indexOf(beat.soundId); const point = getStackedPosition( beat, beats, totalSteps, - getCategoryOrbitRadius(categoryIndex, orderedCategoryIds.length), + getSoundOrbitRadius(soundIndex, orderedSoundIds.length), ); const selected = selectedBeatId === beat.id; return ( handlePointerDown(event, beat.id)} onClick={() => onSelectBeat(beat.id)} diff --git a/src/components/SoundRingSelector.tsx b/src/components/SoundRingSelector.tsx index 029ba9a..d1e53b6 100644 --- a/src/components/SoundRingSelector.tsx +++ b/src/components/SoundRingSelector.tsx @@ -1 +1,82 @@ -export { CategoryRingSelector as SoundRingSelector } from './CategoryRingSelector'; +import { memo } from 'react'; +import { IonButton, IonIcon, IonPopover } from '@ionic/react'; +import { chevronDown } from 'ionicons/icons'; +import { INSTRUMENT_BY_ID } from '../domain/instruments'; +import type { InstrumentId } from '../domain/instruments'; +import { usePopoverDisclosure } from '../hooks/usePopoverDisclosure'; + +interface SoundRingSelectorProps { + soundIds: readonly InstrumentId[]; + focusedSoundId: InstrumentId | null; + colorBySoundId: Partial>; + onSelectSound: (soundId: InstrumentId) => void; +} + +export const SoundRingSelector = memo(function SoundRingSelector({ + soundIds, + focusedSoundId, + colorBySoundId, + onSelectSound, +}: SoundRingSelectorProps) { + const popover = usePopoverDisclosure(); + const focusedSound = focusedSoundId ? INSTRUMENT_BY_ID[focusedSoundId] : undefined; + + return ( + <> + + + {focusedSoundId && ( + + )} + {focusedSound ? `${focusedSound.label} ring` : 'Sound rings'} + + + + + +
+ {soundIds.map((soundId) => { + const selected = soundId === focusedSoundId; + return ( + onSelectSound(soundId)} + > + + + {INSTRUMENT_BY_ID[soundId].label} ring + + + ); + })} +
+
+ + ); +}); diff --git a/src/domain/sequencerReducer.ts b/src/domain/sequencerReducer.ts index 5a465f1..ce566f4 100644 --- a/src/domain/sequencerReducer.ts +++ b/src/domain/sequencerReducer.ts @@ -5,13 +5,13 @@ import { TEMPO_BPM, } from './sequencer'; import type { BeatsPerBar, BeatPoint } from './sequencer'; -import { clampPitchSemitones, INSTRUMENT_BY_ID } from './instruments'; -import type { InstrumentCategoryId } from './instruments'; +import { clampPitchSemitones } from './instruments'; +import type { InstrumentId } from './instruments'; export interface SequencerState { beats: BeatPoint[]; selectedBeatId: string | null; - focusedCategoryId: InstrumentCategoryId | null; + focusedSoundId: InstrumentId | 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: 'category-selected'; categoryId: InstrumentCategoryId } + | { type: 'sound-selected'; soundId: InstrumentId } | { type: 'selected-beat-removed' } - | { type: 'focused-category-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 }; @@ -32,7 +32,7 @@ export function createInitialSequencerState(): SequencerState { return { beats: [], selectedBeatId: null, - focusedCategoryId: null, + focusedSoundId: 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, - focusedCategoryId: INSTRUMENT_BY_ID[action.beat.soundId].categoryId, + focusedSoundId: action.beat.soundId, }; case 'beat-moved': return { @@ -73,40 +73,35 @@ export function sequencerReducer(state: SequencerState, action: SequencerAction) return { ...state, selectedBeatId: beat.id, - focusedCategoryId: INSTRUMENT_BY_ID[beat.soundId].categoryId, + focusedSoundId: beat.soundId, }; } - case 'category-selected': - return { ...state, focusedCategoryId: action.categoryId, selectedBeatId: null }; + 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 removedCategoryId = INSTRUMENT_BY_ID[removedBeat.soundId].categoryId; - const categoryRemains = beats.some( - (beat) => INSTRUMENT_BY_ID[beat.soundId].categoryId === removedCategoryId, - ); + const soundRemains = beats.some((beat) => beat.soundId === removedBeat.soundId); return { ...state, beats, selectedBeatId: null, - focusedCategoryId: state.focusedCategoryId === removedCategoryId && !categoryRemains - ? beats[0] ? INSTRUMENT_BY_ID[beats[0].soundId].categoryId : null - : state.focusedCategoryId, + focusedSoundId: state.focusedSoundId === removedBeat.soundId && !soundRemains + ? beats[0]?.soundId ?? null + : 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, - ); + case 'focused-sound-removed': { + if (!state.focusedSoundId) return state; + const beats = state.beats.filter((beat) => beat.soundId !== state.focusedSoundId); return { ...state, beats, selectedBeatId: null, - focusedCategoryId: beats[0] ? INSTRUMENT_BY_ID[beats[0].soundId].categoryId : null, + focusedSoundId: beats[0]?.soundId ?? null, }; } case 'tempo-changed': @@ -115,16 +110,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 focusedCategoryRemains = visibleBeats.some( - (beat) => INSTRUMENT_BY_ID[beat.soundId].categoryId === state.focusedCategoryId, + const focusedSoundRemains = visibleBeats.some( + (beat) => beat.soundId === state.focusedSoundId, ); return { ...state, beatsPerBar: action.beatsPerBar, selectedBeatId: selectedBeat?.id ?? null, - focusedCategoryId: focusedCategoryRemains - ? state.focusedCategoryId - : visibleBeats[0] ? INSTRUMENT_BY_ID[visibleBeats[0].soundId].categoryId : null, + focusedSoundId: focusedSoundRemains + ? state.focusedSoundId + : visibleBeats[0]?.soundId ?? null, }; } case 'template-loaded': { @@ -133,7 +128,7 @@ export function sequencerReducer(state: SequencerState, action: SequencerAction) ...state, beats: action.beats, selectedBeatId: firstBeat?.id ?? null, - focusedCategoryId: firstBeat ? INSTRUMENT_BY_ID[firstBeat.soundId].categoryId : null, + focusedSoundId: firstBeat?.soundId ?? null, tempoBpm: clampTempoBpm(action.tempoBpm), beatsPerBar: action.beatsPerBar, }; diff --git a/src/domain/timeline.ts b/src/domain/timeline.ts index afdfde0..87a62a6 100644 --- a/src/domain/timeline.ts +++ b/src/domain/timeline.ts @@ -1,5 +1,4 @@ import type { BeatPoint } from './sequencer'; -import { INSTRUMENT_BY_ID } from './instruments'; import { PLAYBACK_SUBSTEPS_PER_STEP, TOTAL_STEPS, @@ -48,12 +47,10 @@ export function wrapStepPosition(step: number, totalSteps: number): number { return ((step % totalSteps) + totalSteps) % totalSteps; } -export function getCategoryOrbitRadius(categoryIndex: number, categoryCount: number): number { - return ORBIT_RADIUS * (categoryCount + 1 - categoryIndex) / (categoryCount + 1); +export function getSoundOrbitRadius(soundIndex: number, soundCount: number): number { + return ORBIT_RADIUS * (soundCount + 1 - soundIndex) / (soundCount + 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; @@ -67,8 +64,7 @@ export function getStackedPosition( ): CirclePoint { const siblings = beats .filter((candidate) => ( - INSTRUMENT_BY_ID[candidate.soundId].categoryId - === INSTRUMENT_BY_ID[beat.soundId].categoryId + candidate.soundId === beat.soundId && getPlaybackTick(candidate.step, totalSteps) === getPlaybackTick(beat.step, totalSteps) )) .sort((first, second) => first.id.localeCompare(second.id)); diff --git a/src/hooks/useBeatSequencer.ts b/src/hooks/useBeatSequencer.ts index 59a396f..a3f9457 100644 --- a/src/hooks/useBeatSequencer.ts +++ b/src/hooks/useBeatSequencer.ts @@ -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, diff --git a/src/hooks/useBeatStudio.ts b/src/hooks/useBeatStudio.ts index 2cf92fd..b7c3b4c 100644 --- a/src/hooks/useBeatStudio.ts +++ b/src/hooks/useBeatStudio.ts @@ -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 }; } diff --git a/src/hooks/useSoundPalette.ts b/src/hooks/useSoundPalette.ts index 2883060..98db676 100644 --- a/src/hooks/useSoundPalette.ts +++ b/src/hooks/useSoundPalette.ts @@ -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>; colorByCategoryId: Partial>; activateSounds: (instrumentIds: readonly InstrumentId[]) => void; } export function useSoundPalette(): SoundPalette { - const [activeCategories, setActiveCategories] = useState([]); + const [activeSounds, setActiveSounds] = useState([]); 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> = {}; - activeCategories.forEach((category) => { - colors[category.id] = category.color; + const colorBySoundId = useMemo(() => { + const colors: Partial> = {}; + activeSounds.forEach((sound) => { + colors[sound.id] = sound.color; }); return colors; - }, [activeCategories]); + }, [activeSounds]); - return { activeCategories, colorByCategoryId, activateSounds }; + const colorByCategoryId = useMemo(() => { + const colors: Partial> = {}; + 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 }; } diff --git a/src/ionicons.d.ts b/src/ionicons.d.ts index 14f6951..3808760 100644 --- a/src/ionicons.d.ts +++ b/src/ionicons.d.ts @@ -1,5 +1,6 @@ declare module 'ionicons/icons' { export const add: string; + export const arrowBack: string; export const chevronDown: string; export const ellipsisVertical: string; export const musicalNotes: string; diff --git a/src/sequencer.test.ts b/src/sequencer.test.ts index fb0c844..9ad8911 100644 --- a/src/sequencer.test.ts +++ b/src/sequencer.test.ts @@ -33,7 +33,7 @@ import { TEMPO_BPM, } from './sequencer'; import type { BeatPoint } from './sequencer'; -import { getStackedPosition } from './domain/timeline'; +import { getSoundOrbitRadius, getStackedPosition } from './domain/timeline'; describe('circular step positions', () => { it('derives playback timing from tempo and fine-grained subdivisions', () => { @@ -76,15 +76,16 @@ describe('circular step positions', () => { expect(getStepFromPosition(point.x, point.y)).toBeCloseTo(fractionalStep, 8); }); - it('visually separates same-category instruments at the same beat tick', () => { + it('gives each sound its own orbit and stacks repeated beats of that sound', () => { const beats: BeatPoint[] = [ { id: 'kick-a', soundId: 'kick', letter: 'a', step: 4 }, - { id: 'snare-b', soundId: 'snare', letter: 'b', step: 4 }, + { id: 'kick-b', soundId: 'kick', letter: 'b', step: 4 }, ]; const kickPosition = getStackedPosition(beats[0], beats, TOTAL_STEPS, 180); - const snarePosition = getStackedPosition(beats[1], beats, TOTAL_STEPS, 180); + const secondKickPosition = getStackedPosition(beats[1], beats, TOTAL_STEPS, 180); - expect(kickPosition).not.toEqual(snarePosition); + expect(kickPosition).not.toEqual(secondKickPosition); + expect(getSoundOrbitRadius(0, 2)).not.toBe(getSoundOrbitRadius(1, 2)); }); it('wraps fractional positions and reports loop percentages', () => { @@ -175,7 +176,7 @@ describe('instrument catalog and pitch', () => { }); describe('sequencer state transitions', () => { - it('loads a template and removes every instrument in the focused category ring', () => { + it('loads a template and removes every beat in the focused sound ring', () => { const templateBeats: BeatPoint[] = [ { id: 'kick-1', soundId: 'kick', letter: 'a', step: 0 }, { id: 'snare-1', soundId: 'snare', letter: 'b', step: 4 }, @@ -189,12 +190,12 @@ describe('sequencer state transitions', () => { }); expect(loaded.selectedBeatId).toBe('kick-1'); - expect(loaded.focusedCategoryId).toBe('drums'); + expect(loaded.focusedSoundId).toBe('kick'); - const afterRingRemoval = sequencerReducer(loaded, { type: 'focused-category-removed' }); - expect(afterRingRemoval.beats).toEqual([templateBeats[2]]); + const afterRingRemoval = sequencerReducer(loaded, { type: 'focused-sound-removed' }); + expect(afterRingRemoval.beats).toEqual([templateBeats[1], templateBeats[2]]); expect(afterRingRemoval.selectedBeatId).toBeNull(); - expect(afterRingRemoval.focusedCategoryId).toBe('keys'); + expect(afterRingRemoval.focusedSoundId).toBe('snare'); }); it('clears out-of-range selections when the time signature gets shorter', () => { @@ -216,14 +217,15 @@ describe('sequencer state transitions', () => { expect(shortened.beats).toEqual(beats); expect(shortened.selectedBeatId).toBeNull(); - expect(shortened.focusedCategoryId).toBe('drums'); + expect(shortened.focusedSoundId).toBe('hi-hat'); }); - it('removes one category ring and focuses a remaining category', () => { + it('removes one sound ring without removing other sounds in the same category', () => { const beats: BeatPoint[] = [ { id: 'kick-1', soundId: 'kick', letter: 'a', step: 0 }, { id: 'hat-1', soundId: 'hi-hat', letter: 'b', step: 4 }, { id: 'bass-1', soundId: 'bass', letter: 'a', step: 8 }, + { id: 'bass-2', soundId: 'bass', letter: 'b', step: 12 }, ]; const loaded = sequencerReducer(createInitialSequencerState(), { type: 'template-loaded', @@ -232,13 +234,13 @@ describe('sequencer state transitions', () => { beatsPerBar: 4, }); const bassSelected = sequencerReducer(loaded, { - type: 'category-selected', - categoryId: 'bass', + type: 'sound-selected', + soundId: 'bass', }); - const removed = sequencerReducer(bassSelected, { type: 'focused-category-removed' }); + const removed = sequencerReducer(bassSelected, { type: 'focused-sound-removed' }); expect(removed.beats).toEqual([beats[0], beats[1]]); - expect(removed.focusedCategoryId).toBe('drums'); + expect(removed.focusedSoundId).toBe('kick'); expect(removed.selectedBeatId).toBeNull(); }); diff --git a/src/styles.css b/src/styles.css index 59485c1..cec47fe 100644 --- a/src/styles.css +++ b/src/styles.css @@ -291,26 +291,24 @@ ion-content.app-content { } .about-return-row { - margin-top: 24px; - text-align: center; + margin: 0 12px 24px; + text-align: left; } .about-return-button { - --background: var(--accent); - --background-hover: var(--accent-hover); - --background-activated: var(--accent-pressed); - --color: #111116; - --border-radius: 9px; + --background: transparent; + --background-hover: rgba(var(--primary-rgb), 0.08); + --background-activated: rgba(var(--primary-rgb), 0.12); + --border-radius: 8px; + --color: var(--muted); margin: 0; - font-size: 12px; + font-size: 11px; font-weight: 600; text-transform: none; } -.about-return-row p { - margin: 12px 0 0; - color: var(--soft); - font-size: 10px; +.about-return-button ion-icon { + font-size: 17px; } .studio-heading { @@ -586,17 +584,6 @@ ion-content.app-content { transition: r 720ms cubic-bezier(0.22, 1.15, 0.36, 1); } -.timeline-category-letter circle { - fill: var(--base); - stroke: currentColor; - stroke-width: 1.5; -} - -.timeline-category-letter text { - fill: currentColor; - font: 700 9px var(--mono); -} - .timeline-tick line { stroke: rgba(var(--primary-rgb), 0.34); stroke-width: 1; @@ -734,13 +721,13 @@ ion-content.app-content { .hint-swatch, .legend-swatch { - width: 8px; - height: 8px; flex: 0 0 auto; border-radius: 50%; } .hint-swatch { + width: 8px; + height: 8px; display: inline-block; margin-right: 7px; vertical-align: middle; @@ -756,41 +743,20 @@ ion-content.app-content { display: flex; flex-wrap: wrap; justify-content: center; - align-items: flex-start; - gap: 8px 12px; + align-items: center; + gap: 8px 14px; padding-top: 17px; } -.legend-category-entry { - display: flex; - max-width: 220px; - flex-direction: column; - align-items: center; -} - -.legend-category { +.legend-sample { display: inline-flex; - height: 30px; align-items: center; - margin: 0; - --color: var(--muted); - --background: transparent; - --background-hover: rgba(var(--primary-rgb), 0.08); - --background-activated: rgba(var(--primary-rgb), 0.12); - --border-radius: 7px; - font-size: 10px; - text-transform: none; - touch-action: manipulation; - user-select: none; -} - -.legend-category::part(native) { gap: 6px; - min-height: 30px; - padding: 0 8px; + color: var(--soft); + font-size: 10px; + white-space: nowrap; } -.legend-category-letter, .category-letter-badge { display: inline-flex; width: 17px; @@ -804,29 +770,9 @@ ion-content.app-content { font: 600 9px var(--mono); } -.legend-category .legend-category-letter { - color: var(--muted); -} - -.legend-beat-list { - display: flex; - flex-wrap: wrap; - justify-content: center; - gap: 2px 7px; - padding: 0 4px; - color: var(--soft); - font-size: 9px; - line-height: 1.5; -} - -.legend-beat-list span { - white-space: nowrap; -} - -.legend-beat-list strong { - color: var(--ink); - font-family: var(--mono); - text-transform: lowercase; +.legend-swatch { + width: 7px; + height: 7px; } .legend-empty { @@ -834,11 +780,6 @@ ion-content.app-content { font-size: 11px; } -.legend-swatch { - width: 7px; - height: 7px; -} - .audio-error { margin: 13px auto 0; color: #ffad8b; @@ -895,6 +836,7 @@ ion-content.app-content { width: 170px; min-width: 140px; height: 44px; + min-height: 44px; flex: 1 1 150px; margin: 0; --background: var(--surface-raised); @@ -1274,6 +1216,10 @@ ion-popover.instrument-picker-popover { padding: 31px 0 24px; } + .about-return-row { + margin-bottom: 20px; + } + .about-heading h1 { font-size: 36px; } diff --git a/src/views/AboutView.tsx b/src/views/AboutView.tsx index 57c3d02..b1a0045 100644 --- a/src/views/AboutView.tsx +++ b/src/views/AboutView.tsx @@ -1,4 +1,5 @@ -import { IonButton } from '@ionic/react'; +import { IonButton, IonIcon } from '@ionic/react'; +import { arrowBack } from 'ionicons/icons'; import { PROJECT_INFO } from '../config/projectInfo'; interface AboutViewProps { @@ -8,6 +9,18 @@ interface AboutViewProps { export function AboutView({ onReturnToStudio }: AboutViewProps) { return (
+
+ + + Back to the beat studio + +
+

Project details

About BeatHound

@@ -102,11 +115,6 @@ export function AboutView({ onReturnToStudio }: AboutViewProps) { -
- - Back to the beat studio - -
); } diff --git a/src/views/StudioView.tsx b/src/views/StudioView.tsx index a4a422c..378405b 100644 --- a/src/views/StudioView.tsx +++ b/src/views/StudioView.tsx @@ -24,7 +24,6 @@ export function StudioView({ studio }: StudioViewProps) { {studio.sequencer.audioError && (