我寫了下面的方法來轉換票據(標到最後八度)到相應的MIDI間距:轉換音符絃樂到MIDI音高數字
// Converts a note string (MUST HAVE OCTAVE) to an integer pitch.
public static int convertToPitch(String note) {
String sym = "";
int oct = 0;
String[] notes = { "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B" };
char[] splitNote = note.toCharArray();
// If the length is two, then grab the symbol and number.
// Otherwise, it must be a two-char note.
if (splitNote.length == 2) {
sym += splitNote[0];
oct = splitNote[1];
} else if (splitNote.length == 3) {
sym += Character.toString(splitNote[0]);
sym += Character.toString(splitNote[1]);
oct = splitNote[2];
}
// Find the corresponding note in the array.
for (int i = 0; i < notes.length; i++) {
if (notes[i].equals(sym)) {
return Character.getNumericValue(oct) * 12 + i;
}
}
// If nothing was found, we return -1.
return -1;
}
,它工作得很好。但是,我還希望能夠使用convertToPitch()
以備用音符值(Db變爲C#等)爲每個音符指定一個備用名稱。有沒有辦法做到這一點,而不撕裂我的方法呢?
儘管它起作用,但這個原始實現似乎還遠沒有優化,所以請不要擔心將它們分開。設置一個'Map'來執行sym-integer字符串是一種改進(可以很容易地採用'#'註釋)。不必要的字符串連接也是你可能想要查看的東西。 Imho,甚至更好,是通過邏輯確定sym-integer:例如通過第一個音符切換來獲得「基數」,並在'b'/''#'上加/減一個基數以獲得最終數字,這樣您就可以擺脫所有字符串concat和線性搜索等。 –