public void ChordController_checkChordNameTest() { chordName = "A"; Assert.IsTrue(ChordController.checkChordName(chordName)); chordName = chordName + "#"; Assert.IsTrue(ChordController.checkChordName(chordName)); chordName = chordName + "m"; Assert.IsTrue(ChordController.checkChordName(chordName)); chordName = "B"; Assert.IsTrue(ChordController.checkChordName(chordName)); chordName = chordName + "b"; Assert.IsTrue(ChordController.checkChordName(chordName)); chordName = chordName + "7"; Assert.IsTrue(ChordController.checkChordName(chordName)); chordName = "C"; Assert.IsTrue(ChordController.checkChordName(chordName)); chordName = "D"; Assert.IsTrue(ChordController.checkChordName(chordName)); chordName = "E"; Assert.IsTrue(ChordController.checkChordName(chordName)); chordName = "F"; Assert.IsTrue(ChordController.checkChordName(chordName)); chordName = "G"; Assert.IsTrue(ChordController.checkChordName(chordName)); }
public static Chord getChordByName(string name) { if (!(ChordController.checkChordName(name))) { throw new System.ArgumentException("Parameter must have a valid name", "Chord Name: " + name); } int i = 0; bool needsMore = true; string tonicName; string slashBass; Note tonic; Chord chord = new Chord(); chord.setName(name); var chordName = name.ToCharArray(); tonicName = chordName[i].ToString(); i++; if (i + 1 <= chordName.Length && (chordName[i] == 'b' || chordName[i] == '#')) { tonicName = tonicName + chordName[i].ToString(); i++; } tonic = NoteFactory.getNoteByName(tonicName); chord.addNote(tonic); // Calculate first interval (dim3, m3, M3, aug3) if (needsMore) { // X5 if (i + 1 <= chordName.Length && chordName[i] == '5') { chord.addNote(calculateNote(7, tonic)); i++; needsMore = false; } // Xsus else if (i + 3 <= chordName.Length && chordName[i] == 's' && chordName[i + 1] == 'u' && chordName[i + 2] == 's') { i = i + 3; if (i + 1 <= chordName.Length && chordName[i] == '2') { chord.addNote(calculateNote(2, tonic)); i++; } else { chord.addNote(calculateNote(5, tonic)); } chord.addNote(calculateNote(7, tonic)); needsMore = false; } // X7sus else if (i + 4 <= chordName.Length && chordName[i] == '7' && chordName[i + 1] == 's' && chordName[i + 2] == 'u' && chordName[i + 3] == 's') { chord.addNote(calculateNote(5, tonic)); chord.addNote(calculateNote(7, tonic)); chord.addNote(calculateNote(10, tonic)); i = i + 4; needsMore = false; } // Xdim else if (i + 3 <= chordName.Length && chordName[i] == 'd' && chordName[i + 1] == 'i' && chordName[i + 2] == 'm') { chord.addNote(calculateNote(3, tonic)); chord.addNote(calculateNote(6, tonic)); i = i + 3; needsMore = false; } //Xm else if (i + 1 == chordName.Length && chordName[i] == 'm') { chord.addNote(calculateNote(3, tonic)); i++; } // Xm... else if (i + 1 <= chordName.Length && chordName[i] == 'm' && chordName[i + 1] != 'a') { chord.addNote(calculateNote(3, tonic)); i++; } // X... else { chord.addNote(calculateNote(4, tonic)); } } // Add next interval (dim5, P5, aug5) if (needsMore) { // X(b5) if (i + 4 <= chordName.Length && chordName[i] == '(' && chordName[i + 1] == 'b' && chordName[i + 2] == '5' && chordName[i + 3] == ')') { chord.addNote(calculateNote(6, tonic)); i = i + 4; needsMore = false; } // X7(b5) else if (i + 5 <= chordName.Length && chordName[i] == '7' && chordName[i + 1] == '(' && chordName[i + 2] == 'b' && chordName[i + 3] == '5' && chordName[i + 4] == ')') { chord.addNote(calculateNote(6, tonic)); chord.addNote(calculateNote(10, tonic)); i = i + 5; needsMore = false; } // Xmaj7(b5) else if (i + 8 <= chordName.Length && chordName[i] == 'm' && chordName[i + 1] == 'a' && chordName[i + 2] == 'j' && chordName[i + 3] == '7' && chordName[i + 4] == '(' && chordName[i + 5] == 'b' && chordName[i + 6] == '5') { chord.addNote(calculateNote(6, tonic)); chord.addNote(calculateNote(11, tonic)); i = i + 8; needsMore = false; } // X9(b5) else if (i + 5 <= chordName.Length && chordName[i] == '9' && chordName[i + 1] == '(' && chordName[i + 2] == 'b' && chordName[i + 3] == '5') { chord.addNote(calculateNote(6, tonic)); chord.addNote(calculateNote(10, tonic)); chord.addNote(calculateNote(2, tonic)); i = i + 5; needsMore = false; } // X13(b9b5) else if (i + 8 <= chordName.Length && chordName[i] == '1' && chordName[i + 1] == '3' && chordName[i + 2] == '(' && chordName[i + 3] == 'b' && chordName[i + 4] == '9' && chordName[i + 5] == 'b' && chordName[i + 6] == '5') { chord.addNote(calculateNote(6, tonic)); chord.addNote(calculateNote(10, tonic)); chord.addNote(calculateNote(1, tonic)); chord.addNote(calculateNote(9, tonic)); i = i + 8; needsMore = false; } // Xaug else if (i + 3 <= chordName.Length && chordName[i] == 'a' && chordName[i + 1] == 'u' && chordName[i + 2] == 'g') { chord.addNote(calculateNote(8, tonic)); i = i + 3; needsMore = false; } // X7+ else if (i + 2 <= chordName.Length && chordName[i] == '7' && chordName[i + 1] == '+') { chord.addNote(calculateNote(8, tonic)); chord.addNote(calculateNote(10, tonic)); i = i + 2; // X7+(b9) if (i + 4 <= chordName.Length && chordName[i] == '(' && chordName[i + 1] == 'b' && chordName[i + 2] == '9') { chord.addNote(calculateNote(1, tonic)); i = i + 4; } needsMore = false; } // X9+ else if (i + 2 <= chordName.Length && chordName[i] == '9' && chordName[i + 1] == '+') { chord.addNote(calculateNote(8, tonic)); chord.addNote(calculateNote(10, tonic)); chord.addNote(calculateNote(2, tonic)); i = i + 2; needsMore = false; } // X... else { chord.addNote(calculateNote(7, tonic)); } } // Add final intervals (M6, m7, M7) if (needsMore) { // X(add9) if (i + 6 <= chordName.Length && chordName[i] == '(' && chordName[i + 1] == 'a' && chordName[i + 2] == 'd' && chordName[i + 3] == 'd' && chordName[i + 4] == '9') { chord.addNote(calculateNote(2, tonic)); i = i + 6; needsMore = false; } // X6 else if (i + 1 <= chordName.Length && chordName[i] == '6') { chord.addNote(calculateNote(9, tonic)); i++; // X6/9 if (i + 2 <= chordName.Length && chordName[i] == '/' && chordName[i + 1] == '9') { chord.addNote(calculateNote(2, tonic)); i = i + 2; } needsMore = false; } // X13 else if (i + 2 <= chordName.Length && chordName[i] == '1' && chordName[i + 1] == '3') { chord.addNote(calculateNote(10, tonic)); i = i + 2; // X13(b9) if (i + 4 <= chordName.Length && chordName[i] == '(' && chordName[i + 1] == 'b' && chordName[i + 2] == '9') { chord.addNote(calculateNote(1, tonic)); i = i + 4; } // X13(#9) else if (i + 4 <= chordName.Length && chordName[i] == '(' && chordName[i + 1] == '#' && chordName[i + 2] == '9') { chord.addNote(calculateNote(3, tonic)); i = i + 4; } else { chord.addNote(calculateNote(2, tonic)); } chord.addNote(calculateNote(9, tonic)); needsMore = false; } // X7 else if (i + 1 <= chordName.Length && chordName[i] == '7') { chord.addNote(calculateNote(10, tonic)); i++; // X7(b9) if (i + 4 <= chordName.Length && chordName[i] == '(' && chordName[i + 1] == 'b' && chordName[i + 2] == '9') { chord.addNote(calculateNote(1, tonic)); i = i + 4; } // X7(#9) else if (i + 4 <= chordName.Length && chordName[i] == '(' && chordName[i + 1] == '#' && chordName[i + 2] == '9') { chord.addNote(calculateNote(3, tonic)); i = i + 4; } needsMore = false; } // X9 else if (i + 1 <= chordName.Length && chordName[i] == '9') { i++; // X9(#11) if (i + 5 <= chordName.Length && chordName[i] == '(' && chordName[i + 1] == '#' && chordName[i + 2] == '1' && chordName[i + 3] == '1') { chord.addNote(calculateNote(10, tonic)); chord.addNote(calculateNote(2, tonic)); chord.addNote(calculateNote(6, tonic)); i = i + 5; } // X9maj7 else if (i + 6 <= chordName.Length && chordName[i + 1] == 'm' && chordName[i + 2] == 'a' && chordName[i + 3] == 'j' && chordName[i + 4] == '7') { chord.addNote(calculateNote(11, tonic)); chord.addNote(calculateNote(2, tonic)); i = i + 6; needsMore = false; } else { chord.addNote(calculateNote(10, tonic)); chord.addNote(calculateNote(2, tonic)); } needsMore = false; } // X11 else if (i + 1 <= chordName.Length && chordName[i] == '1' && chordName[i + 1] == '1') { chord.addNote(calculateNote(10, tonic)); chord.addNote(calculateNote(2, tonic)); chord.addNote(calculateNote(5, tonic)); i = i + 2; needsMore = false; } // XM11 else if (i + 3 <= chordName.Length && chordName[i] == 'M' && chordName[i + 1] == '1' && chordName[i + 2] == '1') { chord.addNote(calculateNote(11, tonic)); chord.addNote(calculateNote(2, tonic)); chord.addNote(calculateNote(5, tonic)); i = i + 3; needsMore = false; } // Xmaj7 else if (i + 3 <= chordName.Length && chordName[i] == 'm' && chordName[i + 1] == 'a' && chordName[i + 2] == 'j') { chord.addNote(calculateNote(11, tonic)); i = i + 3; if (i + 1 <= chordName.Length && chordName[i] == '9') { chord.addNote(calculateNote(2, tonic)); } i++; needsMore = false; } // X(maj7) else if (i + 6 <= chordName.Length && chordName[i] == '(' && chordName[i + 1] == 'm' && chordName[i + 2] == 'a' && chordName[i + 3] == 'j' && chordName[i + 4] == '7') { chord.addNote(calculateNote(11, tonic)); i = i + 6; needsMore = false; } } // X/bassNote if (i < chordName.Count() && chordName[i] == '/') { i++; if (i < chordName.Count() && NoteController.checkNoteName(chordName[i].ToString())) { slashBass = chordName[i].ToString(); i++; } else { throw new System.ArgumentException("\nThe character length for " + name + " is: " + chordName.Length + "\nThe index for " + name + " is: " + i, "Chord Name: " + name); } if (i < chordName.Count() && (chordName[i] == 'b' || chordName[i] == '#')) { slashBass = slashBass + chordName[i].ToString(); i++; } chord.insertNote(NoteFactory.getNoteByName(slashBass), 0); } if (i < chordName.Length) { throw new System.ArgumentException("\nThe character length for " + name + " is: " + chordName.Length + "\nThe index for " + name + " is: " + i, "Chord Name: " + name); } chord.color = RandomColorFactory.GetRandomColor(); return(chord); }