Skip to content

4. Melodic Writing ​

A note can be vertically legal and still wreck the piece. Counterpoint demands that every voice, read alone, remain a plausible melody — historically, a singable one. The validator therefore checks melodic intervals inside each voice, not only vertical sonorities between voices.

Vertical interval vs melodic interval

A vertical interval compares two voices at the same time. A melodic interval compares one voice against its own next note. The validator checks both dimensions; this chapter is about the second.

Forbidden leaps ​

The engine conservatively bans three interval categories as direct melodic motion in generated lines: augmented, diminished, and tritone spans. Historical acceptability depends on interval spelling, scale context, and musical setting.

Augmented and diminished, in one line

An interval is augmented when it is one semitone wider than its perfect or major form. It is diminished when it is one semitone narrower than its perfect form or, for a major/minor interval, one semitone narrower than its minor form (legend). An augmented second spans 3 semitones; in 12-tone equal temperament that is the same semitone distance as a minor third, while the letter spelling and tonal context determine the written interval and its treatment.

MelodyForbidden melodic leap
The validator checks each generated voice as a melody. Augmented, diminished, and tritone leaps are rejected unless an applied-harmony context exempts the motion.
tritone_melodicaugmented_melodicdiminished_melodicThe red melodic leap crosses a tritone-class gap.
MelodyAugmented second in minor
In harmonic minor, moving directly from the natural sixth degree to the leading tone produces an augmented second — the interval that makes a line sound instrumental rather than vocal. Bach avoids it by using the melodic-minor scale forms; the validator simply rejects the interval. Secondary-dominant regions are exempt, because applied harmony legitimately introduces chromatic steps.
augmented_melodicThe step from the sixth to the raised seventh degree spans three semitones.

The augmented second deserves its own example because it arises so naturally in minor keys: harmonic minor raises the seventh degree (the leading tone), leaving a three-semitone gap from the natural sixth. In historical tonal writing, however, scale-degree alterations follow harmony and voice leading; raised sixths and sevenths can occur in either direction. The familiar ascending/descending melodic-minor pattern is a useful teaching model, not a rule Bach applies mechanically. MIDI Sketch Bach's form builders choose carrier material for this interval check, and the validator rejects a forbidden leap if one reaches the final score. See the common-practice scale overview.

The three flavors of minor

A minor key has one chord vocabulary but three useful textbook spellings of its scale: natural minor (no raised notes), harmonic minor (7th degree raised a semitone to supply a leading tone), and melodic minor (often described as 6th and 7th raised on the way up and natural on the way down). These labels describe common patterns rather than three exclusive scales: Bach and other composers alter degrees 6 and 7 in either direction when the harmony or voice leading calls for it. The raised 7th of harmonic minor opens the augmented-second gap between degrees 6 and 7; the melodic form is one way to avoid that gap.

That walk-around is not a theory abstraction — it is the first bar of the Fifth Cello Suite:

From BachBach: Cello Suite No. 5, Prélude — the ascent that dodges the augmented second
The first bar of the Fifth Cello Suite (BWV 1011), in C minor. The opening C hangs in the air, then the line climbs from G straight through A♮ and B♮ back to C. The key signature says A♭ and B♭; written that way, the ascent would contain the augmented second A♭→B♮ — the leap-sized "step" the previous figure shows being rejected. Bach raises both degrees in this local ascent, matching the validator's key-aware spelling check. The later descent through E♭ shows one context for these raised degrees; in Bach, sixth and seventh degrees vary with harmony and voice leading rather than following an ascending-only rule. (The opening C sounds over its lower octave in the source; the double stop is omitted here.)
augmented_melodicClimbing toward C, the line raises degrees 6 and 7 — A♮ and B♮ in C minor — so every step stays singable.

This Fifth Suite excerpt is one ascending realization of that pattern, not a general rule for every Bach descent. Bach also uses chromatic inflection inside a subject:

From BachBach: WTC I Fugue in F minor (BWV 857) — chromatic subject descent
In m. 3 of WTC I’s F minor fugue (BWV 857), the subject descends A♮ on beat 1, A♭ on beat 2, and G across beats 3–4. A♮ is chromatic against the F-minor signature; A♭ is the diatonic third, and the line continues to G. This is a one-voice reduction: the lower staff is silent in this bar and the other voices are omitted; the next entry begins in m. 4, beat 2.
The subject spells A♮–A♭–G, a chromatic descent inside one line.

The applied-harmony exemption

Inside a declared secondary-dominant region (chapter 5), chromatic motion is the point — so augmented_melodic, diminished_melodic, and tritone_melodic are all exempt there. The borrowed leading tone gets to behave like a leading tone.

Where is the diminished seventh?

MIDI records semitone distance, not letter spelling. A diminished seventh (9 semitones — G♯ up to F in A minor) therefore cannot be distinguished from a major sixth by this data, so diminished_melodic cannot and does not flag that spelling. The validator treats 6- and 11-semitone melodic spans conservatively in its melodic rules; a semitone count alone does not determine one written interval, and chromatic context can change the musical interpretation. For the 3-semitone A2/m3 cases, the engine uses the key: it flags the leap when the endpoints occupy adjacent scale degrees and treats a span touching a foreign note conservatively. Secondary-dominant regions remain exempt.

Bach shows what that scope leaves open. The C♯ minor fugue's subject leaps a written diminished fourth — four semitones, the same equal-tempered semitone span as a major third — and handles it with the care the rule is really about:

From BachBach: WTC I Fugue in C♯ minor — a spelled diminished fourth
The subject of the C♯ minor fugue (WTC I, BWV 849) contains the notated leap B♯→E. Its spelling names a diminished fourth; its sounding size is four semitones, equal to a major third in 12-tone equal temperament. This is a melodic interval, so the example does not classify a vertical dissonance. diminished_melodic checks the semitone span and leaves this four-semitone leap alone; the following E→D♯ moves by step. The notation keeps the chromatic voice leading visible.
diminished_melodicB♯ up to E is a diminished fourth by spelling but spans 4 semitones in sound, the same size as a major third; the line leaves the leap by step.

Leaps need recovery ​

Even consonant leaps are rationed. A step moves to the adjacent scale note; anything larger is a leap (primer). A leap spends melodic energy; stepwise motion in the opposite direction pays it back. Two large leaps back to back with no recovery make the line stop sounding like a voice:

MelodyConsecutive large leaps
One leap can be idiomatic. Repeated large leaps make the line stop behaving like a singable contrapuntal voice.
consecutive_leapsTwo large leaps occur back to back without stepwise recovery.
Allowed patternLeap, then step back
The classical recovery rule: a large leap spends melodic energy, and stepwise motion in the opposite direction pays it back. The opt-in Passacaglia free-counterpoint search prefers this shape when it builds its counterline; default form generation replays authored material.
consecutive_leapsAfter the sixth upward, the line turns around and descends by step.

Contour: one peak per phrase ​

Line shapeA single-peak melodic arch
Good contrapuntal lines tend to have one climax per phrase. No single validator rule enforces this. The opt-in Passacaglia free-counterpoint scorer rewards stepwise motion and penalizes aimless zig-zag; the default forms express the same principle in their authored material.
The line rises to one clear high point and settles back down.

No single rule enforces the arch. In the default path, the contour comes from the line authored by the form builder and replayed by its carrier. Scored search is not responsible for default melodic shape; it is available only for the opt-in Passacaglia counterline. The arch is still useful when reading a generated voice: a line that rises to one clear high point and settles has a coherent phrase shape.

The opening subject of Bach's "Little" Fugue in G minor (BWV 578) shows a compact arch with an early high point and a return toward its opening register:

From BachBach: "Little" Fugue in G minor — one peak, then a varied return
The subject of the "Little" G minor organ fugue (BWV 578) rises from G to a high D, then returns through a mix of stepwise motion and smaller leaps: D→B♭, G→B♭, F♯→A, and the closing A→D. The single high point remains clear, while the recovery is not stepwise throughout. The optional free-counterpoint scorer rewards this broader arch.
The subject rises to a single high D, then returns through stepwise motion and several smaller leaps.

The leading tone's obligation ​

The seventh scale degree — one semitone below the tonic — is called the leading tone because it leads. Once a voice sounds it in a dominant context, the listener expects the tonic next. Material can mark a note as a leading tone, and the validator holds the voice to the promise:

Tonal syntaxLeading tone must resolve
When material marks a leading tone, the next note in that voice must resolve to the tonic pitch class by step.
leading_tone_resolutionThe leading tone does not continue upward to tonic.

Range integrity ​

Each voice also declares a playable range in its texture plan (soprano, alto, tenor, bass — or manual and pedal compass for organ writing). Any note outside the inclusive MIDI bounds fails voice_range_integrity. This is less a stylistic rule than a physical one; it pairs with the instrument-specific ranges described in Instruments.

Physical limitNote outside the declared range
Every voice declares an inclusive MIDI range [lo, hi] in its texture plan — a soprano compass, an organ pedal compass, a cello's strings. This is a physical rule, not a stylistic one: a single note outside the bounds fails the voice, however good the counterpoint around it is.
voice_range_integrityThe red note exceeds the upper bound of the voice's declared MIDI range.

How the validator sees this chapter ​

RuleFailKindCheck
augmented_melodicMusicalFailFlags 6-semitone leaps, and 3-semitone leaps whose endpoints sit on adjacent scale degrees or off the scale (the augmented second; spelling reconstructed from the key). Exempt in secondary-dominant regions.
diminished_melodicMusicalFailConservatively flags 6- and 11-semitone melodic spans (possible spellings include A4/d5 and M7/d8). The 9-semitone diminished seventh/major sixth span is indistinguishable in MIDI and is not flagged. Exempt in secondary-dominant regions.
tritone_melodicMusicalFailNo direct 6-semitone leaps. Exempt in secondary-dominant regions.
consecutive_leapsMusicalFailNo two consecutive leaps of a fifth or more, regardless of direction. Cadence-cell notes exempt.
leading_tone_resolutionMusicalFailA note marked as a leading tone must step up to the tonic pitch class in the same voice's next note.
voice_range_integrityMusicalFailEvery note stays inside its voice's declared [lo, hi] MIDI range.

Continue with Chapter 5 — Tonal Grammar.

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