BeatScript Editor

#Radio: stations

Long note for docs/BEATSCRIPT.md "Radio: stations": stations v1, phase 1. Code:
beatscript-api::station (station_json, the one answer of the MCP station tool, POST /api/station and the page's wasm
beatscript_station) and the station and set_next productions of crates/djlive/grammar.ebnf. Tests:
crates/beatscript-api/tests/station.rs. Example: examples/beatscript/techno-radio.beatscript.

#What a station is

A station is an endless, deterministic program of programs. Position N of a station is always the same track: the same
text, the same CID, the same notes, on any host and at any time. Nobody writes a station's tracks one by one, as a set's are
written; a walk of the seeded generator gives them, one position after another, and it never ends.

A station is itself a BeatScript program (CC0, with a CID of its own). It says three things: where the walk starts (the first
track's generator call), how it steps from one track to the next, and the forms the tracks take in turn.

Listeners do not steer a station. They tune it, turning one of its knobs, and they lock knobs. Turning a knob moves
the listener to a neighbouring station that already exists, at the same position N: every station is its text, and the
neighbour's text is there to be read before anyone turns to it. A small turn must be a small musical change (the locality
rule, below).

#The program

BeatScript
#! name: techno radio
#! license: CC0-1.0
tempo 128
key am
station {
  from gen@2 techno seed=42
  next seed=~
  forms seeded passacaglia rondo fugue
}

#The walk

The walk says which seed is at position N. s_0 is the from call's seed; every later seed comes from the step.

text
s_0          = the from call's seed
seed=+n      s_N     = (s_0 + n*N) mod 2^32
seed=~       s_{N+1} = fold32(CID of the program "arrange from gen@V GENRE seed=s_N [length=L]")
fold32       FNV-1a 32 over the CID's bytes

#The track at N

The form of track N is F = forms[N mod R], R the length of the rotation. Each pass of the rotation is a chapter:
c = N div R, and the chapter's theme is t = s_{c*R}, the seed of its first track. Every form has a theme role, bound to t,
so the theme recurs through the chapter ("a theme bound as a role recurs across a station"); the form's other role takes the
track's own fresh seed s_N.

form the track's arrange from line theme role (t) fresh role (s_N)
seeded arrange from gen@V G seed=s_N
passacaglia template(passacaglia, @PHRASE, ground=..., upper=...) ground upper
canon template(canon, @PHRASE, subject=..., ground=...) ground subject
rondo template(rondo, @PHRASE, refrain=..., episodes=... part=arp) refrain episodes, part=arp
fugue template(fugue, @PHRASE, subject=..., counter=...) subject counter
theme-and-variations template(theme-and-variations, @PHRASE, theme=...) theme (none: its one role is the theme)
cross template(cross, @PHRASE, a=..., b=...) a b

The track's text is a tempo line, a key line and the arrange from line, under header lines that say where it came from:

BeatScript
#! station: <the station's CID> track 7
#! from: next(<the CID of track 6>) seed=~
tempo 128
key am
arrange from template(fugue, @<phrase>, subject=gen@2 techno seed=<s_4>, counter=gen@2 techno seed=<s_7>)

#The segue

Between track N and track N+1 is a segue: the existing cross form, the two tracks' seeds crossed.

BeatScript
tempo 128
key am
arrange from template(cross, @<phrase>, a=gen@2 techno seed=<s_N>, b=gen@2 techno seed=<s_N+1>)

#Knobs, notches, retunes and locks

A station has seven knobs. A retune sets one; the station's position N is kept, so the listener hears track N of the new
station. One notch of a knob is a neighbour:

knob what it is one notch
genre the from call's genre none: in phase 1 a genre is retuned only to a value said explicitly (its neighbourhoods are phase 2)
seed the from call's seed, s_0 +1 or -1
step the next line +1 to ~, and ~ to +1
forms the rotation rotated by one (seeded rondo fugue to rondo fugue seeded)
tempo the tempo line, 40 to 300 +2 or -2
key the key line one step round the circle of fifths in its mode (am to em or dm)
length the from call's length= one step along short, standard, long

#The locality rule

A small turn must be a small change, and each knob moves one dimension of the station and nothing else:

Two adjacent seeds of the generator are different tracks: seed 42 and seed 43 are two tracks of the same genre, tempo, key and
form, and seed 43 is not a variation of seed 42. What keeps a seed neighbour close is the station's structure (the
same form at each position, the same tempo, key and length, the chapters' rhythm) and the recurring theme of each chapter. How
close they come is measured by listen (its loudness per section, the tempo and key it hears), not promised by the seed.

#next in a set

The step of a station is also a track of a set (docs/BEATSCRIPT.md "Sets"):

text
set {
  play gen@2 techno seed=42
  mix 16 bars with crossfade
  next seed=+1
  mix 16 bars with echo_out
  next seed=~
}

#The answer: MCP station and POST /api/station

One function, beatscript-api::station::station_json, answers the MCP station tool, POST /api/station and the page's wasm
beatscript_station: the same request gives the same answer from each.

text
curl -s https://beatscript.app/api/station -H 'Content-Type: application/json' \
  -d '{"station": "tempo 128\nkey am\nstation {\n  from gen@2 techno seed=42\n  next seed=~\n  forms seeded passacaglia rondo fugue\n}\n", "n": 7, "segue": true, "check": true}'
field what it asks
station the station's program text
n the position (default 0)
count how many tracks from n, 1 to 16 (default 1); with check, 1 to 4 (each track and segue is checked, a few seconds each)
segue the segue after each track
check each track and segue checked: ok, expanded_cid, bars, seconds, errors
retune one or more knobs set before anything else: a value ({"tempo": 130}), or "+1" / "-1" for one notch up or down ({"seed": "+1"})
locks the knobs held fixed (["tempo", "key"])
neighbours the one-notch neighbours, each with its station text, its CID and its track at n

The answer has this shape (the numbers and CIDs here stand in for real ones):

json
{"ok": true, "version": "stations/1",
 "station": {"text": "...", "cid": "z...", "genre": "techno", "generator": "gen@2", "seed": 42, "step": "~",
             "forms": ["seeded", "passacaglia", "rondo", "fugue"], "tempo": 128, "key": "am", "length": "standard",
             "start_cid": "z..."},
 "n": 7,
 "tracks": [{"n": 7, "seed": 3141, "theme": 2718, "form": "fugue", "phrase": "@tempest_such_stuff",
             "text": "...", "cid": "z...", "parent": "z...",
             "check": {"ok": true, "expanded_cid": "z...", "bars": 168, "seconds": 315.0, "errors": []}}],
 "segues": [{"from": 7, "to": 8, "text": "...", "cid": "z...", "check": {"ok": true, "...": "..."}}],
 "neighbours": [{"knob": "tempo", "value": 130, "text": "...", "cid": "z...", "track": {"n": 7, "cid": "z...", "...": "..."}}],
 "retuned": ["tempo"], "locks": ["key"],
 "diagnostics": [{"severity": "error", "message": "...", "line": 3, "col": 3}]}

#The /radio page

beatscript.app/radio is a player for a station. It plays track N, the segue after it, track N+1, and on, each from the
station's answer for that position (the page's wasm beatscript_station gives the same answer as POST /api/station). Its knobs are
the station's knobs: a turn is a retune, so the next track is the new station's at the same position, and a lock holds a knob
still. Nothing a listener does is written into the station they were on: a tuned station is another text, which was there
before they turned to it.

While it plays the page is a player: a transport (previous, play/pause, next, stop), a readout (the title, elapsed and total
time, the section and bar, the tempo and key), a level meter and the decks' roles lit as they fire. Under it, a playlist: the
tab's "up next" queue first (MCP's queue tool and the playlist itself write it: add by CID, share link or example name,
remove, reorder; double-click or Enter plays a row), then the station's next tracks, N+1, N+2, ... Each row says its render
status (none, queued, rendering, ready with the Opus link, failed) and whether it plays from the render or live in the tab
("Play from": auto, the default, plays a row from its render when the render is ready; render waits for it; live never). The
statuses are asked for sparingly (once when a row shows, again only while it renders, never more than two at once or 30 a
minute, nothing while the tab is hidden), and the page starts one render only, the next row's, only while someone is listening.

Never dead air. What is next is resolved while the current item plays: its text fetched and compiled into a run of its own,
or its render found ready, so the change is a swap at the boundary. When it is not ready in time (a render pending or failed, a
program that does not compile, a slow network, a queue that answers late), the page plays a bridge: a small CC0 loop at the
outgoing track's tempo and key (handoff-model.js BRIDGES), hot-reloaded into the running program so it lands on the next bar
line, looping until the next item is ready, which then comes in on a bar line. The row says "bridging". A next item that fails
is passed over for the one after it while the bridge plays on; after ten minutes of bridging with nothing ready the page stops
and says why. The one intended stop is the queue's presence pause (nobody listening). A swap from one compiled run to the next
starts the engine afresh, which costs the time it takes to install the new program's nets; a gapless swap needs the engine to
land a new program at its own bar 0 on a bar line (as a scene does), which is not built yet.

Peek. The { } code key opens the playing program's text in a drawer, without stopping it. An edit applies on the next bar
(a hot reload, as Cmd+Enter in the editor), the row says "edited" with the edit's CID, "back to station" puts the track back, and
"keep my version" puts a link to the edit in the clipboard.

#A station on any page: <beatscript-radio>

The player package, @stackdump/beatscript-player from 0.4.0 (site/player), has the radio as an element beside
<beatscript-track>, made for a page with a strict Content Security Policy, such as a Claude artifact:

html
<script type="module" src="https://cdn.jsdelivr.net/npm/@stackdump/beatscript-player@0.4.0/beatscript-radio.js"></script>
<beatscript-radio station="tempo 128&#10;key am&#10;station {&#10;  from gen@2 techno seed=42&#10;  next seed=~&#10;}&#10;"></beatscript-radio>

The MCP artifact tool makes the page: {"station": "<the station's text>"} (or the CID of a station stored on the site,
or the name of an example that is one; n for the position) answers a whole HTML page with <beatscript-radio> from jsDelivr,
pinned to one exact version with Subresource Integrity on every module, the station's text and the CID it must have. Publish
it as it is.

#An example station: techno radio

examples/beatscript/techno-radio.beatscript is the station at the top of this note: techno from the seeded generator v2, seed 42,
next seed=~, the rotation seeded passacaglia rondo fugue (R = 4), at 128 in A minor. Its first tracks, form by position:

N chapter, theme t form the track's arrange from line
0 0, 42 seeded gen@2 techno seed=42
1 0, 42 passacaglia template(passacaglia, @byron_sheen_of_their_spears, ground=gen@2 techno seed=42, upper=gen@2 techno seed=878718783)
2 0, 42 rondo template(rondo, @hopkins_pied_beauty_swift_slow, refrain=gen@2 techno seed=42, episodes=gen@2 techno seed=424155996 part=arp)
3 0, 42 fugue template(fugue, @percy_patrick_spens_king, subject=gen@2 techno seed=42, counter=gen@2 techno seed=3893902092)
4 1, 2771245628 seeded gen@2 techno seed=2771245628
5 1, 2771245628 passacaglia template(passacaglia, @lear_derry_down_derry, ground=gen@2 techno seed=2771245628, upper=gen@2 techno seed=4075880107)
6 1, 2771245628 rondo template(rondo, @percy_patrick_spens_king, refrain=gen@2 techno seed=2771245628, episodes=gen@2 techno seed=2151947734 part=arp)
7 1, 2771245628 fugue template(fugue, @hopkins_pied_beauty_glory, subject=gen@2 techno seed=2771245628, counter=gen@2 techno seed=4043716043)

Here 878718783 = fold32(CID of arrange from gen@2 techno seed=42), each next seed the same hash of the call before, and each
phrase line s_N mod 46 of the pinned list. Seed 42 is the ground, the refrain and the subject of the first chapter; 2771245628 is
all three in the second. The station's answer gives every number ("count": 8; with "check": true at most 4 a request).

Track 0 and track 1 in full:

BeatScript
#! station: z4EBG9j79FsQaBchMGcKHL55KLeH6FjsjLQ7V1AE9oj2PencNUE track 0
tempo 128
key am
arrange from gen@2 techno seed=42
BeatScript
#! station: z4EBG9j79FsQaBchMGcKHL55KLeH6FjsjLQ7V1AE9oj2PencNUE track 1
#! from: next(z4EBG9j8WpGPQpiVt6DgPnDa5Luuo9hDJCXk9akNi5dsyUFcRie) seed=~
tempo 128
key am
arrange from template(passacaglia, @byron_sheen_of_their_spears, ground=gen@2 techno seed=42, upper=gen@2 techno seed=878718783)

and the segue between them, 192 bars, its phrase line (42 + 878718783) mod 46 (CID z4EBG9j1xkcjngCue9QURULs1W3tpbaxrd3CtFWsZdffBbYkxJf):

BeatScript
tempo 128
key am
arrange from template(cross, @hiawatha_should_you_ask, a=gen@2 techno seed=42, b=gen@2 techno seed=878718783)

Its neighbours, one notch along each knob: tempo 126 and tempo 130 (the same seeds and forms, two BPM away), key em
and key dm (the same seeds and forms in the keys either side round the circle), seed=41 and seed=43 (two other walks),
next seed=+1 (the walk 42, 43, 44 ..., so its track 1 is the passacaglia of ground 42 and upper 43),
forms passacaglia rondo fugue seeded (track 0 a passacaglia with seed 42 in both roles), and length=short and
length=long (under ~, two other walks). It has no genre notch.

#check

#Not yet (phase 2)

View source (the raw markdown of this page, for models and scripts)