Slice
The algorithms/slice directory contains software pertaining
to breaking scores into sequences of “vertical slices,” e.g,
the portions of all notes within a sequence of time intervals.
In the “Salami Slice” algorithm, time intervals are non-overlapping
and their boundaries are all note onset and offset times. A list of
slices can be created by calling the
salami_slice function.
The returned slices are instances of the
Slice class.
A more basic way to slice a score is to construct a
Window, which is basically
just a Slice with a special
constructor that selects and clips notes that fall within a given
time interval, resulting in a single slice. You can create a sequence
of Windows
using any criteria for time intervals, including overlapping windows.
salami
¶
Salami slice algorithm for segmenting musical scores.
This module implements the salami slice algorithm, which segments a musical score into vertical slices at each note onset and offset. Each slice contains all notes that are sounding at that point in time.
Author: Peter Harrison
Note: The algorithm is named after the way a salami sausage is sliced into thin, vertical segments.
Timepoint
dataclass
¶
Timepoint(
time: float,
note_ons: list[Note] = list(),
note_offs: list[Note] = list(),
sounding_notes: set[Note] = set(),
)
A point in time with associated note events.
Parameters:
salami_slice
¶
salami_slice(
passage: Union[Score, Iterable[Note]],
remove_duplicated_pitches: bool = True,
include_empty_slices: bool = False,
include_note_end_slices: bool = True,
min_slice_duration: float = 0.01,
) -> List[Slice]
Segment a musical passage into vertical slices at note onsets and offsets ('salami slices').
Parameters:
-
passage(Score or Iterable[Note]) –The musical passage to slice
-
remove_duplicated_pitches(bool, default:True) –Whether to remove duplicate pitches within each slice
-
include_empty_slices(bool, default:False) –Whether to include slices with no sounding notes
-
include_note_end_slices(bool, default:True) –Whether to create slices at note ends
-
min_slice_duration(float, default:0.01) –Minimum duration for a slice to be included
Returns:
-
List[Slice]–The sequence of vertical slices
Source code in amads/algorithms/slice/salami.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
Slice
¶
Slice(
original_notes: List[Note], onset: float = 0, duration: float = 0
)
Bases: Concurrence
A slice of a musical score between two timepoints.
This is the base class for different slicing algorithms like salami slicing and windowing. A slice contains a list of notes that are sounding between its start and end times, as well as references to the original notes from which these were derived.
Author: Peter Harrison
Parameters:
-
original_notes(List[Note]) –The original unmodified notes from which the slice notes were derived
-
onset(float, default:0) –The start time offset of the slice
-
duration(float, default:0) –The duration of the slice
Source code in amads/algorithms/slice/slice.py
26 27 28 29 30 31 32 33 34 35 | |
Window
¶
Window(
time: float,
size: float,
align: str,
candidate_notes: Iterable[Note],
skip: int = 0,
)
Bases: Slice
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.)
Additional 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.
Author: Peter Harrison
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, or "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.
Source code in amads/algorithms/slice/window.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
sliding_window
¶
sliding_window(
passage: Union[Score, Iterable[Note]],
size: float,
step: float = 1.0,
align: str = "right",
onset: float = 0.0,
offset: Optional[float] = None,
times: Optional[Iterable[float]] = None,
) -> Iterator[Window]
Slice a score into (possibly overlapping) windows of a given size.
Author: Peter Harrison
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
timeproperty that points to a particular timepoint in the musical passage. Thealignparameter determines how the window is aligned to this timepoint: - "left": the window starts atwindow.time- "center":window.timecorresponds 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, default:None) –If set, the windowing will stop once the offset time is reached. Following the behaviour of Python's built-in range function,
offsetis 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], default:None) –Optional iterable of times to generate windows for. If provided,
onsetandoffsetare 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[Window]–Iterator over the windows
Source code in amads/algorithms/slice/window.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |