Skip to content

7. Form-Specific Constraints ​

The previous chapters apply to every generated piece. This chapter covers contracts that arise from what kind of piece is being built. Historically, passacaglia and chaconne labels can describe an ostinato scheme realized through varied voicings; this engine chooses stricter carrier contracts. A chorale prelude keeps its cantus-firmus skeleton, and a cello prelude uses one instrumental line to imply coherent multi-voice counterpoint. The engine encodes each promise as material carriers plus a validating rule.

Immutable material: the ground ​

Engine carrierEngine ground: one held note per bar, repeated exactly
Passacaglia and chaconne traditions can use recurring bass or harmonic frameworks whose voicing and surface realization change. The engine chooses the strict version: it emits its ground as one sustained note per bar and repeats it exactly in every cycle, while the voices above it change. The rule checks the pitch at every bar head and that the replayed notes cover the declared timeline exactly once; a changed bar-head pitch is a structural failure.
ground_bass_immutablepassacaglia_ground_immutableThe ground holds one note per bar, C–B♭–A♭–G, while the upper voice varies above it.

A passacaglia and a chaconne are both continuous variations over a repeating foundation, historically related ostinato forms. The names were used loosely, but Bach's two great examples show the usual distinction: the organ Passacaglia in C minor (BWV 582) repeats an eight-bar bass melody, while the Chaconne for solo violin (BWV 1004) varies over a repeating four-bar harmonic scheme whose bass line itself changes. In actual repertoire the scheme may be transferred, voiced, varied, or ornamented. This engine deliberately tightens that idea into a carrier contract: it emits its ground as one sustained note per bar and repeats it exactly in every cycle. ground_bass_immutable (the solo-string form inspired by the BWV 1004 Chaconne) and passacaglia_ground_immutable (the organ form inspired by BWV 582) check the replay two ways: the pitch at every bar head must match the canonical material at the same cycle-relative position, and the replayed notes must cover the declared timeline exactly once, with no holes or overlaps. The check is looser than the builder (a bar split into several notes with the same head and coverage would pass), but the builder never splits one. The comparison uses provenance bits, the origin tags carried beside each note. The provenance.v1 sidecar serializes them as source: "Material" | "Compose" | "Ornament"; generated.v1 note records omit provenance/source, while observations and other validation metadata are exported separately. These violations are StructuralFails because they break the engine's declared carrier identity.

BWV numbers

BWV (Bach-Werke-Verzeichnis) is the standard catalogue of Bach's works — BWV 582 is the C minor Passacaglia for organ, BWV 1004 the violin partita whose final movement is the Chaconne. When a rule says it is "inspired by" a BWV number, the engine's form template borrows selected proportions or surface material; it does not claim to reproduce the work's historical instrumentation or every sounding event.

A related rule keeps the variation layer honest: variation_role_ornament_constraint prevents a variation span that plays the ground role from subdividing below quarter notes — the ground must stay recognizable as a slow line even when restated by another voice.

In Bach's Goldberg Variations the written bass changes from variation to variation while the scheme underneath it stays. The engine is stricter: every non-coda variation block replays its declared aria-bass events exactly, and goldberg_aria_bass_immutable compares every onset, duration and pitch of the replay with the declaration, as well as every bar head. Switch the excerpt above the score to hear how freely Bach's own variations walk the same scheme:

From BachBach: Goldberg Variations — the Aria's bass schemevoices play in turn
over the same declared carrier —
The first eight bars of the Aria of the Goldberg Variations (BWV 988): the actual bass voice above, its reduction to one tone per bar below. The famous descent G–F♯–E–D, then B–C–D–G closing the half-phrase. The variations preserve this bass and harmonic scheme rather than the ornamented melody, while their sounding bass notes and durations can change. This display is a schematic reduction of Bach's bass and harmonic scheme, not a byte-exact replay of the engine carrier. The dedicated goldberg_aria_bass_immutable rule checks each declared carrier onset, duration, and pitch exactly in every non-coda variation block. Bar 8's mordent is shown plain. Switch to a variation above to hear a different surface over the same scheme. The demo's Goldberg preset uses the same distinction.
goldberg_aria_bass_immutableStrip the passing notes from the Aria's bass and one scheme tone per bar remains — a compact reduction of the harmonic plan underlying the variations.

Bach's Passacaglia starts closer to the engine: its first cycles repeat the pedal theme note for note, the same exact repeat the engine's carrier makes. Later variations move or decorate it.

From BachBach: Passacaglia in C minor (BWV 582) — the pedal returnsvoices play in turn
Bach’s organ Passacaglia in C minor (BWV 582) begins with a quarter-note pickup. The upper staff shows the first four full bars of the pedal after that pickup (corpus beats 1–13); the lower staff shows the corresponding four bars at the first return (beats 25–37). Both excerpts contain the same eight attacks in 3/4. The pickup and the final four bars of the eight-bar ground are omitted. The staves are aligned for comparison and play sequentially, not as simultaneous parts. An exact corpus search finds the full 24-beat ground at beats 0, 24, 48, 72, and 96; later variations can move or decorate it.
passacaglia_ground_immutableThe pedal repeats the same pitches and durations in the opening and at the first return.

Immutable material: the cantus firmus ​

Immutable carrierCantus firmus in the tenor, figuration above
The engine's chorale prelude uses the layout of Bach's Schübler chorale "Wachet auf" (BWV 645): running figuration on top, the chorale tune in the tenor, a walking bass in quarter notes below. The tune is a skeleton of one tone per bar, shown here as whole notes. Two contracts hold: each bar head of the cantus restates its skeleton tone (the engine sounds it as a half note and fills the rest of the bar with chord tones), and the figuration's bar-opening note belongs to the bar's chord. Between downbeats, the figuration is free.
cantus_firmus_immutablefiguration_harmonic_consistencyThe chorale tone fixes each downbeat in the tenor; the figuration above must open each bar on a chord tone.

A chorale prelude is an organ piece built on a Lutheran hymn tune, a chorale. Bach's come in several types: the tune in long notes against faster voices (the cantus-firmus type), the tune ornamented in the top voice, and the tune split into phrases that are each treated in imitation (the chorale fugue). The hymn tune, carried in long notes, is the cantus firmus.

The engine writes the first type and takes its layout from the Schübler chorale "Wachet auf, ruft uns die Stimme" (BWV 645): running figuration on top, the chorale tune in the tenor, a walking bass in quarter notes below. It composes its own hymn-like tune in four-bar phrases, one skeleton tone per bar, rather than quoting a chorale. cantus_firmus_immutable checks each bar's downbeat against the declared skeleton tone and requires the cantus to cover its timeline exactly once. The engine sounds each skeleton tone as a half note at the bar head and fills the rest of the bar with chord tones; the ornament pass keeps cantus bar heads plain, and under the Severe character it leaves the whole cantus line unornamented.

Bach's Orgelbüchlein uses other layouts. In "Ich ruf zu dir" the tune sits on top, above a sixteenth-note inner voice and a murmuring pedal:

From BachBach: "Ich ruf zu dir" (Orgelbüchlein) — a chorale carried in long notes
The first bar of "Ich ruf zu dir, Herr Jesu Christ" (BWV 639). The cantus firmus sits on top in long values; this excerpt includes a dotted eighth and a sixteenth at the end of the bar. The engine's cantus_firmus_immutable contract fixes the cantus bar heads; eligible within-bar notes can receive decoration under other characters, while Severe keeps the full cantus plain. Beneath it an inner voice embroiders in sixteenths, and the pedal follows the actual displayed line F–F–F–F–F–E–F–F. (The pedal's first two notes sound an octave lower in the source recording — kept as recorded.)
cantus_firmus_immutablefiguration_harmonic_consistencyThree voices, three layers: a long-note hymn tune, an embroidering inner line, and a moving pedal line.

Figuration harmony ​

The free-prelude style (think BWV 543's opening) runs continuous figuration over a slow harmonic rhythm — the rate at which the underlying chords change, here typically one chord per bar. figuration_harmonic_consistency anchors it: the note that opens each bar must be a tone of that bar's chord, including a declared seventh. Off-downbeat notes are unconstrained — that freedom is what makes it figuration and not chorale writing. Pedal notes are exempt, as are FigurationAnchorRelaxed notes when the builder has proved that no playable chord tone satisfied the full set of constraints.

Free preludeFiguration anchored at the downbeat
The free-prelude texture runs continuous figuration over a slow harmonic rhythm — here one chord per bar, I then V. The validator constrains exactly one note per bar: the downbeat must belong to the bar's chord. The scalework between downbeats is unconstrained; that freedom is what makes it figuration rather than chorale writing.
figuration_harmonic_consistencyEach bar-opening note is a chord tone of that bar's harmony; everything between is free.

The purest illustration in the repertoire opens WTC I — two bars of the C major prelude, where a single line of sixteenths over two held notes spells out one chord per bar:

From BachBach: WTC I Prelude in C — figuration over a slow harmony
The opening of the C major prelude (WTC I, BWV 846) — the most famous figuration in keyboard music, written on three rhythmic layers exactly as shown: a held bass, a held inner note, and a rising five-note figure. Nothing here is melody; everything is harmony drawn out in time, changing once per bar (the C major tonic, then ii⁴² over the same bass note). The bar-opening note is always a chord tone — the anchor figuration_harmonic_consistency checks — and the ear assembles five voices from a single written line.
figuration_harmonic_consistencyOne broken-chord figure per half bar, one chord per bar — each downbeat states a chord tone of that bar's harmony.

Solo strings: counterpoint inside one line ​

A solo cello or violin can sound a four-note chord, but it cannot sustain four independent lines at once; Bach's solo writing nevertheless makes the ear hear several. The cello prelude is the engine's only single-voice form, and these rules are how one voice still gets counterpoint validation.

The reconstruction is three concrete steps. The engine collects the arpeggio line's notes in onset order, partitions them into contiguous cells of the declared group_size, then takes each cell's lowest pitch as the implied bass stream and its highest as the implied top stream. Register decides, not slot position — in the BWV 1007 figuration the perceived melody note sits in the middle of the written cell, and the min/max extraction still finds it:

Solo stringsOne line, two voices: extracting the streamsvoices play in turn
This is what the music actually looks like: one unbroken line of broken chords, BWV 1007-style. The engine partitions the line into cells of the declared group size, then takes each cell's lowest note (amber) as the implied bass and its highest (green) as the implied melody. Register decides, not position — here the top note sits in the middle of the cell, and the extraction still finds it. Play it in sequence: first the real line, then the bass your ear was already hearing.
implicit_voice_counterpointarpeggio_no_parallel_perfectEach 4-note cell contributes its lowest pitch to a bass stream and its highest to a top stream.

And here is the original behind that figure — the opening of the first cello suite, with the extraction the engine computes written out underneath:

From BachBach: Cello Suite No. 1 — the voices inside one linevoices play in turn
The opening of the G major cello suite (BWV 1007) — the most famous implied counterpoint ever written for one instrument. The cello plays nothing but sixteenths, yet every eight-note cell anchors G at the bottom and rings B (then C) at the top, so the ear hears a pedal bass under a slow upper line. And the melody note really does sit in the middle of the written cell — register finds it, position would not. The extraction below each line is exactly what the engine computes before checking the streams like real voices: here the top line steps up over a held bass — oblique motion, no parallel perfects, pass.
implicit_voice_counterpointarpeggio_no_parallel_perfectEach eight-note cell projects its lowest pitch into a pedal bass and its highest into a melody that steps B–B–C–C.

The extracted streams are then held to the same standards as real voices. The two reductions below show the streams alone — first failing, then passing:

Solo stringsImplied parallel perfects in arpeggio
Solo cello or violin writing can imply multiple voices. The validator reconstructs low and high streams and checks them like real counterpoint: a twelfth is a compound fifth, so each step here is a parallel fifth between the implied voices.
implicit_voice_counterpointarpeggio_no_parallel_perfectThe bass and top streams rise together in twelfths (an octave plus a fifth) three times in a row.
Solo stringsImplied streams in contrary motion
This is the shape the cello prelude generator aims for: when each broken-chord cell changes, its lowest and highest notes trace independent, contrary lines. Both examples start from the same twelfth over C. Here the top stream falls while the bass rises, so the next intervals are a tenth and an octave reached in contrary motion, and nothing moves in parallel perfects.
implicit_voice_counterpointarpeggio_no_parallel_perfectThe reconstructed bass stream rises while the top stream descends — real two-part counterpoint inside one instrument.
RuleContract
implicit_voice_counterpointBetween consecutive cells, the implied bass and top streams obey the melodic-leap rules from chapter 4 — the same forbidden-leap predicate used for scored Compose notes.
arpeggio_no_parallel_perfectConsecutive cells must not frame the same perfect interval (fifth or octave) with both streams moving in the same direction. Oblique, contrary, and static motion are permitted — exactly the escape hatches real voices get in chapter 2.

The physical side of solo-string writing — each instrument's playable range — lives in Instruments.

Phrase architecture ​

Two rules keep generated music on the Baroque bar grid. An anacrusis (upbeat) is the short lead-in that sounds before a downbeat — the "and-a" pickup before bar 1:

Phrase gridAnacrusis: the upbeat lead-in
An anacrusis starts the music before the bar: a pickup that resolves into the first downbeat. The engine treats it as a declared property — if the piece declares an upbeat of a given length, every upbeat fragment must begin exactly that distance before a phrase start, and if no upbeat is declared, none may appear. Consistency, not existence, is what the rule checks.
anacrusis_consistentThe short note before the barline leads into the downbeat where the phrase really starts.

Bach's dance movements live on this gesture — the Courante of the first cello suite spends one eighth note getting airborne:

From BachBach: Cello Suite No. 1 Courante — starting before the bar
The Courante of the first cello suite (BWV 1007) begins one eighth note early: a single G whose only job is to launch the downbeat G that actually starts the phrase. This is the anacrusis as a declared property — the engine's contract says that once a piece declares an upbeat of a given length, every upbeat fragment must begin exactly that distance before a phrase start. anacrusis_consistent checks the consistency, not the existence.
anacrusis_consistentA single eighth note sounds before the first barline and leads into the downbeat where the phrase begins.
RuleContract
phrase_periodicity_4_or_8_barConsecutive declared phrase starts are 3 through 8 whole bars apart. The stable rule ID keeps its older, narrower name, but every integer bar gap in that inclusive range passes.
anacrusis_consistentIf the piece declares an upbeat (anacrusis), every upbeat fragment begins exactly that distance before a phrase start; if not declared, no stray upbeat material exists.

Texture and instrument contracts ​

A Bach organ trio sonata has three lines of equal weight: one for each hand, on its own keyboard (manual), and one for the feet on the pedal. The two upper lines share and imitate the material as equals over the bass. The trio sonata's independence contract is the one texture rule whose target can be drawn on staves. The violation itself is a property of whole sections, but this — three voices on three rhythmic grids — is the texture the engine scores toward:

Trio sonataThree voices, three rhythms
The trio sonata has one job: three lines that stay audibly independent. Here the two manuals are equal partners. The second states the first one's figure C–D–E–D–C a fifth lower, two beats later, and the pedal walks underneath, holding when the others move. The engine scores each voice pair at its note boundaries (a boundary counts as independent when only one voice moves, or when both move but not in the same direction) and fails the section when the mean of the three pair scores falls below 0.6. Here the mean is about 0.8.
voice_independence_thresholdThe second manual answers the first a fifth lower, two beats later; the pedal moves in its own rhythm below.

The organ trio sonatas are this rule pursued for six works straight. Three bars in, the first of them already shows the full texture:

From BachBach: Trio Sonata No. 1 — three voices, three grids
Bar 3 of the E♭ major organ trio sonata (BWV 525): one manual takes the theme while the other spins the continuous sixteenth line, and the pedal walks underneath. Three voices, three rhythmic grids — at almost every note boundary the other voices are either holding or moving the other way. This is the texture voice_independence_threshold scores toward: every voice pair is measured on rhythmic offset and contrary/oblique motion, and the section fails when the mean of the three pair scores drops below 0.6. (The theme's last G is tied on into the next bar; the excerpt clips it at the barline.)
voice_independence_thresholdTheme in eighths and quarters, an unbroken sixteenth counterline, a walking eighth-note pedal — no voice shadows another.
RuleFormContract
pedal_range_soft_penaltyorgan formsPedal notes stay inside the physically playable compass (hard bounds MIDI 12–62; the comfortable C1–D3 target is preferred at scoring time).
voice_independence_thresholdtrio sonataEach pair of the three voices is scored at its note boundaries: a boundary counts as independent when only one voice moves, or when both move but not in the same direction. The mean of the three pair scores must be at least 0.6. A trio sonata whose voices shadow each other has failed at its one job.
section_contrast_requiredfantasiaAdjacent sections differ audibly in density (≥2 notes/bar) or register (≥5 semitones mean pitch) — the fantasia's defining drama of contrasts.
toccata_archetype_compatibletoccataThe selected sectional archetype fits the declared character (the noble character is rejected for toccata forms at config time; incompatible archetype pairings fail here).

The remaining structural rules have no single staff moment to point at — the violation is a property of whole sections — so they are documented as tables rather than examples. When one fires, inspect the section layout in the event JSON rather than individual notes.

A declared doubling is checked before any exemption is applied. It must name two distinct voices and a valid, nonempty half-open [start_tick, end_tick) window containing at least one lead note. Inside that window, the doubled voice must have the same note count, onsets, and durations as the lead voice, with every pitch offset by the declared semitones. A mismatch emits declared_doubling_integrity as a StructuralFail. A verified window treats only two-voice vertical findings inside it as one line; linear checks and vertical findings outside the window still apply.

How the validator sees this chapter ​

RuleFailKind
declared_doubling_integrityStructuralFail
ground_bass_immutable, passacaglia_ground_immutable, goldberg_aria_bass_immutable, cantus_firmus_immutableStructuralFail
variation_role_ornament_constraint, figuration_harmonic_consistency, toccata_archetype_compatibleMusicalFail
implicit_voice_counterpoint, arpeggio_no_parallel_perfectMusicalFail
phrase_periodicity_4_or_8_bar, anacrusis_consistentMusicalFail
pedal_range_soft_penalty, voice_independence_threshold, section_contrast_requiredMusicalFail

This concludes the course. For the flat rule index, see the Validator Rule Reference; for how these rules interact with the generation pipeline, see Generation Pipeline.

Dual-licensed: AGPL-3.0 · commercial licensing available. Generated MIDI is yours to use freely.