Types and units
Every value in Sonata has a type, and most carry a unit. The compiler uses them to refuse programs that mix things that do not go together — seconds with beats, hertz with decibels — and tells you which conversion to use.
The types
| Type | Written as | Meaning |
|---|---|---|
Number |
3, 1/4, 0.75, 60% |
exact when written with integers, a float otherwise |
Bool |
true, false |
|
String |
"text", mode names such as minor |
|
Pitch |
D4, midi(62) |
a MIDI note number, fractional for microtones |
PitchClass |
D, Bb |
a note name without octave, for key |
Interval |
7st, 25cents |
a distance between pitches, in semitones |
Duration |
1/4 in a duration position, 2bars, 1beat, note_value(1/8) |
musical time, an exact fraction of a whole note |
Time |
5ms, 2s |
clock time, in seconds |
Frequency |
440Hz, 2kHz |
hertz |
Gain |
-6dB |
level, in decibels |
Tempo |
120bpm |
quarter notes per minute |
Angle |
90deg |
degrees |
List<T> |
[D3, A3, D4], 0..4 |
Chord and Scale are lists of pitches |
Velocity and Pan are numbers with a range: 0 to 1 and −1 to 1.
Three kinds of time
Sonata keeps three notions of time apart, because confusing them is how timing bugs happen:
- Musical time (
Duration) is counted in fractions of a whole note and is exact:1/3 + 1/6is exactly1/2. Positions in a piece never accumulate rounding errors. - Clock time (
Time) is in seconds. It is what envelopes, delays and reverbs use. - Samples exist only in the engine. A time in seconds becomes a sample index by one rule
used everywhere:
floor(seconds × rate + 0.5).
After duration, rest and at, a bare exact number is a duration: 1/4 means a quarter
note. Decimals are refused there, because musical time must stay exact:
composition "Float Duration" {
instrument piano: GrandPiano {
play C4 duration 0.25
}
}The message suggests the fix: write the duration as an exact fraction, for example 3/8 instead of 0.375.
bars and beats are durations that depend on the time signature in effect:
composition "Bars and Beats" {
signature 6/8
instrument piano: GrandPiano {
play C4 duration 1beat // one eighth note in 6/8
play E4 duration 2beats
rest 1bar - 3beats // the rest of the bar
play G4 duration 1bar // six eighths
}
}Mixing musical and clock time is an error:
composition "Mixed Time" {
instrument piano: GrandPiano {
let wrong = 1s + 1/4
play C4
}
}Convert explicitly instead: seconds(d) gives the clock time of a duration at the tempo of
the beginning of the piece, and beats(d) its number of beats.
composition "Conversions" {
tempo 90bpm
instrument piano: GrandPiano {
// A quarter note at 90 bpm lasts 2/3 s; use it as a delay time.
play C4 duration 1/2
effects {
delay(time: seconds(note_value(1/4)), feedback: 0.3, mix: 0.2)
}
}
}Where a time is expected (an effect or instrument parameter), a musical duration is also
accepted and converted at the tempo of position 0: delay(time: note_value(3/16)). A bare
number is not: the compiler asks for a unit (T3006).
Pitch arithmetic
| Expression | Result |
|---|---|
Pitch + Interval, Pitch - Interval |
a pitch: A4 + 7st is E5 |
Pitch - Pitch |
an interval: E5 - A4 is 7st |
List<Pitch> + Interval |
every pitch moves: [C4, E4, G4] + 12st |
Interval × Number, Interval ÷ Interval |
an interval, a number |
Microtones are ordinary arithmetic: A4 + 25cents is a quarter of a semitone above A4, and
12st * 7 / 19 is seven steps of a 19-tone equal temperament.
composition "Pitch Arithmetic" {
instrument harp: Harp {
let root = D4
let fifth = root + 7st
play root
play fifth
play fifth + 50cents // a quarter tone higher
chord [root, root + 4st - 13.7cents, fifth] duration 1/2 // a just major third
play root + (fifth - root) * 2 // two fifths up
}
}Levels
Gains are in decibels and add: -6dB + -3dB is -9dB. db(x) converts a linear ratio to a
gain and linear(g) does the reverse: db(0.5) is about -6.02dB.
What each operator accepts
| Expression | Result |
|---|---|
Number ± × ÷ % Number |
Number, exact while both are exact |
Duration ± Duration, Duration × ÷ Number |
Duration |
Duration ÷ Duration |
Number |
Gain ± Gain |
Gain |
Frequency, Time, Tempo: ± same type, × ÷ Number |
same type |
String + String, List + List |
concatenation |
| comparisons | Bool, both sides of the same kind |
Anything else is error T3001, which names both types. Division by zero is T3007.
Annotations
Function and pattern parameters are annotated; let may be:
fn louder(v: Velocity, by: Number = 0.1) -> Velocity {
return clamp(v + by, 0, 1)
}
composition "Annotations" {
instrument piano: GrandPiano {
let step: Duration = 1/8
let top: Pitch = C5
play top duration step velocity louder(0.6)
play top - 12st duration step velocity louder(0.6, by: 0.3)
}
}Types and units are checked when an expression is evaluated, with the exact place in the source. A composition is evaluated completely at compile time, so everything that contributes to the music is checked; a branch that is never taken is only checked for names and argument counts.
Ranges are part of the type
Parameters have ranges and the compiler enforces them: a track gain lives in [−120, 24] dB, a velocity in [0, 1], a tempo in [1, 1000] bpm.
composition "Too Loud" {
instrument piano: GrandPiano {
gain 40dB
play C4
}
}The ranges, units and defaults of every instrument and effect parameter are in the reference, generated from the same tables the compiler checks against.