MELODY GENERATOR

I built a Python-based Melody Generator that composes new melodic lines over a provided backing track.

It uses rule-based pitch selection, weighted rhythm generation, and a phrase-ending constraint system to produce musical, non-random melodies that follow harmonic context.


Original Background Track

With Generated Melody


Demo Video


Project Description

Melody Generator (Python · Rule-Based System · Algorithmic Composition)
I built a Python-based Melody Generator that writes melodic phrases over a provided backing track.
The system selects pitches based on chord and scale context, generates rhythm through a weighted probability model, and applies musical constraints to control contour, interval motion, and phrase endings.

A key idea in this prototype is the relationship between a motif and its developed form. The generator first creates a motif that follows local harmonic rules, then produces a developed motif by copying its core material while reshaping the final phrase to relate to the previous motif, and creates further emotions.

The generator reflects my early approach to algorithmic composition: designing rule systems, debugging interactions between constraints, and shaping musical behavior through iterative testing.

Phrase Layout · Motif and Developed Motif

The generator divides the 16-bar progression into four sections. Each section starts with a motif, then repeats it as a developed motif with a changed ending.

Motif
bars 1–2
Developed Motif
bars 3–4
Motif
bars 5–6
Developed Motif
bars 7–8
Motif
bars 9–10
Developed Motif
bars 11–12
Motif
bars 13–14
Developed Motif
bars 15–16

How a Note Is Chosen

For each note, the generator starts from all chord tones, then removes options step by step using simple musical rules.

1. Start from chord notes

Pick all allowed notes for the current chord and bar (different sets for first half and second half of the song).

2. Remove weak options

  • Drop passing tones on even positions (no 62 / 74 on even steps).
  • Limit notes that repeat more than two times in a row.

3. Control jumps

  • Remove notes that are more than an octave away from the last note.
  • Avoid specific bad pairs like B–D♭ and some big leaps.

4. Shape direction

  • Force B → C at the right moments (71 → 72).
  • Use higher notes at the start of the second half.
  • Apply special rules when the chord is C and we start a new phrase.

Melody Generator · System Overview

Background Track
WAV file
Chord Progression
C · Am · C · Am
F · Dm · F · G
Rhythm Engine
different weights → select rhythm → aim for 8 beats phrase
Motif Generator
pitch selection based on harmonic rules
Developed Motif
copies motif with a reshaped ending
Synth + Mix
stereo WAV