Exemplo n.º 1
0
        public static IEnumerable <Interval> GetIntervalsBetweenNotes(this Chord chord)
        {
            ThrowIfArgument.IsNull(nameof(chord), chord);

            return(GetIntervals(chord).Select(i => Interval.FromHalfSteps(i)).ToList());
        }
        /// <summary>
        /// Gets intervals from the root note of the specified chord. For example, +4 and +7 for C major
        /// (+4 for C and E, +7 for C and G).
        /// </summary>
        /// <param name="chord">Chord to get intervals from root note.</param>
        /// <returns>Intervals from the root note of the <paramref name="chord"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="chord"/> is <c>null</c>.</exception>
        /// <exception cref="InvalidOperationException">Some intervals are greater than <see cref="SevenBitNumber.MaxValue"/>.</exception>
        public static IEnumerable <Interval> GetIntervalsFromRootNote(this Chord chord)
        {
            ThrowIfArgument.IsNull(nameof(chord), chord);

            return(GetIntervalsFromRootNote(chord.NotesNames));
        }
 private static IEnumerable <SevenBitNumber> GetIntervals(Chord chord)
 {
     return(GetIntervals(chord.NotesNames));
 }
Exemplo n.º 4
0
 /// <summary>
 /// Converts the string representation of a musical chord to its <see cref="Chord"/> equivalent.
 /// A return value indicates whether the conversion succeeded.
 /// </summary>
 /// <param name="input">A string containing a chord to convert.</param>
 /// <param name="chord">When this method returns, contains the <see cref="Chord"/>
 /// equivalent of the musical chord contained in <paramref name="input"/>, if the conversion succeeded,
 /// or null if the conversion failed. The conversion fails if the <paramref name="input"/> is null or
 /// <see cref="string.Empty"/>, or is not of the correct format. This parameter is passed uninitialized;
 /// any value originally supplied in result will be overwritten.</param>
 /// <returns>true if <paramref name="input"/> was converted successfully; otherwise, false.</returns>
 public static bool TryParse(string input, out Chord chord)
 {
     return(ParsingUtilities.TryParse(input, ChordParser.TryParse, out chord));
 }
Exemplo n.º 5
0
        private static ParsingResult TryParseChordIntervals(Match match, NoteName rootNoteName, out Chord chord)
        {
            chord = null;

            var intervalGroup           = match.Groups[IntervalGroupName];
            var intervalsParsingResults = intervalGroup
                                          .Captures
                                          .OfType <Capture>()
                                          .Select(c =>
            {
                Interval interval;
                var parsingResult = IntervalParser.TryParse(c.Value, out interval);

                return(new
                {
                    Interval = interval,
                    ParsingResult = parsingResult
                });
            })
                                          .ToArray();

            var notParsedResult = intervalsParsingResults.FirstOrDefault(r => r.ParsingResult.Status != ParsingStatus.Parsed);

            if (notParsedResult != null)
            {
                return(notParsedResult.ParsingResult);
            }

            var intervals = intervalsParsingResults.Select(r => r.Interval).ToArray();

            chord = new Chord(rootNoteName, intervals);
            return(ParsingResult.Parsed);
        }
Exemplo n.º 6
0
        internal static ParsingResult TryParseChordName(Match match, NoteName rootNoteName, out Chord chord)
        {
            chord = null;

            //

            Quality?quality = null;

            var qualityGroup = match.Groups[ChordQualityGroupName];

            if (qualityGroup.Success)
            {
                quality = GroupsQualities.FirstOrDefault(gq => match.Groups[gq.Key].Success).Value;
            }

            //

            var             extensionIntervalNumber  = -1;
            IntervalQuality?extensionIntervalQuality = null;

            var extensionGroup = match.Groups[ExtensionGroupName];

            if (extensionGroup.Success && !string.IsNullOrWhiteSpace(extensionGroup.Value))
            {
                if (match.Groups[ExtensionQualityGroupName].Success)
                {
                    extensionIntervalQuality = GroupsExtensionQualities.FirstOrDefault(gq => match.Groups[gq.Key].Success).Value;
                }

                if (!ParsingUtilities.ParseInt(match, ExtensionNumberGroupName, -1, out extensionIntervalNumber) ||
                    extensionIntervalNumber < 5)
                {
                    return(ParsingResult.Error(ExtensionNumberIsOutOfRange));
                }
            }

            if (quality == Quality.HalfDiminished || quality == Quality.Dominant)
            {
                if (extensionIntervalNumber >= 0 && extensionIntervalNumber != 7)
                {
                    return(ParsingResult.Error(HalfDiminishedOrDominantIsNotSeventh));
                }

                if (extensionIntervalNumber < 0)
                {
                    extensionIntervalNumber = 7;
                }
            }

            var extensionNotes   = new List <NoteName>();
            var extensionNumbers = new List <int>();

            if (extensionIntervalNumber >= 0)
            {
                var extensionNotesDictionary = GetExtensionNotes(quality, rootNoteName, extensionIntervalNumber, extensionIntervalQuality);
                extensionNotes.AddRange(extensionNotesDictionary.Keys);
                extensionNumbers.AddRange(extensionNotesDictionary.Values);
            }

            if (quality == Quality.HalfDiminished)
            {
                quality = Quality.Diminished;
            }
            else if (quality == Quality.Dominant)
            {
                quality = Quality.Major;
            }

            if (quality == null)
            {
                quality = Quality.Major;
            }

            //

            var notes = new List <NoteName>(Chord.GetByTriad(rootNoteName, ChordQualities[quality.Value]).NotesNames);

            notes.AddRange(extensionNotes);
            extensionNumbers.InsertRange(0, new[] { 1, 3, 5 });
            if (extensionIntervalNumber == 5)
            {
                notes.Clear();
                notes.AddRange(new[] { rootNoteName, extensionNotes.First() });

                extensionNumbers.Clear();
                extensionNumbers.AddRange(new[] { 1, 5 });
            }

            //

            var alteredToneGroup = match.Groups[AlteredToneGroupName];

            if (alteredToneGroup.Success)
            {
                int alteredToneNumber;
                if (!ParsingUtilities.ParseInt(match, AlteredToneNumberGroupName, -1, out alteredToneNumber))
                {
                    return(ParsingResult.Error(AlteredToneNumberIsOutOfRange));
                }

                var transposeBy = 0;

                var accidental = match.Groups[AlteredToneAccidentalGroupName].Value;
                switch (accidental)
                {
                case "#":
                case "+":
                    transposeBy = 1;
                    break;

                case "b":
                case "-":
                    transposeBy = -1;
                    break;
                }

                var maxExtensionNumber = extensionNumbers.Max();
                if (maxExtensionNumber < alteredToneNumber)
                {
                    var extensionNotesDictionary = GetExtensionNotes(quality, rootNoteName, alteredToneNumber, null)
                                                   .Where(kv => kv.Value > maxExtensionNumber);

                    notes.AddRange(extensionNotesDictionary.Select(kv => kv.Key));
                    extensionNumbers.AddRange(extensionNotesDictionary.Select(kv => kv.Value));
                }

                var index = extensionNumbers.IndexOf(alteredToneNumber);
                if (index >= 0)
                {
                    notes[index] = notes[index].Transpose(Interval.FromHalfSteps(transposeBy));
                }
            }

            //

            var suspendedGroup = match.Groups[SuspendedGroupName];

            if (suspendedGroup.Success)
            {
                int suspensionNumber;
                if (!ParsingUtilities.ParseInt(match, SuspendedNumberGroupName, -1, out suspensionNumber) ||
                    suspensionNumber != 2 && suspensionNumber != 4)
                {
                    return(ParsingResult.Error(SuspensionNumberIsOutOfRange));
                }

                var interval = suspensionNumber == 2
                    ? Interval.Get(IntervalQuality.Major, 2)
                    : Interval.Get(IntervalQuality.Perfect, 4);
                notes[1] = rootNoteName.Transpose(interval);
            }

            //

            var addedToneGroup = match.Groups[AddedToneGroupName];

            if (addedToneGroup.Success)
            {
                int addedToneNumber;
                if (!ParsingUtilities.ParseInt(match, AddedToneNumberGroupName, -1, out addedToneNumber))
                {
                    return(ParsingResult.Error(AddedToneNumberIsOutOfRange));
                }

                var interval = Interval.IsPerfect(addedToneNumber)
                    ? Interval.Get(IntervalQuality.Perfect, addedToneNumber)
                    : Interval.Get(IntervalQuality.Major, addedToneNumber);
                notes.Add(rootNoteName.Transpose(interval));
            }

            //

            var bassNoteNameGroup = match.Groups[BassNoteNameGroupName];

            if (bassNoteNameGroup.Success)
            {
                NoteName bassNoteName;
                var      bassNoteNameParsingResult = NoteNameParser.TryParse(bassNoteNameGroup.Value, out bassNoteName);
                if (bassNoteNameParsingResult.Status != ParsingStatus.Parsed)
                {
                    return(bassNoteNameParsingResult);
                }

                notes.Insert(0, bassNoteName);
            }

            //

            chord = new Chord(notes);
            return(ParsingResult.Parsed);
        }