public static ChordPattern FromString(string pattern) { if (string.IsNullOrEmpty(pattern)) { throw new ArgumentNullException("pattern"); } string[] words = pattern.Split(new char[] { ',' }); var cp = new ChordPattern(); foreach (string word in words.Where(w => !String.IsNullOrWhiteSpace(w))) { string candidate = word.Trim(); Accidental accidental = Accidental.FromString(candidate); if (accidental != null) { candidate = candidate.Substring(1); } int value = Convert.ToInt32(candidate, CultureInfo.CurrentCulture); if (value > 0) { if (accidental != null) { value += accidental.Value; } var degrees = new ScaleDegree[] { ScaleDegree.First, ScaleDegree.Second, ScaleDegree.Third, ScaleDegree.Fourth, ScaleDegree.Fifth, ScaleDegree.Sixth, ScaleDegree.Seventh, ScaleDegree.Octave, ScaleDegree.FlatNinth, ScaleDegree.Ninth, ScaleDegree.SharpNinth, ScaleDegree.Eleventh, ScaleDegree.SharpEleventh, ScaleDegree.Thirteen }; if (value < 1 || value > degrees.Length) { throw new ArgumentOutOfRangeException("pattern", string.Format(CultureInfo.CurrentCulture, "Don't understand chord degree {0} -> {1}", words, value)); } cp.Add(degrees[value - 1]); } } return(cp); }
public static Note FromName(string note, MidiOctaveFormat format) { const int MinimumTextLength = 2; if (String.IsNullOrEmpty(note) || note.Length < MinimumTextLength) { throw new NoteFormatException("Note text is too short"); } if (!Char.IsLetter(note.First())) { throw new NoteFormatException("Note must start with a letter"); } Accidental accidental = Accidental.FromString(note.Substring(1, 1)); int NamePartLength = 1; if (accidental != null) { NamePartLength = 2; } string scaleDegreeText = note.Substring(0, NamePartLength).ToUpper(); int scaleDegree = 0; try { scaleDegree = NoteValues[scaleDegreeText]; } catch (KeyNotFoundException e) { throw new NoteFormatException(scaleDegreeText, e); } string octaveText = note.Substring(NamePartLength); int octaveValue = Convert.ToInt32(octaveText, CultureInfo.CurrentCulture); return(new Note(scaleDegree, octaveValue, format)); }
public static IntervalPattern FromString(string pattern) { if (string.IsNullOrEmpty(pattern)) { throw new ArgumentNullException("pattern"); } string[] words = pattern.Split(new char[] { ',' }); var ip = new IntervalPattern(); foreach (string word in words.Where(w => !String.IsNullOrWhiteSpace(w))) { string candidate = word.Trim(); Accidental accidental = Accidental.FromString(candidate); if (accidental != null) { candidate = candidate.Substring(1); } int value = Convert.ToInt32(candidate, CultureInfo.CurrentCulture); if (value > 0) { if (accidental != null) { value += accidental.Value; } ip.Add(new Interval(value)); } } return(ip); }