Repository Public
1<!--2ORIX Sonata3File: Documentation/manual/harmony.md4Author: Simon-Pierre Boucher5Contact: contact@spboucher.ai6-->7# Harmony89## Pitches1011A note literal is a letter, an optional accidental and an octave: `C4` is middle C (MIDI 60),12`A4` is 440 Hz (MIDI 69). Accidentals are `#`, `##`, `b` and `bb`. A pitch is a number of13semitones, so it can be computed:1415```sonata16composition "Pitches" {17 instrument piano: GrandPiano {18 play C419 play C4 + 4st // E420 play midi(67) // G421 play C4 + 12st // C522 let p = Bb323 play p + 2 * 7st - 12st // two fifths up, an octave down: C424 }25}26```2728Members give the parts of a pitch: `p.midi` (the number), `p.octave`, `p.class` (0 for C to 1129for B) and `p.hz`.3031## Keys and degrees3233`key D minor` records the key of the composition. It is written in the score and the MIDI34file, and it gives meaning to `degree(n)`: the n-th degree of the key's scale.3536```sonata37composition "Degrees" {38 key D minor39 instrument harp: Harp {40 // 1 3 5 8 of D minor, then the same an octave lower.41 for d in [1, 3, 5, 8] {42 play degree(d) duration 1/843 }44 for d in [1, 3, 5, 8] {45 play degree(d, 3) duration 1/846 }47 chord [degree(1, 3), degree(5, 3), degree(3)] duration 1/248 }49}50```5152`degree(n, octave)` counts from the tonic in the given octave (4 when left out); degrees above537 continue into the next octave. Modes: `major`, `minor`, `dorian`, `phrygian`, `lydian`,54`mixolydian`, `locrian`, `harmonic_minor`, `melodic_minor`, `pentatonic_major`,55`pentatonic_minor`, `blues`, `whole_tone`, `chromatic`.5657## Scales5859`scale(root, mode)` returns the pitches of one octave of a scale, without the octave itself.60`scale_run(root, mode, octaves)` from the standard library spans several octaves and ends on61the root.6263```sonata64composition "Scales" {65 tempo 132bpm66 instrument flute: Flute {67 for n in scale(D5, dorian) {68 play n duration 1/869 }70 play D6 duration 1/871 for n in reverse(scale_run(D5, dorian, 1)) {72 play n duration 1/1673 }74 }75}76```7778## Chords7980`chord(root, quality)` builds a chord. Qualities: `major`, `minor`, `maj`, `min`, `dim`,81`aug`, `maj7`, `min7`, `dom7`, `dim7`, `min7b5`, `sus2`, `sus4`, `add9`, `maj9`, `min9`,82`power`.8384```sonata85composition "Chords" {86 tempo 72bpm87 key C major88 instrument keys: ElectricPiano {89 gain -6dB90 for c in [chord(C3, maj7), chord(A2, min7), chord(D3, min7), chord(G2, dom7)] {91 chord c duration 1/292 // The first inversion, an octave higher, as an answer.93 chord invert(c, 1) + 12st duration 1/2 velocity 0.594 }95 }96}97```9899The statement `chord LIST` plays a list of pitches together; the function `chord(root,100quality)` builds such a list. `invert(c, n)` moves the lowest `n` notes up an octave.101Because a chord is a list, the list functions work on it: `c[0]` is its root, `take(c, 3)`102its triad, `reverse(c)` its notes from the top, `c + 12st` the chord an octave higher.103104The standard patterns `arpeggio` and `strum` play a chord as a line or as repeated strokes:105106```sonata107composition "Arpeggio and Strum" {108 instrument guitar: NylonGuitar {109 arpeggio(up_down(chord(A2, min9)), step: 1/8)110 strum(chord(E3, dom7), count: 2, step: 1/4, vel: 0.6)111 }112}113```114115## Transposition116117`transpose I { }` moves every pitch of a block by an interval. `transpose I` without a block118applies from that point to the end of the enclosing block.119120```sonata121pattern motif() {122 play C4 duration 1/8123 play E4 duration 1/8124 play G4 duration 1/4125}126127composition "Transposition" {128 instrument clarinet: Clarinet {129 motif()130 transpose 5st {131 motif()132 }133 transpose -3st {134 motif()135 }136 }137}138```139140The interval between two pitches is their difference: `E4 - C4` is `4st`. Standard names141exist for the common ones: `major_third`, `perfect_fifth`, `octave_up` and the others listed in142`std.music`.143144## Microtones and tuning145146A pitch is not limited to the twelve semitones. Add or subtract cents (hundredths of a147semitone), or compute any division of the octave:148149```sonata150composition "Microtones" {151 instrument harp: Harp {152 // Seven equal steps to the octave.153 for n in 0..=7 {154 play C4 + 12st * n / 7 duration 1/8155 }156 // A neutral third: halfway between minor and major.157 chord [C4, E4 - 50cents, G4] duration 1/2158 }159}160```161162`tuning 432Hz` sets the frequency of A4 for the whole composition (220 to 880 Hz; 440 when left163out).164165Glides and inflections inside a note are written with `bend`: a list of points, each a time166after the start of the note and an offset in semitones.167168```sonata169composition "Bend" {170 instrument cello: Cello {171 // Slide up a whole tone over the second half-second.172 play D3 duration 1/1 bend { 0ms: 0st, 500ms: 0st, 1000ms: 2st }173 }174}175```176177Fractional pitches are kept exactly in the audio. In a MIDI export they are rounded to the178nearest semitone and reported; in MusicXML they are written as fractional alterations. See179[Exports](exports.md).180181## Range182183Each instrument has a playable range. A note outside it is still rendered, with a warning:184185```sonata,warning=M4001186composition "Out of Range" {187 instrument flute: Flute {188 play C2189 }190}191```192193```text194warning[M4001]: C2 is outside the range of Flute (G3–E7); it is rendered anyway195```196