我正在研究一种方法,其参数是一个音程字符串和一个字符串根音。例如,音乐中的小调音阶是以下音程:
1 2 b3 4 5 b6 b7 8
并给定根音 C,输出应为:
C4 D4 Eb4 F4 G4 Ab4 Bb4 C5
我也试图包含所有改变的音程,例如:b9、#9、b5、b11、b13。
基本 Octave 音程假定为 4。如果音程大于一个 Octave 音程,则应将其跨越的多个 Octave 音程添加到 4。理论上,该方法应该能够将极大的音程处理为音符,例如 1400 的音程,这将超出人类听觉的范围。
我该如何尝试做到这一点?我创建了一个局部变量字符串“interval”来单独存储每个间隔以进行处理,一个局部变量“noteValue”来保存半步的间隔值,以及一个局部变量“notes”来包含转换后的音符。我获取所有间隔的输入字符串,并为 for 循环中的每个位置 i 创建一个子字符串“c”。我测试该子字符串中的字符是否为数字 0-9、“b”(平号)或“#”(升号)。否则,假设遇到了空格,并且局部变量“interval”继续进行处理。
如果String c的字符是数字0-9,则将其添加到String区间。如果字符串c的字符是'b'则noteValue减1,如果是'#'则noteValue增加1。
例如,如果输入 b13 间隔,则应处理“b”并将 noteValue 减 1。然后应将“1”添加到名称间隔的字符串中。然后读取“3”并将其添加到名称间隔的字符串中。当子串c中的字符不是数字、“b”或“#”后,值为“13”的字符串间隔继续转换为半步。它被解析为 整数并根据大调音阶的半步值进行转换(所有音程都与之比较)。 a b13 的半步值应为 Octave 音阶 (12) + a b6 (8) = 20。
假设根音是 C,b13 应返回 Ab。这是通过使用一系列 if 语句来查找正确的根音来完成的,每个语句都包含一个开关。通过应用 %12 运算从 0 到 11 个可能的半音位置查找音符值,确定音符值并将其添加到音符字符串中。一旦处理完整个intervalString参数,就应该返回notes String。
我在第 38 行遇到了 NumberFormatException 问题,我尝试将“interval”字符串解析为整数。我已经放置了一个 println() 语句来查看发生了什么,并且字符串间隔似乎没有正确更新。不太确定从这里去哪里,任何帮助将不胜感激!
这是我的代码:
public class testIntervals {
public static void main(String[] args) {
new testIntervals();
}
public testIntervals() {
String intervals = "1 2 b3 4 5 b6 b7 8";
String rootNote = "C";
String notes = intervalsToNotes(intervals, rootNote);
System.out.println(notes);
}
public String intervalsToNotes(String intervalString, String rootNote) {
int noteValue = 0;
int octave = 4;
String interval = "";
String notes = "";
for (int i = 0; i < intervalString.length(); i++) {
String c = intervalString.substring(i);
System.out.println(c);
if (c.charAt(0) >= '0' && c.charAt(0) <= '9')
interval = interval.concat(c);
else if (c.charAt(0) == 'b')
noteValue--;
else if (c.charAt(0) == '#')
noteValue++;
else {
System.out.println(interval + c);
int process = Integer.parseInt(interval);
interval = "";
for (int j = 1; i <= process; i++) {
if (j%7 == 2 || j%7 == 3 || j%7 == 5 || j%7 == 6 || j%7 == 0) {
noteValue += 2;
}
else if (j%7 == 1 || j%7 == 4) {
noteValue++;
}
}
octave += (noteValue / 12);
if (rootNote.equals("Ab") || rootNote.equals("G#")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("A" + octave + " ");break;
case 2: notes = notes.concat("Bb" + octave + " ");break;
case 3: notes = notes.concat("B" + octave + " ");break;
case 4: notes = notes.concat("C" + octave + " ");break;
case 5: notes = notes.concat("Db" + octave + " ");break;
case 6: notes = notes.concat("D" + octave + " ");break;
case 7: notes = notes.concat("Eb" + octave + " ");break;
case 8: notes = notes.concat("E" + octave + " ");break;
case 9: notes = notes.concat("F" + octave + " ");break;
case 10: notes = notes.concat("Gb" + octave + " ");break;
case 11: notes = notes.concat("G" + octave + " ");break;
}
}
else if (rootNote.equals("A")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("Bb" + octave + " ");break;
case 2: notes = notes.concat("B" + octave + " ");break;
case 3: notes = notes.concat("C" + octave + " ");break;
case 4: notes = notes.concat("C#" + octave + " ");break;
case 5: notes = notes.concat("D" + octave + " ");break;
case 6: notes = notes.concat("D#" + octave + " ");break;
case 7: notes = notes.concat("E" + octave + " ");break;
case 8: notes = notes.concat("F" + octave + " ");break;
case 9: notes = notes.concat("F#" + octave + " ");break;
case 10: notes = notes.concat("G" + octave + " ");break;
case 11: notes = notes.concat("G#" + octave + " ");break;
}
}
else if (rootNote.equals("A#") || rootNote.equals("Bb")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("B" + octave + " ");break;
case 2: notes = notes.concat("C" + octave + " ");break;
case 3: notes = notes.concat("Db" + octave + " ");break;
case 4: notes = notes.concat("D" + octave + " ");break;
case 5: notes = notes.concat("Eb" + octave + " ");break;
case 6: notes = notes.concat("E" + octave + " ");break;
case 7: notes = notes.concat("F" + octave + " ");break;
case 8: notes = notes.concat("Gb" + octave + " ");break;
case 9: notes = notes.concat("G" + octave + " ");break;
case 10: notes = notes.concat("Ab" + octave + " ");break;
case 11: notes = notes.concat("A" + octave + " ");break;
}
}
else if (rootNote.equals("B") || rootNote.equals("Cb")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("C" + octave + " ");break;
case 2: notes = notes.concat("C#" + octave + " ");break;
case 3: notes = notes.concat("D" + octave + " ");break;
case 4: notes = notes.concat("D#" + octave + " ");break;
case 5: notes = notes.concat("E" + octave + " ");break;
case 6: notes = notes.concat("F" + octave + " ");break;
case 7: notes = notes.concat("F#" + octave + " ");break;
case 8: notes = notes.concat("G" + octave + " ");break;
case 9: notes = notes.concat("G#" + octave + " ");break;
case 10: notes = notes.concat("A" + octave + " ");break;
case 11: notes = notes.concat("A#" + octave + " ");break;
}
}
else if (rootNote.equals("B#") || rootNote.equals("C")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("C#" + octave + " ");break;
case 2: notes = notes.concat("D" + octave + " ");break;
case 3: notes = notes.concat("D#" + octave + " ");break;
case 4: notes = notes.concat("E" + octave + " ");break;
case 5: notes = notes.concat("F" + octave + " ");break;
case 6: notes = notes.concat("F#" + octave + " ");break;
case 7: notes = notes.concat("G" + octave + " ");break;
case 8: notes = notes.concat("G#" + octave + " ");break;
case 9: notes = notes.concat("A" + octave + " ");break;
case 10: notes = notes.concat("A#" + octave + " ");break;
case 11: notes = notes.concat("B" + octave + " ");break;
}
}
else if (rootNote.equals("C#") || rootNote.equals("Db")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("D" + octave + " ");break;
case 2: notes = notes.concat("D#" + octave + " ");break;
case 3: notes = notes.concat("E" + octave + " ");break;
case 4: notes = notes.concat("F" + octave + " ");break;
case 5: notes = notes.concat("F#" + octave + " ");break;
case 6: notes = notes.concat("G" + octave + " ");break;
case 7: notes = notes.concat("G#" + octave + " ");break;
case 8: notes = notes.concat("A" + octave + " ");break;
case 9: notes = notes.concat("A#" + octave + " ");break;
case 10: notes = notes.concat("B" + octave + " ");break;
case 11: notes = notes.concat("C" + octave + " ");break;
}
}
else if (rootNote.equals("D")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("D#" + octave + " ");break;
case 2: notes = notes.concat("E" + octave + " ");break;
case 3: notes = notes.concat("F" + octave + " ");break;
case 4: notes = notes.concat("F#" + octave + " ");break;
case 5: notes = notes.concat("G" + octave + " ");break;
case 6: notes = notes.concat("G#" + octave + " ");break;
case 7: notes = notes.concat("A" + octave + " ");break;
case 8: notes = notes.concat("A#" + octave + " ");break;
case 9: notes = notes.concat("B" + octave + " ");break;
case 10: notes = notes.concat("C" + octave + " ");break;
case 11: notes = notes.concat("C#" + octave + " ");break;
}
}
else if (rootNote.equals("D#") || rootNote.equals("Eb")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("E" + octave + " ");break;
case 2: notes = notes.concat("F" + octave + " ");break;
case 3: notes = notes.concat("F#" + octave + " ");break;
case 4: notes = notes.concat("G" + octave + " ");break;
case 5: notes = notes.concat("G#" + octave + " ");break;
case 6: notes = notes.concat("A" + octave + " ");break;
case 7: notes = notes.concat("A#" + octave + " ");break;
case 8: notes = notes.concat("B" + octave + " ");break;
case 9: notes = notes.concat("C" + octave + " ");break;
case 10: notes = notes.concat("C#" + octave + " ");break;
case 11: notes = notes.concat("D" + octave + " ");break;
}
}
else if (rootNote.equals("E") || rootNote.equals("Fb")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("F" + octave + " ");break;
case 2: notes = notes.concat("F#" + octave + " ");break;
case 3: notes = notes.concat("G" + octave + " ");break;
case 4: notes = notes.concat("G#" + octave + " ");break;
case 5: notes = notes.concat("A" + octave + " ");break;
case 6: notes = notes.concat("A#" + octave + " ");break;
case 7: notes = notes.concat("B" + octave + " ");break;
case 8: notes = notes.concat("C" + octave + " ");break;
case 9: notes = notes.concat("C#" + octave + " ");break;
case 10: notes = notes.concat("D" + octave + " ");break;
case 11: notes = notes.concat("D#" + octave + " ");break;
}
}
else if (rootNote.equals("E#") || rootNote.equals("F")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("F#" + octave + " ");break;
case 2: notes = notes.concat("G" + octave + " ");break;
case 3: notes = notes.concat("G#" + octave + " ");break;
case 4: notes = notes.concat("A" + octave + " ");break;
case 5: notes = notes.concat("A#" + octave + " ");break;
case 6: notes = notes.concat("B" + octave + " ");break;
case 7: notes = notes.concat("C" + octave + " ");break;
case 8: notes = notes.concat("C#" + octave + " ");break;
case 9: notes = notes.concat("D" + octave + " ");break;
case 10: notes = notes.concat("D#" + octave + " ");break;
case 11: notes = notes.concat("E" + octave + " ");break;
}
}
else if (rootNote.equals("F#") || rootNote.equals("Gb")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("G" + octave + " ");break;
case 2: notes = notes.concat("G#" + octave + " ");break;
case 3: notes = notes.concat("A" + octave + " ");break;
case 4: notes = notes.concat("A#" + octave + " ");break;
case 5: notes = notes.concat("B" + octave + " ");break;
case 6: notes = notes.concat("C" + octave + " ");break;
case 7: notes = notes.concat("C#" + octave + " ");break;
case 8: notes = notes.concat("D" + octave + " ");break;
case 9: notes = notes.concat("D#" + octave + " ");break;
case 10: notes = notes.concat("E" + octave + " ");break;
case 11: notes = notes.concat("F" + octave + " ");break;
}
}
else if (rootNote.equals("G")) {
switch (noteValue % 12) {
case 0: notes = notes.concat(rootNote + octave + " ");break;
case 1: notes = notes.concat("G#" + octave + " ");break;
case 2: notes = notes.concat("A" + octave + " ");break;
case 3: notes = notes.concat("A#" + octave + " ");break;
case 4: notes = notes.concat("B" + octave + " ");break;
case 5: notes = notes.concat("C" + octave + " ");break;
case 6: notes = notes.concat("C#" + octave + " ");break;
case 7: notes = notes.concat("D" + octave + " ");break;
case 8: notes = notes.concat("D#" + octave + " ");break;
case 9: notes = notes.concat("E" + octave + " ");break;
case 10: notes = notes.concat("F" + octave + " ");break;
case 11: notes = notes.concat("F#" + octave + " ");break;
}
}
}
}
return notes;
}
最佳答案
您不小心连接了一个不可转换的字符串:interval = Interval.concat(c);
(第 31 行)。我认为应该是 interval = Interval.concat(c.charAt(0));
关于java - 将音程转换为音符的方法,遇到 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40792859/