SPB Git

Repository Public

modules-and-projects.md 5.0 KB

# Modules and projects

# Modules

A file without a composition is a module. Another file brings its definitions into scope with import:

sonata,module=motifs
// motifs.sonata
const home = D4

fn answer(p: Pitch) -> Pitch {
    return p + 7st
}

pattern call(p: Pitch) {
    play p duration 1/8
    play p + 2st duration 1/8
    play p + 3st duration 1/4
}
sonata
import motifs

composition "Uses a Module" {
    instrument flute: Flute {
        call(home)
        call(answer(home))
    }
}
  • A module path is dotted: import lib.motifs reads the file lib/motifs.sonata.
  • Paths are relative to the folder of the file being compiled. For a lone file that is the folder it is in. In a project the main source is src/main.sonata, so import motifs reads src/motifs.sonata.
  • Everything at the top level of a module becomes visible without a prefix. Defining the same name in two of your files is an error (S2002).
  • Import cycles are refused (I7002), as is a module that cannot be found (I7001).
sonata,error=I7001
import not.there

composition "Missing Module" {
    instrument piano: GrandPiano {
        play C4
    }
}

# The standard library

Three modules are part of the compiler and always in scope; importing them is allowed and changes nothing.

Module Contents
std.music note values (whole, half, quarter, eighth, sixteenth), named intervals (major_third, perfect_fifth, …), dynamics (pp, mp, mf, f, ff, …), scale_run, up_down, and the patterns arpeggio, strum, rhythm
std.instruments the named instruments: Standard instruments
std.effects split_level(n)

The standard library is itself written in Sonata (StandardLibrary/std/). A definition of yours with the same name as a standard one replaces it in your project.

sonata
composition "Standard Library" {
    instrument piano: GrandPiano {
        default velocity mp
        arpeggio(up_down(scale_run(C4, major, 1)), step: sixteenth)
        chord [C4, C4 + major_third, C4 + perfect_fifth] duration half velocity ff
    }
}

# What a program can reach

A Sonata program can read the modules it imports and name audio files of its project. That is all. The language has no way to open other files, use the network or start a process, so opening a composition you received cannot do anything except make sound. Module and asset paths that leave the project — through .., an absolute path or a symbolic link — are refused.

# Projects

The app saves your work as a project: a folder with the extension .sonataproject that Finder shows as a single document.

text
Aurora.sonataproject/
  project.json     name, identifier, format version, main source, list of imported audio
  studio.json      what you changed in the Studio: mixer settings, note edits, loop range
  src/             your sources; src/main.sonata is the entry
  instruments/     instrument definitions saved from the Instrument Lab
  assets/          imported audio files
  • The code is the piece. Everything musical is in src/. studio.json holds only explicit adjustments made in the Studio (see Studio); deleting it returns the project to exactly what the code says.
  • Sources are plain text. You can edit them with any editor and keep the project in a version-control system.
  • Add a module with the + button at the right of the editor's tabs. A module named motifs is the file src/motifs.sonata and is imported with import motifs.
  • The app also opens and saves a single .sonata file, without a project around it. A lone file has no studio settings and no imported audio.

Outside the project, the app keeps data it can rebuild or that belongs to your machine:

What Where
Rendered previews and waveforms (safe to delete) ~/Library/Caches/ai.spboucher.orix.sonata/
Copies of the sources taken when saving (the 20 most recent) ~/Library/Application Support/ORIX Sonata/Backups/

No secret is ever written into a project, and the app sends nothing anywhere.

# Audio files in a project

An audio track places imported audio files on the timeline:

sonata
composition "With Audio" {
    tempo 100bpm
    audio field {
        gain -6dB
        clip "rain.wav" at 0bars
        clip "rain.wav" at 4bars gain -3dB fade_in 500ms fade_out 2s
    }
    instrument piano: GrandPiano {
        play C4 duration 1bar
    }
}

The path is relative to the project's assets folder. The compiler checks the path and the placement; the audio itself is read when the piece is played or rendered, so a missing file is reported at that moment, by name. WAV, AIFF and FLAC files are read.

From the command line, a project is compiled by naming it:

text
sonata check Aurora.sonataproject
sonata render Aurora.sonataproject --output aurora.wav

The exact layout and the rules of the format are in Documentation/spec/project-format.md.

Files main
Menu

Theme