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

Three interval qualities are banned as direct melodic motion in generated lines: augmented, diminished, and the tritone. They are exactly the leaps a Baroque singer would stumble over — the distances that do not fit the scale the ear is tracking.

Augmented and diminished, in one line

An interval is augmented when it is one semitone wider than its perfect or major form, diminished when one semitone narrower (legend). An augmented second spans 3 semitones — the same sound as a harmless minor third, but spelled across adjacent letter names, so the ear hears a stretched, unsingable step rather than a leap.

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. Bach's lines avoid it by choosing the melodic-minor forms on the way up and down; the candidate search reaches the same outcome because the validator rejects the interval.

The three flavors of minor

A minor key has one chord vocabulary but three melodic spellings of its scale: natural minor (no raised notes), harmonic minor (7th degree raised a semitone — this manufactures the leading tone that cadences need), and melodic minor (6th and 7th raised on the way up, natural on the way down). The raised 7th of harmonic minor is what opens the augmented-second trap between degrees 6 and 7; the melodic form exists precisely to walk around it.

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 on the way up, exactly the melodic-minor practice the validator's key-aware spelling check encodes. Descending later in the bar, the line relaxes back onto E♭ — the raised notes exist only to serve the climb. (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.

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?

In MIDI, a diminished seventh (9 semitones — G♯ up to F in A minor) is byte-identical to a major sixth, a perfectly legal consonant leap, so diminished_melodic cannot and does not flag it. (Conveniently, Bach uses the d7 leap as an expressive device anyway.) The rule covers the spans that are unambiguous in semitones: 6 (the tritone / diminished fifth) and 11 (major seventh or diminished octave — unsingable under either name). The one case where spelling truly changes the verdict — the forbidden augmented second versus the harmless minor third, both 3 semitones — is resolved from the key: the leap is flagged when its two notes sit on adjacent scale degrees, which is how a "second" is recognized without letter names. (A 3-semitone leap touching a note foreign to the scale is flagged conservatively as well — though in practice such chromatic notes usually sit inside the secondary-dominant exemption.)

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

From BachBach: WTC I Fugue in C♯ minor — a leap that is dissonant only on paper
The subject of the C♯ minor fugue (WTC I, BWV 849) — four notes, among the most analyzed in the literature. On paper the leap B♯→E is a diminished fourth; in semitones it spans 4, byte-identical to a plain major third, so diminished_melodic has nothing to flag — and the ear agrees, hearing a consonant leap. What gives the subject its dark intensity is the spelling and the handling: the leap is approached by half step and left by step, every note a tendency tone. The rule bans only the spans unsingable under any name (6 and 11 semitones); this is Bach working expressively inside everything the ban leaves open.
diminished_melodicB♯ up to E is a diminished fourth in spelling — but only 4 semitones in sound, identical to a major third, and the line resolves it 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. This is the shape the candidate search prefers when it builds free counterpoint.
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; it emerges from the candidate search scoring, which rewards stepwise motion and penalizes aimless zig-zag. Reading generated voices, you will usually find this arch.
The line rises to one clear high point and settles back down.

No single rule enforces the arch — it emerges from the candidate search scoring, which rewards stepwise motion and chord-tone arrivals. But it is worth knowing the target shape when you read generated voices: a line that rises to one clear high point and settles is the engine behaving well.

Bach wrote the reference implementation. The subject of the "Little" G minor organ fugue spends its one leap immediately, touches its peak once, and walks the rest of the way home by step:

From BachBach: "Little" Fugue in G minor — one peak, then the line pays it back
The subject of the "Little" G minor organ fugue (BWV 578), one of the most singable themes Bach ever wrote — and a model of the leap economy. The line spends its entire melodic budget in the first two notes: a clean fifth up to D, the peak, touched once and never regained. Everything after is repayment — B♭-A, then G-B♭-A-G-F♯ stepping down through the octave, until the cadence figure A→D closes the curve where it began. One climax, stepwise recovery, a clear arch: the shape the candidate search's scoring rewards, written in 1700 or so.
One leap to the high D — the only one — and the rest of the subject walks back down by step.

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_melodicMusicalFailFlags 6- and 11-semitone leaps (diminished fifth; diminished octave / major seventh). The 9-semitone diminished seventh is indistinguishable from a major sixth 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.