Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TimedEventsManager"/> with the specified events
        /// collection and comparison delegate for events that have same time.
        /// </summary>
        /// <param name="eventsCollection"><see cref="EventsCollection"/> that holds events to manage.</param>
        /// <param name="sameTimeEventsComparison">Delegate to compare events with the same absolute time.</param>
        /// <remarks>
        /// If the <paramref name="sameTimeEventsComparison"/> is not specified events with the same time
        /// will be placed into the underlying events collection in order of adding them through the manager.
        /// If you want to specify custom order of such events you need to specify appropriate comparison delegate.
        /// </remarks>
        /// <exception cref="ArgumentNullException"><paramref name="eventsCollection"/> is <c>null</c>.</exception>
        public TimedEventsManager(EventsCollection eventsCollection, Comparison <MidiEvent> sameTimeEventsComparison = null)
        {
            ThrowIfArgument.IsNull(nameof(eventsCollection), eventsCollection);

            _eventsCollection = eventsCollection;
            Events            = new TimedEventsCollection(eventsCollection.GetTimedEventsLazy(), sameTimeEventsComparison);
        }
        /// <summary>
        /// Performs the specified action on each <see cref="TimedEvent"/> contained in the <see cref="EventsCollection"/>.
        /// </summary>
        /// <param name="eventsCollection"><see cref="EventsCollection"/> to search for events to process.</param>
        /// <param name="action">The action to perform on each <see cref="TimedEvent"/> contained in the
        /// <paramref name="eventsCollection"/>.</param>
        /// <param name="match">The predicate that defines the conditions of the <see cref="TimedEvent"/> to process.</param>
        /// <returns>Count of processed timed events.</returns>
        /// <exception cref="ArgumentNullException">
        /// <para>One of the following errors occured:</para>
        /// <list type="bullet">
        /// <item>
        /// <description><paramref name="eventsCollection"/> is <c>null</c>.</description>
        /// </item>
        /// <item>
        /// <description><paramref name="action"/> is <c>null</c>.</description>
        /// </item>
        /// <item>
        /// <description><paramref name="match"/> is <c>null</c>.</description>
        /// </item>
        /// </list>
        /// </exception>
        public static int ProcessTimedEvents(this EventsCollection eventsCollection, Action <TimedEvent> action, Predicate <TimedEvent> match)
        {
            ThrowIfArgument.IsNull(nameof(eventsCollection), eventsCollection);
            ThrowIfArgument.IsNull(nameof(action), action);
            ThrowIfArgument.IsNull(nameof(match), match);

            var iMatched = 0;

            var timesChanged = false;
            var timedEvents  = new List <TimedEvent>(eventsCollection.Count);

            foreach (var timedEvent in eventsCollection.GetTimedEventsLazy(false))
            {
                if (match(timedEvent))
                {
                    var time = timedEvent.Time;
                    action(timedEvent);
                    timesChanged = timedEvent.Time != time;
                    iMatched++;
                }

                timedEvents.Add(timedEvent);
            }

            if (timesChanged)
            {
                eventsCollection.SortAndUpdateEvents(timedEvents);
            }

            return(iMatched);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Extracts objects of the specified types from a <see cref="EventsCollection"/>.
        /// </summary>
        /// <param name="eventsCollection"><see cref="EventsCollection"/> to extract objects from.</param>
        /// <param name="objectType">Combination of desired objects types.</param>
        /// <param name="settings">Settings according to which objects should be detected and built.</param>
        /// <returns>Collection of objects of the specified types extracted from <paramref name="eventsCollection"/>.
        /// Objects are ordered by time.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="eventsCollection"/> is <c>null</c>.</exception>
        public static ICollection <ITimedObject> GetObjects(
            this EventsCollection eventsCollection,
            ObjectType objectType,
            ObjectDetectionSettings settings = null)
        {
            ThrowIfArgument.IsNull(nameof(eventsCollection), eventsCollection);

            return(eventsCollection
                   .GetTimedEventsLazy()
                   .GetObjectsFromSortedTimedObjects(eventsCollection.Count / 2, objectType, settings));
        }
        /// <summary>
        /// Gets timed events contained in the specified <see cref="EventsCollection"/>.
        /// </summary>
        /// <param name="eventsCollection"><see cref="EventsCollection"/> to search for events.</param>
        /// <returns>Collection of timed events contained in <paramref name="eventsCollection"/> ordered by time.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="eventsCollection"/> is <c>null</c>.</exception>
        public static ICollection <TimedEvent> GetTimedEvents(this EventsCollection eventsCollection)
        {
            ThrowIfArgument.IsNull(nameof(eventsCollection), eventsCollection);

            var result = new List <TimedEvent>(eventsCollection.Count);

            foreach (var timedEvent in eventsCollection.GetTimedEventsLazy())
            {
                result.Add(timedEvent);
            }

            return(result);
        }