Search code examples
jfugue

jFugue ChordProgression sample from the complete guide v5 doesn't work


I'm using jFugue 5.0.9. My code is a modified version of a sample (which doesn't work either. :# ) from the purchased complete guide to jFugue. Here's the code:

private void chordProgression2() {
		ChordProgression cp = new ChordProgression("I IV V");
		for(Chord c:cp.getChords()){
			DevLog.debug("chord: "+c);
		}
		Player player = new Player();

		//-------------------------
		ChordProgression cp1= cp.eachChordAs("$0 $0 $0 $0 $1 $1 $0 $0 $2 $1 $0 $0");
		DevLog.debug("generated1::"+cp1.getPattern());
		for(Chord c:cp1.getChords()){
			DevLog.debug("chord: "+c);
		}
		ChordProgression cp2=cp1.allChordsAs("$0i $1i $2i $3i $4i $3i $2i $1i");
		DevLog.debug("generated2::"+cp2.getPattern());
		player.play(cp2);
		//player.play(cp.allChordsAs("$0q $0q $0q $0q $1q $1q $2q $0q"));

		//player.play(cp.allChordsAs("$0 $0 $0 $0 $1 $1 $2 $0").eachChordAs("V0 $0s $1s $2s Rs V1 $!q"));
	}

Result:

2017-06-29 17:26:24.986 1 D chord: C4MAJ 2017-06-29 17:26:25.044 1 D chord: F4MAJ 2017-06-29 17:26:25.044 1 D chord: G4MAJ 2017-06-29 17:26:26.087 1 D generated1::C4 C4 C4 C4 E4 E4 C4 C4 G4 E4 C4 C4 F4 F4 F4 F4 A4 A4 F4 F4 C5 A4 F4 F4 G4 G4 G4 G4 B4 B4 G4 G4 D5 B4 G4 G4 2017-06-29 17:26:26.088 1 D chord: C4MAJ 2017-06-29 17:26:26.088 1 D chord: F4MAJ 2017-06-29 17:26:26.088 1 D chord: G4MAJ Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at org.jfugue.pattern.ReplacementFormatUtil.replaceDollarsWithCandidates(ReplacementFormatUtil.java:53) at org.jfugue.pattern.ReplacementFormatUtil.replaceDollarsWithCandidates(ReplacementFormatUtil.java:16) at org.jfugue.theory.ChordProgression.getPattern(ChordProgression.java:90) at mozzart.test.jfugueHelloWorld.HelloWorld.chordProgression2(HelloWorld.java:147) at mozzart.test.jfugueHelloWorld.HelloWorld.main(HelloWorld.java:40)

Here is the original sample from the guide

ChordProgression cp = new ChordProgression("I IV V") .eachChordAs("$0 $0 $0 $0 $1 $1 $0 $0 $2 $1 $0 $0") .allChordAs("$0i $1i $2i $3i $4i $3i $2i $1i"); Pattern pattern = cp.getPattern();

In the guide: .allChordAs must be a typo, as it should be .allChordsAs. (an s after Chord). it assumes new ChordProgression("I IV V") .eachChordAs("$0 $0 $0 $0 $1 $1 $0 $0 $2 $1 $0 $0" to return a ChordProgression of at least 5 chords, however my code just proved it returns only 3 chords.

So how can I make the sample work?

Thanks in adavance.


Solution

  • Thanks for pointing this out - there's an error in the sample in the book, which I'll have to fix.

    You're right that it should say "allChordsAs" (with an 's' after 'Chord'). The other problem - the one that's causing the ArrayIndexOutOfBounds error - is the $3 and $4.

    Together, I, IV, and V are only three chords; they can be accessed using $0, $1, and $2. There are no chords for $3 and $4 to find, so you're getting the out-of-bounds exception. You would need 5 chords in your ChordProgression for $3 and $4 to work.

    So, you can: 1. Remove $3 and $4 from allChordsAs, or... 2. Add two more chords to the ChordProgression

    (And just so you know, the index values in eachChordAs refer to individual notes in the chord. I, IV, and V are each major chords and each have 3 notes. Other chords, like sevenths, have 4 notes.)