I have this line of chord obtained from text file. For example,
String chordLine = "C G Am C";
String transposedChordLine;
Next, I need to transpose the chordLine
into a new transposedChordLine
using the class below using two parameters, a String
chord and integer increment of transpose. For example, transpose("C", 2)
will return D
.
public class Transposer{
private int inc;
private static ArrayList<String> keysSharp;
private static ArrayList<String> keysFlat;
Transposer(){
keysSharp = new ArrayList<String>(Arrays.asList("C", "C#", "D", "D#","E", "F","F#", "G","G#", "A","A#", "B"));
keysFlat = new ArrayList<String>(Arrays.asList("C", "Db", "D", "Eb","E", "F","Gb", "G","Ab", "A","Bb", "B"));
}
public String transpose(String chord,int inc){
this.inc = inc;
String newChord;
if(chord.contains("/")){
String[] split = chord.split("/");
newChord = transposeKey(split[0]) + "/" + transposeKey(split[1]);
}else
newChord = transposeKey(chord);
return newChord;
}
private String transposeKey(String key){ // C#m/D# must pass C#m or D#
String nKey, tempKey;
if(key.length()>1){
nKey = key.substring(0, 2);
}
else{ nKey = key; }
int oldIndex, newIndex;
if(key.contains("b")){
oldIndex = (keysFlat.indexOf(nKey)>-1) ? keysFlat.indexOf(nKey) : keysFlat.indexOf(similarKey(nKey));
newIndex = (oldIndex + inc + keysFlat.size())%keysFlat.size();
tempKey = keysFlat.get(newIndex);
nKey = (key.length() < 3) ? tempKey : key.replace(nKey, tempKey);
//(nKey + key.substring(nKey.length(), key.length()));
}
else if(key.contains("#")){
oldIndex = (keysSharp.indexOf(nKey)>-1) ? keysSharp.indexOf(nKey) : keysSharp.indexOf(similarKey(nKey));
newIndex = (oldIndex + inc + keysSharp.size())%keysSharp.size();
tempKey = keysSharp.get(newIndex);
nKey = (key.length() < 3) ? tempKey : key.replace(nKey, tempKey);
}
else{
nKey = nKey.substring(0, 1);
oldIndex = (keysSharp.indexOf(nKey)>-1) ? keysSharp.indexOf(nKey) : keysSharp.indexOf(similarKey(nKey));
newIndex = (oldIndex + inc + keysSharp.size())%keysSharp.size();
tempKey = keysSharp.get(newIndex);
nKey = (key.length() < 2) ? tempKey : key.replace(nKey, tempKey);
}
return nKey;
}
private String similarKey(String nKey) {
String newKey;
switch(nKey){
case "Cb":
newKey = "B";
break;
case "Fb":
newKey = "E";
break;
case "E#":
newKey = "F";
break;
case "B#":
newKey = "c";
break;
default:
newKey = null;
}
return newKey;
}
}
How do I replace the chordLine
without losing the spaces?
increment by 2 should have transposedChordLine="D A Bm D"
Here is my current attempt:
public static void main(String[] args) {
String chordLine = "C G Am C";
String transposedChordLine;
String normalize = chordLine.replaceAll("\\s+", " ");
String[] split = normalize.split(" ");
//System.out.println(normalize);
Transposer tran = new Transposer();
String[] temp = new String[split.length];
for(int i=0 ; i<split.length ; i++){
temp[i] = tran.transpose(split[i], 2);
//System.out.println(split[i]);
System.out.print(temp[i]);
}
transposedChordLine = chordLine.replaceAll([split], temp); //which is wrong
}