I write this from CtJ HQ, waiting on a heat repair. The experience has been rather infuriating. I am tempted to write a personal rant or make a comic , but instead I thought I would make some music with JFugue and Groovy. As I've blogged before, it is completely addictive.
The Setup
My friend and colleague (we need a new term for that), Lance Finney, has written a comprehensive article over at the OCI Java News Brief.
For a blog post, we don't have to be as formal with respect to background, examples, or setup. Let's jump in. Here's the setup:
Examples
The following examples are trivial both from a coding standpoint and musical arrangement. The point is two-fold: (a) Groovy makes everything easier and (b) playing with music is fun. As I've mentioned, this might be a great way to introduce kids to computing.
Eg. 1 Crazy TrainThe first example is minimal. This is the main hook to
Crazy Train, which sounds rather eerie on the default instrument of piano. (As a musical aside, it is interesting that this hook uses each note in the F# natural minor scale -- quite rare for blues-based rock of that era.)
import org.jfugue.Player
new Player().play("""
F# F# C#6 F#
D6 F# C#6 F#
B A Ab A
B A Ab E
""")
Eg. 2 Ode to JoyWith Groovy's G-Strings, there is a temptation to pick Bach's
Air on a G-String. However, this obligation is trumped by choosing part of the extraordinary melody from which this blog's theme is derived. (Check out
this tribute by Leonard Bernstein).
(Note: G-Strings are used in the next example. This one has hard-coded rhythm notation.)
import org.jfugue.Player
// q = quarter note, i = eighth note
// the "." acts like the dot in music notation
new Player().play("""
I[60]
E E F G
G F E D
C C D E
Eq. Di D
E E F G
G F E D
C C D E
Dq. Ci Cw
"""
)
Note that I[60] sets the instrument as French Horn.
Here is a list of MIDI instruments.
Eg. 3 Waiting for the Repairman Blues
Not that it compares in any way, but here is a little bluesy hook that I wrote on guitar some years back. Thanks again to Groovy's G-strings, we can set the rhythm as a relative variable: this allows one to tweak rhythm settings across an entire piece. (It's easy to see that a DSL in Groovy would be good fit for this subject).
import org.jfugue.Player
// q = quarternote, i = eighth, s = sixteenth
// doo, be, bop are relative: easier to read and tweak
def doo = "q"
def be = "i"
def bop = "s"
def hum = "w"
def phrase1 =
"D${doo} E${doo} D${be} E${be} C#${doo} A${doo} R"
new Player().play("""
I[55]
${phrase1}
C${doo} F#${doo} C${be} F#${be} B4${doo} G${doo} R
${phrase1}
C${bop}+F#${doo} C#${bop}+G${doo}
C${bop}+F#${doo} C#${bop}+G${doo}
E${hum}+B4${hum}
"""
)
For this example, the instrument is an Orchestra Hit. This has a dual effect: (a) possibly introducing you to a commonly used MIDI sound and (b) distracting you from the rhythm problems in the 3rd bar. (It should be played more fluidly. This is left as an exercise for the listener.)
Bottomline
Try it out... Tinkering in this way is both easy and enchanting. It isn't hard to imagine a custom Ant task that plays homegrown tunes based on the success or failure of a build.