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. MIDI cannot tell an augmented fourth from a diminished fifth, so one six-semitone leap is reported by all three rules at once: tritone_melodic, augmented_melodic and diminished_melodic. Only a declared applied-dominant region exempts it.
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, a step that sounds like a leap. Vocal-style writing usually avoids it by raising the sixth as well or lowering the seventh; Bach still uses it on purpose in expressive or chromatic lines. The validator rejects it outside declared secondary-dominant regions.
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. Raised degrees are not reserved for rising lines: the countersubject of the C minor fugue (chapter 6) descends C–B♮–A♮–G over a dominant chord. (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 reads the key in the direction of travel: in minor, the raised sixth and seventh count as scale notes on the way up, the natural sixth and seventh on the way down, and the raised seventh also whenever dominant harmony is sounding. A 3-semitone leap with either end outside that reading is flagged. 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. diminished_melodic checks the semitone span and leaves this four-semitone leap alone; the following E→D♯ moves by step. B♯ is the leading tone of C♯ minor, yet it leaps away instead of rising to C♯: the subject circles the tonic rather than cadencing on it. The engine asks a leading tone to rise only when its harmonic plan puts it under dominant harmony. Bach notates the fugue alla breve (two half-note beats per bar); the excerpt keeps his note values in 4/4.
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
the same span, three ways —
Good contrapuntal lines tend to have one climax per phrase, usually past the middle. 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. Switch the line above the score to compare a leaping line that still has a shape with one that has none.
The line climbs by step to one high point two thirds of the way through, then settles.

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 over dominant harmony (the V chord, built on the fifth degree), the listener expects the tonic next. The engine reads that context from its harmonic plan: the seventh degree under V, V7 or vii° (chapter 5 introduces the symbols), or the borrowed leading tone of an applied dominant, must rise to its resolution in the same voice by at most two semitones:

Tonal syntaxLeading tone must resolve
The harmonic plan decides when a note is a leading tone: the seventh degree sounding under V, V7 or vii°, or the local leading tone of an applied dominant. The next note in that voice must then rise to the resolution pitch class, by at most two semitones. Here B4 over G falls to A instead.
leading_tone_resolutionThe leading tone does not continue upward to tonic.

This is stricter than Bach. In his four-part writing an inner voice often drops from the leading tone to the fifth of the tonic chord, so that the final chord has all three notes; the engine asks every voice to resolve upward.

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 with either end outside the key's scale as read for the direction of motion (the augmented second). Exempt in secondary-dominant regions and on committed cadence-cell notes.
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. Same exemptions.
tritone_melodicMusicalFailNo direct 6-semitone leaps. A tritone therefore trips this rule, augmented_melodic and diminished_melodic together. Same exemptions.
consecutive_leapsMusicalFailNo two consecutive leaps of a fifth or more, regardless of direction. Cadence-cell notes exempt.
leading_tone_resolutionMusicalFailA leading tone in a dominant context from the harmonic plan (V, V7, vii°, or an applied dominant's local leading tone) must rise by at most two semitones to its resolution 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.