问题
I'm trying to transpose a note, but the result it returns is not what it should be. Could you please check my code and tell me where I am wrong?
public int changeTone(String chord) {
int changeTone = 0;
switch(chord) {
case "I":
changeTone = 0;
break;
case "II":
changeTone = 1;
break;
case "III":
changeTone = 2;
break;
case "IV":
changeTone = 3;
break;
case "V":
changeTone = 4;
break;
case "VI":
changeTone = 5;
break;
case "VII":
changeTone = 6;
break;
case "i":
changeTone = 0;
break;
case "ii":
changeTone = 2;
break;
case "iii":
changeTone = 4;
break;
case "iv":
changeTone = 5;
break;
case "v":
canviDeTo = 7;
break;
case "vi":
changeTone = 9;
break;
case "vii":
changeTone = 11;
break;
default:
System.out.println("Vaya");
break;
}
return changeTone ;
}
public String getChord(int interval) {
String chord = "";
switch(interval) {
case "0":
chord = "C";
break;
case "2":
chord = "D";
break;
case "4":
chord = "E";
break;
case "5":
chord = "F";
break;
case "7":
chord = "G";
break;
case "9":
chord = "A";
break;
case "11":
chord = "B";
}
return chord;
}
public String WriteChord(String chords, String tone) {
String finalChord;
String[] chordArray = chords.split("-");
for(int i=0; i < chordArray.length; i++){
String chord = chordArray[i];
int interval = changeTone(chord);
chord = getChord(interval);
Note note = new Note(chord);
finalChord += note.changeValue(interval).toString() += "-";
}
return finalChord;
}
OK, so what this tries to do is to change the value of a chord given a chord progession with intervals. Like I-III-IV-iv. The user would choose a tone (the tonical, I note) and the chord would be changed taking the note as a reference. So, for example, running the code should do the following:
- The user chooses a tone, say "E".
- The code generates a chord progression, say "I-III-IV-iv".
- The code gets the interval between I and III, I and IV, and I and iv.
- The initial note, "E", changes its value with the interval.
The expected output is: E-G#-A#-A# The actual output is: C-G#-G#-Bb
Why doesn't this work? I have simplified my code, so if you need a bit more let me know! Thanks in advance.
Edit: I have corrected the code and added the expected/gotten output.
回答1:
JFugue already has support for intervals and chord progressions. I hope the following code satisfies your needs:
import org.jfugue.theory.ChordProgression;
public static void main(String[] args) {
ChordProgression cp = new ChordProgression("I-III-IV-iv").setKey("E");
System.out.println(cp);
}
The output from this code is:
E4MAJ G#4MAJ A4MAJ A4MIN
You can play this directly in a Player:
Player player = new Player(); // Don't forget to import org.jfugue.player.Player
player.play(cp);
If that's playing a little too fast for you because of the default duration of a quarter note, you can play each chord as something longer, say a whole note:
player.play(cp.eachChordAs("$!w"));
If you just want the roots, which in this case are [E, G#, A, A] (if you see an error with my music theory, please let me know), you can use:
cp.eachChordAs("$0");
There are several Chord Progression examples at http://www.jfugue.org/examples.html
来源:https://stackoverflow.com/questions/53169518/jfugue-note-changing-not-working-properly