Describing Music in Scala with Domain Specific Languages
In our humble experience, there is no easy or scalable way to write and typeset music on the computer. Professional tools are too expensive and feature-heavy, most open source projects are not user friendly, and neither solution offers functional abstractions or room for extensions.
This repository is an experiment to create an expressive music tool that fills the gap that projects like Abjad and Lilypond have left open. Ideally, we aim to create a Scala library that offers a simple yet powerful DSL to succinctly and easily write music, which can be parsed by Scala to be manipulated, typeset, and performed live.
Potential users should be highly wary of using this library until the syntax is completely nailed down.
Here is a quick example you can do to make sure you have your environment set up correctly.
- Make sure you have Scala, SBT, and LilyPond installed.
- Clone this repositiory.
- Open a terminal and navigate to the root directory of this repo.
- Start SBT and open a REPL by typing
DSLParser("| B4 C4 D4 E4 |")You can also use
m"| B4 C4 D4 E4|"as a shortcut.
- You should get back a music object.
ly"| B4 C4 D4 E4 |"
- You should get back a String that is in LilyPond format.
- You can also create the Lilypond file directly.
show"| B4 C4 D4 E4 |"
- Assuming your LilyPond is set up correctly, you should now have a .ly file generated in the current directory and a corresponding pdf.
- If you can see the pdf then you are good to go! Time to start exploring.
The project uses two major StringContext shortcuts.
m, which is a StringContext shortcut to create music objects in the DSL format, and
which is a StringContext shortcut to create a LilyPond file and its corresponding PDF.
m"| C4 C4 C4 C4|" would make a DSL music object.
This section will introduce syntax for most of the currently implemented features.
You can create a note by entering the letter followed by the duration.
- C quarter note:
- D half note:
Notes can also have pitches. Here's some examples with C.
- Double Sharp:
- Double Flat:
The pitch goes after the note and before the duration. For example, a C sharp quarter note:
You can go up an octave by typing
', or go up more octaves by typing
You can down an octave by typing
, or go down more octaves by typing
The octave goes after the note (and pitch if there is one) and before the beat:
Measures consist of Notes (with pitches, beats, octaves etc) between two
| C#4 D#4 E4 F4 |.
Optionally, within a measure you can denote the time signature with
| [3/4] C4 D4 E4 |.
A staff consists of one or more measures.
| C4 E4 G4 C'4 | D4 F4 A4 C'4 |
This project is Open Source and is released under the MIT license. We welcome any contributions, whether they be bug fixes, new features, or documentation.
If you want to report a bug, or suggest a feature, feel free to open an issue in the project's issue tracker.
If you want to submit a patch, simply issue a pull request and we will get in touch.