9 Ways to NRT, part 4: Generating music staff notation with NRT and Lilypond
Welcome to Part 4 of 9 Ways to NRT!
Today, I'm going to show how this NRT notation:
d4. r8 m f s2 t4 D4.
can be used to generate the following music staff notation:
The music notation is created with a free notation program called Lilypond. Lilypond is text based: it reads in a special markup language and spits out a pdf. It also runs on the command line and can read from standard input, which makes it an ideal pairing with NRT.
A Tiny Rant
When I was at Berklee College of Music, I preferred Lilypond for any assignments that needed music notation, instead of the program Finale that they gave to all students (they didn't give us Sibelius, so I've never used it). Lilypond was free; Finale was not. Lilypond could run on Linux; Finale could not. I found Lilypond's documentation to be a lot better than Finale's. Learning how to do things in Lilypond involved copying and pasting example code from the documentation. Finale involved figuring out how to navigate their crappy UI. Blech. And at the end of the day, I found Lilypond notation to look better than Finale notation.
NRT vs Lilypond
NRT is largely inspired by Lilypond syntax. Their similarities actually made it difficult to convert NRT code to Lilypond code. My solution was a bit of a hack. Instead of using the NRT parser I wrote in Flex/Bison, I wrote one in Perl. The Perl code will not be displayed here (heh), but the program nrt2ly is available in the Nine Ways to NRT github.
Here is the full program. This program is a bit longer than the previous programs, so instead of being a single command, it is a bash script:
#!/bin/bash MODE="sharp" #"sharp" or "flat" notes KEY=9 #0-11, offset in semitomes OCTOFFSET=-1 #octave offset FILENAME=lilynotes.png
echo $KEY $MODE $OCTOFFSET |\
cat - <(echo "d4. r8 m f s2 t4 D4.") |\ nrt2ly | lilypond -o out -
#convert pdf to png, remove pdf convert -flatten out.pdf $FILENAME rm out.pdf out.ps
echo -e "\nSuccessfully wrote to file $FILENAME"
The first command concatenates parameters and the NRT notation together, then the output is sent to the perl script I wrote called nrt2ly, which parses the parameters and NRT notation to create Lilypond code. The parameters tell Lilypond to create notation in the key of A, to use sharps instead of flats, and to offset the octave down by one. Lilypond code generated from nrt2ly is then sent into Lilypond, where a PDF is generated.
The next commands then convert the PDF file into a PNG file using ImageMagick, and the files that lilypond generates are then removed.
Saving this program and then running it on the commandline without any arguments should generate a file called lilynotes.png.