Four chords in three lines of code: jfugue and chord progressions

Four chords in three lines of code: jfugue and chord progressions


Play all audios:


Maybe you’ve seen “4 Chords” by The Axis of Awesome. > “Recognize this?” >  > “Yeah, that’s ‘Don’t Stop Believing’ by Journey.” >  > “There are a few more songs with the same 


chords. Check it out.” The chord progression that The Axis of Awesome is playing here is the “I V vi IV” progression. You can read more about this popular progression on Wikipedia: I–V–vi–IV


progression I’d like to show you how to play this chord progression in JFugue. _(If you haven’t already heard about JFugue, or if you need to download the library so you can follow along


with this article, please visit __JFugue.org__)_ JFugue provides a ChordProgression class that lets you construct a chord progression using standard progression notation. You can then do all


sorts of things with the resulting progression, such as setting the key of the chord or changing the style by which the chords are played. Let’s start with a simple program that plays the I


V vi IV progression! import org.jfugue.player.Player; import org.jfugue.theory.ChordProgression;public class FourChords {     public static void main(String[] args) {         


ChordProgression cp = new ChordProgression(“I V vi IV”);         Player player = new Player();         player.play(cp);     } } If you run this little program, assuming you have the JFugue


library set up correctly, you’ll hear a chord progression! Behind the scenes, JFugue is using a default key (C-Major) and its knowledge of what notes comprise the I, V, iv, and IV chords, so


it’s generating a bunch of notes just before it plays the music. In The Axis of Awesome’s video, they’re playing the chords with a bit more style than just one chord at a time. You can do


that in JFugue, too. The method to help with this is called .eachChordAs, since your goal is to play each chord according to some style that you’ll specify. The method takes a string that


provides instructions for playing the notes. Take a look at the line of the program, then we’ll unpack what this all means.         


player.play(cp.eachChordAs("$0w+$1q_$2q_$1q_$2q"); In eachChordAs(), $0, $1, and $2 are indexes into the chord: $0 means “the first note” (with counting starting from 0), $1 means


“the second note,” and $2 means “the third note.” If you had a chord with more notes, you could use more index values. The W and Q characters are durations: _whole_ and _quarter_ notes.


JFugue supports other durations as well, from H (half) all the way to O (1/128th); altogether, JFugue supports these durations: W, H, Q, I, S, T, X, and O. _(All of these can be concatenated


together — you can have “Chq” for a C-note, 3/4 duration, or even “Cwwww” (or “Cw4”) for four whole notes — and you can make them all dotted as well (“Bq.”))_ The + tells JFugue to play


notes at the same time, and the _ tells JFugue that the note following the _ should be considered part of the next sequence of notes to be played at the same time. _(“What?”)_ Notice that


$0W on the left side of the plus is a whole duration, and on the right side of the plus, the four quarter notes being played sequentially make another whole duration, so this is essentially


$0W + A BUNCH OF STUFF THAT ALSO SUMS TO _W _-but that’s only true because of the underscores; if there were spaces in here, the notes would be parsed as a separate sequence altogether. Now


give this program a try! Experiment with other strings you could pass to eachChordAs(). By the way, if you want to play the full chord itself inside of eachChordAs(), you can use $!. One


last thing for this entry: Notice that the default key for the chord progression is C-Major. You can change this with a call to setKey() — let’s try A-flat Major. And, since JFugue provides


a fluent API, you can chain setKey() and eachChordAs() together in one line, like this (switching up the eachChordAs() string as well):


player.play(cp.setKey("Abmaj").eachChordAs("V0 I[Piano] $!w   V1 I[Flute] $1q $0q Rq $1q   V2 I[Guitar] $2q $2q $0q Rq")); Have fun exploring different chords and


different ways that you can play them with JFugue’s ChordProgression class!