/// <summary> /// Checks if an <see cref="ILengthedObject"/> is at the specified time. /// </summary> /// <typeparam name="TObject">Type of an object.</typeparam> /// <param name="obj"><see cref="ILengthedObject"/> to check.</param> /// <param name="time">Time to check the <paramref name="obj"/>.</param> /// <param name="matchBy">Part of the <paramref name="obj"/> which have to be at <paramref name="time"/>.</param> /// <returns>true if <paramref name="obj"/> is at <paramref name="time"/>; false - otherwise.</returns> /// <exception cref="ArgumentNullException"><paramref name="obj"/> is null.</exception> private static bool IsObjectAtTime <TObject>(TObject obj, long time, LengthedObjectPart matchBy) where TObject : ILengthedObject { var startTime = obj.Time; if (startTime == time && (matchBy == LengthedObjectPart.Start || matchBy == LengthedObjectPart.Entire)) { return(true); } var endTime = startTime + obj.Length; if (endTime == time && (matchBy == LengthedObjectPart.End || matchBy == LengthedObjectPart.Entire)) { return(true); } return(matchBy == LengthedObjectPart.Entire && time >= startTime && time <= endTime); }
/// <summary> /// Filters collection of <see cref="ILengthedObject"/> to return objects at the specified time. /// </summary> /// <typeparam name="TObject">The type of the elements of <paramref name="objects"/>.</typeparam> /// <param name="objects">A collection to filter.</param> /// <param name="time">Time to filter objects by.</param> /// <param name="tempoMap">Tempo map to filter <paramref name="objects"/> by <paramref name="time"/>.</param> /// <param name="matchBy">Part of an object which have to be at <paramref name="time"/>.</param> /// <returns>A collection that contains objects from the input sequence that are at the specified time.</returns> /// <remarks> /// Note that changes made on the objects returned by this method will not be saved to an underlying /// data source (events collection, track chunk, file). To change properties of lengthed objects and /// save them you need to use a manager appropriate for an object's type. /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="objects"/> is null. -or- <paramref name="time"/> is null. -or- /// <paramref name="tempoMap"/> is null. -or- One of the objects is null.</exception> /// <exception cref="InvalidEnumArgumentException"><paramref name="matchBy"/> specified an invalid value.</exception> public static IEnumerable <TObject> AtTime <TObject>(this IEnumerable <TObject> objects, ITime time, TempoMap tempoMap, LengthedObjectPart matchBy) where TObject : ILengthedObject { ThrowIfArgument.IsNull(nameof(objects), objects); ThrowIfArgument.IsNull(nameof(time), time); ThrowIfArgument.IsNull(nameof(tempoMap), tempoMap); ThrowIfArgument.IsInvalidEnumValue(nameof(matchBy), matchBy); var convertedTime = TimeConverter.ConvertFrom(time, tempoMap); return(AtTime(objects, convertedTime, matchBy)); }
/// <summary> /// Filters collection of <see cref="ILengthedObject"/> to return objects at the specified time. /// </summary> /// <typeparam name="TObject">The type of the elements of <paramref name="objects"/>.</typeparam> /// <param name="objects">A collection to filter.</param> /// <param name="time">Time to filter objects by.</param> /// <param name="matchBy">Part of an object which have to be at <paramref name="time"/>.</param> /// <returns>A collection that contains objects from the input sequence that are at the specified time.</returns> /// <remarks> /// Note that changes made on the objects returned by this method will not be saved to an underlying /// data source (events collection, track chunk, file). To change properties of lengthed objects and /// save them you need to use a manager appropriate for an object's type. /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="objects"/> is null. -or- One of the objects is null.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="time"/> is negative.</exception> /// <exception cref="InvalidEnumArgumentException"><paramref name="matchBy"/> specified an invalid value.</exception> public static IEnumerable <TObject> AtTime <TObject>(this IEnumerable <TObject> objects, long time, LengthedObjectPart matchBy) where TObject : ILengthedObject { ThrowIfArgument.IsNull(nameof(objects), objects); ThrowIfTimeArgument.IsNegative(nameof(time), time); ThrowIfArgument.IsInvalidEnumValue(nameof(matchBy), matchBy); return(objects.Where(o => o != null && IsObjectAtTime(o, time, matchBy))); }