SPB Git

Repository Public

harmony.md 5.4 KB

# Harmony

# Pitches

A note literal is a letter, an optional accidental and an octave: C4 is middle C (MIDI 60), A4 is 440 Hz (MIDI 69). Accidentals are #, ##, b and bb. A pitch is a number of semitones, so it can be computed:

sonata
composition "Pitches" {
    instrument piano: GrandPiano {
        play C4
        play C4 + 4st            // E4
        play midi(67)            // G4
        play C4 + 12st           // C5
        let p = Bb3
        play p + 2 * 7st - 12st  // two fifths up, an octave down: C4
    }
}

Members give the parts of a pitch: p.midi (the number), p.octave, p.class (0 for C to 11 for B) and p.hz.

# Keys and degrees

key D minor records the key of the composition. It is written in the score and the MIDI file, and it gives meaning to degree(n): the n-th degree of the key's scale.

sonata
composition "Degrees" {
    key D minor
    instrument harp: Harp {
        // 1 3 5 8 of D minor, then the same an octave lower.
        for d in [1, 3, 5, 8] {
            play degree(d) duration 1/8
        }
        for d in [1, 3, 5, 8] {
            play degree(d, 3) duration 1/8
        }
        chord [degree(1, 3), degree(5, 3), degree(3)] duration 1/2
    }
}

degree(n, octave) counts from the tonic in the given octave (4 when left out); degrees above 7 continue into the next octave. Modes: major, minor, dorian, phrygian, lydian, mixolydian, locrian, harmonic_minor, melodic_minor, pentatonic_major, pentatonic_minor, blues, whole_tone, chromatic.

# Scales

scale(root, mode) returns the pitches of one octave of a scale, without the octave itself. scale_run(root, mode, octaves) from the standard library spans several octaves and ends on the root.

sonata
composition "Scales" {
    tempo 132bpm
    instrument flute: Flute {
        for n in scale(D5, dorian) {
            play n duration 1/8
        }
        play D6 duration 1/8
        for n in reverse(scale_run(D5, dorian, 1)) {
            play n duration 1/16
        }
    }
}

# Chords

chord(root, quality) builds a chord. Qualities: major, minor, maj, min, dim, aug, maj7, min7, dom7, dim7, min7b5, sus2, sus4, add9, maj9, min9, power.

sonata
composition "Chords" {
    tempo 72bpm
    key C major
    instrument keys: ElectricPiano {
        gain -6dB
        for c in [chord(C3, maj7), chord(A2, min7), chord(D3, min7), chord(G2, dom7)] {
            chord c duration 1/2
            // The first inversion, an octave higher, as an answer.
            chord invert(c, 1) + 12st duration 1/2 velocity 0.5
        }
    }
}

The statement chord LIST plays a list of pitches together; the function chord(root, quality) builds such a list. invert(c, n) moves the lowest n notes up an octave. Because a chord is a list, the list functions work on it: c[0] is its root, take(c, 3) its triad, reverse(c) its notes from the top, c + 12st the chord an octave higher.

The standard patterns arpeggio and strum play a chord as a line or as repeated strokes:

sonata
composition "Arpeggio and Strum" {
    instrument guitar: NylonGuitar {
        arpeggio(up_down(chord(A2, min9)), step: 1/8)
        strum(chord(E3, dom7), count: 2, step: 1/4, vel: 0.6)
    }
}

# Transposition

transpose I { } moves every pitch of a block by an interval. transpose I without a block applies from that point to the end of the enclosing block.

sonata
pattern motif() {
    play C4 duration 1/8
    play E4 duration 1/8
    play G4 duration 1/4
}

composition "Transposition" {
    instrument clarinet: Clarinet {
        motif()
        transpose 5st {
            motif()
        }
        transpose -3st {
            motif()
        }
    }
}

The interval between two pitches is their difference: E4 - C4 is 4st. Standard names exist for the common ones: major_third, perfect_fifth, octave_up and the others listed in std.music.

# Microtones and tuning

A pitch is not limited to the twelve semitones. Add or subtract cents (hundredths of a semitone), or compute any division of the octave:

sonata
composition "Microtones" {
    instrument harp: Harp {
        // Seven equal steps to the octave.
        for n in 0..=7 {
            play C4 + 12st * n / 7 duration 1/8
        }
        // A neutral third: halfway between minor and major.
        chord [C4, E4 - 50cents, G4] duration 1/2
    }
}

tuning 432Hz sets the frequency of A4 for the whole composition (220 to 880 Hz; 440 when left out).

Glides and inflections inside a note are written with bend: a list of points, each a time after the start of the note and an offset in semitones.

sonata
composition "Bend" {
    instrument cello: Cello {
        // Slide up a whole tone over the second half-second.
        play D3 duration 1/1 bend { 0ms: 0st, 500ms: 0st, 1000ms: 2st }
    }
}

Fractional pitches are kept exactly in the audio. In a MIDI export they are rounded to the nearest semitone and reported; in MusicXML they are written as fractional alterations. See Exports.

# Range

Each instrument has a playable range. A note outside it is still rendered, with a warning:

sonata,warning=M4001
composition "Out of Range" {
    instrument flute: Flute {
        play C2
    }
}
text
warning[M4001]: C2 is outside the range of Flute (G3–E7); it is rendered anyway
Files main
Menu

Theme