140 lines
4.5 KiB
TypeScript
140 lines
4.5 KiB
TypeScript
import {
|
|
clampTempoBpm,
|
|
DEFAULT_BEATS_PER_BAR,
|
|
getTotalSteps,
|
|
TEMPO_BPM,
|
|
} from './sequencer';
|
|
import type { BeatsPerBar, BeatPoint } from './sequencer';
|
|
import { clampPitchSemitones } from './instruments';
|
|
import type { InstrumentId } from './instruments';
|
|
|
|
export interface SequencerState {
|
|
beats: BeatPoint[];
|
|
selectedBeatId: string | null;
|
|
focusedSoundId: InstrumentId | null;
|
|
tempoBpm: number;
|
|
beatsPerBar: BeatsPerBar;
|
|
}
|
|
|
|
export type SequencerAction =
|
|
| { type: 'beat-added'; beat: BeatPoint }
|
|
| { 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: 'selected-beat-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 };
|
|
|
|
export function createInitialSequencerState(): SequencerState {
|
|
return {
|
|
beats: [],
|
|
selectedBeatId: null,
|
|
focusedSoundId: null,
|
|
tempoBpm: TEMPO_BPM,
|
|
beatsPerBar: DEFAULT_BEATS_PER_BAR,
|
|
};
|
|
}
|
|
|
|
function assertNever(action: never): never {
|
|
throw new Error(`Unhandled sequencer action: ${action}`);
|
|
}
|
|
|
|
export function sequencerReducer(state: SequencerState, action: SequencerAction): SequencerState {
|
|
switch (action.type) {
|
|
case 'beat-added':
|
|
return {
|
|
...state,
|
|
beats: [...state.beats, action.beat],
|
|
selectedBeatId: action.beat.id,
|
|
focusedSoundId: action.beat.soundId,
|
|
};
|
|
case 'beat-moved':
|
|
return {
|
|
...state,
|
|
beats: state.beats.map((beat) => (
|
|
beat.id === action.beatId ? { ...beat, step: action.step } : beat
|
|
)),
|
|
};
|
|
case 'beat-pitch-changed':
|
|
return {
|
|
...state,
|
|
beats: state.beats.map((beat) => (
|
|
beat.id === action.beatId
|
|
? { ...beat, pitchSemitones: clampPitchSemitones(action.pitchSemitones) }
|
|
: beat
|
|
)),
|
|
};
|
|
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,
|
|
};
|
|
}
|
|
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 soundRemains = beats.some((beat) => beat.soundId === removedBeat.soundId);
|
|
return {
|
|
...state,
|
|
beats,
|
|
selectedBeatId: null,
|
|
focusedSoundId: state.focusedSoundId === removedBeat.soundId && !soundRemains
|
|
? beats[0]?.soundId ?? null
|
|
: state.focusedSoundId,
|
|
};
|
|
}
|
|
case 'focused-sound-removed': {
|
|
if (!state.focusedSoundId) return state;
|
|
const beats = state.beats.filter((beat) => beat.soundId !== state.focusedSoundId);
|
|
return {
|
|
...state,
|
|
beats,
|
|
selectedBeatId: null,
|
|
focusedSoundId: beats[0]?.soundId ?? null,
|
|
};
|
|
}
|
|
case 'tempo-changed':
|
|
return { ...state, tempoBpm: clampTempoBpm(action.tempoBpm) };
|
|
case 'time-signature-changed': {
|
|
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,
|
|
);
|
|
return {
|
|
...state,
|
|
beatsPerBar: action.beatsPerBar,
|
|
selectedBeatId: selectedBeat?.id ?? null,
|
|
focusedSoundId: focusedSoundRemains
|
|
? state.focusedSoundId
|
|
: visibleBeats[0]?.soundId ?? null,
|
|
};
|
|
}
|
|
case 'template-loaded': {
|
|
const firstBeat = action.beats[0];
|
|
return {
|
|
...state,
|
|
beats: action.beats,
|
|
selectedBeatId: firstBeat?.id ?? null,
|
|
focusedSoundId: firstBeat?.soundId ?? null,
|
|
tempoBpm: clampTempoBpm(action.tempoBpm),
|
|
beatsPerBar: action.beatsPerBar,
|
|
};
|
|
}
|
|
default:
|
|
return assertNever(action);
|
|
}
|
|
}
|