Files
adminandClaude Opus 5.5 d505f6ed27 Show what the song plays around a pattern as ghost notes (#339)
A bass line is written against the chords it goes with. Until now the
piano roll showed only the pattern itself, so writing a part to fit the
rest of a song meant switching between the Pattern and the Song tab.

- A double-click on a pattern item remembers song, item and the round
  clicked into for that pattern. The place lives in the session, not in
  the project, and goes by the item's id, so moving the item moves the
  ghost notes along.
- Opened from the Assets panel, the pattern shows the item of the last
  double-click. Without one, a pattern that lies in exactly one item of
  all the songs shows that item's first round.
- Ghost notes are the pattern items of the other tracks all along the
  song, and on the own track the items before and after and the other
  rounds of the item. The round edited itself does not count. Sound
  items have no notes.
- A ghost note sits on the row it sounds with: its own transpose, less
  the transpose of the item the pattern was opened from.
- The roll draws them grey, without an edge, under the notes. Resting
  on one names its track and its pattern.
- The header has a Ghosts switch, on by default, and names where they
  come from, e.g. "Tune · Bass · bar 5".
- The time axis pans back to the start of the song, with bar and grid
  lines before the pattern. Fit still frames only the pattern.
- A click or a paste outside the pattern no longer lands. Before, only
  the time left of bar 1 was closed, notes could still be drawn past
  the end, where they are kept but never played.
- The rounds of a pattern item are now worked out in one place,
  itemNotes() in song.ts, which songSequence() and the ghost notes
  share.

Tests:
- Unit ghosts (new): 5 green. Counter-check with the round edited
  not left out and the own transpose not taken off: 2 red.
- Unit piano-roll: 1 new (pan back to the song, lines before bar 1).
  Counter-check without the song's start: red.
- e2e 65 (melektaus-atx): ALL PASSED - 38 checks (3 new). The first
  run was red on the tooltip check: the ghost note it rested on lay a
  fifth up, outside the rows Fit frames. The test now uses a tone.
- e2e 64 (melektaus-atx): ALL PASSED - 30 checks.
- Gate (melektaus-atx): CHECK EXIT 0, 260 files, 4027 tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 15:06:32 +02:00

132 lines
6.3 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { CreateAssetCommand, SetAssetPropertyCommand } from '../../src/core/commands/assetCommands';
import { SetPatternNotesCommand } from '../../src/core/commands/patternCommands';
import { SetSongCommand } from '../../src/core/commands/songCommands';
import { ghostNotes, placeOf, roundAt, roundsOf } from '../../src/core/music/ghosts';
import { newItem, newTrack, type SongItem } from '../../src/core/music/song';
import type { SongAsset } from '../../src/core/model/Asset';
import { makeProject } from '../helpers';
const BAR = 1920;
/** A song with two tracks; "Riff" plays C and E in a bar, "Bass" a whole bar of C2. */
function makeSong() {
const f = makeProject();
const create = new CreateAssetCommand('song', 'Tune');
const riff = new CreateAssetCommand('pattern', 'Riff');
const bass = new CreateAssetCommand('pattern', 'Bass');
for (const command of [create, riff, bass]) f.stack.execute(command);
const song = () => f.project.assets.find((a) => a.id === create.asset.id) as SongAsset;
song().tracks.push(newTrack(2));
for (const id of [riff.asset.id, bass.asset.id])
f.stack.execute(new SetAssetPropertyCommand(id, 'bars', 1));
f.stack.execute(
new SetPatternNotesCommand(riff.asset.id, [
{ id: 'n1', tick: 0, length: 480, note: 60, velocity: 1 },
{ id: 'n2', tick: 960, length: 1200, note: 64, velocity: 0.5 },
]),
);
f.stack.execute(
new SetPatternNotesCommand(bass.asset.id, [{ id: 'b1', tick: 0, length: BAR, note: 36, velocity: 1 }]),
);
f.project.assets.push({ id: 'snd', type: 'sound', name: 'Drum loop', props: {}, file: 'loop.wav' });
const track = (i: number) => song().tracks[i]!.id;
const item = (fields: Partial<SongItem> & Pick<SongItem, 'tick' | 'length'>): SongItem => ({
...newItem({ track: track(0), kind: 'pattern', asset: riff.asset.id, tick: 0, length: BAR }),
...fields,
});
const lay = (...items: SongItem[]) => f.stack.execute(new SetSongCommand(song().id, { items }));
return { ...f, song, track, item, lay, riff: riff.asset.id, bass: bass.asset.id };
}
/** Riff twice from bar 2 and once more in bar 5 on track 1, a sound in bar 1; Bass for six bars on track 2. */
function laidOut(
f: ReturnType<typeof makeSong>,
riffFields: Partial<SongItem> = {},
laterFields: Partial<SongItem> = {},
) {
const a = f.item({ id: 'a', tick: BAR, length: 2 * BAR, ...riffFields });
const later = f.item({ id: 'c', tick: 4 * BAR, length: BAR, ...laterFields });
const sound = f.item({ id: 's', kind: 'sound', asset: 'snd', tick: 0, length: BAR });
const bass = f.item({ id: 'b', track: f.track(1), asset: f.bass, tick: 0, length: 6 * BAR });
f.lay(a, later, sound, bass);
return { a, later, bass };
}
const brief = (notes: { tick: number; note: number; length: number; track: string; pattern: string }[]) =>
notes.map((n) => `${n.tick}:${n.note}:${n.length}:${n.track}:${n.pattern}`);
describe('ghost notes of a pattern in a song (#339)', () => {
it('shows the other tracks all along the song and the own track around the round, counted from the round', () => {
const f = makeSong();
laidOut(f);
const ghosts = ghostNotes(f.project, f.riff, { songId: f.song().id, itemId: 'a', round: 1 })!;
expect(ghosts.origin).toBe(2 * BAR);
expect(ghosts.song).toBe('Tune');
expect(ghosts.track).toBe('Track 1');
expect(ghosts.bar).toBe(3);
// The round edited (bar 3) is not there; its first round (bar 2) and the item in bar 5 are.
expect(brief(ghosts.notes)).toEqual([
`${-2 * BAR}:36:${BAR}:Track 2:Bass`,
`${-BAR}:36:${BAR}:Track 2:Bass`,
`${-BAR}:60:480:Track 1:Riff`,
`${-BAR + 960}:64:960:Track 1:Riff`,
`0:36:${BAR}:Track 2:Bass`,
`${BAR}:36:${BAR}:Track 2:Bass`,
`${2 * BAR}:36:${BAR}:Track 2:Bass`,
`${2 * BAR}:60:480:Track 1:Riff`,
`${2 * BAR + 960}:64:960:Track 1:Riff`,
`${3 * BAR}:36:${BAR}:Track 2:Bass`,
]);
});
it('puts a note on the row it sounds with: its own transpose, less that of the item the pattern is seen in', () => {
const f = makeSong();
laidOut(f, { transpose: 5 }, { transpose: 2 });
const ghosts = ghostNotes(f.project, f.riff, { songId: f.song().id, itemId: 'a', round: 0 })!;
expect(ghosts.notes.filter((n) => n.pattern === 'Bass').map((n) => n.note)).toEqual(Array(6).fill(31));
// The item's own second round is moved as far as the round edited: on the pattern's own rows.
expect(ghosts.notes.filter((n) => n.pattern === 'Riff').map((n) => n.note)).toEqual([60, 64, 57, 61]);
});
it('counts the rounds of an item from before its start, by its offset', () => {
const item = {
...newItem({ track: 't', kind: 'pattern', asset: 'p', tick: BAR, length: 2 * BAR }),
offset: 480,
};
// Rounds start at bar 2 less 480, then a bar on: three of them touch the item.
expect(roundsOf(item, BAR)).toBe(3);
expect(roundAt(item, BAR, BAR + 100)).toBe(0);
expect(roundAt(item, BAR, 2 * BAR - 481)).toBe(0);
expect(roundAt(item, BAR, 2 * BAR - 480)).toBe(1);
expect(roundAt(item, BAR, 9 * BAR)).toBe(2);
expect(roundAt(item, BAR, 0)).toBe(0);
});
it('keeps the place remembered while its item plays the pattern, at most at its last round', () => {
const f = makeSong();
laidOut(f);
const songId = f.song().id;
expect(placeOf(f.project, f.riff, { songId, itemId: 'a', round: 9 })).toEqual({
songId,
itemId: 'a',
round: 1,
});
// Without a place remembered: Riff lies twice in the song, so nowhere; Bass once, so there.
expect(placeOf(f.project, f.riff)).toBeNull();
expect(placeOf(f.project, f.bass)).toEqual({ songId, itemId: 'b', round: 0 });
});
it('falls back to the only item of the pattern once the remembered one is gone or plays another', () => {
const f = makeSong();
const { later, bass } = laidOut(f);
const songId = f.song().id;
const remembered = { songId, itemId: 'a', round: 1 };
f.lay(f.item({ id: 'a', tick: BAR, length: 2 * BAR, asset: f.bass }), later, { ...bass, tick: 8 * BAR });
expect(placeOf(f.project, f.riff, remembered)).toEqual({ songId, itemId: 'c', round: 0 });
f.lay(later, f.item({ id: 'x', tick: 6 * BAR, length: BAR }));
expect(placeOf(f.project, f.riff, remembered)).toBeNull();
expect(ghostNotes(f.project, f.riff, remembered)).toBeNull();
});
});