/// <summary> /// Initializes a new instance of the <see cref="TempoMapManager"/> with the /// specified time division. /// </summary> /// <param name="timeDivision">Time division of a new tempo that will be managed by this manager.</param> /// <exception cref="ArgumentNullException"><paramref name="timeDivision"/> is null.</exception> public TempoMapManager(TimeDivision timeDivision) { ThrowIfArgument.IsNull(nameof(timeDivision), timeDivision); TempoMap = new TempoMap(timeDivision); }
/// <summary> /// Gets name of the controller presented by an instance of <see cref="ControlChangeEvent"/>. /// </summary> /// <param name="controlChangeEvent">Control Change event to get controller name of.</param> /// <returns>Controller name of the <paramref name="controlChangeEvent"/> event.</returns> /// <exception cref="ArgumentNullException"><paramref name="controlChangeEvent"/> is null.</exception> public static ControlName GetControlName(this ControlChangeEvent controlChangeEvent) { ThrowIfArgument.IsNull(nameof(controlChangeEvent), controlChangeEvent); return(GetControlName(controlChangeEvent.ControlNumber)); }
/// <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 null.</exception> public static IEnumerable <TimedEvent> GetTimedEvents(this EventsCollection eventsCollection) { ThrowIfArgument.IsNull(nameof(eventsCollection), eventsCollection); return(eventsCollection.ManageTimedEvents().Events); }
/// <summary> /// Creates a MIDI file with the specified timed events. /// </summary> /// <param name="events">Collection of timed events to create a MIDI file.</param> /// <returns><see cref="MidiFile"/> containing the specified timed events.</returns> /// <exception cref="ArgumentNullException"><paramref name="events"/> is null.</exception> public static MidiFile ToFile(this IEnumerable <TimedEvent> events) { ThrowIfArgument.IsNull(nameof(events), events); return(new MidiFile(events.ToTrackChunk())); }
/// <summary> /// Creates an instance of the <see cref="TimedEventsManager"/> initializing it 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> /// <returns>An instance of the <see cref="TimedEventsManager"/> that can be used to manage /// events represented by the <paramref name="eventsCollection"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="eventsCollection"/> is null.</exception> public static TimedEventsManager ManageTimedEvents(this EventsCollection eventsCollection, Comparison <MidiEvent> sameTimeEventsComparison = null) { ThrowIfArgument.IsNull(nameof(eventsCollection), eventsCollection); return(new TimedEventsManager(eventsCollection, sameTimeEventsComparison)); }
/// <summary> /// Removes all the <see cref="TimedEvent"/> that match the conditions defined by the specified predicate. /// </summary> /// <param name="file"><see cref="MidiFile"/> to search for events to remove.</param> /// <param name="match">The predicate that defines the conditions of the <see cref="TimedEvent"/> to remove.</param> /// <exception cref="ArgumentNullException"><paramref name="file"/> is null.</exception> public static void RemoveTimedEvents(this MidiFile file, Predicate <TimedEvent> match = null) { ThrowIfArgument.IsNull(nameof(file), file); file.GetTrackChunks().RemoveTimedEvents(match); }
/// <summary> /// Gets points in time of the current grid. /// </summary> /// <param name="tempoMap">Tempo map used to get grid's times.</param> /// <returns>Collection of points in time of the current grid.</returns> /// <exception cref="ArgumentNullException"><paramref name="tempoMap"/> is null.</exception> public IEnumerable <long> GetTimes(TempoMap tempoMap) { ThrowIfArgument.IsNull(nameof(tempoMap), tempoMap); return(Times.Select(t => TimeConverter.ConvertFrom(t, tempoMap))); }
/// <summary> /// Adds objects to this collection. /// </summary> /// <param name="objects">Objects to add to the collection.</param> /// <exception cref="ArgumentNullException"><paramref name="objects"/> is <c>null</c>.</exception> public void Add(params TObject[] objects) { ThrowIfArgument.IsNull(nameof(objects), objects); Add((IEnumerable <TObject>)objects); }
/// <summary> /// Adds an event to the end of collection. /// </summary> /// <param name="midiEvent">The event to be added to the end of the collection.</param> /// <remarks> /// Note that End Of Track events cannot be added into the collection since it may cause inconsistence in a /// track chunk structure. End Of Track event will be written to the track chunk automatically on /// <see cref="MidiFile.Write(string, bool, MidiFileFormat, WritingSettings)"/>. /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="midiEvent"/> is null.</exception> public void Add(MidiEvent midiEvent) { ThrowIfArgument.IsNull(nameof(midiEvent), midiEvent); _events.Add(midiEvent); }
/// <summary> /// Adds events to the end of collection. /// </summary> /// <param name="events">Events to be added to the end of the collection.</param> /// <remarks> /// Note that End Of Track events cannot be added into the collection since it may cause inconsistence in a /// track chunk structure. End Of Track event will be written to the track chunk automatically on /// <see cref="MidiFile.Write(string, bool, MidiFileFormat, WritingSettings)"/>. /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="events"/> is null.</exception> public void AddRange(IEnumerable <MidiEvent> events) { ThrowIfArgument.IsNull(nameof(events), events); _events.AddRange(events.Where(e => e != null)); }
/// <summary> /// Searches for the specified event and returns the zero-based index of the first /// occurrence within the entire <see cref="EventsCollection"/>. /// </summary> /// <param name="midiEvent">The event to locate in the <see cref="EventsCollection"/>.</param> /// <returns>The zero-based index of the first occurrence of event within the entire /// <see cref="EventsCollection"/>, if found; otherwise, –1.</returns> /// <exception cref="ArgumentNullException"><paramref name="midiEvent"/> is null.</exception> public int IndexOf(MidiEvent midiEvent) { ThrowIfArgument.IsNull(nameof(midiEvent), midiEvent); return(_events.IndexOf(midiEvent)); }
/// <summary> /// Removes all the events that match the conditions defined by the specified predicate. /// </summary> /// <param name="match">The <see cref="Predicate{T}"/> delegate that defines the conditions /// of the events to remove.</param> /// <returns>The number of events removed from the <see cref="EventsCollection"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="match"/> is null.</exception> public int RemoveAll(Predicate <MidiEvent> match) { ThrowIfArgument.IsNull(nameof(match), match); return(_events.RemoveAll(match)); }
/// <summary> /// Removes the first occurrence of a specific event from the collection. /// </summary> /// <param name="midiEvent">The event to remove from the collection. The value cannot be null.</param> /// <returns>true if event is successfully removed; otherwise, false. This method also returns /// false if event was not found in the collection.</returns> /// <exception cref="ArgumentNullException"><paramref name="midiEvent"/> is null.</exception> public bool Remove(MidiEvent midiEvent) { ThrowIfArgument.IsNull(nameof(midiEvent), midiEvent); return(_events.Remove(midiEvent)); }
/// <summary> /// Writes a byte array to the underlying stream. /// </summary> /// <param name="bytes">A byte array containing the data to write.</param> /// <exception cref="ArgumentNullException"><paramref name="bytes"/> is null.</exception> /// <exception cref="ObjectDisposedException">Method was called after the writer was disposed.</exception> /// <exception cref="IOException">An I/O error occurred on the underlying stream.</exception> public void WriteBytes(byte[] bytes) { ThrowIfArgument.IsNull(nameof(bytes), bytes); _binaryWriter.Write(bytes); }
/// <summary> /// Subtracts a time span from the current one. /// </summary> /// <remarks> /// If <paramref name="timeSpan"/> and the current time span have the same type, /// the result time span will be of this type too; otherwise - of the <see cref="MathTimeSpan"/>. /// </remarks> /// <param name="timeSpan">Time span to subtract from the current one.</param> /// <param name="mode">Mode of the operation that defines meaning of time spans the /// operation will be performed on.</param> /// <returns>Time span that is a difference between the <paramref name="timeSpan"/> and the /// current time span.</returns> public ITimeSpan Subtract(ITimeSpan timeSpan, TimeSpanMode mode) { ThrowIfArgument.IsNull(nameof(timeSpan), timeSpan); return(TimeSpanUtilities.Subtract(this, timeSpan, mode)); }
/// <summary> /// Initializes a new instance of the <see cref="MusicalLength"/> with the specified /// fraction of the whole note length. /// </summary> /// <param name="fraction">Fraction of the whole note length.</param> /// <exception cref="ArgumentNullException"><paramref name="fraction"/> is null.</exception> public MusicalLength(Fraction fraction) { ThrowIfArgument.IsNull(nameof(fraction), fraction); Fraction = fraction; }
/// <summary> /// Iterates through the specified collection of <see cref="TimedEvent"/> returning /// <see cref="Note"/> for Note On/Note Off event pairs and original <see cref="TimedEvent"/> /// for all other events. /// </summary> /// <remarks> /// If there is no corresponding Note Off event for Note On (or if there is no correspinding /// Note On event for Note Off) the event will be returned as is. /// </remarks> /// <param name="timedEvents">Collection of <see cref="TimedEvent"/> to iterate over.</param> /// <returns>Collection of <see cref="ITimedObject"/> where an element either <see cref="TimedEvent"/> /// or <see cref="Note"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="timedEvents"/> is null.</exception> public static IEnumerable <ITimedObject> GetTimedEventsAndNotes(this IEnumerable <TimedEvent> timedEvents) { ThrowIfArgument.IsNull(nameof(timedEvents), timedEvents); var noteEventsDescriptors = new List <NoteEventsDescriptor>(); List <TimedEvent> eventsTail = null; foreach (var timedEvent in timedEvents) { var midiEvent = timedEvent?.Event; var noteOnEvent = midiEvent as NoteOnEvent; if (noteOnEvent != null) { noteEventsDescriptors.Add(new NoteEventsDescriptor(timedEvent, eventsTail = new List <TimedEvent>())); continue; } var noteOffEvent = midiEvent as NoteOffEvent; if (noteOffEvent != null) { var noteEventsDescriptor = noteEventsDescriptors.FirstOrDefault(d => d.IsCorrespondingNoteOffEvent(noteOffEvent)); if (noteEventsDescriptor != null) { noteEventsDescriptor.CompleteNote(timedEvent); if (noteEventsDescriptors.First() != noteEventsDescriptor) { continue; } for (int i = 0; i < noteEventsDescriptors.Count; i++) { var descriptor = noteEventsDescriptors[i]; if (!descriptor.IsNoteCompleted) { break; } foreach (var timedObject in descriptor.GetTimedObjects()) { yield return(timedObject); } noteEventsDescriptors.RemoveAt(i); i--; } if (!noteEventsDescriptors.Any()) { eventsTail = null; } continue; } } if (eventsTail != null) { eventsTail.Add(timedEvent); } else { yield return(timedEvent); } } foreach (var timedObject in noteEventsDescriptors.SelectMany(d => d.GetTimedObjects())) { yield return(timedObject); } }
/// <summary> /// Creates a MIDI file with the specified chords. /// </summary> /// <param name="chords">Collection of chords to create a MIDI file.</param> /// <returns><see cref="MidiFile"/> containing the specified chords.</returns> /// <exception cref="ArgumentNullException"><paramref name="chords"/> is null.</exception> public static MidiFile ToFile(this IEnumerable <Chord> chords) { ThrowIfArgument.IsNull(nameof(chords), chords); return(new MidiFile(chords.ToTrackChunk())); }
/// <summary> /// Iterates through the events contained in the specified collection of <see cref="TrackChunk"/> returning /// <see cref="Note"/> for Note On/Note Off event pairs and original <see cref="TimedEvent"/> /// for all other events. /// </summary> /// <remarks> /// If there is no corresponding Note Off event for Note On (or if there is no correspinding /// Note On event for Note Off) the event will be returned as is. /// </remarks> /// <param name="trackChunks"><see cref="TrackChunk"/> containing events to iterate over.</param> /// <returns>Collection of <see cref="ITimedObject"/> where an element either <see cref="TimedEvent"/> /// or <see cref="Note"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="trackChunks"/> is null.</exception> public static IEnumerable <ITimedObject> GetTimedEventsAndNotes(this IEnumerable <TrackChunk> trackChunks) { ThrowIfArgument.IsNull(nameof(trackChunks), trackChunks); return(trackChunks.GetTimedEvents().GetTimedEventsAndNotes()); }
/// <summary> /// Removes all the <see cref="TimedEvent"/> that match the conditions defined by the specified predicate. /// </summary> /// <param name="trackChunk"><see cref="TrackChunk"/> to search for events to remove.</param> /// <param name="match">The predicate that defines the conditions of the <see cref="TimedEvent"/> to remove.</param> /// <exception cref="ArgumentNullException"><paramref name="trackChunk"/> is null.</exception> public static void RemoveTimedEvents(this TrackChunk trackChunk, Predicate <TimedEvent> match = null) { ThrowIfArgument.IsNull(nameof(trackChunk), trackChunk); trackChunk.Events.RemoveTimedEvents(match); }
/// <summary> /// Iterates through the events contained in the specified <see cref="MidiFile"/> returning /// <see cref="Note"/> for Note On/Note Off event pairs and original <see cref="TimedEvent"/> /// for all other events. /// </summary> /// <remarks> /// If there is no corresponding Note Off event for Note On (or if there is no correspinding /// Note On event for Note Off) the event will be returned as is. /// </remarks> /// <param name="midiFile"><see cref="MidiFile"/> containing events to iterate over.</param> /// <returns>Collection of <see cref="ITimedObject"/> where an element either <see cref="TimedEvent"/> /// or <see cref="Note"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="midiFile"/> is null.</exception> public static IEnumerable <ITimedObject> GetTimedEventsAndNotes(this MidiFile midiFile) { ThrowIfArgument.IsNull(nameof(midiFile), midiFile); return(midiFile.GetTimedEvents().GetTimedEventsAndNotes()); }
/// <summary> /// Creates an instance of the <see cref="TimedEventsManager"/> initializing it with the /// events collection of the specified track chunk and comparison delegate for events /// that have same time. /// </summary> /// <param name="trackChunk"><see cref="TrackChunk"/> that holds events to manage.</param> /// <param name="sameTimeEventsComparison">Delegate to compare events with the same absolute time.</param> /// <returns>An instance of the <see cref="TimedEventsManager"/> that can be used to manage /// events represented by the <paramref name="trackChunk"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="trackChunk"/> is null.</exception> public static TimedEventsManager ManageTimedEvents(this TrackChunk trackChunk, Comparison <MidiEvent> sameTimeEventsComparison = null) { ThrowIfArgument.IsNull(nameof(trackChunk), trackChunk); return(trackChunk.Events.ManageTimedEvents(sameTimeEventsComparison)); }
/// <summary> /// Removes all changes of tempo that occured since the specified time. /// </summary> /// <param name="startTime">Time to remove changes of tempo since.</param> /// <exception cref="ArgumentNullException"><paramref name="startTime"/> is null.</exception> public void ClearTempo(ITimeSpan startTime) { ThrowIfArgument.IsNull(nameof(startTime), startTime); ClearTempo(TimeConverter.ConvertFrom(startTime, TempoMap)); }
/// <summary> /// Gets timed events contained in the specified <see cref="TrackChunk"/>. /// </summary> /// <param name="trackChunk"><see cref="TrackChunk"/> to search for events.</param> /// <returns>Collection of timed events contained in <paramref name="trackChunk"/> ordered by time.</returns> /// <exception cref="ArgumentNullException"><paramref name="trackChunk"/> is null.</exception> public static IEnumerable <TimedEvent> GetTimedEvents(this TrackChunk trackChunk) { ThrowIfArgument.IsNull(nameof(trackChunk), trackChunk); return(trackChunk.Events.GetTimedEvents()); }
/// <summary> /// Gets timed events contained in the specified <see cref="MidiFile"/>. /// </summary> /// <param name="file"><see cref="MidiFile"/> to search for events.</param> /// <returns>Collection of timed events contained in <paramref name="file"/> ordered by time.</returns> /// <exception cref="ArgumentNullException"><paramref name="file"/> is null.</exception> public static IEnumerable <TimedEvent> GetTimedEvents(this MidiFile file) { ThrowIfArgument.IsNull(nameof(file), file); return(file.GetTrackChunks().GetTimedEvents()); }