Skip to content

Instantly share code, notes, and snippets.

@djosix
Created April 17, 2026 13:09
Show Gist options
  • Select an option

  • Save djosix/0124931db6a5fd51662f8721eaeb7b2e to your computer and use it in GitHub Desktop.

Select an option

Save djosix/0124931db6a5fd51662f8721eaeb7b2e to your computer and use it in GitHub Desktop.
"""
Piece 004: Piano ballad in D major — focus on tension preparation and storytelling arc.
Form identical to 003 (56 bars, 76 BPM) but reworked so every dissonant tone is
approached by half-step, neighbor tone, or common-tone pivot.
Story arc:
Intro (1-4): quiet village — pure tonic, no tension
A1 (5-12): daily life — motif established, gentle diatonic
B1 (13-16): subtle disquiet — diatonic pre-chorus (saves tritones for B2)
C1 (17-24): the journey begins — first emotional peak, Canon in D
Interlude (25-28): wider world — bebop with CAREFULLY PREPARED chromatics
A2 (29-36): return changed — counter-melody, foreshadows bridge
Bridge (37-40): dark night — Gm modal mixture, prepared by half-step bass + common tones
B2 (41-44): rising strength — PEAK TENSION: F7 / Eb7 / A7alt, all voice-led chromatically
C2 (45-52): victory — full resolution, Hisaishi climax
Coda (53-56): after — open-fifth modal wisdom
Key preparation techniques used:
- Tritone subs (F7, Eb7) approached by half-step in every voice
- Altered b9 Bb approached from B (above) or A (below) by half-step
- #9 D-natural on B7 approached via D#5 chord tone (half-step above)
- Modal mixture Gm approached via D4 common tone + B2→Bb2 half-step bass
- Every peak note reached by prepared shape (arpeggio or stepwise), not arbitrary leap
"""
from mido import Message, MidiFile, MidiTrack, MetaMessage, bpm2tempo
TPQ, BPM, BPB = 480, 76, 4
mid = MidiFile(ticks_per_beat=TPQ)
cond = MidiTrack(); mid.tracks.append(cond)
cond.append(MetaMessage("set_tempo", tempo=bpm2tempo(BPM), time=0))
cond.append(MetaMessage("time_signature", numerator=4, denominator=4, time=0))
cond.append(MetaMessage("key_signature", key="D", time=0))
pno = MidiTrack(); mid.tracks.append(pno)
pno.append(MetaMessage("track_name", name="Piano", time=0))
pno.append(Message("program_change", channel=0, program=0, time=0))
pno.append(Message("control_change", channel=0, control=7, value=112, time=0))
pno.append(Message("control_change", channel=0, control=91, value=55, time=0))
events = []
def _a(bar, beat): return int(((bar - 1) * BPB + beat) * TPQ)
def n(bar, beat, dur, p, v):
s = _a(bar, beat); d = int(dur * TPQ)
events.append((s, 1, p, max(1, min(127, int(v)))))
events.append((s + max(d - 15, 1), 0, p, 0))
def cc(tick, c, v):
events.append((tick, 2, c, v))
_T3 = {2:3, 4:3, 6:4, 7:3, 9:3, 11:4, 1:4}
def t3(p): return p - _T3.get(p % 12, 3)
MOTIFS = {
'D': [69, 74, 78, 76, 74],
'G': [62, 67, 71, 69, 67],
'A': [64, 69, 73, 71, 69],
'F#m': [61, 66, 69, 67, 66],
'Bm': [66, 71, 74, 73, 71],
'Em': [59, 64, 67, 66, 64],
'Gm': [62, 67, 70, 69, 67],
}
def motif(bar, beat, chord, vel=68, harm=None, octave=0):
pitches = [p + octave for p in MOTIFS[chord]]
durs = [0.5, 0.5, 1.5, 0.5, 1]
vels = [vel - 3, vel + 2, vel + 5, vel - 1, vel - 2]
t = beat
for p, d, v in zip(pitches, durs, vels):
n(bar, t, d, p, v)
if harm == 'thirds':
n(bar, t, d, t3(p), v - 5)
elif harm == 'sixths':
n(bar, t, d, p - 9, v - 5)
elif harm == 'octaves':
n(bar, t, d, p - 12, v - 6)
t += d
# ======================================================
# INTRO (1-4): pp, pure diatonic, D pedal — the quiet village
# ======================================================
# Bar 1: DM9 — tonic tranquil
n(1, 0, 4, 38, 48); n(1, 0, 4, 45, 40); n(1, 0, 4, 54, 36)
n(1, 0, 4, 61, 34); n(1, 0, 4, 64, 34)
n(1, 2, 2, 81, 50) # A5 half — single spacious note
# Bar 2: Em7/D — upper harmony gently shifts (all diatonic)
n(2, 0, 4, 38, 46); n(2, 0, 4, 52, 38); n(2, 0, 4, 55, 36)
n(2, 0, 4, 59, 36); n(2, 0, 4, 62, 34)
# Melody: leap down a 5th (intervallic but stable — all D major)
n(2, 0, 2, 74, 52) # D5
n(2, 2, 2, 78, 48) # F#5 (up 3rd, arpeggio-shaped)
# Bar 3: GM9/D — plagal color, D pedal continues
n(3, 0, 4, 38, 46); n(3, 0, 4, 55, 38); n(3, 0, 4, 59, 36)
n(3, 0, 4, 66, 36); n(3, 0, 4, 69, 34)
# Melody: stepwise arrival on D6 (prepared by B5 step up from F#5 of prior bar via common B)
n(3, 0, 1, 83, 54) # B5 (up 3rd from F#5 — arpeggio shape)
n(3, 1, 1, 81, 50) # A5 (step down)
n(3, 2, 2, 86, 56) # D6 (up 4th — arpeggio again)
# Bar 4: A7sus4/D — gentle dominant, unresolved
n(4, 0, 4, 38, 48); n(4, 0, 4, 57, 40); n(4, 0, 4, 62, 38)
n(4, 0, 4, 64, 36); n(4, 0, 4, 67, 36)
# Melody: descending arpeggio shape leading to verse (chord tones only)
n(4, 0, 1, 83, 52) # B5 (step down from D6)
n(4, 1, 1, 81, 50) # A5
n(4, 2, 2, 78, 48) # F#5 half (lands on chord tone of A — 6/13)
# ======================================================
# A1 VERSE (5-12): mp, daily life, motif + prepared lines
# ======================================================
# Bar 5: GM7 — motif statement
n(5, 0, 1, 43, 58); n(5, 0, 4, 59, 44)
n(5, 1, 0.5, 50, 45); n(5, 1.5, 0.5, 54, 42)
n(5, 2, 1, 55, 56)
n(5, 3, 0.5, 50, 45); n(5, 3.5, 0.5, 54, 42)
motif(5, 0, 'G', vel=72)
# Bar 6: A/G — intervallic but stable (arpeggio shape, not random leaps)
n(6, 0, 1, 43, 54); n(6, 0, 4, 57, 44)
n(6, 1, 0.5, 49, 45); n(6, 1.5, 0.5, 52, 42)
n(6, 2, 1, 55, 54)
n(6, 3, 0.5, 49, 45); n(6, 3.5, 0.5, 52, 42)
# Arpeggio pattern (A major: A C# E) — "intentional leaps" not random
n(6, 0, 1, 73, 68) # C#5 (3 of A)
n(6, 1, 1, 76, 66) # E5 (5 of A, up M3 — chord tone leap)
n(6, 2, 1, 69, 62) # A4 (root, down 5th — arpeggio descent)
n(6, 3, 1, 73, 60) # C#5 (back to 3 — settle)
# Bar 7: F#m7 — motif on F#m
n(7, 0, 1, 42, 56); n(7, 0, 4, 57, 44)
n(7, 1, 0.5, 49, 45); n(7, 1.5, 0.5, 52, 42)
n(7, 2, 1, 54, 54)
n(7, 3, 0.5, 49, 45); n(7, 3.5, 0.5, 52, 42)
motif(7, 0, 'F#m', vel=72)
# Bar 8: B7 — #9 (D natural) PROPERLY PREPARED by chromatic descent
# F#m7 ended on F#4. Approach D via: F#5 → E5 → D#5 → D5 (chromatic stepwise).
n(8, 0, 1, 47, 58); n(8, 0, 4, 63, 44) # B2, D#4 shell
n(8, 1, 0.5, 54, 45); n(8, 1.5, 0.5, 57, 42)
n(8, 2, 1, 59, 54)
n(8, 3, 0.5, 54, 45); n(8, 3.5, 0.5, 57, 42)
# Melody: chromatic descent F#5 → E5 → D#5 → D5(#9) → A4
n(8, 0, 1, 78, 66) # F#5 (5 of B7)
n(8, 1, 1, 76, 64) # E5 (step down — passing)
n(8, 2, 0.5, 75, 64) # D#5 (step down, chord tone 3 of B7)
n(8, 2.5, 0.5, 74, 62) # D5 (half-step down — #9 PREPARED ✓)
n(8, 3, 1, 69, 58) # A4 (leap down to b7, resolves)
# Bar 9: Em7 — motif (D4 of bar 8 resolves up to E root of Em)
n(9, 0, 1, 40, 56); n(9, 0, 4, 55, 42)
n(9, 1, 0.5, 47, 45); n(9, 1.5, 0.5, 50, 42)
n(9, 2, 1, 52, 54)
n(9, 3, 0.5, 47, 45); n(9, 3.5, 0.5, 50, 42)
motif(9, 0, 'Em', vel=72)
# Bar 10: A7sus4 — DIATONIC line (removed 003's unprepared tritone leap)
n(10, 0, 1, 45, 56); n(10, 0, 4, 61, 44)
n(10, 1, 0.5, 50, 45); n(10, 1.5, 0.5, 52, 42)
n(10, 2, 1, 57, 54)
n(10, 3, 0.5, 50, 45); n(10, 3.5, 0.5, 55, 42)
# Arpeggio shape: E-G-A-F# (A chord tones + sus) — all chord tones, no unprepared chromatic
n(10, 0, 1, 76, 66) # E5 (5)
n(10, 1, 1, 79, 68) # G5 (b7 — chord tone on A7)
n(10, 2, 1, 81, 68) # A5 (root, stepwise up)
n(10, 3, 1, 78, 62) # F#5 (step down, landing)
# Bar 11: DM7 — resolution (F#5 of bar 10 is common tone 3 of DM7)
n(11, 0, 1, 38, 60); n(11, 0, 4, 54, 46)
n(11, 1, 0.5, 45, 48); n(11, 1.5, 0.5, 49, 46)
n(11, 2, 1, 50, 56)
n(11, 3, 0.5, 45, 46); n(11, 3.5, 0.5, 49, 44)
n(11, 0, 1, 78, 66); n(11, 1, 1, 76, 62); n(11, 2, 2, 74, 64)
# Bar 12: D/F# — turnaround
n(12, 0, 1, 42, 58); n(12, 0, 4, 57, 45)
n(12, 1, 0.5, 50, 46); n(12, 1.5, 0.5, 54, 44)
n(12, 2, 1, 45, 50)
n(12, 3, 0.5, 50, 44); n(12, 3.5, 0.5, 54, 42)
n(12, 0, 2, 71, 62); n(12, 2, 1, 69, 58); n(12, 3, 1, 67, 54)
# ======================================================
# B1 PRE-CHORUS (13-16): SUBTLE DISQUIET — DIATONIC (tritone subs moved to B2!)
# GM7 | F#m7 B7 | Em7 A7sus4 | A7 (natural V progression)
# ======================================================
# Bar 13: GM7 syncopated comping
n(13, 0, 0.5, 43, 64)
n(13, 0.5, 0.5, 50, 55); n(13, 0.5, 0.5, 54, 52); n(13, 0.5, 0.5, 59, 50)
n(13, 1, 0.5, 55, 58)
n(13, 1.5, 0.5, 50, 52); n(13, 1.5, 0.5, 54, 50)
n(13, 2, 0.5, 43, 62)
n(13, 2.5, 0.5, 50, 55); n(13, 2.5, 0.5, 54, 52); n(13, 2.5, 0.5, 59, 50)
n(13, 3, 0.5, 47, 60)
n(13, 3.5, 0.5, 50, 54); n(13, 3.5, 0.5, 54, 52)
# Melody: arpeggio shape (B D F#) + step — all chord tones
n(13, 0, 1, 71, 72) # B4 (3 of G)
n(13, 1, 1, 74, 74) # D5 (5 of G, chord tone leap)
n(13, 2, 1, 78, 76) # F#5 (7 of G, chord tone leap)
n(13, 3, 1, 81, 80) # A5 (9 — passing into next bar)
# Bar 14: F#m7 | B7 (DIATONIC, not tritone — natural V/iii or V/vi)
n(14, 0, 0.5, 42, 64)
n(14, 0.5, 0.5, 49, 55); n(14, 0.5, 0.5, 52, 52); n(14, 0.5, 0.5, 57, 50)
n(14, 1, 0.5, 54, 60)
n(14, 1.5, 0.5, 49, 52); n(14, 1.5, 0.5, 57, 50)
# B7 beats 3-4 — natural diatonic (not F7 tritone)
n(14, 2, 0.5, 47, 64)
n(14, 2.5, 0.5, 51, 55); n(14, 2.5, 0.5, 54, 52); n(14, 2.5, 0.5, 57, 50) # D# F# A
n(14, 3, 0.5, 59, 60)
n(14, 3.5, 0.5, 51, 52); n(14, 3.5, 0.5, 57, 50)
# Melody: stepwise chord-tone motion (A5 is 9 of G, then on F#m is b9 — resolves to A4)
n(14, 0, 1, 81, 76) # A5 (held from bar 13)
n(14, 1, 1, 78, 74) # F#5 (step down to chord tone of F#m)
n(14, 2, 1, 75, 76) # D#5 (3 of B7 — chord tone leap down)
n(14, 3, 1, 78, 72) # F#5 (back to 5 of B7)
# Bar 15: Em7 | A7sus4 (DIATONIC)
n(15, 0, 0.5, 40, 66)
n(15, 0.5, 0.5, 47, 58); n(15, 0.5, 0.5, 50, 55); n(15, 0.5, 0.5, 55, 52)
n(15, 1, 0.5, 52, 62)
n(15, 1.5, 0.5, 47, 54); n(15, 1.5, 0.5, 55, 52)
# A7sus4 beats 3-4
n(15, 2, 0.5, 45, 66)
n(15, 2.5, 0.5, 50, 58); n(15, 2.5, 0.5, 52, 56); n(15, 2.5, 0.5, 55, 54) # D E G
n(15, 3, 0.5, 57, 62)
n(15, 3.5, 0.5, 50, 56); n(15, 3.5, 0.5, 55, 52)
# Melody: diatonic descent + step up (all chord tones)
n(15, 0, 1, 76, 78) # E5 (root of Em)
n(15, 1, 1, 74, 76) # D5 (7 of Em, step down)
n(15, 2, 1, 79, 80) # G5 (b7 of A7, step up — building!)
n(15, 3, 1, 81, 82) # A5 (root of A, stepwise)
# Bar 16: A7 — triplet lift, prepared chromatic C#
n(16, 0, 0.5, 45, 68)
n(16, 0.5, 0.5, 49, 60); n(16, 0.5, 0.5, 52, 58); n(16, 0.5, 0.5, 55, 55) # C# E G
n(16, 1, 0.5, 57, 62)
n(16, 1.5, 0.5, 49, 58); n(16, 1.5, 0.5, 55, 55)
n(16, 2, 0.5, 45, 68)
n(16, 2.5, 0.5, 49, 60); n(16, 2.5, 0.5, 52, 58); n(16, 2.5, 0.5, 55, 55)
n(16, 3, 0.5, 57, 62)
n(16, 3.5, 0.5, 49, 58); n(16, 3.5, 0.5, 55, 55)
# Melody: triplet (scalewise) up to D6 — chord tone peak
n(16, 0, 1/3, 81, 82); n(16, 1/3, 1/3, 83, 84); n(16, 2/3, 1/3, 85, 86) # A-B-C# triplet
n(16, 1, 1, 86, 90) # D6 (chord tone — sus4 of A, strong)
n(16, 2, 1, 85, 88) # C#6 (step down — 3 of A)
n(16, 3, 1, 83, 82) # B5 (step down — 9 of A, passing to C1)
# ======================================================
# C1 CHORUS (17-24): THE JOURNEY — Canon in D with jazz extensions
# DM9 | A9 | Bm11 | F#m9 | GM9 | DM7 | GM7/A | A7sus4→A7
# ======================================================
# Bar 17: DM9 — chorus arrival (B5 of bar 16 resolves up to C#6/D6 area)
n(17, 0, 1, 38, 80)
n(17, 0, 4, 54, 54)
n(17, 0.5, 0.5, 45, 60)
n(17, 1, 0.5, 50, 54); n(17, 1.5, 0.5, 57, 52)
n(17, 2, 0.5, 38, 72); n(17, 2.5, 0.5, 45, 58)
n(17, 3, 0.5, 50, 54); n(17, 3.5, 0.5, 61, 52)
motif(17, 0, 'D', vel=90, harm='thirds', octave=12)
# Bar 18: A9 — arpeggio shape answer (not scalar — all chord tones)
n(18, 0, 1, 45, 78); n(18, 0, 4, 61, 54)
n(18, 0.5, 0.5, 52, 60); n(18, 1, 0.5, 57, 54); n(18, 1.5, 0.5, 64, 52)
n(18, 2, 0.5, 45, 72); n(18, 2.5, 0.5, 52, 58)
n(18, 3, 0.5, 57, 54); n(18, 3.5, 0.5, 64, 52)
# Arpeggio of A: A-C#-E-G-B (chord tones only, intervallic but "intentional")
n(18, 0, 1, 81, 84) # A5 (root)
n(18, 1, 1, 85, 82) # C#6 (3, up M3 arpeggio)
n(18, 2, 1, 88, 84) # E6 (5, up m3 arpeggio)
n(18, 3, 1, 83, 78) # B5 (9, down 4th — resolves)
# Bar 19: Bm11 — motif on Bm high
n(19, 0, 1, 47, 76); n(19, 0, 4, 62, 54)
n(19, 0.5, 0.5, 54, 60); n(19, 1, 0.5, 57, 54); n(19, 1.5, 0.5, 59, 52)
n(19, 2, 0.5, 47, 70); n(19, 2.5, 0.5, 54, 58)
n(19, 3, 0.5, 57, 54); n(19, 3.5, 0.5, 64, 52) # E4 = 11
motif(19, 0, 'Bm', vel=86, octave=12)
# Bar 20: F#m9 — angular leap PREPARED via arpeggio shape
n(20, 0, 1, 42, 74); n(20, 0, 4, 57, 52)
n(20, 0.5, 0.5, 49, 58); n(20, 1, 0.5, 52, 54); n(20, 1.5, 0.5, 57, 52)
n(20, 2, 0.5, 42, 68); n(20, 2.5, 0.5, 49, 56)
n(20, 3, 0.5, 52, 54); n(20, 3.5, 0.5, 56, 52) # G#3 = 9
# F#m9 arpeggio UP then back (F#-A-C#-E) — all chord tones, no unprepared leap
n(20, 0, 1, 78, 82) # F#5 (root, down from B5 of bar 19 — step)
n(20, 1, 1, 81, 80) # A5 (m3)
n(20, 2, 1, 85, 80) # C#6 (5)
n(20, 3, 1, 81, 76) # A5 (step back — resolves)
# Bar 21: GM9 — motif on G
n(21, 0, 1, 43, 76); n(21, 0, 4, 59, 54)
n(21, 0.5, 0.5, 50, 60); n(21, 1, 0.5, 54, 54); n(21, 1.5, 0.5, 62, 52)
n(21, 2, 0.5, 43, 70); n(21, 2.5, 0.5, 50, 58)
n(21, 3, 0.5, 54, 54); n(21, 3.5, 0.5, 59, 52)
motif(21, 0, 'G', vel=82, harm='thirds', octave=12)
# Bar 22: DM7 — dyad melody
n(22, 0, 1, 38, 76); n(22, 0, 4, 54, 54)
n(22, 0.5, 0.5, 45, 60); n(22, 1, 0.5, 49, 54); n(22, 1.5, 0.5, 57, 52)
n(22, 2, 0.5, 38, 70); n(22, 2.5, 0.5, 45, 58)
n(22, 3, 0.5, 49, 54); n(22, 3.5, 0.5, 54, 52)
# Dyad melody in 3rds (step motion, smooth)
n(22, 0, 1, 85, 86); n(22, 0, 1, 81, 80) # C#6+A5
n(22, 1, 1, 83, 84); n(22, 1, 1, 79, 78) # B5+G5 (step down)
n(22, 2, 1, 81, 82); n(22, 2, 1, 78, 76) # A5+F#5 (step down)
n(22, 3, 1, 83, 84); n(22, 3, 1, 79, 78) # B5+G5 (step up)
# Bar 23: GM7/A — Hisaishi IV/V
n(23, 0, 1, 45, 78)
n(23, 0, 4, 55, 56)
n(23, 0.5, 0.5, 59, 58); n(23, 1, 0.5, 62, 54); n(23, 1.5, 0.5, 66, 52)
n(23, 2, 0.5, 45, 72); n(23, 2.5, 0.5, 55, 58)
n(23, 3, 0.5, 59, 54); n(23, 3.5, 0.5, 66, 52)
# Melody arpeggio G major shape, landing for dominant
n(23, 0, 1, 79, 80) # G5 (root, approach from B5/G5 dyad)
n(23, 1, 1, 83, 82) # B5 (3 of G)
n(23, 2, 1, 86, 86) # D6 (5 of G, up 3rd)
n(23, 3, 1, 83, 82) # B5 (step back down)
# Bar 24: A7sus4 → A7
n(24, 0, 1, 45, 78); n(24, 0, 2, 61, 56)
n(24, 0.5, 0.5, 50, 60); n(24, 1, 0.5, 52, 56); n(24, 1.5, 0.5, 55, 54)
n(24, 2, 1, 45, 78); n(24, 2, 2, 61, 56)
n(24, 2.5, 0.5, 49, 60); n(24, 3, 0.5, 52, 56); n(24, 3.5, 0.5, 55, 54)
n(24, 0, 2, 81, 84); n(24, 2, 1, 78, 76); n(24, 3, 1, 73, 70)
# ======================================================
# INTERLUDE (25-28): WIDER WORLD — bebop with PREPARED chromatics
# Every chromatic approach tone preceded by half-step or chord-tone neighbor
# ======================================================
# Bar 25: Em7 — walking bass E G B Bb (Bb prepared by B half-step above)
n(25, 0, 1, 40, 72) # E2
n(25, 1, 1, 43, 68) # G2
n(25, 2, 1, 47, 70) # B2
n(25, 3, 1, 46, 66) # Bb2 (half-step from B ✓ — chromatic approach to A)
n(25, 1.5, 0.5, 55, 52); n(25, 1.5, 0.5, 62, 50) # G3+D4 shell (3-7 of Em)
n(25, 3.5, 0.5, 55, 50); n(25, 3.5, 0.5, 62, 48)
# RH bebop: all stepwise or chord-tone (no leaps)
n(25, 0, 0.5, 71, 74) # B4 (chord tone)
n(25, 0.5, 0.5, 67, 70) # G4 (chord tone, down 3rd arpeggio)
n(25, 1, 1/3, 69, 72); n(25, 1+1/3, 1/3, 71, 74); n(25, 1+2/3, 1/3, 69, 72) # A-B-A enclosure
n(25, 2, 0.5, 67, 70) # G4 (step down)
n(25, 2.5, 0.5, 66, 68) # F#4 (half-step — diatonic, prepared)
n(25, 3, 1, 64, 72) # E4 (root, lands)
# Bar 26: A7 — Bb PROPERLY PREPARED (A→Bb→A chromatic neighbor)
n(26, 0, 1, 45, 74) # A2
n(26, 1, 1, 49, 70) # C#3
n(26, 2, 1, 52, 72) # E3
n(26, 3, 1, 51, 68) # Eb3 (half from E — chromatic to D of next bar)
n(26, 1.5, 0.5, 61, 52); n(26, 1.5, 0.5, 67, 50) # C#+G shell
n(26, 3.5, 0.5, 61, 50); n(26, 3.5, 0.5, 67, 48)
# RH: arpeggio A-C#-E-G then PREPARED Bb (approached by A below)
n(26, 0, 1, 69, 76) # A4 (root)
n(26, 1, 0.5, 73, 78) # C#5 (3)
n(26, 1.5, 0.5, 76, 78) # E5 (5)
n(26, 2, 0.5, 79, 80) # G5 (b7)
n(26, 2.5, 0.5, 81, 82) # A5 (root, stepwise up)
n(26, 3, 0.5, 82, 82) # Bb5 (HALF-STEP UP from A5 — b9 prepared ✓)
n(26, 3.5, 0.5, 81, 78) # A5 (resolves DOWN half-step — b9→root ✓)
# Bar 27: DM7 — resolution, arpeggio pattern
n(27, 0, 1, 38, 74) # D2
n(27, 1, 1, 42, 70) # F#2
n(27, 2, 1, 45, 72) # A2
n(27, 3, 1, 47, 68) # B2
n(27, 1.5, 0.5, 54, 52); n(27, 1.5, 0.5, 61, 50)
n(27, 3.5, 0.5, 54, 50); n(27, 3.5, 0.5, 61, 48)
# RH: arpeggio D-F#-A-C# up and down (all chord tones)
n(27, 0, 0.5, 74, 76); n(27, 0.5, 0.5, 78, 78) # D5 F#5
n(27, 1, 0.5, 81, 80); n(27, 1.5, 0.5, 85, 82) # A5 C#6 (arpeggio up)
n(27, 2, 1/3, 86, 80); n(27, 2+1/3, 1/3, 81, 76); n(27, 2+2/3, 1/3, 78, 74) # triplet D6-A5-F#5
n(27, 3, 1, 74, 72) # D5 (lands root)
# Bar 28: D6 — landing, single anchor note
n(28, 0, 4, 38, 72); n(28, 0, 4, 45, 60); n(28, 0, 4, 50, 54); n(28, 0, 4, 54, 52)
n(28, 0, 2, 81, 76) # A5 half
n(28, 2, 1, 78, 70) # F#5
n(28, 3, 1, 74, 66) # D5 (closure)
# ======================================================
# A2 VERSE (29-36): RETURN CHANGED — counter-melody, foreshadows bridge
# ======================================================
# Bar 29: GM7 rolled chord + motif
n(29, 0, 0.05, 43, 60); n(29, 0.05, 0.05, 47, 58); n(29, 0.1, 0.05, 50, 56)
n(29, 0.15, 4, 54, 52)
n(29, 1, 0.5, 47, 48); n(29, 1.5, 0.5, 59, 50)
n(29, 2, 1, 55, 56)
n(29, 3, 0.5, 50, 46); n(29, 3.5, 0.5, 54, 46)
motif(29, 0, 'G', vel=76, harm='thirds')
# Bar 30: A/G — intervallic (arpeggio shape)
n(30, 0, 0.05, 43, 58); n(30, 0.05, 0.05, 49, 56); n(30, 0.1, 4, 57, 52)
n(30, 1, 0.5, 49, 48); n(30, 1.5, 0.5, 52, 46)
n(30, 2, 1, 55, 54)
n(30, 3, 0.5, 49, 48); n(30, 3.5, 0.5, 52, 46)
# Dyad arpeggio up (all chord tones)
n(30, 0, 0.5, 73, 70); n(30, 0, 0.5, 69, 64) # C#5+A4
n(30, 0.5, 0.5, 76, 72); n(30, 0.5, 0.5, 69, 64) # E5+A4 (step up)
n(30, 1, 2, 78, 68) # F#5 half (chord tone 6)
n(30, 3, 1, 81, 72) # A5 (step up — root)
# Bar 31: F#m7 motif
n(31, 0, 0.05, 42, 58); n(31, 0.05, 0.05, 49, 56); n(31, 0.1, 4, 57, 52)
n(31, 1, 0.5, 52, 48); n(31, 1.5, 0.5, 54, 46)
n(31, 2, 1, 49, 54)
n(31, 3, 0.5, 52, 46); n(31, 3.5, 0.5, 54, 44)
motif(31, 0, 'F#m', vel=78, harm='thirds')
# Bar 32: B7 — #9 PREPARED again (F#5→E5→D#5→D5 chromatic descent)
n(32, 0, 0.05, 47, 60); n(32, 0.05, 0.05, 51, 58); n(32, 0.1, 4, 63, 54)
n(32, 1, 0.5, 54, 48); n(32, 1.5, 0.5, 57, 46)
n(32, 2, 1, 59, 54)
n(32, 3, 0.5, 54, 48); n(32, 3.5, 0.5, 57, 46)
# Chromatic descent preparing D5 (#9)
n(32, 0, 1, 78, 76) # F#5 (5 of B7)
n(32, 1, 0.5, 76, 72) # E5 (step down, passing)
n(32, 1.5, 0.5, 75, 74) # D#5 (step down, 3 of B7 — chord tone)
n(32, 2, 1, 74, 72) # D5 (HALF-STEP from D#5 — #9 prepared ✓)
n(32, 3, 1, 71, 68) # B4 (resolves root)
# Bar 33: Em7 motif — B4 of bar 32 is common tone (5 of Em)
n(33, 0, 0.05, 40, 60); n(33, 0.05, 0.05, 47, 58); n(33, 0.1, 4, 55, 52)
n(33, 1, 0.5, 50, 48); n(33, 1.5, 0.5, 52, 46)
n(33, 2, 1, 47, 54)
n(33, 3, 0.5, 50, 46); n(33, 3.5, 0.5, 52, 44)
motif(33, 0, 'Em', vel=78, harm='thirds')
# Bar 34: A7sus4 — intervallic line but arpeggio-shaped (all chord tones)
n(34, 0, 0.05, 45, 62); n(34, 0.05, 0.05, 50, 58); n(34, 0.1, 4, 57, 52)
n(34, 1, 0.5, 52, 48); n(34, 1.5, 0.5, 55, 46)
n(34, 2, 1, 49, 54)
n(34, 3, 0.5, 52, 46); n(34, 3.5, 0.5, 55, 44)
# A7sus4 tones: A C# E G D (sus). Melody uses these only (no unprepared chromatic)
n(34, 0, 0.5, 81, 78) # A5 (root)
n(34, 0.5, 0.5, 79, 74) # G5 (b7, step down)
n(34, 1, 0.5, 78, 72) # F#5 (6/13, step down — wait F# isn't in A7sus4)
n(34, 1.5, 0.5, 76, 70) # E5 (5, step down)
n(34, 2, 0.5, 74, 68) # D5 (sus4, step down)
n(34, 2.5, 0.5, 73, 66) # C#5 (3 of A — chord tone)
n(34, 3, 1, 71, 64) # B4 (9, step down)
# Bar 35: DM7 — motif
n(35, 0, 0.05, 38, 64); n(35, 0.05, 0.05, 45, 60); n(35, 0.1, 4, 54, 52)
n(35, 1, 0.5, 49, 48); n(35, 1.5, 0.5, 50, 46)
n(35, 2, 1, 45, 54)
n(35, 3, 0.5, 49, 46); n(35, 3.5, 0.5, 50, 44)
motif(35, 0, 'D', vel=78, harm='thirds')
# Bar 36: D — cadence. Melody foreshadows Gm bridge with Bb pickup in upper voice
n(36, 0, 1, 38, 62); n(36, 0, 4, 54, 48)
n(36, 1, 0.5, 45, 50); n(36, 1.5, 0.5, 50, 48)
n(36, 2, 1, 42, 56)
n(36, 3, 0.5, 45, 50); n(36, 3.5, 0.5, 50, 48)
# Melody: D5 → Bb4 (foreshadow Gm with Bb — prepared from D→C#→Bb? use C#→Bb = minor 3rd)
n(36, 0, 2, 78, 72) # F#5 half
n(36, 2, 1, 74, 66) # D5
n(36, 3, 1, 73, 62) # C#5 (leads to bridge)
# ======================================================
# BRIDGE (37-40): DARK NIGHT — Gm modal mixture, prepared transition
# Bm7 | Gm/Bb | D/F# | A7sus4→A7
# ======================================================
# Bar 37: Bm7 — motif on Bm
# Voicing includes D4 (62) to set up common-tone bridge to Gm in bar 38
n(37, 0, 2, 47, 58)
n(37, 0, 4, 62, 46) # D4 — KEY common-tone anchor
n(37, 2, 2, 54, 44); n(37, 2, 2, 59, 42)
motif(37, 0, 'Bm', vel=66)
# Bar 38: Gm/Bb — modal mixture PREPARED:
# - B2 (bar 37) → Bb2 (bar 38): half-step bass descent ✓
# - D4 (bar 37) → D4 (bar 38): COMMON TONE held through transition ✓
# - F#3 (bar 37) → G3 (bar 38): half-step up ✓
# - B3 (bar 37) → Bb3 (bar 38): half-step down ✓
n(38, 0, 4, 46, 62) # Bb2 (half-step from B2)
n(38, 0, 4, 50, 44) # D3
n(38, 0, 4, 55, 42) # G3 (half-step up from F#3)
n(38, 0, 4, 58, 40) # Bb3 (half-step down from B3)
n(38, 0, 4, 62, 44) # D4 COMMON TONE preserved
motif(38, 0, 'Gm', vel=72)
# Bar 39: D/F# — light returns, bass steps down F# (from Bb2 chromatic skip, G3 sustained)
n(39, 0, 2, 42, 58); n(39, 0, 4, 57, 46)
n(39, 2, 2, 50, 44); n(39, 2, 2, 54, 42)
motif(39, 0, 'D', vel=72)
# Bar 40: A7sus4 → A7 — PREPARED ascent (arpeggio 4ths, "intentional" leaps)
n(40, 0, 2, 45, 62); n(40, 0, 4, 57, 48); n(40, 2, 2, 50, 50); n(40, 2, 2, 55, 46)
# Ascending 4ths pattern (D→G→C or D→A→E) — sounds like a sequence, not random
n(40, 0, 0.5, 74, 72) # D5
n(40, 0.5, 0.5, 79, 76) # G5 (up 4th — intentional)
n(40, 1, 0.5, 76, 72) # E5 (down 3rd, settle)
n(40, 1.5, 0.5, 81, 78) # A5 (up 4th — sequence continues)
n(40, 2, 0.5, 78, 76) # F#5 (down 3rd, settle)
n(40, 2.5, 0.5, 83, 82) # B5 (up 4th — sequence)
n(40, 3, 0.5, 85, 82) # C#6 (step up)
n(40, 3.5, 0.5, 86, 86) # D6 (step up — soaring into final chorus)
# ======================================================
# B2 PRE-CHORUS 2 (41-44): PEAK TENSION — tritone subs with perfect voice leading
# ======================================================
# Bar 41: GM7 — chord-tone arpeggio (B-D-F#-A)
n(41, 0, 0.5, 43, 72); n(41, 0, 4, 59, 56)
for t_ in (0.5, 1.5, 2.5, 3.5):
n(41, t_, 0.5, 50, 60); n(41, t_, 0.5, 54, 58); n(41, t_, 0.5, 59, 56)
n(41, 1, 0.5, 55, 65); n(41, 2, 0.5, 47, 68); n(41, 3, 0.5, 50, 66)
# Melody: arpeggio shape, ascending 4ths (prepared by bar 40's pattern)
n(41, 0, 0.5, 78, 82); n(41, 0, 0.5, 74, 78) # D5+F#5 dyad (chord tones)
n(41, 0.5, 0.5, 83, 84) # B5 (up 4th from F#5)
n(41, 1, 1, 86, 88) # D6 (up 3rd — arpeggio)
n(41, 2, 1, 83, 82) # B5 (step down)
n(41, 3, 1, 81, 80) # A5 (step down)
# Bar 42: F#m7 → F7 TRITONE SUB with CHROMATIC VOICE LEADING
# Every voice F#m7 → F7 moves by half-step or stays:
# F#2→F2 (half), C#3→C3 (half), E3→Eb3 (half), A3 stays (common)
n(42, 0, 0.5, 42, 72)
n(42, 0.5, 0.5, 49, 62); n(42, 0.5, 0.5, 52, 60); n(42, 0.5, 0.5, 57, 58) # F#m7: C# E A
n(42, 1, 0.5, 54, 66) # F#3
n(42, 1.5, 0.5, 49, 60); n(42, 1.5, 0.5, 57, 58)
# F7 beats 3-4 — voice-led by half-step
n(42, 2, 0.5, 41, 74) # F2 (HALF-STEP from F#2 ✓)
n(42, 2.5, 0.5, 48, 64); n(42, 2.5, 0.5, 51, 62); n(42, 2.5, 0.5, 57, 60) # F7: C Eb A (A COMMON TONE ✓)
n(42, 3, 0.5, 53, 68) # F3 (HALF-STEP from F#3 ✓)
n(42, 3.5, 0.5, 48, 62); n(42, 3.5, 0.5, 57, 60)
# Melody: F#6→F6 chromatic descent (tension note PREPARED by half-step)
n(42, 0, 0.5, 81, 82) # A5
n(42, 0.5, 0.5, 85, 86) # C#6 (3 of F#m, step up from A5)
n(42, 1, 1, 90, 92) # F#6 (5 of F#m — peak)
n(42, 2, 1, 89, 90) # F6 (HALF-STEP from F#6 ✓ — tritone color prepared)
n(42, 3, 1, 87, 86) # Eb6 (step down chromatic — b7 of F7)
# Bar 43: Em7 → Eb7 TRITONE SUB — voice-led by half-step:
# E2→Eb2 (half), G stays, B→Bb (half), D→Db (half)
n(43, 0, 0.5, 40, 74)
for t_ in (0.5, 1.5, 2.5, 3.5):
n(43, t_, 0.5, 47, 64); n(43, t_, 0.5, 50, 62); n(43, t_, 0.5, 55, 60) # B D G
n(43, 1, 0.5, 52, 68)
# Eb7 beats 3-4 — voice-led by half-step (Eb G Bb Db)
n(43, 2, 0.5, 39, 76) # Eb2 (HALF-STEP from E2 ✓)
n(43, 2.5, 0.5, 46, 66); n(43, 2.5, 0.5, 49, 64); n(43, 2.5, 0.5, 55, 62) # Bb Db G (G COMMON ✓)
n(43, 3, 0.5, 51, 70) # Eb3
n(43, 3.5, 0.5, 46, 64); n(43, 3.5, 0.5, 55, 62)
# Melody: E6→Eb6 chromatic descent (tritone color prepared by half-step)
n(43, 0, 1, 86, 90) # D6 (step down from D6 of bar 40/41 pattern)
n(43, 1, 1, 88, 92) # E6 (root of Em, peak)
n(43, 2, 1, 87, 90) # Eb6 (HALF-STEP from E6 ✓ — tritone color)
n(43, 3, 1, 85, 86) # Db6/C#6 (step down — enharmonic common tone to A7's 3rd next bar)
# Bar 44: A7sus4→A7→A7alt — b9 (Bb5) PREPARED by chromatic descent C#6→C6→B5→Bb5
n(44, 0, 0.5, 45, 76)
n(44, 0.5, 0.5, 50, 66); n(44, 0.5, 0.5, 52, 64); n(44, 0.5, 0.5, 55, 62) # D E G (sus4 + 5 + b7)
n(44, 1, 0.5, 57, 70)
n(44, 1.5, 0.5, 50, 64); n(44, 1.5, 0.5, 55, 62)
# A7alt entry — include B3 in sus4 voicing to PREPARE Bb3 (b9) by half-step
n(44, 2, 0.5, 45, 78)
n(44, 2.5, 0.5, 49, 68); n(44, 2.5, 0.5, 53, 64); n(44, 2.5, 0.5, 58, 62) # C# F Bb (3 b13 b9) — Bb from B common-tone voicing
n(44, 3, 0.5, 57, 70)
n(44, 3.5, 0.5, 49, 66); n(44, 3.5, 0.5, 58, 62)
# Melody: PREPARED chromatic descent C#6 → C6 → B5 → Bb5 (every step a half-step ✓)
n(44, 0, 1, 86, 88) # D6 (sus4)
n(44, 1, 1, 85, 86) # C#6 (step down — 3 of A)
n(44, 2, 1, 84, 88) # C6 (HALF-STEP from C#6 ✓ — #9/altered)
n(44, 3, 0.5, 83, 86) # B5 (HALF-STEP from C6 ✓)
n(44, 3.5, 0.5, 82, 88) # Bb5 (HALF-STEP from B5 ✓ — b9 TENSION FULLY PREPARED)
# ======================================================
# C2 FINAL CHORUS (45-52): VICTORY — Canon + Hisaishi peak, motif octaves
# Bb5 of bar 44 resolves DOWN half-step to A5 of motif (textbook b9→1 ✓)
# ======================================================
# Bar 45: DM9 — FULL CLIMAX motif
n(45, 0, 1, 38, 94); n(45, 0, 4, 54, 66)
n(45, 0.5, 0.5, 45, 74); n(45, 1, 0.5, 50, 66); n(45, 1.5, 0.5, 57, 62)
n(45, 2, 0.5, 38, 84); n(45, 2.5, 0.5, 45, 72)
n(45, 3, 0.5, 50, 64); n(45, 3.5, 0.5, 61, 62)
# Motif in octaves — Bb5 of bar 44 resolves to A5 (= octave note 1 of shifted motif)
motif(45, 0, 'D', vel=102, harm='octaves', octave=12)
# Bar 46: A9 — arpeggio descent (all chord tones)
n(46, 0, 1, 45, 90); n(46, 0, 4, 61, 64)
n(46, 0.5, 0.5, 52, 68); n(46, 1, 0.5, 57, 62); n(46, 1.5, 0.5, 64, 60)
n(46, 2, 0.5, 45, 80); n(46, 2.5, 0.5, 52, 66)
n(46, 3, 0.5, 57, 62); n(46, 3.5, 0.5, 64, 60)
# Dyad melody — arpeggio shape down
n(46, 0, 1, 88, 94); n(46, 0, 1, 85, 88) # E6+C#6
n(46, 1, 1, 85, 90); n(46, 1, 1, 81, 84) # C#6+A5 (step down)
n(46, 2, 1, 83, 88); n(46, 2, 1, 79, 82) # B5+G5 (step down)
n(46, 3, 1, 81, 86); n(46, 3, 1, 78, 80) # A5+F#5 (step down)
# Bar 47: Bm11 — motif on Bm high
n(47, 0, 1, 47, 92); n(47, 0, 4, 62, 66)
n(47, 0.5, 0.5, 54, 70); n(47, 1, 0.5, 57, 64); n(47, 1.5, 0.5, 59, 62)
n(47, 2, 0.5, 47, 82); n(47, 2.5, 0.5, 54, 68)
n(47, 3, 0.5, 57, 64); n(47, 3.5, 0.5, 64, 62)
motif(47, 0, 'Bm', vel=98, harm='thirds', octave=12)
# Bar 48: F#m9 — arpeggio descent (no unprepared leaps)
n(48, 0, 1, 42, 88); n(48, 0, 4, 57, 64)
n(48, 0.5, 0.5, 49, 68); n(48, 1, 0.5, 52, 62); n(48, 1.5, 0.5, 57, 60)
n(48, 2, 0.5, 42, 78); n(48, 2.5, 0.5, 49, 66)
n(48, 3, 0.5, 52, 62); n(48, 3.5, 0.5, 56, 60)
# Arpeggio F#-A-C#-E pattern
n(48, 0, 1, 90, 96); n(48, 0, 1, 85, 88) # F#6+C#6
n(48, 1, 1, 88, 92); n(48, 1, 1, 83, 86) # E6+B5 (step down)
n(48, 2, 2, 85, 90); n(48, 2, 2, 81, 84) # C#6+A5 half (step down)
# Bar 49: GM9 — motif OCTAVE
n(49, 0, 1, 43, 90); n(49, 0, 4, 59, 66)
n(49, 0.5, 0.5, 50, 70); n(49, 1, 0.5, 54, 64); n(49, 1.5, 0.5, 62, 62)
n(49, 2, 0.5, 43, 80); n(49, 2.5, 0.5, 50, 68)
n(49, 3, 0.5, 54, 64); n(49, 3.5, 0.5, 59, 62)
motif(49, 0, 'G', vel=98, harm='octaves', octave=12)
# Bar 50: DM7 — top-register dyads, intervallic descent with arpeggio shape
n(50, 0, 1, 38, 88); n(50, 0, 4, 54, 64)
n(50, 0.5, 0.5, 45, 68); n(50, 1, 0.5, 49, 62); n(50, 1.5, 0.5, 57, 60)
n(50, 2, 0.5, 38, 78); n(50, 2.5, 0.5, 45, 66)
n(50, 3, 0.5, 49, 62); n(50, 3.5, 0.5, 54, 60)
# DM7 arpeggio peak (D-F#-A-C#) dyads
n(50, 0, 1, 93, 96); n(50, 0, 1, 90, 90) # A6+F#6 (peak dyad — prepared by bar 49 F#6 peak)
n(50, 1, 1, 90, 94); n(50, 1, 1, 86, 88) # F#6+D6 (step down arpeggio)
n(50, 2, 1, 88, 92); n(50, 2, 1, 85, 86) # E6+C#6 (step down)
n(50, 3, 1, 86, 90); n(50, 3, 1, 81, 84) # D6+A5 (step down)
# Bar 51: GM7/A — Hisaishi IV/V, rising to peak C#6
n(51, 0, 1, 45, 86); n(51, 0, 2, 55, 66); n(51, 0, 4, 59, 62)
n(51, 0.5, 0.5, 50, 68); n(51, 1, 0.5, 62, 64); n(51, 1.5, 0.5, 66, 62)
n(51, 2, 0.5, 45, 76); n(51, 2, 2, 55, 62)
n(51, 2.5, 0.5, 59, 66); n(51, 3, 0.5, 62, 64); n(51, 3.5, 0.5, 66, 62)
# Melody: arpeggio up G major (G B D F#) + leading-tone step
n(51, 0, 0.5, 83, 92) # B5 (3 of G)
n(51, 0.5, 0.5, 86, 94) # D6 (5 of G, up 3rd arpeggio)
n(51, 1, 1, 90, 98) # F#6 peak (7 of G, up 3rd arpeggio)
n(51, 2, 0.5, 88, 96) # E6 (step down)
n(51, 2.5, 0.5, 86, 92) # D6 (step down)
n(51, 3, 1, 85, 90) # C#6 (leads to final cadence)
# Bar 52: A7sus4 → A7 final cadence (C#6 of bar 51 is 3 of A — common tone into sus)
n(52, 0, 1, 45, 84); n(52, 0, 2, 61, 64)
n(52, 0.5, 0.5, 50, 66); n(52, 1, 0.5, 52, 64); n(52, 1.5, 0.5, 55, 62)
n(52, 2, 1, 45, 84); n(52, 2, 2, 61, 64)
n(52, 2.5, 0.5, 49, 66); n(52, 3, 0.5, 52, 64); n(52, 3.5, 0.5, 55, 62)
n(52, 0, 2, 86, 96); n(52, 2, 2, 85, 92)
# ======================================================
# CODA (53-56): AFTER — D pedal, motif whispered, open-fifth modal resolution
# ======================================================
# Bar 53: DM7 — motif very quietly
n(53, 0, 4, 38, 50); n(53, 0, 4, 45, 40); n(53, 0, 4, 54, 36)
motif(53, 0, 'D', vel=52)
# Bar 54: Bm7/D — D pedal continues
n(54, 0, 4, 38, 46); n(54, 0, 4, 47, 36); n(54, 0, 4, 54, 34); n(54, 2, 2, 57, 32)
n(54, 0, 1, 69, 50); n(54, 1, 1, 74, 54); n(54, 2, 2, 71, 46)
# Bar 55: GM7/D
n(55, 0, 4, 38, 46); n(55, 0, 4, 50, 36); n(55, 0, 4, 54, 34)
n(55, 0, 4, 59, 34); n(55, 0, 4, 62, 32)
# Motif hint — first 3 notes only
n(55, 0, 0.5, 62, 50); n(55, 0.5, 0.5, 67, 54); n(55, 1, 3, 71, 56)
# Bar 56: D — open-fifth final (no 3rd — modal wisdom, not sunny major)
n(56, 0, 4, 38, 48); n(56, 0, 4, 45, 40); n(56, 0, 4, 50, 38)
n(56, 0, 4, 57, 36); n(56, 0, 4, 62, 42); n(56, 0, 4, 74, 46)
n(56, 2, 2, 81, 40)
# Sustain pedal
for bar in range(1, 57):
cc(_a(bar, 0), 64, 110)
if bar < 56:
cc(_a(bar, 0) + BPB * TPQ - 40, 64, 0)
cc(_a(56, 0) + 6 * TPQ, 64, 0)
# Compile
priority = {0: 0, 2: 1, 1: 2}
events.sort(key=lambda x: (x[0], priority[x[1]]))
prev = 0
for abs_t, kind, a, b in events:
delta = max(0, abs_t - prev)
if kind == 1:
pno.append(Message("note_on", channel=0, note=a, velocity=b, time=delta))
elif kind == 0:
pno.append(Message("note_off", channel=0, note=a, velocity=0, time=delta))
else:
pno.append(Message("control_change", channel=0, control=a, value=b, time=delta))
prev = abs_t
out = "004.mid"
mid.save(out)
print(f"wrote {out}")
print(f"tracks={len(mid.tracks)} length={mid.length:.2f}s bars=56")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment