Build BeatHound sequencer app
This commit is contained in:
1 parent
226ababcf1
commit
e23825cdb1
49 files changed
+6455
-2
No files matched your search
@@ -0,0 +1,197 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { BEAT_TEMPLATES } from './sequencer';
|
||||
import {
|
||||
createInitialSequencerState,
|
||||
sequencerReducer,
|
||||
} from './domain/sequencerReducer';
|
||||
import { generateUniqueSoundColor } from './domain/soundColors';
|
||||
import {
|
||||
getNextAvailableStep,
|
||||
getPlaybackTickDurationMs,
|
||||
getLoopPositionPercent,
|
||||
getPointForStep,
|
||||
getStepFromPosition,
|
||||
getTotalSteps,
|
||||
MAX_TEMPO_BPM,
|
||||
MIN_TEMPO_BPM,
|
||||
SVG_CENTER,
|
||||
wrapStepPosition,
|
||||
TOTAL_STEPS,
|
||||
TEMPO_BPM,
|
||||
} from './sequencer';
|
||||
import type { BeatPoint } from './sequencer';
|
||||
|
||||
describe('circular step positions', () => {
|
||||
it('derives playback timing from tempo and fine-grained subdivisions', () => {
|
||||
expect(getPlaybackTickDurationMs(120)).toBeCloseTo(60_000 / 120 / 16, 8);
|
||||
});
|
||||
|
||||
it('maps each of the 16 points back to its step', () => {
|
||||
for (let step = 0; step < TOTAL_STEPS; step += 1) {
|
||||
const point = getPointForStep(step, 180);
|
||||
expect(getStepFromPosition(point.x, point.y)).toBeCloseTo(step, 8);
|
||||
}
|
||||
});
|
||||
|
||||
it('wraps points just before the first step to the end of the loop', () => {
|
||||
const point = getPointForStep(TOTAL_STEPS - 1, 180);
|
||||
expect(getStepFromPosition(point.x, point.y)).toBeCloseTo(TOTAL_STEPS - 1, 8);
|
||||
});
|
||||
|
||||
it('maps all steps around a three-beat bar', () => {
|
||||
const totalSteps = getTotalSteps(3);
|
||||
|
||||
for (let step = 0; step < totalSteps; step += 1) {
|
||||
const point = getPointForStep(step, 180, SVG_CENTER, totalSteps);
|
||||
expect(getStepFromPosition(point.x, point.y, SVG_CENTER, totalSteps)).toBeCloseTo(step, 8);
|
||||
}
|
||||
});
|
||||
|
||||
it('maps all steps around an eight-beat bar', () => {
|
||||
const totalSteps = getTotalSteps(8);
|
||||
|
||||
for (let step = 0; step < totalSteps; step += 1) {
|
||||
const point = getPointForStep(step, 180, SVG_CENTER, totalSteps);
|
||||
expect(getStepFromPosition(point.x, point.y, SVG_CENTER, totalSteps)).toBeCloseTo(step, 8);
|
||||
}
|
||||
});
|
||||
|
||||
it('preserves fractional positions between the regular steps', () => {
|
||||
const fractionalStep = 3.375;
|
||||
const point = getPointForStep(fractionalStep, 180);
|
||||
expect(getStepFromPosition(point.x, point.y)).toBeCloseTo(fractionalStep, 8);
|
||||
});
|
||||
|
||||
it('wraps fractional positions and reports loop percentages', () => {
|
||||
expect(wrapStepPosition(16.25, 16)).toBeCloseTo(0.25);
|
||||
expect(wrapStepPosition(-0.25, 16)).toBeCloseTo(15.75);
|
||||
expect(getLoopPositionPercent(4, 16)).toBe(25);
|
||||
});
|
||||
|
||||
it('adds a sound at the next empty step', () => {
|
||||
const beats: BeatPoint[] = [
|
||||
{ id: 'first', soundId: 'kick', step: 0 },
|
||||
{ id: 'second', soundId: 'hi-hat', step: 1 },
|
||||
];
|
||||
expect(getNextAvailableStep(beats)).toBe(2);
|
||||
});
|
||||
|
||||
it('reuses the first step when every step is occupied', () => {
|
||||
const beats: BeatPoint[] = Array.from({ length: TOTAL_STEPS }, (_, step) => ({
|
||||
id: `beat-${step}`,
|
||||
soundId: 'kick',
|
||||
step,
|
||||
}));
|
||||
expect(getNextAvailableStep(beats)).toBe(0);
|
||||
});
|
||||
|
||||
it('reuses the first step when a three-beat bar is full', () => {
|
||||
const totalSteps = getTotalSteps(3);
|
||||
const beats: BeatPoint[] = Array.from({ length: totalSteps }, (_, step) => ({
|
||||
id: `beat-${step}`,
|
||||
soundId: 'kick',
|
||||
step,
|
||||
}));
|
||||
expect(getNextAvailableStep(beats, totalSteps)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sound colors', () => {
|
||||
it('generates a distinct color for each added sound', () => {
|
||||
const colors: string[] = [];
|
||||
|
||||
for (let soundIndex = 0; soundIndex < 8; soundIndex += 1) {
|
||||
const color = generateUniqueSoundColor(colors);
|
||||
expect(colors).not.toContain(color);
|
||||
colors.push(color);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('sequencer state transitions', () => {
|
||||
it('loads a template with a focused first beat and removes a focused sound ring', () => {
|
||||
const templateBeats: BeatPoint[] = [
|
||||
{ id: 'kick-1', soundId: 'kick', step: 0 },
|
||||
{ id: 'kick-2', soundId: 'kick', step: 4 },
|
||||
{ id: 'hat-1', soundId: 'hi-hat', step: 8 },
|
||||
];
|
||||
const loaded = sequencerReducer(createInitialSequencerState(), {
|
||||
type: 'template-loaded',
|
||||
beats: templateBeats,
|
||||
tempoBpm: 120,
|
||||
beatsPerBar: 4,
|
||||
});
|
||||
|
||||
expect(loaded.selectedBeatId).toBe('kick-1');
|
||||
expect(loaded.focusedSoundId).toBe('kick');
|
||||
|
||||
const afterRingRemoval = sequencerReducer(loaded, { type: 'focused-sound-removed' });
|
||||
expect(afterRingRemoval.beats).toEqual([templateBeats[2]]);
|
||||
expect(afterRingRemoval.selectedBeatId).toBeNull();
|
||||
expect(afterRingRemoval.focusedSoundId).toBe('hi-hat');
|
||||
});
|
||||
|
||||
it('clears out-of-range selections when the time signature gets shorter', () => {
|
||||
const beats: BeatPoint[] = [
|
||||
{ id: 'hat-1', soundId: 'hi-hat', step: 0 },
|
||||
{ id: 'kick-1', soundId: 'kick', step: 12 },
|
||||
];
|
||||
const loaded = sequencerReducer(createInitialSequencerState(), {
|
||||
type: 'template-loaded',
|
||||
beats,
|
||||
tempoBpm: 120,
|
||||
beatsPerBar: 4,
|
||||
});
|
||||
const selected = sequencerReducer(loaded, { type: 'beat-selected', beatId: 'kick-1' });
|
||||
const shortened = sequencerReducer(selected, {
|
||||
type: 'time-signature-changed',
|
||||
beatsPerBar: 2,
|
||||
});
|
||||
|
||||
expect(shortened.beats).toEqual(beats);
|
||||
expect(shortened.selectedBeatId).toBeNull();
|
||||
expect(shortened.focusedSoundId).toBe('hi-hat');
|
||||
});
|
||||
|
||||
it('removes a focused sound and keeps the remaining ring selected', () => {
|
||||
const beats: BeatPoint[] = [
|
||||
{ id: 'kick-1', soundId: 'kick', step: 0 },
|
||||
{ id: 'hat-1', soundId: 'hi-hat', step: 4 },
|
||||
];
|
||||
const loaded = sequencerReducer(createInitialSequencerState(), {
|
||||
type: 'template-loaded',
|
||||
beats,
|
||||
tempoBpm: TEMPO_BPM,
|
||||
beatsPerBar: 4,
|
||||
});
|
||||
const hatSelected = sequencerReducer(loaded, { type: 'sound-selected', soundId: 'hi-hat' });
|
||||
const removed = sequencerReducer(hatSelected, { type: 'focused-sound-removed' });
|
||||
|
||||
expect(removed.beats).toEqual([beats[0]]);
|
||||
expect(removed.focusedSoundId).toBe('kick');
|
||||
expect(removed.selectedBeatId).toBeNull();
|
||||
});
|
||||
|
||||
it('clamps tempo changes to the supported range', () => {
|
||||
const initial = createInitialSequencerState();
|
||||
const tooSlow = sequencerReducer(initial, { type: 'tempo-changed', tempoBpm: 10 });
|
||||
const tooFast = sequencerReducer(initial, { type: 'tempo-changed', tempoBpm: 300 });
|
||||
|
||||
expect(tooSlow.tempoBpm).toBe(MIN_TEMPO_BPM);
|
||||
expect(tooFast.tempoBpm).toBe(MAX_TEMPO_BPM);
|
||||
});
|
||||
});
|
||||
|
||||
describe('dance templates', () => {
|
||||
it('keeps each beat inside its template time signature', () => {
|
||||
BEAT_TEMPLATES.forEach((template) => {
|
||||
const totalSteps = getTotalSteps(template.beatsPerBar);
|
||||
expect(template.tempoBpm).toBeGreaterThanOrEqual(MIN_TEMPO_BPM);
|
||||
expect(template.tempoBpm).toBeLessThanOrEqual(MAX_TEMPO_BPM);
|
||||
template.beats.forEach((beat) => {
|
||||
expect(beat.step).toBeGreaterThanOrEqual(0);
|
||||
expect(beat.step).toBeLessThan(totalSteps);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user