Add category rings and beat labels
This commit is contained in:
1 parent
9d8f277971
commit
4f221ea122
21 files changed
+752
-339
No files matched your search
@@ -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,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user