Verified Generative Music Programs: Proving Loop-Based Music Correct for Every Bar and Every Knob Setting

Much electronic and popular music is loop-based: a short pattern repeats indefinitely, with gradual variation driven by counters, modular arithmetic, seeded randomness and performer-controlled knobs such as density or transposition. Neural music generators produce this material as a fixed-length score. Examples include 8-bar bass-and-drum loops from a discrete-token autoencoder (Symbolic Music Loop Generation with Neural Discrete Representations) and 1-bar drum spectrograms (A Benchmarking Initiative for Audio-domain Music Generation using the FreeSound Loop Dataset). They follow user constraints only statistically. MuseCoco, a 1.2-billion-parameter text-to-music model, still produces samples that violate requested attributes about 13% of the time (MuseCoco: Generating Symbolic Music from Text). We propose a different output format for controllable generation: a short generator program with a bar loop and exposed knobs, written by a language model and then formally checked. The check uses bounded loop unrolling, which expands the loop into a fixed number of straight-line copies that a logic solver can reason about. It also uses k-induction, which extends that bounded check to every future iteration. Together they establish that user-specified musical rules hold for every bar the program will ever emit and for every allowed knob value. When a rule fails, the checker returns the exact bar and knob setting that breaks it, and the language model uses that counterexample to repair the program.

The practical case for this setup is that loop-based music fails in ways that sampling a few renders does not reveal. A variation counter with period 12 interacts with a 16-step drum pattern and first produces a clash at bar 48. A transposition knob pushes the bass below the instrument's range only at its extreme setting. Both are cases of the "bug at iteration k+1" problem that software verification has studied for decades. The proposal brings that machinery to controllable music. It also extends it beyond the fixed-length artifacts that current music models emit, which neither field has addressed.

Core Claim

Writing controllable music as a small stateful program, rather than as a finite score, makes hard compositional guarantees achievable that are quantified over unbounded time and over continuous control ranges. The proof step that delivers these guarantees uses bounded unrolling plus k-induction over the bar loop. It catches violations that render-and-listen testing at matched compute misses, and the resulting counterexamples make language-model repair converge faster than test-failure feedback does. The claim is deliberately narrow. For a single fixed 16-bar piece, the proposal concedes that simpler hard-constraint methods suffice (see below). The new ground is guarantees for open-ended, knob-driven generators.

Relation to Prior Work

The verification side supplies the method. Automatic Analysis of Scratch-Pad Memory Code for Heterogeneous Multicore Processors applied k-induction directly to software loops. It added "ghost" tracker arrays, which are extra bookkeeping variables that exist only for the proof, and showed that bounded-lifetime properties often close with k=3. Musical rules such as "no parallel fifths between consecutive bars" or "the loop seam from last bar to first is smooth" are bounded-lifetime properties in exactly this sense. We reuse the ghost-state idea to remember the previous bar's voicing. Proving Safety with Trace Automata and Bounded Model Checking showed that plain CBMC solved 0 of 15 crafted safe programs with unbounded loops, while loop acceleration with trace automata solved all 15. That result constrains our design: unrolling alone will not prove anything about indefinitely running generators, so induction or acceleration is required. The same paper warns that acceleration produces nonlinear and quantified formulas that time out on Z3. We therefore restrict the generator language to bit-vector integer arithmetic (modular counters, table lookups, bounded seeded pseudo-random generators), where unrolled formulas stay in decidable, fast fragments.

The language-model-plus-verifier loop follows LLM-Vectorizer: LLM-based Verified Loop Vectorizer. That system had GPT-4 rewrite C loops into vectorized form and validated equivalence with bounded Alive2 checks. It found 24 correct vectorizations that single-shot prompting missed, but formally proved only 38.2% of its benchmark, and only for fixed iteration counts. We adopt its agent loop but differ on what is proved: we target properties over all iterations via induction, not equivalence at a fixed bound. Syntax-Guided Automated Program Repair for Hyperproperties repairs programs with solver-synthesized patches under multi-trace constraints, and its notion of a minimal "transparent" patch informs our repair objective. We do not use its syntax-guided synthesis machinery, because it was limited to small programs.

The music side motivates the gap. MuseCoco (MuseCoco: Generating Symbolic Music from Text) and MuseMorphose (MuseMorphose: Full-Song and Fine-Grained Music Style Transfer with Just One Transformer VAE) steer generation with prefix attribute tokens or with bar-level embeddings injected at every attention layer. MuseMorphose reaches Spearman correlations of 0.95 for rhythmic intensity and 0.88 for polyphony. These are correlations, not guarantees, and both systems emit fixed-length scores. MuPT's synchronized multi-track notation (MuPT: A Generative Symbolic Music Pretrained Transformer) improves bar alignment through tokenization, but alignment remains a learned tendency. The loop generator in Symbolic Music Loop Generation with Neural Discrete Representations is hard-coded to 8 bars. Our programs are length-free by construction, which addresses its fixed-template limitation directly.

Research Program

We will define a small generator language called LoopDSL. A program has a fixed set of integer knobs, each with a declared range (for example density 0–7 or transposition −5 to +7). It also has persistent state and a bar loop body that emits a bounded list of note events per track for each bar. LoopDSL compiles to C, with each bar's emitted events stored in arrays. Musical rules are written as assertions over the current and previous bars:

  • pitch-range limits per instrument;
  • in-key or chord-tone membership against a chord table;
  • an onset grid determined by the meter;
  • no parallel perfect fifths or octaves between bass and melody across bar boundaries;
  • note-density bounds tied monotonically to the density knob, which is the controllability contract;
  • a seam rule stating that the transition back into the loop's first bar obeys the same voice-leading constraints.

Verification proceeds in two stages, both treating knobs as symbolic inputs constrained to their ranges. The first stage runs CBMC-style bounded unrolling to depth k. It either finds a concrete counterexample (bar index plus knob values) or establishes that the base case holds. The second stage attempts a k-induction step, aided by ghost variables for previous-bar voicing and by invariants the language model is asked to propose. If the induction step fails because the invariant is too weak, the system falls back to a bounded-only certificate, and we report which kind of certificate each program received.

The repair loop works as follows. A language model receives a request, such as a reference 8-bar loop drawn from Lakh MIDI, a chord progression from POP909, and a target knob behavior. It writes a LoopDSL program. Counterexamples come back to it as rendered bars along with the specific violated rule, and the cycle repeats under a fixed attempt budget.

Baselines

The naive reference point is the same language model writing LoopDSL programs with no checking, accepted on the first attempt. It shows how often unverified generator code is musically wrong, much as single-shot prompting did in LLM-Vectorizer: LLM-based Verified Loop Vectorizer.

The strongest competitor is render-and-test repair. Here the same model and attempt budget are used, but feedback comes from rendering the program for N bars on a sampled grid of knob settings and reporting any violated rules. This is exactly what a practitioner would build. We match it to our method on wall-clock compute per attempt, and N and the grid size are tuned on a validation split under the same budget. It counts as beaten only if our method has a lower rate of escaped violations at equal or better musicality. Escaped violations are rule failures in accepted programs, measured by a ground-truth audit that renders 10,000 bars across the full knob grid.

The strongest existing music system is MuseCoco, which reports an average sample-wise attribute accuracy of 87.15% at 1.2B parameters (MuseCoco: Generating Symbolic Music from Text). We compare on attribute satisfaction for the finite-length attributes MuseCoco supports. This comparison is a reference point for the reliability of statistical conditioning, not a like-for-like contest. For loop musicality, the reference is the 3.585/5 human rating reported for the VQ-VAE loop generator against MuseGAN's 3.060 (Symbolic Music Loop Generation with Neural Discrete Representations). We will rerun that listening protocol on our outputs rather than compare against the reported number across studies.

Two ablations isolate the key ingredient. The first is bounded-only checking at k equal to the reference loop length with no induction step. It tests whether the unbounded guarantee matters or whether a short unroll catches nearly everything. The second checks only knob midpoints rather than treating knobs as symbolic inputs. It tests whether quantifying over the control range matters.

Why not just…?

Why not just use constrained decoding, masking illegal notes as the model writes the score? For a single fixed-length score with purely local rules, this is a real and simpler competitor, and we expect it to work. We do not claim that regime. Token masking constrains one finite output. It says nothing about a generator that runs for arbitrarily many bars or responds to a knob the performer turns later. It also cannot see cross-bar periodic interactions until it is already past them. The experiments include a fixed 16-bar condition in which we expect parity with constrained decoding. Reporting that condition keeps the claim honest.

Why not just render the program for a long time and test it? This is the main threat. If violations in practice appear within a few hundred bars and at grid knob values, cheap rendering will match formal checking. Our prediction rests on two failure classes. The first is interacting periods whose least common multiple exceeds the render length. The second is violations confined to a single knob combination that a sampled grid misses. The render-and-test baseline at matched compute, scored against the 10,000-bar full-grid audit, settles the question directly. If escaped-violation rates are statistically indistinguishable, we will report that the formal step adds a certificate but no practical reliability.

Why not just scale soft conditioning? MuseCoco's 87.15% at 1.2B parameters shows that scale narrows the gap without closing it. MuPT's 79% human preference over GPT-4 (MuPT: A Generative Symbolic Music Pretrained Transformer) shows that better representations help, yet alignment there is still learned rather than guaranteed. Neither approach yields an object that can be checked for all future bars.

Why not just run plain bounded model checking? The 0/15 result for standard CBMC on unbounded-loop safety (Proving Safety with Trace Automata and Bounded Model Checking) predicts that unrolling alone cannot certify indefinitely running generators. The bounded-only ablation measures how much this matters for music specifically.

Proposed Experiments

We will build a benchmark of roughly 1,000 tasks. Each task pairs a reference loop with a rule set and a knob specification. Reference loops come from the Lakh MIDI 8-bar bass/drum subset extracted in Symbolic Music Loop Generation with Neural Discrete Representations. Chord and key tables come from POP909 annotations (POP909: A Pop-Song Dataset for Music Arrangement Generation). Text descriptions of targets come from MidiCaps captions (MidiCaps - A large-scale MIDI dataset with text captions). We will deliberately seed a held-out set of tasks whose natural variation schemes create long-period interactions, so that the hard cases are represented.

The primary metric is the escaped-violation rate under the full audit. Secondary metrics are the fraction of programs receiving an inductive (unbounded) certificate versus a bounded-only certificate; solver time per check; repair attempts to convergence; controllability, measured as the Spearman correlation between the density knob and measured note density, comparable in form to MuseMorphose's 0.95; and human musicality and loop-ness ratings under the protocol of the loop-generation paper.

The decisive check is the comparison against render-and-test at matched compute on the long-period held-out set. The proposal succeeds if formal checking cuts escaped violations substantially while keeping musicality within the human-rating noise of the unverified baseline. It fails, and we will say so, if rendering catches the same violations. A second decisive check is whether the induction step closes at small k for musical rules as it did for memory-race properties in Automatic Analysis of Scratch-Pad Memory Code for Heterogeneous Multicore Processors. If most programs need invariants the language model cannot supply, the unbounded guarantee would be largely theoretical, and the contribution would shrink to bounded checking with symbolic knobs.