Interpreter for a Musical Language
This project is a double interpreter for a musical language called JSBach. The language was designed to help implement musical compositions through programming algorithms. This was an assignment developed in the Programming Languages course at UPC. The interpreter is implemented with ANTLR and Python and it works as follows:
flowchart LR
a[JSBach] --> b[Python]
b --> c[Executable]
b --> d[LilyPond]
d --> e[PDF]
d --> f[Audio]
LilyPond is a programming language that lets you write music and then convert it into pdf, wav and mp3 files. While LilyPond allows you to write music, JSBach lets you generate music from any algorithm you write.
Let us see the functionality of this with the Euclides Algorithm to compute the Maximum Common Divisor.
Main |:
<!> "Input two numbers"
<?> a
<?> b
Euclides a b
:|
Euclides a b |:
notes <- {C3 D3 E3 F3 G3 A3 B3}
size <- #notes
while a /= b |:
if a > b |:
a <- a - b
<:> notes[a%size+1]
:| else |:
b <- b - a
<:> notes[b%size+1]
:|
:|
<!> "the MCD is" a
:|
The syntax might be a bit confusing but it is overall understandable. The generated notes are obtained with the computed values within the algorithm. The interpreter first generates the .lily file
\version "2.20.0"
\score {
\absolute {
\tempo 4 = 120
fis' g
b d' fis' g b d' fis' g b d' fis' g fis' b d'
b
}
\layout { }
\midi { }
}
And then, when executed, this outputs a set of audio files (.mp3 and .wav) and a pdf file with the chore.
Since the code generates "random" notes from a C major scale, this sounds good enough. Another example is this audio generated by the solution of the Hanoi Towers problem.
The language is very simple but has some fun capabilities. Since it is interpreted by Python, the memory mangement is the same. For now, one can make recursive programs, list definitions, numeric computations, random generation of values, playing of notes and boolean operations. The full code can be checked in the Github, however for now the descriptions are all written in Catalan.