예제 #1
0
        /// <summary>
        /// Determines whether the given note starts on a off-beat in the context of
        /// the given bar, or whether it starts on an on-beat.
        /// <para>
        /// The note is considered to be an off-beat if it does not start on a strong beat,
        /// i.e., it does not start in the beginning of the bar nor in the middle of the bar.
        /// </para>
        /// <para>
        /// Incase this method is intended to be called repeatedly through a bar,
        /// it is recommended  to use the more effecient analogous version of INote extension
        /// method, which carries out the same task, but saves calculations of data that is
        /// passed from the caller through additional parameters. Therefore the caller could
        /// calculate them once per bar and pass them on continuous calls on the individual
        /// notes.
        /// </para>
        /// For more information, check it out: <see cref="IsOffBeatNote(IBar, float, float?)"/>
        /// </summary>
        /// <param name="note"> The subject note in question.</param>
        /// <param name="contextBar"> The bar which contains the note in question.</param>
        /// <returns></returns>
        public static bool IsOffBeatNote(this INote note, IBar contextBar)
        {
            // init
            IDuration noteDuration;
            float     beatAccumulator = 0; // accumulates preceding notes length

            // find starting point in bar for the given note
            int noteIndex = contextBar.Notes.IndexOf(note);

            // assure the given note exists in the given context bar
            if (noteIndex < 0)
            {
                return(false);
            }

            // accumulate length of beats that precede the given note
            for (int i = 0; i < noteIndex; i++)
            {
                noteDuration     = contextBar.Notes[i].Duration;
                beatAccumulator += (float)noteDuration.Numerator / noteDuration.Denominator;
            }

            // delegate task to optimized method version
            return(contextBar.IsOffBeatNote(startingBeat: beatAccumulator));
        }