# Music in Plain Text
Jonathan Gregory
[[!template id=vid src="https://mirror.csclub.uwaterloo.ca/emacsconf/2020/emacsconf-2020--04-music-in-plain-text--jonathan-gregory.webm" subtitles="/2020/subtitles/emacsconf-2020--04-music-in-plain-text--jonathan-gregory.vtt"]]
[Download compressed .webm video (13.8M)](https://mirror.csclub.uwaterloo.ca/emacsconf/2020/smaller/emacsconf-2020--04-music-in-plain-text--jonathan-gregory--vp9-q56-video-original-audio.webm)
[View transcript](#transcript)
LilyPond is an extensible program for producing high-quality sheet
music engraved with traditional layout rules. Similar to LaTeX and
other typesetting programs, its input format describes the visual
layout of the score using commands to define musical expressions. This
makes collaboration easier, prevents users from having to adjust
layout settings manually, and facilitates digital archiving and
distribution of musical scores. In this talk, I begin with an overview
of the LilyPond syntax and move on to showcase ways for producing
algorithmic compositions and music books using Make, Org and
LilyPond-mode.
Source files and other materials can be found at
<https://gitlab.com/jagrg/emacsconf-2020> and
<https://gitlab.com/drumbook>.
<!-- from the pad --->
# Questions
## Do you have any thoughts on generating scores in chant notation (neumes)?
I'm not familiar with typesetting chant notation, so I suggest reading
this specific section of the manual for information.
## Do you use this to compose or to write up compositions…?
No and yes. I use pencil and paper to compose the first draft. Then I
move to Emacs to input the notes. Either way it's certainly possible
to compose from Emacs directly, especially if you're doing this
programatically, so I guess it depends on what you're trying to do.
## Can one use MIDI/USB instruments (like keyboards) to input LilyPond? For example for note heights?
[Don't know about Emacs, but Frescobaldi supports MIDI input.]
There is a package called `midi-kbd` which creates keyboard events
from MIDI input, so I believe the answer is yes, but I don't own a
MIDI device, so I haven't tried it.
## Did you ever write huge scores (BigBand/Orchestra) in Emacs?
Never, but that's certainly possible.
## Is there a decent OCR for handwritten music→LilyPond?
I'm not sure, but if the OCR works with MusicXML, then you can use the
`musicxml2ly` command to convert the XML file to a LilyPond file.
## What shell are you using with the fancy autocomplete?
Zsh with fzf.
## Do you use any kind of Emacs to MIDI interface besides exporting MIDI from LilyPond?
No.
# Notes
- Emacs + LilyPond.
- Similar to LaTeX — has its own file format and syntax, can also export to MIDI.
- `(info "(lilypond-learning) Top")`
- The contrast between background and foreground is a little too weak.
- Uses LilyPond-mode, flycheck.
- <https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-lilypond.html>
<!-- transcript: 2020/subtitles/emacsconf-2020--04-music-in-plain-text--jonathan-gregory.vtt -->
<a name="transcript"></a>
# Transcript
Hello, everyone, and welcome to the EmacsConf. I am Jonathan. In this
talk, I'm going to demonstrate ways of producing sheet music in Emacs
using Lilypond, and maybe also convince you to use Emacs for writing
your scores. I'll start with an overview of the syntax for those who
are new to using text-based notation as a shallow dive into the deep
pond of lilies and Lilypond, and move on to showcase some of its
functionalities using Org Mode and lilypond-mode. One disclaimer,
however: I am not a Lilypond developer.
(00:40) So what is Lilypond? Lilypond is a file format and music engraving
system for producing high-quality sheet music. It translates textual
representations of music to graphical objects. It's similar to LaTeX
in that its input format describes the visual layouts of the score,
using commands to define musical expressions. Commands begin with a
backslash. For example, the formatter command, as shown on the left,
yields its graphical equivalents on the right, the fermata symbol over
the low B and so on and so forth. It's also fully extensible, like
Emacs, allowing users to extend and override Lilypond's
functionalities using the Scheme scripting language. It can be used
for early and contemporary music tablature, vocal music lead sheets,
and so on. Above all, it works with Emacs. In fact, Lilypond ships
with Emacs Lisp libraries, including a major mode for editing Lilypond
files.
(01:47) So the input files are similar to source files. They contain
expressions formed with curly braces, comments that start with the
percent sign, and the code is indented. Notes are entered using
lowercase letters, and rests with the letter r. In this case, the
lowercase r or r4 is the equivalence of a crotchet or quarter note
rest. Durations are entered using numbers and dots after the note
name. If you do not specify one, the previous duration is used. You
can also tie notes together using the tilde symbol (~). In fact, you
can input chords, lyrics, embellishments, and a lot more. I encourage
you to read the manual for more information. Now let's switch to a
terminal window. With Lilypond installed, let's create a test file
with the extension .ly and open it in Emacs.
(02:50) At the top of the file is the version statement, which tells Lilypond
which version to use when compiling the file. Here I'm using version
2.20.0. I've added the clef and time signature. Let's add some notes.
I'm going to close this now and compile the file by running lilypond
followed by the file name. So now let's view the output. Okay.
(03:27) So here's a more complex example for randomizing note sequences. The
idea is to create new reading materials each time the code blocks are
evaluated. As usual, we begin with a header. I've added the title and
composer. Then we add the note sequences to use in the composition. In
this case, sn is a note name just like a b c d and so on, and stands
for snare drum, the percussion instruments. Now here's a function
that's going to shuffle the notes in the table. Finally, we expand the
notes inside the Lilypond source block. So whatever the function
returns is expanded inside the drums block. Now let's press C-c C-c to
view the results. Okay. And if I run this again, it should create a
new composition. Great. You can also audition a piece using the midi
command, which creates a midi file of the score.
(04:34) Note also that the ob library-- sorry, the ob-lilypond library comes
with two modes. The one I'm using now is called arrange mode and is
useful for assembling complete scores. The basic mode on the other
hand allows you to mix text and music by embedding Lilypond snippets
and export them using typical Org Mode commands.
(05:00) Now to demonstrate the basic mode in action. I'm going to export this
document to a PDF file. In this case, the :file header argument is
required, so you have to provide one and include the file name. Again,
you can run the code and view the results. Here it is. So now let's
export this to a PDF file. And here it is, what it generates.
(05:39) Now I'm going to show you the workflow I used to produce music books
in Emacs, combining Lilypond and LaTeX for a perfect marriage. I begin
by sketching the first draft of the manuscript using pencil and paper.
Then I move to Emacs to input the notes in a git repository. This is a
typical source file. It begins with a stylesheet where I set variables
and layout settings, although in general, there's no need for tweaking
the layout unless you have specific requirements to do so. The easiest
way to compile the file from Emacs is by pressing C-c C-l, so let's do
this now, and the compilation buffer will tell you if there were any
errors in the file. Now to automate the process of compiling several
files and building the PDF, I use GNU Make, so all I have to do is
open the shell and run the make command. Don't worry, I'll provide a
link to the source code on the last slide.
(06:41) As I moved forward with the project, I found at least two things
missing. One, I had no access to a metronome, at least not from the
editor, so I built one for casual use and made it available in the
MELPA repository. I also missed bar numbers in the source file. This
is useful when going back and forth between input and output files
without getting lost. So I wrote a command for toggling bar numbers,
which I hope you can see on the left. Also, some expressions are
difficult or slow to write on the keyboard-- accents and tuplets, for
example--so I use template expansion extensively for this purpose,
mainly yasnippet.
(07:23) So what do I think? Well, I think Lilypond can be a
sharp paradigm shift for people used to GUI alternatives, but the
results are impressive. You don't have to dive too deeply to start
using Lilypond. Likewise, the ability to extend the software, I think,
is especially appealing for music professionals, enthusiasts,
composers, and the academic community: for example, allowing users to
create alternative notation systems required in non-Western music
traditions and other non-conventional requirements. Also, Lilypond and
Emacs both have extensive and well-written manuals and active
communities of users. But if you're still not sure where to start and
when to wedge your feet in the deep but warm pond of lilies, Lilypond,
and Lilypond users, I invite you to contribute to my Lilypond
projects, which you can do so from the links on the screen. So, thank
you all. I look forward to your comments, and I hope you enjoy the
rest of the conference.
<!-- /transcript -->