public static string ToString(InternalNote note, InternalNoteStringStyle style) { if (NoteUtils.IsNatural(note) || style != InternalNoteStringStyle.ShowBoth) { return(NoteUtils.ToString(NoteUtils.ToNote(note, style))); } else { switch (note) { case InternalNote.Cs_Db: return("C#/Db"); case InternalNote.Ds_Eb: return("D#/Eb"); case InternalNote.Fs_Gb: return("F#/Gb"); case InternalNote.Gs_Ab: return("G#/Ab"); case InternalNote.As_Bb: return("A#/Bb"); } } throw new ArgumentException(); }
public static async Task <ScaleFinderResultSet> FindScalesAsync(IScaleFinderOptions scaleFinderOptions, CancellationToken cancelToken) { if (null == scaleFinderOptions) { throw new ArgumentNullException("scaleFinderOptions"); } InternalNote root = NoteUtils.ToInternalNote(scaleFinderOptions.RootNote); IScale scale = scaleFinderOptions.Scale; InternalNote[] notesInScale = NamedInterval.GetNotes(root, scale.Intervals); ScaleFinderResultSet results = new ScaleFinderResultSet(scaleFinderOptions); if (cancelToken.IsCancellationRequested) { return(results); } foreach (NoteNode startingNode in FindNodes(notesInScale[0], scaleFinderOptions)) { await FindAllScalesAsync(results, startingNode, notesInScale, 1, startingNode.String, scaleFinderOptions, cancelToken); } return(results); }
private string GetText(InternalNote?[] notes, int str, MarkTextOption markTextOption) { if (str < 0 || str >= notes.Length) { throw new ArgumentOutOfRangeException(nameof(str)); } string text = ""; if (notes[str].HasValue) { switch (markTextOption) { case MarkTextOption.ShowNote_PreferFlats: text = NoteUtils.ToString(notes[str].Value, InternalNoteStringStyle.PreferFlat); break; case MarkTextOption.ShowNote_PreferSharps: text = NoteUtils.ToString(notes[str].Value, InternalNoteStringStyle.PreferSharp); break; case MarkTextOption.ShowNote_ShowBoth: text = NoteUtils.ToString(notes[str].Value, InternalNoteStringStyle.ShowBoth); break; } } return(text); }
public FullNote Shift(int steps, InternalNoteStringStyle style) { if (steps == 0) { return(new FullNote(NoteUtils.ToNote(InternalNote, style), Octave)); } int direction = Math.Sign(steps); InternalNote note = InternalNote; int octave = Octave; for (int i = 0; i < steps; i++) { int noteIndex = (int)note + direction; if (noteIndex < 0) { noteIndex += 12; } note = (InternalNote)(noteIndex % 12); if (direction > 0 && note == InternalNote.C) { octave++; } else if (direction < 0 && note == InternalNote.B) { octave--; } } return(new FullNote(NoteUtils.ToNote(note, style), octave)); }
public Tuning(Instrument instrument, XmlNode tuningNode) { if (null == instrument) { throw new ArgumentNullException("instrument"); } if (null == tuningNode) { throw new ArgumentNullException("tuningNode"); } this.Instrument = instrument; this.Name = tuningNode.Attributes["name"].Value; string notes = tuningNode.Attributes["notes"].Value; string[] s = notes.Split(';'); Note[] rootNotes = new Note[s.Length]; if (rootNotes.Length != instrument.NumStrings) { throw new ArgumentOutOfRangeException(); } for (int i = 0; i < rootNotes.Length; i++) { rootNotes[i] = NoteUtils.ParseNote(s[i]); } this.RootNotes = rootNotes; }
public bool IsRoot(MarkPosition mark) { if (null == mark) { throw new ArgumentOutOfRangeException("mark"); } return(NoteAt(mark) == NoteUtils.ToInternalNote(Parent.ScaleFinderOptions.RootNote)); }
public void Set(string key, Note value) { if (StringUtils.IsNullOrWhiteSpace(key)) { throw new ArgumentNullException(nameof(key)); } Set(key, NoteUtils.ToString(value)); }
public bool TryGet(string key, out Note result, bool recursive = true) { if (TryGet(key, out string rawResult, recursive)) { return(NoteUtils.TryParseNote(rawResult, out result)); } result = default(Note); return(false); }
public InternalNote[] GetNotes(InternalNote root) { InternalNote[] notes = new InternalNote[Intervals.Length]; for (int i = 0; i < notes.Length; i++) { notes[i] = NoteUtils.Shift(root, Intervals[i]); } return(notes); }
private static async Task FindAllScalesAsync(ScaleFinderResultSet results, NoteNode noteNode, InternalNote[] targetNotes, int nextNote, int str, IScaleFinderOptions scaleFinderOptions, CancellationToken cancelToken) { if (cancelToken.IsCancellationRequested) { return; } IInstrument instrument = scaleFinderOptions.Instrument; if (nextNote == targetNotes.Length) // Build a scale result { List <MarkPosition> marks = new List <MarkPosition>(); NoteNode nn = noteNode; // Walk back up the tree to set the marks on the result and flag each target note while (null != nn) { if (nn.Fret >= 0) { marks.Add(new MarkPosition(nn.String + 1, nn.Fret)); } nn = nn.Parent; } results.AddResult(marks); } else if (str < instrument.NumStrings) // Keep building the tree { // Look at all the notes on the string int startingFret = scaleFinderOptions.AllowOpenStrings ? 0 : 1; if (null != noteNode && noteNode.String == str) { startingFret = noteNode.Fret + 1; } for (int fret = startingFret; fret <= scaleFinderOptions.MaxFret; fret++) { InternalNote note = NoteUtils.Shift(scaleFinderOptions.Tuning.RootNotes[str].InternalNote, fret); // See if the note is the next target note if (note == targetNotes[nextNote]) { NoteNode child = new NoteNode(str, fret, note, noteNode); // Add found note await FindAllScalesAsync(results, child, targetNotes, nextNote + 1, str, scaleFinderOptions, cancelToken); // Look for the next note on the same string } } await FindAllScalesAsync(results, noteNode, targetNotes, nextNote, str + 1, scaleFinderOptions, cancelToken); // Look for the next note on the next string } }
public string ToXml() { string rootNotes = ""; for (int i = 0; i < RootNotes.Length; i++) { rootNotes += NoteUtils.ToString(RootNotes[i]) + ";"; } rootNotes = rootNotes.TrimEnd(';'); return(String.Format("<tuning name=\"{0}\" notes=\"{1}\" />", Name, rootNotes)); }
public void SetTarget(Note rootNote, IChordQuality chordQuality) { if (null == chordQuality) { throw new ArgumentNullException(nameof(chordQuality)); } Settings[Prefix + "rootnote"] = NoteUtils.ToString(rootNote); Settings[Prefix + "chordquality"] = chordQuality.LongName; ChordQualityLevel = chordQuality.Level; _cachedChordQuality = chordQuality; }
public void SetTarget(Note rootNote, IScale scale) { if (null == scale) { throw new ArgumentNullException(nameof(scale)); } Settings[Prefix + "rootnote"] = NoteUtils.ToString(rootNote); Settings[Prefix + "scale"] = scale.LongName; ScaleLevel = scale.Level; _cachedScale = scale; }
public InternalNote InternalNoteAt(int str, int fret) { if (str < 0 || str >= RootNotes.Length) { throw new ArgumentOutOfRangeException("str"); } if (fret < 0) { throw new ArgumentOutOfRangeException("fret"); } return(NoteUtils.Shift(RootNotes[str].InternalNote, fret)); }
public static InternalNote[] GetNotes(InternalNote root, int[] intervals) { if (null == intervals || intervals.Length == 0) { throw new ArgumentNullException(nameof(intervals)); } InternalNote[] notes = new InternalNote[intervals.Length]; for (int i = 0; i < notes.Length; i++) { notes[i] = NoteUtils.Shift(root, intervals[i]); } return(notes); }
private static IEnumerable <NoteNode> FindNodes(InternalNote targetNote, IScaleFinderOptions scaleFinderOptions) { for (int str = 0; str < scaleFinderOptions.Instrument.NumStrings; str++) { InternalNote rootNote = scaleFinderOptions.Tuning.RootNotes[str].InternalNote; int fret = NoteUtils.GetShift(rootNote, targetNote); while (fret <= scaleFinderOptions.MaxFret) { yield return(new NoteNode(str, fret, targetNote, null)); fret += 12; } } }
public Chord ChordAt(int index) { int[] absoluteMarks = ResultAt(index); int baseLine; int[] marks = MarkUtils.AbsoluteToRelativeMarks(absoluteMarks, out baseLine, ChordFinderOptions.NumFrets); string title = NoteUtils.ToString(Root) + ChordQuality.Abbreviation; string fileName = title + index; int numStrings = marks.Length; int numFrets = ChordFinderOptions.NumFrets; int barre = ChordFinderOptions.AutoAddBarres ? -1 : 0; return(new Chord(fileName, title, numStrings, numFrets, baseLine, barre, marks)); }
public ChordResultSet FindChords(Note root, ChordQuality chordQuality, ChordFinderOptions chordFinderOptions) { if (null == chordQuality) { throw new ArgumentNullException("chordQuality"); } if (null == chordFinderOptions) { throw new ArgumentNullException("chordFinderOptions"); } InternalNote[] notesInChord = chordQuality.GetNotes(NoteUtils.ToInternalNote(root)); ChordResultSet results = new ChordResultSet(root, chordQuality, chordFinderOptions); FindAllChords(results, null, notesInChord, 0, chordFinderOptions); return(results); }
public string GetText(MarkPosition mark, MarkTextOption markTextOption) { string text = ""; switch (markTextOption) { case MarkTextOption.ShowNote_PreferFlats: text = NoteUtils.ToString(NoteAt(mark), InternalNoteStringStyle.PreferFlat); break; case MarkTextOption.ShowNote_PreferSharps: text = NoteUtils.ToString(NoteAt(mark), InternalNoteStringStyle.PreferSharp); break; case MarkTextOption.ShowNote_ShowBoth: text = NoteUtils.ToString(NoteAt(mark), InternalNoteStringStyle.ShowBoth); break; } return(text); }
public static InternalNote?[] GetInternalNotes(int[] marks, ITuning tuning) { if (null == marks || marks.Length == 0) { throw new ArgumentNullException("marks"); } if (null == tuning) { throw new ArgumentNullException("tuning"); } InternalNote?[] notes = new InternalNote?[marks.Length]; for (int i = 0; i < marks.Length; i++) { notes[i] = marks[i] == -1 ? null : (InternalNote?)NoteUtils.Shift(tuning.RootNotes[i].InternalNote, marks[i]); } return(notes); }
public static async Task <ChordFinderResultSet> FindChordsAsync(IChordFinderOptions chordFinderOptions, CancellationToken cancelToken) { if (null == chordFinderOptions) { throw new ArgumentNullException("chordFinderOptions"); } InternalNote root = NoteUtils.ToInternalNote(chordFinderOptions.RootNote); IChordQuality chordQuality = chordFinderOptions.ChordQuality; InternalNote[] notesInChord = NamedInterval.GetNotes(root, chordQuality.Intervals); ChordFinderResultSet results = new ChordFinderResultSet(chordFinderOptions); if (cancelToken.IsCancellationRequested) { return(results); } await FindAllChordsAsync(results, null, notesInChord, 0, chordFinderOptions, cancelToken); return(results); }
public static FullNote Parse(string s) { if (StringUtils.IsNullOrWhiteSpace(s)) { throw new ArgumentNullException(nameof(s)); } s = s.Trim(); int splitIndex = s.IndexOfAny(digits); if (splitIndex <= 0) { throw new ArgumentException(Strings.InvalidNoteArgumentExceptionMessage); } string notePortion = s.Substring(0, splitIndex); string octavePortion = s.Substring(splitIndex); Note note = NoteUtils.ParseNote(notePortion); int octave = int.Parse(octavePortion); return(new FullNote(note, octave)); }
public Diagram ToDiagram(ScaleFinderStyle scaleFinderStyle) { int numStrings = Parent.ScaleFinderOptions.Instrument.NumStrings; int numFrets = Parent.ScaleFinderOptions.NumFrets; int baseLine; IEnumerable <MarkPosition> marks = MarkUtils.AbsoluteToRelativeMarks(Marks, out baseLine, numFrets, numStrings); Diagram d = new Diagram(scaleFinderStyle.Style, numStrings, numFrets); if (scaleFinderStyle.AddTitle) { d.Title = NoteUtils.ToString(Parent.ScaleFinderOptions.RootNote) + " " + Parent.ScaleFinderOptions.Scale.Name; d.Style.TitleLabelStyle = DiagramLabelStyle.Regular; } int markPositionIndex = 0; // Add existing marks foreach (MarkPosition mark in marks) { int @string = mark.String; if (scaleFinderStyle.MirrorResults) { @string = numStrings - (@string - 1); } int fret = mark.Fret; MarkPosition mp = new MarkPosition(@string, fret); DiagramMark dm = d.NewMark(mp); // Set mark type if (scaleFinderStyle.AddRootNotes && IsRoot(markPositionIndex)) // Use markPositionIndex, to get the correct roots in mirror mode { dm.Type = DiagramMarkType.Root; } if (fret == 0) // Open string { dm.Type = (dm.Type == DiagramMarkType.Root) ? DiagramMarkType.OpenRoot : DiagramMarkType.Open; } // Add text dm.Text = GetText(markPositionIndex, scaleFinderStyle.MarkTextOption); // Use markPositionIndex, not mp, to get the correct text in mirror mode markPositionIndex++; } // Add nut or fret label if (baseLine == 0) { d.Style.GridNutVisible = true; } else { d.Style.GridNutVisible = false; FretLabelPosition flp = new FretLabelPosition(FretLabelSide.Right, 1); d.NewFretLabel(flp, baseLine.ToString()); } return(d); }
public override string ToString() { return(string.Format("{0}{1}", NoteUtils.ToString(Note), Octave)); }
private static async Task FindAllChordsAsync(ChordFinderResultSet results, NoteNode noteNode, InternalNote[] targetNotes, int str, IChordFinderOptions chordFinderOptions, CancellationToken cancelToken) { if (cancelToken.IsCancellationRequested) { return; } IInstrument instrument = chordFinderOptions.Instrument; if (str == instrument.NumStrings) // Build a chord result { int[] marks = new int[instrument.NumStrings]; NoteNode nn = noteNode; str--; bool[] hasNotes = new bool[targetNotes.Length]; // Walk back up the tree to set the marks on the result and flag each target note while (null != nn) { marks[str] = nn.Fret; for (int i = 0; i < targetNotes.Length; i++) { if (nn.Note == targetNotes[i]) { hasNotes[i] = true; } } nn = nn.Parent; str--; } InternalNote rootNote = NoteUtils.ToInternalNote(chordFinderOptions.RootNote); // Add result if it had all the target notes bool valid = true; for (int i = 0; i < hasNotes.Length; i++) { // Validate: // 1. All notes when !AllowRootlessChords // 2. Non-root notes only when AllowRootlessChords if (!chordFinderOptions.AllowRootlessChords || (chordFinderOptions.AllowRootlessChords && targetNotes[i] != rootNote)) { valid = valid && hasNotes[i]; } } if (valid) { results.AddResult(marks); } } else // Keep building the tree { // Look at the muted string if (chordFinderOptions.AllowMutedStrings) { NoteNode muted = new NoteNode(); muted.Parent = noteNode; await FindAllChordsAsync(results, muted, targetNotes, str + 1, chordFinderOptions, cancelToken); } // Look at all the notes on the string int startingFret = chordFinderOptions.AllowOpenStrings ? 0 : 1; for (int fret = startingFret; fret <= chordFinderOptions.MaxFret; fret++) { InternalNote note = NoteUtils.Shift(chordFinderOptions.Tuning.RootNotes[str].InternalNote, fret); // See if the note is a target note for (int i = 0; i < targetNotes.Length; i++) { // If it's a target note add it and search on the next string if (note == targetNotes[i]) { NoteNode child = new NoteNode(fret, note, noteNode); await FindAllChordsAsync(results, child, targetNotes, str + 1, chordFinderOptions, cancelToken); break; } } } } }
public Diagram ToDiagram(ChordFinderStyle chordFinderStyle) { int[] marks = MarkUtils.AbsoluteToRelativeMarks(Marks, out int baseLine, Parent.ChordFinderOptions.NumFrets); InternalNote?[] notes = MarkUtils.GetInternalNotes(Marks, Parent.ChordFinderOptions.Tuning); InternalNote rootNote = NoteUtils.ToInternalNote(Parent.ChordFinderOptions.RootNote); if (chordFinderStyle.MirrorResults) { Array.Reverse(marks); Array.Reverse(notes); } int numStrings = marks.Length; int numFrets = Parent.ChordFinderOptions.NumFrets; Diagram d = new Diagram(chordFinderStyle.Style, numStrings, numFrets); if (chordFinderStyle.AddTitle) { d.Title = NoteUtils.ToString(Parent.ChordFinderOptions.RootNote) + Parent.ChordFinderOptions.ChordQuality.Abbreviation; d.Style.TitleLabelStyle = DiagramLabelStyle.ChordName; } // Add marks for (int i = 0; i < marks.Length; i++) { int @string = i + 1; int fret = Math.Max(marks[i], 0); MarkPosition mp = new MarkPosition(@string, fret); DiagramMark dm = d.NewMark(mp); // Set mark type if (marks[i] == -1) // Muted string { dm.Type = DiagramMarkType.Muted; } else if (marks[i] == 0) // Open string { dm.Type = DiagramMarkType.Open; } // Change to root if necessary if (chordFinderStyle.AddRootNotes && notes[i].HasValue && notes[i].Value == rootNote) { dm.Type = (dm.Type == DiagramMarkType.Open) ? DiagramMarkType.OpenRoot : DiagramMarkType.Root; } // Add text if (chordFinderStyle.MarkTextOption != MarkTextOption.None) { dm.Text = GetText(notes, i, chordFinderStyle.MarkTextOption); } // Add bottom marks if (chordFinderStyle.AddBottomMarks) { MarkPosition bottomPosition = new MarkPosition(@string, numFrets + 1); DiagramMark bottomMark = d.NewMark(bottomPosition); bottomMark.Type = DiagramMarkType.Bottom; bottomMark.Text = GetText(notes, i, chordFinderStyle.BottomMarkTextOption); } } // Add nut or fret label if (baseLine == 0) { d.Style.GridNutVisible = true; } else { d.Style.GridNutVisible = false; FretLabelPosition flp = new FretLabelPosition(chordFinderStyle.FretLabelSide, 1); d.NewFretLabel(flp, baseLine.ToString()); } // Add barre BarrePosition bp = MarkUtils.AutoBarrePosition(marks, chordFinderStyle.BarreTypeOption, chordFinderStyle.MirrorResults); if (null != bp) { d.NewBarre(bp); } return(d); }
private bool IsRoot(int markPositionIndex) { return(NoteAt(markPositionIndex) == NoteUtils.ToInternalNote(Parent.ScaleFinderOptions.RootNote)); }
public InternalNote NoteAt(MarkPosition position) { return(NoteUtils.Shift(Parent.ScaleFinderOptions.Tuning.RootNotes[position.String - 1].InternalNote, position.Fret)); }