Skip to content

Commit 2bc0827

Browse files
corepunchclaude
andcommitted
dnb: arrangement timeline scrolling blocks past a fixed playhead
A strip above the sliders draws the arrangement: one row per lane, blocks tinted by family (drums, bass, chords, melody, structure) under a ruler of sections, scrolling right to left at tempo in a Metal view. The playing block brightens and the header names the next section change. Rows follow the lanes of the tracks in view through a retained template. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f19b976 commit 2bc0827

8 files changed

Lines changed: 368 additions & 19 deletions

File tree

‎apps/dnb/Controller.lua‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
local ns = require("AppKit")
22
local xml = require("ui.xml")
3+
local Template = require("ui.template")
34
local Model = require("apps.dnb.Model")
45
local Synth = require("apps.dnb.models.Synth")
56
local Visuals = require("apps.dnb.models.Visuals")
7+
local Timeline = require("apps.dnb.models.Timeline")
68
local Styles = require("apps.dnb.host.Styles")
79
local Visualizers = require("apps.dnb.host.Visualizers")
810
local AudioOutput = require("apps.dnb.services.AudioOutput")
@@ -16,6 +18,9 @@ local VIEWS = "apps/dnb/views/"
1618
-- modal menu) resumes the picture instead of leaping ahead.
1719
local PLAYBACK = {sampleRate = 44100, bufferSeconds = 0.3, frameInterval = 1 / 60, maxFrame = 0.1}
1820

21+
-- The arrangement strip's rows, ruler and lane-name column, in points.
22+
local TIMELINE = {rowHeight = 10, ruler = 10, labelWidth = 52, scale = 2}
23+
1924
local SECTION_TITLES = {intro = "Intro", build = "Build-up", drop = "Drop", breakdown = "Breakdown",
2025
outro = "Outro", halftime = "Half-time", blend = "Mixing in"}
2126

@@ -218,8 +223,38 @@ function Controller:present(refs, stage)
218223
end
219224
end
220225

226+
-- The arrangement strip around set bar `n` of `composer`, the playhead
227+
-- `fraction` into it and moving at `barsPerSecond`. Rows and blocks change
228+
-- only when another track comes into view; the playhead moves every frame.
229+
function Controller:showTimeline(composer, n, fraction, barsPerSecond)
230+
local timeline = self.timeline
231+
if not timeline then return end
232+
local plans = Timeline.plans(composer, n)
233+
local key = tostring(composer) .. ":" .. plans[1].track .. ":" .. #plans
234+
local headline = Timeline.headline(plans, n)
235+
local refs = timeline.refs
236+
if self.timelineKey ~= key then
237+
self.timelineKey = key
238+
self.timelineRows = Timeline.rows(plans)
239+
refs = select(2, timeline:update({rows = self.timelineRows, headline = headline,
240+
rowHeight = TIMELINE.rowHeight, ruler = TIMELINE.ruler, labelWidth = TIMELINE.labelWidth}))
241+
local data = Timeline.instances(plans, self.timelineRows)
242+
refs.timelineCanvas.draws = {{vertex = "timelineBlockVertex", fragment = "timelineBlockFragment",
243+
count = 6, instances = #data // Timeline.stride, data = data, blend = "alpha"}}
244+
elseif refs.timelineNext.text ~= headline then
245+
refs.timelineNext.text = headline
246+
end
247+
local rows = #self.timelineRows
248+
local values = Timeline.values(n + fraction, barsPerSecond, rows)
249+
table.insert(values, TIMELINE.ruler / (TIMELINE.ruler + rows * TIMELINE.rowHeight))
250+
table.insert(values, self.window and self.window.backingScaleFactor or TIMELINE.scale)
251+
refs.timelineCanvas.values = values
252+
end
253+
221254
function Controller:showPlayhead(bar, played)
222-
local info = self:nowPlaying(bar, math.max(0, math.min(1, (played - bar.frame) / bar.frames)))
255+
local fraction = math.max(0, math.min(1, (played - bar.frame) / bar.frames))
256+
self:showTimeline(bar.composer, bar.index, fraction, self.model:value("tempo") / 240)
257+
local info = self:nowPlaying(bar, fraction)
223258
for _, refs in ipairs(self:views()) do
224259
if refs.section.text ~= info.section then refs.section.text = info.section end
225260
if refs.detail.text ~= info.detail then refs.detail.text = info.detail end
@@ -228,8 +263,9 @@ function Controller:showPlayhead(bar, played)
228263
end
229264
end
230265

231-
-- The header while stopped: the first bar of the current set.
266+
-- The header and timeline while stopped: the next bar to play.
232267
function Controller:showIdle()
268+
self:showTimeline(self.composer, self.synth.composerBar, 0, 0)
233269
local info = self:nowPlaying(self.composer:bar(self.synth.composerBar, self.model))
234270
for _, refs in ipairs(self:views()) do refs.detail.text = info.detail end
235271
end
@@ -262,6 +298,10 @@ function Controller:stop()
262298
if not self.playing then return end
263299
self.output:pause()
264300
self:setTransport(false)
301+
-- The strip stops where the music did instead of scrolling on.
302+
local played = self.output:played()
303+
local bar = self.synth:barAt(played)
304+
if bar then self:showTimeline(bar.composer, bar.index, math.max(0, math.min(1, (played - bar.frame) / bar.frames)), 0) end
265305
end
266306

267307
-- Skips to the next track of the set at the next bar line; its intro mixes
@@ -314,6 +354,8 @@ function Controller:createWindow()
314354
self.refs = refs
315355
self:present(refs)
316356
self.window = ns.Window(config)
357+
self.timeline = Template.new(refs.timeline, VIEWS .. "Timeline.etlua", ns)
358+
self:showIdle()
317359
self:setTransport(false)
318360
-- The display loop lives as long as the window's Lua state; closing the
319361
-- window cancels its timer. Timers fire late while the run loop is busy

‎apps/dnb/README.md‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ mix between, each new intro mixed under the outgoing tune.
1818
| UK Garage | 2-Step, Speed Garage, Future Garage | 128–138 |
1919

2020
The visualizer fills the window, running under the Liquid Glass toolbar;
21-
the now-playing cards and the Groove, Sound and Mix sliders float over it in
22-
glass. Each style decides which parts its songs play; the listener shapes
21+
the now-playing cards, the arrangement strip and the Groove, Sound and Mix
22+
sliders float over it in glass. Each track is arranged before it plays, like
23+
the arrange window of Cubase or MTV Music Generator: a ruler of sections and
24+
one lane of blocks per part. The strip scrolls those blocks right to left
25+
past a fixed playhead, brightens the ones playing and shows the next section
26+
change coming. The style decides which parts its songs play; the listener shapes
2327
them with the sliders, and the Mix faders (Drums, Bass, Chords, Melody)
2428
scale the style's own balance. The Style menu switches genre on the next bar
2529
line. The Scene menu pins one visualizer scene
@@ -42,16 +46,23 @@ folder whose `init.lua` returns a manifest. It runs isolated: pure standard
4246
libraries only, with everything else coming from a read-only host API.
4347

4448
**Style plugins** (`plugins/styles/<id>/`) declare a title, symbol, tempo
45-
range, control defaults, the parts they play and a `sound` that
46-
tunes the Synth. `create(kit, seed)` returns a composer whose `bar(n,
47-
settings)` is a pure function of seed, bar and settings. The kit
49+
range, control defaults and a `sound` that tunes the Synth, and compose in
50+
three parts. `material(kit, rng, track, first)` draws one cycle's grooves,
51+
harmony and melody. `arrange(kit, track, cycles)` lays a track out as lanes
52+
of blocks (the "when"). `patterns` render one bar of a part (the "what").
53+
The host's [`Composer`](host/Composer.lua) builds each track's
54+
[`Arrangement`](host/Arrangement.lua) once, from the seed alone, and renders
55+
bar n from the blocks under it. Energy and Complexity shape the patterns bar
56+
by bar. Blocks are plain data, never overlap in a lane, and point at
57+
patterns by id; structure moves (fills, risers, half-time, throws, chops)
58+
are lanes too, so tests read the plan instead of rendering audio. The kit
4859
([`host/StyleKit.lua`](host/StyleKit.lua)) is the shared functionality:
4960
seeded randomness, modes, voice-led chords, a drum kit for every track
5061
(a snare character from the flavour's `snares` — tight, fat, rimshot,
5162
roomy, crunchy, layered with a clap, vintage — and a retuned kick, hats and
5263
clap), a call-and-response melody writer, the DJ set that sequences tracks and sections, the bar score with
53-
humanized hits, arrangement punctuation (crashes, risers, build rolls), the
54-
blend into a new track, and the Amen break. Adding a genre is one folder and
64+
humanized hits, the lane builder, the shared punctuation (crashes, risers,
65+
build rolls, throws), the blend into a new track, and the Amen break. Adding a genre is one folder and
5566
one line in [`host/Styles.lua`](host/Styles.lua).
5667

5768
**Visualizer plugins** (`plugins/visualizers/<id>/`) are declarative: a
@@ -80,12 +91,14 @@ stage rather than the window, and the picture continues behind the panels.
8091

8192
| File | Role |
8293
|---|---|
83-
| `Model.lua` | Parts and controls (the single source for the sliders), the current style's ranges and the parts it plays |
94+
| `Model.lua` | Lanes by family (the timeline's rows and order), controls (the single source for the sliders), the current style's ranges and which lanes sound |
8495
| `Controller.lua` | Window, transport, Style and Scene menus, the mini player, and the 60 Hz display loop |
85-
| `host/Styles.lua`, `host/StyleKit.lua` | The style extension point and its host API |
96+
| `host/Styles.lua`, `host/StyleKit.lua` | The style extension point and its host API, with the lane builder and the shared patterns |
97+
| `host/Arrangement.lua`, `host/Composer.lua` | A track's plan of sections and lanes, checked and searchable; the composer every style shares, which arranges tracks and renders bars from their blocks |
8698
| `host/Visualizers.lua` | The visualizer extension point, the program it links and the draws that show a scene on a layer |
8799
| `plugins/styles/*` | Drum & Bass, Techno, House, Trance, Dubstep, Breakbeat, UK Garage |
88100
| `plugins/visualizers/*` | Synthwave Horizon, Valley Flight, Solar System, Liquid Chrome, Light Trails, Tunnel, Crystals |
101+
| `models/Timeline.lua`, `views/Timeline.etlua`, `shaders/Timeline.metal` | The arrangement strip: rows, instanced block data and the headline; lane names; blocks scrolling past the playhead |
89102
| `models/Synth.lua` | Sample-accurate synthesis: kicks, snares, claps and hats designed per style and voiced anew for every track, shared cymbals and percussion, the Amen loop and its slice sampler, reese/acid/wobble bass with resonance, filter envelope, accents and retriggered LFO, sub, pads, FM electric piano, stabs, arp plucks, a gliding lead, risers, ping-pong delay with throws, reverb, sidechain pump, soft clip |
90103
| `models/Amen.lua` | The recreated Amen break's four-bar pattern and slice bookkeeping |
91104
| `models/Visuals.lua` | Spectrum smoothing, peak holds, kick and snare flashes, the scene director with crossfades, and the shader value layout |

‎apps/dnb/host/Composer.lua‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ function Composer:bar(n, settings)
111111
track = track, tonic = tonic}, StyleKit.random(self.seed, 2, n))
112112
local chord = self:chord(track, cycleIndex, n)
113113
bar.progression, bar.chord = kit.progressionName(mode, material.progression), chord
114+
-- The plan it came from, for the timeline: a bar can sound after a new
115+
-- style or set has replaced this composer.
116+
bar.composer = self
114117
local humanize = settings:value("humanize")
115118
local previous = track.index > 0 and set:track(track.index - 1) or nil
116119
local ctx = {

‎apps/dnb/models/Timeline.lua‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
-- The timeline: the arrangement as the arrange window draws it. Blocks
2+
-- slide right to left past a fixed playhead, one row per lane, under a ruler
3+
-- of sections. This model turns arrangements into plain data: the rows, the
4+
-- block instances the Metal program draws, the values it animates, and the
5+
-- headline naming the next section change. It never touches views.
6+
local Model = require("apps.dnb.Model")
7+
8+
local Timeline = {}
9+
10+
-- The window: bars in view and how many of them lie behind the playhead,
11+
-- so the next section change shows well before it lands.
12+
Timeline.window = {bars = 16, behind = 4}
13+
14+
-- Colour indices the shader tints by: a lane's family, or a section.
15+
local COLOURS = {drums = 0, bass = 1, chords = 2, melody = 3, structure = 4,
16+
intro = 5, build = 6, drop = 7, breakdown = 8, outro = 9}
17+
local RULER = -1 -- the row index of the section ruler
18+
local SECTION_TITLES = {intro = "Intro", build = "Build-up", drop = "Drop", breakdown = "Breakdown", outro = "Outro"}
19+
20+
-- Floats per instance: row, first bar, length in bars, colour.
21+
Timeline.stride = 4
22+
23+
--- The plans in view around set bar `n`: the playing track's and, once its
24+
--- end is inside the window, the next one's.
25+
function Timeline.plans(composer, n)
26+
local plan = composer:arrangement(composer:trackAt(n).index)
27+
local plans = {plan}
28+
if plan.start + plan.length - n <= Timeline.window.bars then
29+
table.insert(plans, composer:arrangement(plan.track + 1))
30+
end
31+
return plans
32+
end
33+
34+
--- The parts with a lane in any of `plans`, in the timeline's part order.
35+
function Timeline.rows(plans)
36+
local present = {}
37+
for _, plan in ipairs(plans) do
38+
for _, lane in ipairs(plan.lanes) do present[lane.part] = true end
39+
end
40+
local rows = {}
41+
for _, part in ipairs(Model.parts) do
42+
if present[part] then table.insert(rows, {part = part, family = Model.family[part]}) end
43+
end
44+
return rows
45+
end
46+
47+
--- The draw's instance data: every section on the ruler and every block on
48+
--- its row, at set bars, `Timeline.stride` floats each.
49+
function Timeline.instances(plans, rows)
50+
local index = {}
51+
for i, row in ipairs(rows) do index[row.part] = i - 1 end
52+
local data = {}
53+
local function add(row, start, length, colour)
54+
table.insert(data, row)
55+
table.insert(data, start)
56+
table.insert(data, length)
57+
table.insert(data, colour)
58+
end
59+
for _, plan in ipairs(plans) do
60+
for _, section in ipairs(plan.sections) do
61+
add(RULER, plan.start + section.start, section.length, COLOURS[section.id])
62+
end
63+
for _, lane in ipairs(plan.lanes) do
64+
for _, block in ipairs(lane.blocks) do
65+
add(index[lane.part], plan.start + block.start, block.length, COLOURS[Model.family[lane.part]])
66+
end
67+
end
68+
end
69+
return data
70+
end
71+
72+
--- The next section change after set bar `n` in `plans`: "Breakdown in 8
73+
--- bars", or the next track's intro once the outro plays.
74+
function Timeline.headline(plans, n)
75+
for p, plan in ipairs(plans) do
76+
for _, section in ipairs(plan.sections) do
77+
local start = plan.start + section.start
78+
if start > n then
79+
local bars = start - n
80+
local title = p > 1 and "Next track" or SECTION_TITLES[section.id]
81+
return string.format("%s in %d bar%s", title, bars, bars == 1 and "" or "s")
82+
end
83+
end
84+
end
85+
local plan = plans[#plans]
86+
local bars = plan.start + plan.length - n
87+
return string.format("Next track in %d bar%s", bars, bars == 1 and "" or "s")
88+
end
89+
90+
--- The values the shader reads: the playhead (a set bar and its fraction),
91+
--- its speed in bars per second to extrapolate between updates, the row
92+
--- count, and the window.
93+
function Timeline.values(playhead, barsPerSecond, rows)
94+
return {playhead, barsPerSecond, rows, Timeline.window.bars, Timeline.window.behind}
95+
end
96+
97+
return Timeline

‎apps/dnb/shaders/Timeline.metal‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// The arrangement timeline (models/Timeline.lua): one draw of instanced
2+
// rounded rects, a section on the ruler or a block on its lane, into layer 1,
3+
// then the finishing pass adds the bar grid and the fixed playhead. Blocks
4+
// slide right to left: every position is a set bar, the playhead advances by
5+
// its speed times `inputs.age` between updates, so the strip scrolls evenly
6+
// on every display frame.
7+
//
8+
// values: [0] playhead (set bar), [1] bars per second, [2] lane rows,
9+
// [3] bars in view, [4] bars behind the playhead, [5] ruler height
10+
// as a fraction of the view, [6] display scale (pixels per point)
11+
// data: per instance row (-1 is the ruler), first bar, bars, colour
12+
13+
// Family and section tints: the system palette in its dark appearance.
14+
constant float3 TIMELINE_COLOURS[10] = {
15+
float3(1.000, 0.216, 0.373), // drums: pink
16+
float3(1.000, 0.624, 0.039), // bass: orange
17+
float3(0.749, 0.353, 0.949), // chords: purple
18+
float3(0.353, 0.784, 0.980), // melody: cyan
19+
float3(1.000, 0.839, 0.039), // structure: yellow
20+
float3(0.039, 0.518, 1.000), // intro: blue
21+
float3(1.000, 0.839, 0.039), // build: yellow
22+
float3(1.000, 0.271, 0.227), // drop: red
23+
float3(0.251, 0.784, 0.878), // breakdown: teal
24+
float3(0.557, 0.557, 0.576), // outro: grey
25+
};
26+
constant float TIMELINE_GAP = 1.0; // points between neighbouring blocks
27+
constant float TIMELINE_RADIUS = 2.5; // block corner radius in points
28+
constant float TIMELINE_RESTING = 0.45; // a block's opacity away from the playhead
29+
constant float TIMELINE_PAST = 0.5; // what has played dims further
30+
constant float TIMELINE_GRID = 0.06; // a bar line's opacity; phrase lines double it
31+
32+
struct TimelineBlock {
33+
float4 position [[position]];
34+
float2 pixel;
35+
float4 rect [[flat]]; // left, top, right, bottom in pixels
36+
float3 colour [[flat]];
37+
float strength [[flat]];
38+
};
39+
40+
static float timelinePlayhead(constant ShaderInputs &inputs) {
41+
return inputs.values[0] + inputs.values[1] * inputs.age;
42+
}
43+
44+
vertex TimelineBlock timelineBlockVertex(uint vid [[vertex_id]], uint iid [[instance_id]],
45+
constant ShaderInputs &inputs [[buffer(0)]], constant float *data [[buffer(1)]]) {
46+
float row = data[iid * 4], start = data[iid * 4 + 1], length = data[iid * 4 + 2];
47+
int colour = int(data[iid * 4 + 3]);
48+
float playhead = timelinePlayhead(inputs);
49+
float rows = max(inputs.values[2], 1.0), window = inputs.values[3], left = playhead - inputs.values[4];
50+
float ruler = inputs.values[5], scale = inputs.values[6];
51+
float2 size = inputs.size;
52+
float lane = (1.0 - ruler) / rows;
53+
float top = row < 0.0 ? 0.0 : ruler + row * lane;
54+
float bottom = row < 0.0 ? ruler : top + lane;
55+
float gap = TIMELINE_GAP * scale * 0.5;
56+
float4 rect = float4((start - left) / window * size.x + gap, top * size.y + gap,
57+
(start + length - left) / window * size.x - gap, bottom * size.y - gap);
58+
const uint corners[6] = {0, 1, 2, 2, 1, 3};
59+
uint corner = corners[vid];
60+
float2 p = float2((corner & 1) ? rect.z : rect.x, (corner & 2) ? rect.w : rect.y);
61+
bool playing = start <= playhead && playhead < start + length;
62+
TimelineBlock out;
63+
out.position = float4(p.x / size.x * 2.0 - 1.0, 1.0 - p.y / size.y * 2.0, 0.0, 1.0);
64+
out.pixel = p;
65+
out.rect = rect;
66+
out.colour = TIMELINE_COLOURS[colour];
67+
// The playing block brightens; what has played dims.
68+
out.strength = playing ? 1.0 : (start + length <= playhead ? TIMELINE_RESTING * TIMELINE_PAST : TIMELINE_RESTING);
69+
return out;
70+
}
71+
72+
fragment float4 timelineBlockFragment(TimelineBlock in [[stage_in]], constant ShaderInputs &inputs [[buffer(0)]]) {
73+
float2 centre = (in.rect.xy + in.rect.zw) * 0.5;
74+
float2 extent = max((in.rect.zw - in.rect.xy) * 0.5, float2(0.0));
75+
float radius = min(TIMELINE_RADIUS * inputs.values[6], min(extent.x, extent.y));
76+
float2 q = abs(in.pixel - centre) - extent + radius;
77+
float d = length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - radius;
78+
float alpha = saturate(0.5 - d) * in.strength;
79+
return float4(in.colour * alpha, alpha);
80+
}
81+
82+
fragment float4 timeline(ShaderVertex in [[stage_in]], constant ShaderInputs &inputs [[buffer(0)]],
83+
texture2d<float> blocks [[texture(0)]]) {
84+
constexpr sampler s(filter::nearest, address::clamp_to_edge);
85+
float4 colour = blocks.sample(s, in.uv, level(0.0));
86+
float window = inputs.values[3], behind = inputs.values[4], ruler = inputs.values[5];
87+
float barsPerPixel = window / inputs.size.x;
88+
float bar = timelinePlayhead(inputs) - behind + in.uv.x * window;
89+
// Bar lines under the lanes, stronger on each eight-bar phrase.
90+
float fromLine = abs(fract(bar + 0.5) - 0.5) / barsPerPixel;
91+
float phrase = abs(fract(bar / 8.0 + 0.5) - 0.5) * 8.0 / barsPerPixel < 1.0 ? 2.0 : 1.0;
92+
float grid = in.uv.y > ruler ? saturate(1.0 - fromLine) * TIMELINE_GRID * phrase : 0.0;
93+
colour += float4(grid) * (1.0 - colour.a);
94+
// The fixed playhead.
95+
float head = saturate(1.5 * inputs.values[6] - abs(in.uv.x - behind / window) * inputs.size.x);
96+
return mix(colour, float4(1.0), head * 0.9);
97+
}

0 commit comments

Comments
 (0)