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.
NoteNamereturns a string the caller frees.ParseNotereads 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.
NoteToFreqCentsis how you ask for it, andKoronCentsandSoriCentsare 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.
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)
endScales 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.
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()
endConstants and variables
A4Note
const A4Note: i32 = 69import 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)
endA4Freq
const A4Freq: f64 = 440The example for this item is shown with the ones above: music-Note.salam
Scale numbers Scale…
| Name | Value |
|---|---|
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 |
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()
endChord numbers Chord…
| Name | Value |
|---|---|
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…
| Name | Value |
|---|---|
DastgahShur | 0 |
DastgahMahur | 1 |
DastgahHomayoun | 2 |
DastgahSegah | 3 |
DastgahChahargah | 4 |
DastgahNava | 5 |
DastgahRastPanjgah | 6 |
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)
endKoronCents
const KoronCents: f64 = -50The example for this item is shown with the ones above: music-Dastgah.salam
SoriCents
const SoriCents: f64 = 50The example for this item is shown with the ones above: music-Dastgah.salam
Functions
NoteToFreq
func NoteToFreq(note: int): f64The example for this item is shown with the ones above: music-Note.salam
NoteToFreqCents
func NoteToFreqCents(note: int, cents: f64): f64The example for this item is shown with the ones above: music-Note.salam
FreqToNote
func FreqToNote(freq: f64): intThe example for this item is shown with the ones above: music-Note.salam
FreqToCents
func FreqToCents(freq: f64, note: int): f64The example for this item is shown with the ones above: music-Note.salam
Octave
func Octave(note: int): intThe example for this item is shown with the ones above: music-Note.salam
PitchClass
func PitchClass(note: int): intThe example for this item is shown with the ones above: music-Note.salam
Transpose
func Transpose(note: int, semitones: int): intThe example for this item is shown with the ones above: music-Note.salam
Interval
func Interval(low: int, high: int): intThe example for this item is shown with the ones above: music-Note.salam
NoteName
func NoteName(note: int): strThe example for this item is shown with the ones above: music-Note.salam
ParseNote
func ParseNote(name: str): intThe example for this item is shown with the ones above: music-Note.salam
Scale
func Scale(kind: int): Vector<int>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>The example for this item is shown with the ones above: music-Scale.salam
Chord
func Chord(kind: int): Vector<int>The example for this item is shown with the ones above: music-Scale.salam
ChordNotes
func ChordNotes(root: int, kind: int): Vector<int>The example for this item is shown with the ones above: music-Scale.salam
Dastgah
func Dastgah(kind: int): Vector<f64>The example for this item is shown with the ones above: music-Dastgah.salam
DastgahFreqs
func DastgahFreqs(root: int, kind: int): Vector<f64>The example for this item is shown with the ones above: music-Dastgah.salam
JustRatio
func JustRatio(degree: int): f64The example for this item is shown with the ones above: music-Dastgah.salam
JustFreq
func JustFreq(root: int, degree: int): f64The example for this item is shown with the ones above: music-Dastgah.salam
PythagoreanRatio
func PythagoreanRatio(degree: int): f64The example for this item is shown with the ones above: music-Dastgah.salam
Cents
func Cents(a: f64, b: f64): f64The example for this item is shown with the ones above: music-Note.salam
BeatSeconds
func BeatSeconds(bpm: f64): f64The example for this item is shown with the ones above: music-Note.salam