public static async Task <ScaleFinderResultSet> FindScalesAsync(IScaleFinderOptions scaleFinderOptions, CancellationToken cancelToken) { if (null == scaleFinderOptions) { throw new ArgumentNullException(nameof(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); }
public bool IsRoot(MarkPosition mark) { if (null == mark) { throw new ArgumentOutOfRangeException(nameof(mark)); } return(NoteAt(mark) == NoteUtils.ToInternalNote(Parent.ScaleFinderOptions.RootNote)); }
public static async Task <ChordFinderResultSet> FindChordsAsync(IChordFinderOptions chordFinderOptions, CancellationToken cancelToken) { if (null == chordFinderOptions) { throw new ArgumentNullException(nameof(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); }
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 { 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)); }