amads.algorithms.slice.window#
Sliding window algorithm.
The algorithm segments a musical score into fixed-size windows that can optionally overlap. Each window contains all notes that are sounding within its time boundaries.
- class amads.algorithms.slice.window.Window(time, size, align, candidate_notes, skip=0)[source]#
A fixed-size window of a musical score. candidate_notes that overlap with this interval are copied and clipped to fit within the window. Notes that overlap less than 1.0e-6 duration units (whether beats or seconds) are mostly excluded from the window to reduce numerical issues. An exception is made for notes that are so short that they do not overlap any window by at least 1.0e-6 duration units. (This seems far-fetched, but zero-length notes representing grace notes are one possibility to consider; there may be others.)
Additionoal details that you may not need: For very short notes, the window is considered closed on the left and open on the right, so that the window is considered to contain the note if it starts at the same time as the window, and a note is not in the window if it starts at the offset time of the window. To guarantee that zero-length notes are included in only one window, the offset of a window should be identical to the onset of the next window. sliding_window() takes care of this by default, but if the Window constructor is used directly where arithmetic with time and size is inexact, this may not be the case.
- Parameters:
time (float) – The reference time for this window
size (float) – The size of the window in time units
align (str) – How to align the window relative to the reference time: - “left”: window starts at reference time - “center”: reference time is at window center - “right”: window ends at reference time
candidate_notes (Iterable[Note]) – Notes to consider for inclusion in this window, sorted by onset time and pitch
skip (int, default=0) – Index to start searching from in candidate_notes. This is used to optimize performance when iterating through multiple windows - each window can tell the next window which notes it can safely skip because they end before the window starts.
- amads.algorithms.slice.window.sliding_window(passage, size, step=1.0, align='right', onset=0.0, offset=None, times=None) Iterator[Window] [source]#
Slice a score into (possibly overlapping) windows of a given size.
- Parameters:
passage (Score or Iterable[Note]) – The musical passage to be windowed
size (float) – The size (duration) of each window (time units)
step (float, default=1.0) – The step size to take between windows (time units). For example, if step is 0.1, then a given slice will start 0.1 time units after the previous slice started. Note that if step is smaller than size, successive windows will overlap
align (str, default="right") – Each generated window has a time property that points to a particular timepoint in the musical passage. The align parameter determines how the window is aligned to this timepoint: - “left”: the window starts at
window.time
- “center”:window.time
corresponds to the midpoint of the window - “right”: the window finishes atwindow.time
onset (float, default=0.0) – The desired time of the first window
offset (float, optional) – If set, the windowing will stop once the offset time is reached. Following the behaviour of Python’s built-in range function,
offset
is not treated inclusively, i.e. the last window will not includeoffset
. The returned iterator will stop early the last window is empty (i.e. contains no notes) and there are no more notes to process.times (Iterable[float], optional) – Optional iterable of times to generate windows for. If provided, onset and offset are ignored. The returned iterator will stop once all times have been processed or when an empty window is generated and there are no more notes to process.
- Returns:
Iterator over the windows
- Return type:
Iterator[Window]