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>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-07 15:06:32 +02:00
1 parent c33d6d98e5
commit d505f6ed27
13 files changed
+521 -33

No files matched your search

+115
View File
@@ -0,0 +1,115 @@
/**
* Ghost notes (#339): what else sounds in a song around a pattern item, shown faint in the piano roll
* of its pattern. The item is the one a double-click in the song opened the pattern from, and of it
* the round clicked into; the notes are counted from where that round starts.
*/
import { HIGHEST_NOTE, LOWEST_NOTE } from '../instrument/notes';
import type { SongAsset } from '../model/Asset';
import { findAssetOf, type Project } from '../model/Project';
import { patternLength } from './pattern';
import { firstRound, itemEnd, itemNotes, type SongItem, songMeter } from './song';
import { ticksPerBar } from './time';
/** Which item of which song a pattern is seen in, and which of its rounds. */
export interface PatternPlace {
songId: string;
itemId: string;
round: number;
}
export interface GhostNote {
/** From the start of the round, in ticks: before it is below 0. */
tick: number;
length: number;
/** The row it is drawn on: as it sounds, less the transpose of the item the pattern is seen in. */
note: number;
track: string;
pattern: string;
}
export interface Ghosts {
song: string;
track: string;
/** The bar of the song the round is heard from, counted from 1. */
bar: number;
/** Where the round starts in the song, in ticks. */
origin: number;
notes: GhostNote[];
}
/** How many rounds of its pattern an item plays, the cut ones at its ends included. */
export function roundsOf(item: SongItem, span: number): number {
return Math.max(1, Math.ceil((itemEnd(item) - firstRound(item, span)) / span));
}
/** The round of an item a song tick falls into, held to the rounds it has. */
export function roundAt(item: SongItem, span: number, tick: number): number {
const round = Math.floor((tick - firstRound(item, span)) / span);
return Math.max(0, Math.min(roundsOf(item, span) - 1, round));
}
function patternItem(song: SongAsset, item: SongItem | undefined, patternId: string): item is SongItem {
return (
item !== undefined &&
item.kind === 'pattern' &&
item.asset === patternId &&
song.tracks.some((t) => t.id === item.track)
);
}
/**
* Where a pattern is seen: the place remembered, while its item is still there and still plays the
* pattern; else the one item that plays it, if there is exactly one in all the songs; else nowhere.
*/
export function placeOf(project: Project, patternId: string, remembered?: PatternPlace): PatternPlace | null {
const pattern = findAssetOf(project, patternId, 'pattern');
if (!pattern) return null;
const span = patternLength(pattern);
if (remembered) {
const song = findAssetOf(project, remembered.songId, 'song');
const item = song?.items.find((i) => i.id === remembered.itemId);
if (song && patternItem(song, item, patternId))
return { ...remembered, round: Math.max(0, Math.min(roundsOf(item, span) - 1, remembered.round)) };
}
const found: PatternPlace[] = [];
for (const song of project.assets)
if (song.type === 'song')
for (const item of song.items)
if (patternItem(song, item, patternId)) found.push({ songId: song.id, itemId: item.id, round: 0 });
return found.length === 1 ? found[0]! : null;
}
/**
* The notes of every pattern item of the song but the round itself: the other tracks all along the
* song, on the item's own track the items before and after and its other rounds. Sound items have
* no notes. Null where the place is gone.
*/
export function ghostNotes(project: Project, patternId: string, place: PatternPlace): Ghosts | null {
const pattern = findAssetOf(project, patternId, 'pattern');
const song = findAssetOf(project, place.songId, 'song');
const item = song?.items.find((i) => i.id === place.itemId);
if (!pattern || !song || !patternItem(song, item, patternId)) return null;
const span = patternLength(pattern);
const origin = firstRound(item, span) + place.round * span;
const tracks = new Map(song.tracks.map((t) => [t.id, t.name]));
const notes: GhostNote[] = [];
for (const other of song.items) {
const track = tracks.get(other.track);
const source = other.kind === 'pattern' ? findAssetOf(project, other.asset, 'pattern') : undefined;
if (track === undefined || !source) continue;
for (const n of itemNotes(source, other)) {
if (other.id === item.id && n.tick >= origin && n.tick < origin + span) continue;
const note = n.note - item.transpose;
if (note < LOWEST_NOTE || note > HIGHEST_NOTE) continue;
notes.push({ tick: n.tick - origin, length: n.length, note, track, pattern: source.name });
}
}
notes.sort((a, b) => a.tick - b.tick || a.note - b.note);
return {
song: song.name,
track: tracks.get(item.track) ?? '',
bar: Math.floor(Math.max(origin, item.tick) / ticksPerBar(songMeter(song.props))) + 1,
origin,
notes,
};
}
+29 -13
View File
@@ -6,7 +6,7 @@
*/
import { newId } from '../ids';
import { HIGHEST_NOTE, LOWEST_NOTE } from '../instrument/notes';
import type { SongAsset } from '../model/Asset';
import type { PatternAsset, SongAsset } from '../model/Asset';
import { type PropertyBag, textOf } from '../model/Properties';
import { findAssetOf, type Project } from '../model/Project';
import { gridTicks, GRID_OPTIONS, patternInstrument, patternLength } from './pattern';
@@ -159,6 +159,33 @@ export function itemFrom(item: SongItem, tick: number, tempo: number): SongItem
return { ...item, tick: item.tick + by, length: item.length - by, offset };
}
/** Where the first round of a pattern item starts in the song: before the item, by its offset. */
export function firstRound(item: SongItem, span: number): number {
return item.tick - (((item.offset % span) + span) % span);
}
/**
* The notes a pattern item plays, in song ticks: its pattern over and over for as long as the item
* is, moved by its transpose, cut at the item's ends and at the end of each round.
*/
export function itemNotes(
pattern: PatternAsset,
item: SongItem,
): { tick: number; length: number; note: number; velocity: number }[] {
const span = patternLength(pattern);
const end = itemEnd(item);
const out: { tick: number; length: number; note: number; velocity: number }[] = [];
for (let round = firstRound(item, span); round < end; round += span)
for (const n of pattern.notes) {
const tick = round + n.tick;
const note = n.note + item.transpose;
if (n.tick >= span || tick < item.tick || tick >= end || note < LOWEST_NOTE || note > HIGHEST_NOTE)
continue;
out.push({ tick, length: Math.min(n.length, span - n.tick, end - tick), note, velocity: n.velocity });
}
return out;
}
/** What the scheduler plays of a song: every item that has its asset, on the bus of its track. */
export function songSequence(project: Project, song: SongAsset): Sequence {
const notes: SequenceNote[] = [];
@@ -184,18 +211,7 @@ export function songSequence(project: Project, song: SongAsset): Sequence {
const pattern = findAssetOf(project, item.asset, 'pattern');
const instrument = pattern && (item.instrument || patternInstrument(pattern.props));
if (!pattern || !instrument) continue;
const span = patternLength(pattern);
const end = itemEnd(item);
const into = ((item.offset % span) + span) % span;
for (let round = item.tick - into; round < end; round += span)
for (const n of pattern.notes) {
const tick = round + n.tick;
const note = n.note + item.transpose;
if (n.tick >= span || tick < item.tick || tick >= end || note < LOWEST_NOTE || note > HIGHEST_NOTE)
continue;
const length = Math.min(n.length, span - n.tick, end - tick);
notes.push({ tick, length, note, velocity: n.velocity, instrument, volume, bus });
}
for (const n of itemNotes(pattern, item)) notes.push({ ...n, instrument, volume, bus });
}
notes.sort((a, b) => a.tick - b.tick || a.note - b.note);
clips.sort((a, b) => a.tick - b.tick);