Standard library · Audio

audio.music

Notes, scales, chords and tunings, Persian dastgah among them.

import audio.music

This package is the arithmetic of music rather than of sound: what a note is, which notes belong together, and what frequency each one wants. It makes no sound itself, which audio.synth does.

Notes are MIDI numbers, so 60 is middle C and 69 is the A tuned to 440. NoteToFreq turns one into a frequency in equal temperament, and JustFreq and PythagoreanRatio are there when equal temperament is not what you want.

Things to know

•

Scales and chords come back as vectors of semitone steps, which the caller frees.

•

NoteName returns a string the caller frees. ParseNote reads one back and accepts flats and either case, so it round-trips its own output.

•

Persian music does not fit twelve semitones. A dastgah is therefore given in cents, not semitone steps: the third of Shur sits at 350 cents, between a minor and a major third, where no piano key is. NoteToFreqCents is how you ask for it, and KoronCents and SoriCents are the accidentals that get you there.

•

The seven dastgah here are the frequencies of their tonic rows. A real performance moves between gusheh within a dastgah, which is a matter of melody rather than of tuning.

Note numbers, names and frequencies

Notes are MIDI numbers: 60 is middle C and 69 is A4 at 440 Hz. NoteName and ParseNote convert between numbers and names such as C#5, NoteToFreq gives the pitch in hertz, and FreqToNote goes back to the nearest note. NoteName returns a heap string; free it with mem.Free.

Transpose moves a note by semitones, Interval measures the distance between two, and BeatSeconds turns a tempo in beats per minute into the length of one beat.

audio-music-guide-notes.salam
import audio.music
import math
import mem

func main:
    each n in [60, 64, 67, 69]:
        name := music.NoteName(n)
        println n, name, math.Round(music.NoteToFreq(n) * 10.0) / 10.0
        mem.Free(name as void*)
    end
    println music.ParseNote("A4"), music.ParseNote("C#5")
    println music.FreqToNote(261.63)
    println music.Transpose(60, 7), music.Interval(60, 67)
    println music.BeatSeconds(120.0)
end
Output
60 C4 261.6
64 E4 329.6
67 G4 392
69 A4 440
69 73
60
67 7
0.5

Scales and chords

ScaleNotes lists the notes of a scale from a root, and ChordNotes does the same for a chord. Both take a kind constant such as ScaleMinor or ChordDominant7 and return a vector you free.

The result is plain note numbers, so it can feed straight into audio.synth or audio.midi, as the next pages show.

audio-music-guide-scale.salam
import audio.music
import mem

func show(label: str, notes: Vector<int>):
    mut line := label
    each n in notes:
        name := music.NoteName(n)
        line = line + " " + name
        mem.Free(name as void*)
    end
    println line
end

func main:
    mut major := music.ScaleNotes(60, music.ScaleMajor, 7)
    show("C major:", major)
    major.Free()

    mut minor := music.ScaleNotes(57, music.ScaleMinor, 7)
    show("A minor:", minor)
    minor.Free()

    mut triad := music.ChordNotes(60, music.ChordMajor)
    show("C chord:", triad)
    triad.Free()

    mut seventh := music.ChordNotes(67, music.ChordDominant7)
    show("G7:", seventh)
    seventh.Free()
end
Output
C major: C4 D4 E4 F4 G4 A4 B4
A minor: A3 B3 C4 D4 E4 F4 G4
C chord: C4 E4 G4
G7: G4 B4 D5 F5

Constants and variables

A4Note

#
const A4Note: i32 = 69

The MIDI number of the A above middle C, 69.

music-Note.salam
import audio.music
import math
import mem

func main:
    println music.A4Note, music.A4Freq
    println music.NoteToFreq(69), music.NoteToFreq(81), music.NoteToFreq(57)
    println math.Abs(music.NoteToFreq(60) - 261.6255653005986) < 0.0001
    println music.FreqToNote(440.0), music.FreqToNote(261.63), music.FreqToNote(0.0)
    println music.Octave(60), music.PitchClass(61), music.PitchClass(-1)
    println music.Transpose(60, 7), music.Interval(60, 67)
    println music.Cents(440.0, 880.0), music.FreqToCents(440.0, 69)
    println music.NoteToFreqCents(69, 1200.0)

    each n in [60, 61, 69, 127]:
        name := music.NoteName(n)
        println name, music.ParseNote(name) == n
        mem.Free(name as void*)
    end
    println music.ParseNote("c4"), music.ParseNote("Db4")
    println music.ParseNote("x9"), music.ParseNote(""), music.ParseNote("C")
    println music.BeatSeconds(120.0), music.BeatSeconds(0.0)
end
Output
69 440
440 880 220
true
69 60 0
4 1 11
67 7
1200 0
880
C4 true
C#4 true
A4 true
G9 true
60 61
-1 -1 -1
0.5 0

A4Freq

#
const A4Freq: f64 = 440

The frequency that note is tuned to, 440 Hz.

The example for this item is shown with the ones above: music-Note.salam

Scale numbers

Scale…#

Which scale to ask Scale or ScaleNotes for.

NameValue
ScaleMajor
0
ScaleMinor
1
ScaleHarmonicMinor
2
ScaleMelodicMinor
3
ScalePentatonic
4
ScalePentatonicMinor
5
ScaleBlues
6
ScaleChromatic
7
ScaleDorian
8
ScalePhrygian
9
ScaleLydian
10
ScaleMixolydian
11
ScaleLocrian
12
ScaleWholeTone
13
music-Scale.salam
import audio.music

func main:
    mut maj := music.Scale(music.ScaleMajor)
    println maj.Len(), maj.Get(0), maj.Get(6)
    maj.Free()
    mut minor := music.Scale(music.ScaleMinor)
    println minor.Get(2), minor.Get(5)
    minor.Free()
    mut chrom := music.Scale(music.ScaleChromatic)
    println chrom.Len()
    chrom.Free()
    mut pent := music.Scale(music.ScalePentatonic)
    println pent.Len()
    pent.Free()
    mut blues := music.Scale(music.ScaleBlues)
    println blues.Len(), blues.Get(3)
    blues.Free()
    mut dor := music.Scale(music.ScaleDorian)
    println dor.Get(2), dor.Get(6)
    dor.Free()

    mut run := music.ScaleNotes(60, music.ScaleMajor, 8)
    println run.Len(), run.Get(0), run.Get(7)
    run.Free()

    mut tri := music.ChordNotes(60, music.ChordMajor)
    println tri.Len(), tri.Get(1), tri.Get(2)
    tri.Free()
    mut m7 := music.ChordNotes(60, music.ChordMinor7)
    println m7.Len(), m7.Get(3)
    m7.Free()
    mut dim := music.Chord(music.ChordDiminished)
    println dim.Get(2)
    dim.Free()
    mut sus := music.Chord(music.ChordSus4)
    println sus.Get(1)
    sus.Free()
    mut aug := music.Chord(music.ChordAugmented)
    println aug.Get(2)
    aug.Free()
    mut dom := music.Chord(music.ChordDominant7)
    println dom.Get(3)
    dom.Free()
end
Output
7 0 11
3 8
12
5
6 6
3 10
8 60 72
3 64 67
4 70
6
5
8
10

Chord numbers

Chord…#

Which chord to ask Chord or ChordNotes for.

NameValue
ChordMajor
0
ChordMinor
1
ChordDiminished
2
ChordAugmented
3
ChordSus2
4
ChordSus4
5
ChordMajor7
6
ChordMinor7
7
ChordDominant7
8
ChordDiminished7
9

The example for this item is shown with the ones above: music-Scale.salam

Dastgah numbers

Dastgah…#

Which Persian dastgah to ask for. Each is a row of cent offsets, not semitones.

NameValue
DastgahShur
0
DastgahMahur
1
DastgahHomayoun
2
DastgahSegah
3
DastgahChahargah
4
DastgahNava
5
DastgahRastPanjgah
6
music-Dastgah.salam
import audio.music

func main:
    mut shur := music.Dastgah(music.DastgahShur)
    println shur.Len(), shur.Get(0), shur.Get(2), shur.Get(6)
    shur.Free()
    mut mahur := music.Dastgah(music.DastgahMahur)
    println mahur.Get(2)
    mahur.Free()
    mut homayoun := music.Dastgah(music.DastgahHomayoun)
    println homayoun.Get(1)
    homayoun.Free()
    mut segah := music.Dastgah(music.DastgahSegah)
    println segah.Get(2)
    segah.Free()

    println music.KoronCents, music.SoriCents

    mut freqs := music.DastgahFreqs(60, music.DastgahShur)
    println freqs.Len()
    println freqs.Get(0) == music.NoteToFreq(60)
    println freqs.Get(2) > music.NoteToFreq(63), freqs.Get(2) < music.NoteToFreq(64)
    freqs.Free()

    println music.JustRatio(0), music.JustRatio(7), music.JustRatio(4)
    println music.PythagoreanRatio(0), music.PythagoreanRatio(7), music.PythagoreanRatio(2)
    println music.JustFreq(69, 7)
end
Output
7 0 350 1000
400
150
350
-50 50
7
true
true true
1 1.5 1.25
1 1.5 1.125
660

KoronCents

#
const KoronCents: f64 = -50

The cents a koron lowers a note by, about half a semitone.

The example for this item is shown with the ones above: music-Dastgah.salam

SoriCents

#
const SoriCents: f64 = 50

The cents a sori raises a note by.

The example for this item is shown with the ones above: music-Dastgah.salam

Functions

NoteToFreq

#
func NoteToFreq(note: int): f64

The frequency of a MIDI note number in equal temperament.

The example for this item is shown with the ones above: music-Note.salam

NoteToFreqCents

#
func NoteToFreqCents(note: int, cents: f64): f64

The same, moved by so many cents, which is how a quarter tone is asked for.

The example for this item is shown with the ones above: music-Note.salam

FreqToNote

#
func FreqToNote(freq: f64): int

The nearest MIDI note to a frequency.

The example for this item is shown with the ones above: music-Note.salam

FreqToCents

#
func FreqToCents(freq: f64, note: int): f64

How many cents a frequency sits away from a note, positive when sharp.

The example for this item is shown with the ones above: music-Note.salam

Octave

#
func Octave(note: int): int

Which octave a note is in, where middle C is 4.

The example for this item is shown with the ones above: music-Note.salam

PitchClass

#
func PitchClass(note: int): int

Which of the twelve pitches a note is, 0 for C.

The example for this item is shown with the ones above: music-Note.salam

Transpose

#
func Transpose(note: int, semitones: int): int

A note moved by so many semitones.

The example for this item is shown with the ones above: music-Note.salam

Interval

#
func Interval(low: int, high: int): int

How many semitones apart two notes are.

The example for this item is shown with the ones above: music-Note.salam

NoteName

#
func NoteName(note: int): str

The name of a note, such as C#4. The caller frees it.

The example for this item is shown with the ones above: music-Note.salam

ParseNote

#
func ParseNote(name: str): int

The note a name means, accepting flats and either case, or -1 when it is not a note.

The example for this item is shown with the ones above: music-Note.salam

Scale

#
func Scale(kind: int): Vector<int>

The semitone steps of a scale, as a vector the caller frees.

The example for this item is shown with the ones above: music-Scale.salam

ScaleNotes

#
func ScaleNotes(root: int, kind: int, count: int): Vector<int>

A run of notes in a scale from a root, climbing through octaves as it runs out of degrees.

The example for this item is shown with the ones above: music-Scale.salam

Chord

#
func Chord(kind: int): Vector<int>

The semitone steps of a chord.

The example for this item is shown with the ones above: music-Scale.salam

ChordNotes

#
func ChordNotes(root: int, kind: int): Vector<int>

The notes of a chord on a root.

The example for this item is shown with the ones above: music-Scale.salam

Dastgah

#
func Dastgah(kind: int): Vector<f64>

The cent offsets of a Persian dastgah. Cents rather than semitones, because these modes do not fall on the piano.

The example for this item is shown with the ones above: music-Dastgah.salam

DastgahFreqs

#
func DastgahFreqs(root: int, kind: int): Vector<f64>

The frequencies of a dastgah from a root note.

The example for this item is shown with the ones above: music-Dastgah.salam

JustRatio

#
func JustRatio(degree: int): f64

The just intonation ratio for a semitone degree.

The example for this item is shown with the ones above: music-Dastgah.salam

JustFreq

#
func JustFreq(root: int, degree: int): f64

A frequency in just intonation from a root.

The example for this item is shown with the ones above: music-Dastgah.salam

PythagoreanRatio

#
func PythagoreanRatio(degree: int): f64

The Pythagorean ratio for a semitone degree.

The example for this item is shown with the ones above: music-Dastgah.salam

Cents

#
func Cents(a: f64, b: f64): f64

How many cents apart two frequencies are.

The example for this item is shown with the ones above: music-Note.salam

BeatSeconds

#
func BeatSeconds(bpm: f64): f64

How long one beat lasts at a tempo.

The example for this item is shown with the ones above: music-Note.salam