/// <summary> /// Takes part of the specified length of a MIDI file (starting at the beginning of the file) /// and returns it as an instance of <see cref="MidiFile"/>. /// </summary> /// <param name="midiFile"><see cref="MidiFile"/> to take part of.</param> /// <param name="partLength">The length of part to take.</param> /// <param name="settings">Settings according to which <paramref name="midiFile"/> /// should be splitted.</param> /// <returns><see cref="MidiFile"/> which is part of the <paramref name="midiFile"/>.</returns> /// <exception cref="ArgumentNullException"> /// <para>One of the following errors occured:</para> /// <list type="bullet"> /// <item> /// <description><paramref name="midiFile"/> is <c>null</c>.</description> /// </item> /// <item> /// <description><paramref name="partLength"/> is <c>null</c>.</description> /// </item> /// </list> /// </exception> public static MidiFile TakePart(this MidiFile midiFile, ITimeSpan partLength, SliceMidiFileSettings settings = null) { ThrowIfArgument.IsNull(nameof(midiFile), midiFile); ThrowIfArgument.IsNull(nameof(partLength), partLength); var grid = new ArbitraryGrid(partLength); settings = settings ?? new SliceMidiFileSettings(); midiFile = PrepareMidiFileForSlicing(midiFile, grid, settings); var tempoMap = midiFile.GetTempoMap(); var time = grid.GetTimes(tempoMap).First(); using (var slicer = MidiFileSlicer.CreateFromFile(midiFile)) { return(slicer.GetNextSlice(time, settings)); } }
/// <summary> /// Splits <see cref="MidiFile"/> by the specified grid. /// </summary> /// <remarks> /// Non-track chunks will not be copied to any of the new files. /// </remarks> /// <param name="midiFile"><see cref="MidiFile"/> to split.</param> /// <param name="grid">Grid to split <paramref name="midiFile"/> by.</param> /// <param name="settings">Settings according to which file should be splitted.</param> /// <returns>Collection of <see cref="MidiFile"/> produced during splitting the input file by grid.</returns> /// <exception cref="ArgumentNullException"> /// <para>One of the following errors occured:</para> /// <list type="bullet"> /// <item> /// <description><paramref name="midiFile"/> is <c>null</c>.</description> /// </item> /// <item> /// <description><paramref name="grid"/> is <c>null</c>.</description> /// </item> /// </list> /// </exception> public static IEnumerable <MidiFile> SplitByGrid(this MidiFile midiFile, IGrid grid, SliceMidiFileSettings settings = null) { ThrowIfArgument.IsNull(nameof(midiFile), midiFile); ThrowIfArgument.IsNull(nameof(grid), grid); if (!midiFile.GetEvents().Any()) { yield break; } settings = settings ?? new SliceMidiFileSettings(); midiFile = PrepareMidiFileForSlicing(midiFile, grid, settings); var tempoMap = midiFile.GetTempoMap(); using (var slicer = MidiFileSlicer.CreateFromFile(midiFile)) { foreach (var time in grid.GetTimes(tempoMap)) { if (time == 0) { continue; } yield return(slicer.GetNextSlice(time, settings)); if (slicer.AllEventsProcessed) { break; } } if (!slicer.AllEventsProcessed) { yield return(slicer.GetNextSlice(long.MaxValue, settings)); } } }