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
funcmain:
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 asvoid*)
endprintln 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
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
funcshow(label: str, notes: Vector<int>):
mut line := label
each n in notes:
name := music.NoteName(n)
line = line + " " + name
mem.Free(name asvoid*)
endprintln line
endfuncmain:
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