SPB Git

Repository Public

getting-started.md 4.9 KB

# Getting started

Sonata is a language for writing music as a program. You describe notes, instruments and a mix in text; the compiler turns that text into sound. This chapter takes you from an empty document to a short piece you can hear, see and export. Nothing here needs an account or a network connection.

# Your first note

A piece of music is a composition. It contains tracks; each track is an instrument that plays notes. This is a complete program:

sonata
composition "First Note" {
    instrument piano: GrandPiano {
        play A4
    }
}

A4 is the A above middle C (440 Hz). With nothing else written, the note lasts a quarter note at 120 beats per minute: half a second, plus the natural decay of the piano.

In the app, create a new project (File › New), replace the text with the program above and press Play (⌘↩). The status badge in the toolbar reads Compiling, then Playing. With the command line:

text
sonata play first.sonata

# Several notes

Statements in a track run one after the other. Each play places a note at the cursor and moves the cursor by the note's duration. rest moves the cursor without playing.

sonata
composition "A Phrase" {
    tempo 96bpm
    instrument piano: GrandPiano {
        play D4 duration 1/8
        play F4 duration 1/8
        play A4 duration 1/4
        rest 1/4
        play G4 duration 1/4 velocity 0.5
        play D4 duration 1/2
    }
}

Durations are fractions of a whole note: 1/4 is a quarter note, 1/8 an eighth, 1/2 a half. velocity is how hard the note is played, from 0 to 1 (the default is 0.8).

# Chords and a second instrument

A list of notes in square brackets is a chord. A composition can hold as many instruments as you like; every track starts at the beginning and they play together.

sonata
composition "Two Hands" {
    tempo 96bpm
    key D minor

    instrument piano: GrandPiano {
        gain -3dB
        chord [D3, A3, D4] duration 1/2
        chord [F3, A3, C4] duration 1/2
        chord [G3, Bb3, D4] duration 1/2
        chord [A3, C#4, E4] duration 1/2
    }

    instrument strings: ConcertStrings {
        gain -9dB
        articulation legato
        play D5 duration 1/1
        play E5 duration 1/2
        play C#5 duration 1/2
    }
}

gain sets the level of a track in decibels. GrandPiano and ConcertStrings are two of the 83 instruments of the standard library; the full list is in Standard instruments.

# Hear it, see it

  • Play (⌘↩) compiles if needed and starts playback; Stop is ⌘. and Return to Start is ⇧⌘↩.
  • Under the editor, the master waveform is drawn from the audio the piece really produces: the app renders the composition in the background after each successful compilation. Click in it to move the playhead.
  • The console under the editor lists errors and warnings. Clicking one moves the cursor to the place in the code. The line at the right shows what the compiler measured: compile time, number of notes, tracks, simultaneous voices and graph nodes.
  • While the piece plays, keep typing: a successful compilation replaces the running program with a short crossfade and the playhead keeps its position. If the code has an error, the last valid version keeps playing and the badge says Playing last valid.

# When something is wrong

The compiler tells you where and, when it can, how to fix it:

sonata,error=S2001
composition "Typo" {
    instrument piano: GrandPiana {
        play A4
    }
}
text
typo.sonata:2:23: error[S2001]: unknown instrument 'GrandPiana'
2 |     instrument piano: GrandPiana {
  |                       ^^^^^^^^^^
  help: did you mean 'GrandPiano'?

In the app, the console shows the same message with a button that applies the correction. More in Diagnostics.

# Save and export

  • File › Save writes a .sonataproject: a folder holding your sources, the studio settings and any imported audio. See Modules and projects.
  • Composition › Render to Audio File… (⌘E) writes WAV, AIFF or FLAC.
  • Composition › Export MIDI… and Export MusicXML… write the notes for other programs.

From the command line:

text
sonata check piece.sonata
sonata render piece.sonata --output piece.wav

Details are in Exports and Command line.

# Where to go next

  • Tutorial builds a complete piece step by step.
  • Syntax and Types and units explain how the language reads.
  • Rhythm and Harmony cover the musical vocabulary.
  • The Library space lists every instrument and effect; the examples in Examples/ are complete pieces you can open, play and change.
Files main
Menu

Theme