By Tania Alwan
When people ask me how to sing harmony, they usually mean: "How do I find a note that sounds good next to the melody, and stay on it?" The good news is that there's a simple rule behind the most common harmony part in pop, folk and choir music. The better news, for developers, is that the rule is easy to express in code.
In this post I explain intervals in plain terms, then build a small Python script that takes a melody, generates a diatonic third above it and writes both parts to a WAV file you can sing along with.
Intervals: the distance between two notes
An interval is the distance between two pitches. In Western music, the smallest common step is a semitone, which is one key to the next on a piano, black keys included. Intervals are named by how many letter names they span and by their exact size in semitones:
- Unison: the same note, 0 semitones
- Minor third: 3 semitones, for example D to F
- Major third: 4 semitones, for example C to E
- Perfect fourth: 5 semitones, for example C to F
- Perfect fifth: 7 semitones, for example C to G
- Octave: 12 semitones, for example C4 to C5
Thirds and sixths sound sweet and blended, which is why they're the go-to harmony intervals. Fifths and octaves sound open and strong. In traditional voice leading, two parts moving in parallel fifths or octaves are usually avoided, because the harmony part tends to disappear into the melody. Parallel thirds don't have that problem.
Diatonic harmony: why the third keeps changing size
Here's the part that confuses many singers. If you always sang exactly 4 semitones above the melody, you'd drift out of the key on some notes. Real harmony parts stay inside the scale. That's what "diatonic" means.
In C major the scale is C D E F G A B. A diatonic third above any note means: count up two scale steps. C goes to E, D goes to F, E goes to G, and so on. Because the scale has uneven gaps, some of those thirds are major (4 semitones) and some are minor (3 semitones). Your ear accepts both because every note belongs to the key.
The same idea gives you a diatonic fifth (four scale steps up). Most of those are perfect fifths, but B to F is only 6 semitones, a diminished fifth, which is one reason fifth harmonies need more care.
The script: a third above any melody
The script below:
- stores a melody as note names, using the opening of "Mary Had a Little Lamb", a traditional tune, in C major
- builds the harmony by moving each note two steps up the C major scale
- converts notes to MIDI numbers and then to frequencies with the equal-temperament formula (A4 = 440 Hz)
- prints the size of each third in semitones so you can see which are major and which are minor
- mixes both parts as sine tones into a 16-bit mono WAV file
It's 52 lines and uses only the Python standard library.
import math import struct import wave # C major scale, one octave, as note names (index = scale degree). SCALE = ["C", "D", "E", "F", "G", "A", "B"] SEMITONES = {"C": 0, "D": 2, "E": 4, "F": 5, "G": 7, "A": 9, "B": 11} # Opening of "Mary Had a Little Lamb", a traditional tune, in C major. MELODY = "E4 D4 C4 D4 E4 E4 E4 D4 D4 D4 E4 G4 G4".split() def to_midi(note): """'C4' -> 60 (middle C).""" return 12 * (int(note[-1]) + 1) + SEMITONES[note[:-1]] def third_above(note): """Move up two scale steps inside C major: a diatonic third.""" name, octave = note[:-1], int(note[-1]) degree = SCALE.index(name) + 2 return SCALE[degree % 7] + str(octave + degree // 7) def freq(midi): """Equal temperament, A4 (MIDI 69) = 440 Hz.""" return 440.0 * 2 ** ((midi - 69) / 12) def render(voices, path, beat=0.4, rate=22050): """Mix several note lists (same length) into one mono 16-bit WAV.""" frames = bytearray() n = int(beat * rate) for chord in zip(*voices): freqs = [freq(to_midi(note)) for note in chord] for i in range(n): fade = min(1.0, i / 300, (n - i) / 300) s = sum(math.sin(2 * math.pi * f * i / rate) for f in freqs) frames += struct.pack("<h", int(0.3 * fade * s / len(freqs) * 32767)) with wave.open(path, "wb") as wav: wav.setnchannels(1) wav.setsampwidth(2) wav.setframerate(rate) wav.writeframes(bytes(frames)) if __name__ == "__main__": harmony = [third_above(note) for note in MELODY] print("melody :", " ".join(MELODY)) print("harmony:", " ".join(harmony)) for low, high in sorted(set(zip(MELODY, harmony))): gap = to_midi(high) - to_midi(low) kind = "major third" if gap == 4 else "minor third" print(f"{low} -> {high}: {gap} semitones ({kind})") render([MELODY, harmony], "harmony.wav") with wave.open("harmony.wav") as wav: print("wrote harmony.wav:", wav.getnframes() / wav.getframerate(), "seconds") Running it with Python 3 prints:
melody : E4 D4 C4 D4 E4 E4 E4 D4 D4 D4 E4 G4 G4 harmony: G4 F4 E4 F4 G4 G4 G4 F4 F4 F4 G4 B4 B4 C4 -> E4: 4 semitones (major third) D4 -> F4: 3 semitones (minor third) E4 -> G4: 3 semitones (minor third) G4 -> B4: 4 semitones (major third) wrote harmony.wav: 5.2 seconds It also writes harmony.wav, a 5.2-second file with both parts playing together, which you can open in any audio player or DAW.
Reading the output
The interval lines are the whole lesson in four rows. The melody uses only C, D, E and G, and the generated harmony mixes two major thirds (C to E, G to B) and two minor thirds (D to F, E to G). A fixed "4 semitones up" rule would have produced F sharp over D and G sharp over E, both outside C major.
The harmony line also shows something singers notice quickly: it has the same shape as the melody. When the melody steps down, the harmony steps down. That parallel motion is what makes a third harmony easy to learn, and also what makes it sound a bit predictable if it never changes.
How to sing harmony with this file
Here's a practice routine that uses the script:
- Learn the melody first. Sing it until you don't have to think about it
- Learn the harmony alone. Edit the script to render only the harmony list, for example
render([harmony], "harmony_only.wav"), and sing along with it - Sing the harmony over the melody. Render only the melody, play it back, and sing the third above it from memory
- Try another tune. Replace
MELODYwith any melody that stays on the white keys (C major), generate a new harmony and learn the pattern rather than specific notes - Record yourself. Play the melody, record your harmony on your phone and listen back for notes that drift toward the melody
Many singers find the third step the hardest, because the ear wants to follow the louder melody. Short phrases and slow tempos help.
Where this simple rule falls short
A few honest caveats:
- Chords matter. Real harmony parts follow the chords underneath, not just the scale. A third above the melody can clash with the backing on some beats
- Range. Thirds above a high melody can quickly leave a comfortable range, so harmony singers often switch to a third below or a sixth below
- Only one key. The script only knows C major. Minor keys and songs that borrow notes from outside the scale need a different scale list
- Sine waves aren't voices. The tones are clear for pitch practice, but they don't help with blend, vowels or dynamics
Practising with real songs
Once the pattern clicks, try it on songs you know. It helps to hear the lead vocal clearly, or to remove it so you can sing your part against the band. A stem splitter, such as the Voicestars AI stems splitter or similar tools like Moises, can separate the vocal from the instrumental for that kind of practice.
Then come back to the code: add a third_below function, make the script read a melody from the command line, or build a two-part harmony in a minor key and listen to how the thirds change.
Disclosure: this article was written in partnership with Voicestars.
