Classes Representing Basic Score Elements¶
basics
¶
Basic Symbolic Music Representation Classes
from amads.core import *
Note: amads.core includes amads.core.basics, amads.core.distribution
and amads.core.timemap.
Overview¶
The basic hierarchy of a score is described here.
Author: Roger B. Dannenberg
Constructor Details
The safe way to construct a score is to fully specify onsets for every Event. These onsets are absolute and will not be adjusted provided that the parent onset is also specified.
However, for convenience and to support simple constructs such as
Chord(Note(pitch=60), Note(pitch=64)),
onsets are optional and default to None. To make this simple example work:
-
Concurrences (Score, Part, and Chord) replace unspecified (None) onsets in their immediate content with the parent's onset (or 0 if it is None).
-
Sequences (Staff, Measure) replace unspecified (None) onsets in their immediate content starting with the parent's onset (or 0 if None) for the first event and the offset of the previous Event for subsequent events.
-
To handle the construction of nested Events, when an unspecified (None) onset of an EventGroup is replaced, the entire subtree of its content is shifted by the same amount. E.g. if a Chord is constructed with Notes with unspecified onsets, the Notes onsets will initially be replaced with zeros. Then, if the Chord onset is unspecified (None) and the Chord is passed in the content of a Measure and the Chord onset is replaced with 1.0, then the Notes are shifted to 1.0. If the Measure is then passed in the content of a Staff, the Measure and all its content might be shifted again.
Author: Roger Dannenberg
Event
¶
Event(
parent: Optional[EventGroup],
onset: Optional[float],
duration: float,
)
A superclass for Note, Rest, EventGroup, and anything happening in time.
Parameters:
-
parent(Optional[EventGroup]) –The containing object or None.
-
onset(float | None) –The onset (start) time. This can be an “idealized” time for a symbolic score or an actual “real” time from a performance. Default is None.
-
duration(float) –The duration of the event in quarters or seconds. This can be zero for objects such as key signatures or time signatures.
Attributes:
-
parent(Optional[Event]) –The containing object or None.
-
_onset(float | None) –The onset (start) time.
-
_duration(float) –The duration of the (untied) event in quarters or seconds.
-
info(Optional[Dict]) –Additional attribute/value information.
Source code in amads/core/basics.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
Attributes¶
onset
property
writable
¶
onset: float
Retrieve the onset (start) time.
If the onset is None, raise an exception. (Events can have None onset times, but they must be set before retrieval. onsets that are None are automatically set when the Event is added to an EventGroup.)
Returns:
-
float | None–The onset (start) time.
Raises:
-
ValueError–If the onset time is not set (None).
duration
property
writable
¶
duration: float
Retrieve the duration of the event.
Returns:
-
float–The duration of the event in quarters or seconds.
units_are_seconds
property
¶
units_are_seconds: bool
Check if the times are in seconds.
This event must be in a Score (where _units_are_seconds is stored).
Returns:
-
bool–True iff the event's times are in seconds. If not in a score, False is returned.
units_are_quarters
property
¶
units_are_quarters: bool
Check if the times are in quarters.
This event must be in a Score (where _units_are_seconds is stored).
Returns:
-
bool–True iff the event's times are in quarters. If not in a score, False is returned.
offset
property
writable
¶
offset: float
Retrieve the global offset (stop) time.
Returns:
-
float–The global offset (stop) time.
part
property
¶
part: Optional[Part]
Retrieve the Part containing this event.
Returns:
-
Optional[Part]–The Part containing this event or None if not found.
score
property
¶
score: Optional[Score]
Retrieve the Score containing this event, or self if it is a score.
Returns:
-
Optional[Score]–The Score containing this event or None if not found.
staff
property
¶
staff: Optional[Staff]
Retrieve the Staff containing this event
Returns:
-
Optional[Staff]–The Staff containing this event or None if not found.
measure
property
¶
measure: Optional[Measure]
Retrieve the Measure containing this event
Returns:
-
Optional[Measure]–The Measure containing this event or None if not found.
Functions¶
set
¶
set(property: str, value: Any) -> Event
Set a named property on this Event.
Every event can be extended with additional properties. Although
Python objects are already extensible with new attributes, new
attributes that are not set in __init__ confuse type checkers
and other tools, so every Event has an info attribute as a
dictionary where additional, application-specific information can
be stored. The info attribute is None to save space until the
first property is set, so you should use set and get methods
and avoid writing event.info[property].
Parameters:
-
property(str) –The name of the property to set.
-
value(Any) –The value to assign to the property.
Returns:
-
Event–returns this object (self)
Examples:
>>> note = Note()
>>> note.get("color", "no color")
'no color'
>>> _ = note.set("color", "red").set("harmonicity", 0.2)
>>> (note.has("color"), note.has("shape"))
(True, False)
>>> (note.get("color"), note.get("harmonicity"))
('red', 0.2)
Source code in amads/core/basics.py
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 | |
get
¶
get(property: str, default: Any = None) -> Any
Get the value of a property from this Event.
See set for more details.
Parameters:
-
property(str.) –The name of the property to get.
-
default(Any, default:None) –The default value to return if the property is not found.
Returns:
-
Any–The value of the specified property.
Source code in amads/core/basics.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
has
¶
has(property) -> bool
Check if the Event has a specific property.
See set for more details.
Parameters:
-
property(str) –The name of the property to check.
Returns:
-
bool–True if the property exists, False otherwise.
Source code in amads/core/basics.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
time_shift
¶
time_shift(increment: float, content_only=False) -> Event
Change the onset by an increment.
Parameters:
-
increment(float) –The time increment (in quarters or seconds).
-
content_only(bool, default:False) –This parameter is ignored. (See EventGroup.time_shift)
Returns:
-
Event–The object. This method modifies the
Event.
Source code in amads/core/basics.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
insert_copy_into
¶
insert_copy_into(parent: Optional[EventGroup] = None) -> Event
Make a (mostly) deep copy of the Event and add to a new parent.
Pitch objects are considered immutable and are shared rather
than copied.
Parameters:
-
parent(Optional(EventGroup), default:None) –The copied
Eventwill be a child ofparentif notNone. The parent is modified by this operation.
Returns:
-
Event–A deep copy (except for parent and pitch) of the Event instance.
Source code in amads/core/basics.py
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 | |
Note
¶
Note(
parent: Optional[EventGroup] = None,
onset: Optional[float] = None,
duration: float = 1.0,
pitch: Union[Pitch, int, float, str, None] = 60,
dynamic: Union[int, str, None] = None,
lyric: Optional[str] = None,
)
Bases: Event
Note represents a musical note.
A Note is normally an element of a Measure in a full score,
and an element of a Part in a flat score.
Parameters:
-
parent(Optional[EventGroup], default:None) –The containing object or None.
-
onset(float, default:None) –The onset (start) time. If None (default) is specified, a default onset will be calculated when the Note is inserted into an EventGroup.
-
duration(float, default:1.0) –The duration of the note in quarters or seconds.
-
pitch(Union[Pitch, int, float], default:60) –A Pitch object or an integer MIDI key number that will be converted to a Pitch object. The default (60) represents middle C.
-
dynamic(Optional[Union[int, str]], default:None) –Dynamic level (integer MIDI velocity or arbitrary string).
-
lyric(Optional[str], default:None) –Lyric text.
Attributes:
-
parent(Optional[EventGroup]) –The containing object or None.
-
_onset(Optional[float]) –The onset (start) time. None represents an unspecified onset.
-
duration(float) –The duration of the note in quarters or seconds, including all notes in a sequence of tied notes. See the
tieproperty. Use_durationdirectly for the duration of this note without any ties. -
pitch(Pitch | None) –The pitch of the note. Unpitched notes have a pitch of None.
-
dynamic(Optional[Union[int, str]]) –Dynamic level (integer MIDI velocity or arbitrary string).
-
lyric(Optional[str]) –Lyric text.
-
tie(Optional[Note]) –The note that this note is tied to, if any.
Grace Notes
A grace note is indicated by the property "is_grace" which can be
accessed using .get("is_grace", False). If true, then you can also
query for "has_slash" using .get("has_slash", False). If true,
this is an acciaccatura; if false, this is an appoggiatura. Note
that Music21 incorrectly (at present) defaults to adding a slash
when it reads a MusicXML grace tag with no slash attribute, and
the acciaccatura/appoggiatura distinction is often ambiguous or
incorrect to begin with.
Other Expressions
Other Note properties are set as follows: "has_trill" and
"trill_pitch" are set for Music21 Trill ("trill_pitch" has an
AMADS Pitch object as its value); "has_trill_extension" is
set for TrillExtension. "has_turn" and "turn_pitches" are set
for Turn expressions ("turn_pitches" consists of a list with the
upper pitch and the lower pitch as AMADS Pitch objects);
"has_inverted_turn" and "inverted_turn_pitches" are set for
InvertedTurn expressions ("inverted_turn_pitches" consists of a
list with the lower pitch and the upper pitch as AMADS Pitch objects);
"has_mordent" and "mordent_pitch" are set for Mordent
expressions ("mordent_pitch" consists of the upper pitch as an
AMADS Pitch object); "has_inverted_mordent" and
"inverted_mordent_pitch" are set for InvertedMordent expressions
("inverted_mordent_pitch" consists of the upper pitch as an
AMADS Pitch object); "has_shake" is set for Shake expressions;
and "has_schleifer" is set for Schleifer expressions.
Source code in amads/core/basics.py
806 807 808 809 810 811 812 813 814 815 816 817 818 819 | |
Attributes¶
onset
property
writable
¶
onset: float
Retrieve the onset (start) time.
If the onset is None, raise an exception. (Events can have None onset times, but they must be set before retrieval. onsets that are None are automatically set when the Event is added to an EventGroup.)
Returns:
-
float | None–The onset (start) time.
Raises:
-
ValueError–If the onset time is not set (None).
units_are_seconds
property
¶
units_are_seconds: bool
Check if the times are in seconds.
This event must be in a Score (where _units_are_seconds is stored).
Returns:
-
bool–True iff the event's times are in seconds. If not in a score, False is returned.
units_are_quarters
property
¶
units_are_quarters: bool
Check if the times are in quarters.
This event must be in a Score (where _units_are_seconds is stored).
Returns:
-
bool–True iff the event's times are in quarters. If not in a score, False is returned.
offset
property
writable
¶
offset: float
Retrieve the global offset (stop) time.
Returns:
-
float–The global offset (stop) time.
part
property
¶
part: Optional[Part]
Retrieve the Part containing this event.
Returns:
-
Optional[Part]–The Part containing this event or None if not found.
score
property
¶
score: Optional[Score]
Retrieve the Score containing this event, or self if it is a score.
Returns:
-
Optional[Score]–The Score containing this event or None if not found.
staff
property
¶
staff: Optional[Staff]
Retrieve the Staff containing this event
Returns:
-
Optional[Staff]–The Staff containing this event or None if not found.
measure
property
¶
measure: Optional[Measure]
Retrieve the Measure containing this event
Returns:
-
Optional[Measure]–The Measure containing this event or None if not found.
duration
property
writable
¶
duration: Union[float, int]
Retrieve the (tied) duration of the note in quarters or seconds.
If the note is the first note of a sequence of tied notes,
return the duration of the entire sequence. However, if there are
preceding notes tied to this note, they will not be considered
part of the duration. In some cases, notes can be tied across
staves, in which case even find_all(Note) and list_all(Note)
can fail. merge_tied_notes()
handles this case properly.
Returns:
-
float–The duration of the note and those it is tied to directly or indirectly, in quarters or seconds. The sum of durations is returned without checking whether notes are contiguous.
step
property
¶
step: str
Retrieve the name of the pitch without accidental, e.g., "G".
If the note is unpitched (pitch is None), return the empty string.
name
property
¶
name: str
Retrieve the name of the pitch with accidental, e.g., "Bb".
If the note is unpitched (pitch is None), return the empty string.
name_with_octave
property
¶
name_with_octave: str
Retrieve the name of the pitch with octave, e.g., A4 or Bb3.
If the note is unpitched (pitch is None), return the empty string.
pitch_class
property
writable
¶
pitch_class: int
Retrieve the pitch class of the note, e.g., 0, 1, 2, ..., 11.
If the note is unpitched (pitch is None), raise ValueError.
octave
property
writable
¶
octave: int
Retrieve the octave number of the note.
The note name is based on key_num - alt, e.g., C4 has
octave 4 while B#3 has octave 3. See also Pitch.register.
If the note is unpitched (pitch is None), raise ValueError.
If the note is unpitched (pitch is None), raise ValueError.
Returns:
-
int–The octave number of the note.
key_num
property
¶
key_num: float | int
Retrieve the MIDI key number of the note, e.g., C4 = 60.
If the note is unpitched (pitch is None), raise ValueError.
Returns:
-
int–The MIDI key number of the note.
Functions¶
set
¶
set(property: str, value: Any) -> Event
Set a named property on this Event.
Every event can be extended with additional properties. Although
Python objects are already extensible with new attributes, new
attributes that are not set in __init__ confuse type checkers
and other tools, so every Event has an info attribute as a
dictionary where additional, application-specific information can
be stored. The info attribute is None to save space until the
first property is set, so you should use set and get methods
and avoid writing event.info[property].
Parameters:
-
property(str) –The name of the property to set.
-
value(Any) –The value to assign to the property.
Returns:
-
Event–returns this object (self)
Examples:
>>> note = Note()
>>> note.get("color", "no color")
'no color'
>>> _ = note.set("color", "red").set("harmonicity", 0.2)
>>> (note.has("color"), note.has("shape"))
(True, False)
>>> (note.get("color"), note.get("harmonicity"))
('red', 0.2)
Source code in amads/core/basics.py
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 | |
get
¶
get(property: str, default: Any = None) -> Any
Get the value of a property from this Event.
See set for more details.
Parameters:
-
property(str.) –The name of the property to get.
-
default(Any, default:None) –The default value to return if the property is not found.
Returns:
-
Any–The value of the specified property.
Source code in amads/core/basics.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
has
¶
has(property) -> bool
Check if the Event has a specific property.
See set for more details.
Parameters:
-
property(str) –The name of the property to check.
Returns:
-
bool–True if the property exists, False otherwise.
Source code in amads/core/basics.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
time_shift
¶
time_shift(increment: float, content_only=False) -> Event
Change the onset by an increment.
Parameters:
-
increment(float) –The time increment (in quarters or seconds).
-
content_only(bool, default:False) –This parameter is ignored. (See EventGroup.time_shift)
Returns:
-
Event–The object. This method modifies the
Event.
Source code in amads/core/basics.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
insert_copy_into
¶
insert_copy_into(parent: Optional[EventGroup] = None) -> Event
Make a (mostly) deep copy of the Event and add to a new parent.
Pitch objects are considered immutable and are shared rather
than copied.
Parameters:
-
parent(Optional(EventGroup), default:None) –The copied
Eventwill be a child ofparentif notNone. The parent is modified by this operation.
Returns:
-
Event–A deep copy (except for parent and pitch) of the Event instance.
Source code in amads/core/basics.py
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 | |
show
¶
show(
indent: int = 0, file: Optional[TextIO] = None, tied: bool = False
) -> Note
Print note information.
Output includes pitch name, onset, duration, and optional tie, dynamic, and lyric information.
Parameters:
-
indent(int, default:0) –The indentation level for display.
-
tied(bool, default:False) –Include information about ties.
Returns:
-
Note–The Note instance itself.
Source code in amads/core/basics.py
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 | |
enharmonic
¶
enharmonic() -> Pitch
Return a Pitch representing the enharmonic.
The enharmonic Pitch's alt will be zero or have the opposite
sign such that alt is minimized. E.g., the enharmonic of
C-double-flat is A-sharp (not B-flat). If alt is zero, return
a Pitch with alt of +1 or -1 if possible. Otherwise, return a
Pitch with alt of -2.
If the note is unpitched (pitch is None), raise ValueError.
If the note is unpitched (pitch is None), raise ValueError.
Returns:
-
Pitch–A Pitch object representing the enharmonic equivalent of the note.
Source code in amads/core/basics.py
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 | |
upper_enharmonic
¶
upper_enharmonic() -> Pitch
Return a valid enharmonic Pitch with alt decreased, e.g., C#->Db.
It follows that the alt is decreased by 1 or 2, e.g., C###
(with alt = +3) becomes D# (with alt = +1).
If the note is unpitched (pitch is None), raise ValueError.
If the note is unpitched (pitch is None), raise ValueError.
Returns:
-
Pitch–A Pitch object representing the upper enharmonic equivalent of the note.
Source code in amads/core/basics.py
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 | |
lower_enharmonic
¶
lower_enharmonic() -> Pitch
Return a valid enharmonic Pitch with alt increased, e.g., Db->C#.
It follows that the alt is increased by 1 or 2, e.g., D#
(with alt = +1) becomes C### (with alt = +3).
If the note is unpitched (pitch is None), raise ValueError.
If the note is unpitched (pitch is None), raise ValueError.
Returns:
-
Pitch–A Pitch object representing the lower enharmonic equivalent of the note.
Source code in amads/core/basics.py
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 | |
simplest_enharmonic
¶
simplest_enharmonic(sharp_or_flat: Optional[str] = 'default') -> Pitch
Return a valid Pitch with the simplest enharmonic representation.
(See [simplest_enharmonic] [amads.core.pitch.Pitch.simplest_enharmonic].)
Parameters:
-
sharp_or_flat(Optional[str], default:'default') –This is only relevant if the pitch needs an alteration, otherwise it is unused. The value can be "sharp" (use sharps), "flat" (use flats), and otherwise use the same enharmonic choice as the Pitch constructor.
Exceptions
If the note is unpitched (pitch is None), raise ValueError.
Returns:
-
Pitch–A Pitch object representing the enharmonic equivalent.
Source code in amads/core/basics.py
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 | |
Rest
¶
Rest(
parent: Optional[EventGroup] = None,
onset: Optional[float] = None,
duration: float = 1,
)
Bases: Event
Rest represents a musical rest.
A Rest is normally an element of a Measure.
Parameters:
-
parent(Optional[EventGroup], default:None) –The containing object or None.
-
onset(float, default:None) –The onset (start) time. An initial value of None might be assigned when the Note is inserted into an EventGroup.
-
duration(float, default:1) –The duration of the rest in quarters or seconds.
Attributes:
-
parent(Optional[EventGroup]) –The containing object or None.
-
_onset(float) –The onset (start) time. None represents an unspecified onset.
-
duration(float) –The duration of the rest in quarters or seconds.
Source code in amads/core/basics.py
697 698 699 | |
Attributes¶
onset
property
writable
¶
onset: float
Retrieve the onset (start) time.
If the onset is None, raise an exception. (Events can have None onset times, but they must be set before retrieval. onsets that are None are automatically set when the Event is added to an EventGroup.)
Returns:
-
float | None–The onset (start) time.
Raises:
-
ValueError–If the onset time is not set (None).
duration
property
writable
¶
duration: float
Retrieve the duration of the event.
Returns:
-
float–The duration of the event in quarters or seconds.
units_are_seconds
property
¶
units_are_seconds: bool
Check if the times are in seconds.
This event must be in a Score (where _units_are_seconds is stored).
Returns:
-
bool–True iff the event's times are in seconds. If not in a score, False is returned.
units_are_quarters
property
¶
units_are_quarters: bool
Check if the times are in quarters.
This event must be in a Score (where _units_are_seconds is stored).
Returns:
-
bool–True iff the event's times are in quarters. If not in a score, False is returned.
offset
property
writable
¶
offset: float
Retrieve the global offset (stop) time.
Returns:
-
float–The global offset (stop) time.
part
property
¶
part: Optional[Part]
Retrieve the Part containing this event.
Returns:
-
Optional[Part]–The Part containing this event or None if not found.
score
property
¶
score: Optional[Score]
Retrieve the Score containing this event, or self if it is a score.
Returns:
-
Optional[Score]–The Score containing this event or None if not found.
staff
property
¶
staff: Optional[Staff]
Retrieve the Staff containing this event
Returns:
-
Optional[Staff]–The Staff containing this event or None if not found.
measure
property
¶
measure: Optional[Measure]
Retrieve the Measure containing this event
Returns:
-
Optional[Measure]–The Measure containing this event or None if not found.
Functions¶
set
¶
set(property: str, value: Any) -> Event
Set a named property on this Event.
Every event can be extended with additional properties. Although
Python objects are already extensible with new attributes, new
attributes that are not set in __init__ confuse type checkers
and other tools, so every Event has an info attribute as a
dictionary where additional, application-specific information can
be stored. The info attribute is None to save space until the
first property is set, so you should use set and get methods
and avoid writing event.info[property].
Parameters:
-
property(str) –The name of the property to set.
-
value(Any) –The value to assign to the property.
Returns:
-
Event–returns this object (self)
Examples:
>>> note = Note()
>>> note.get("color", "no color")
'no color'
>>> _ = note.set("color", "red").set("harmonicity", 0.2)
>>> (note.has("color"), note.has("shape"))
(True, False)
>>> (note.get("color"), note.get("harmonicity"))
('red', 0.2)
Source code in amads/core/basics.py
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 | |
get
¶
get(property: str, default: Any = None) -> Any
Get the value of a property from this Event.
See set for more details.
Parameters:
-
property(str.) –The name of the property to get.
-
default(Any, default:None) –The default value to return if the property is not found.
Returns:
-
Any–The value of the specified property.
Source code in amads/core/basics.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
has
¶
has(property) -> bool
Check if the Event has a specific property.
See set for more details.
Parameters:
-
property(str) –The name of the property to check.
Returns:
-
bool–True if the property exists, False otherwise.
Source code in amads/core/basics.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
time_shift
¶
time_shift(increment: float, content_only=False) -> Event
Change the onset by an increment.
Parameters:
-
increment(float) –The time increment (in quarters or seconds).
-
content_only(bool, default:False) –This parameter is ignored. (See EventGroup.time_shift)
Returns:
-
Event–The object. This method modifies the
Event.
Source code in amads/core/basics.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
insert_copy_into
¶
insert_copy_into(parent: Optional[EventGroup] = None) -> Event
Make a (mostly) deep copy of the Event and add to a new parent.
Pitch objects are considered immutable and are shared rather
than copied.
Parameters:
-
parent(Optional(EventGroup), default:None) –The copied
Eventwill be a child ofparentif notNone. The parent is modified by this operation.
Returns:
-
Event–A deep copy (except for parent and pitch) of the Event instance.
Source code in amads/core/basics.py
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 | |
show
¶
show(indent: int = 0, file: Optional[TextIO] = None) -> Rest
Display the Rest information.
Parameters:
-
indent(int, default:0) –The indentation level for display.
Returns:
-
Rest–The Rest instance itself.
Source code in amads/core/basics.py
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 | |
Measure
¶
Measure(
*args: Event,
parent: Optional[EventGroup] = None,
onset: Optional[float] = None,
duration: float = 4,
number: Optional[str] = None
)
Bases: Sequence
A Measure models a musical measure (bar).
A Measure can contain many object types including Note, Rest, Chord, and (in theory) custom Events. Measures are elements of a Staff.
The duration of a measure may differ from the prevailing time signature.
See Constructor Details.
Parameters:
-
*args(Event, default:()) –A variable number of Event objects to be added to the group.
-
parent(Optional[EventGroup], default:None) –The containing object or None. Must be passed as a keyword parameter due to
*args. -
onset(Optional[float], default:None) –The onset (start) time. None means unknown, to be set when Sequence is added to a parent. Must be passed as a keyword parameter due to
*args. -
duration(Optional[float], default:4) –The duration in quarters or seconds. Must be passed as a keyword parameter due to
*args. -
number(Optional[str], default:None) –A string representing the measure number. Must be passed as a keyword parameter due to
*args.
Attributes:
-
parent(Optional[EventGroup]) –The containing object or None.
-
_onset(Optional[float]) –The onset (start) time. None represents "unknown" and to be determined when this object is added to a parent.
-
duration(float) –The duration in quarters or seconds.
-
content(list[Event]) –Elements contained within this Measure.
-
number(Optional[str]) –A string representing the measure number if any.
Source code in amads/core/basics.py
2980 2981 2982 2983 2984 | |
Attributes¶
onset
property
writable
¶
onset: float
Retrieve the onset (start) time.
If the onset is None, raise an exception. (Events can have None onset times, but they must be set before retrieval. onsets that are None are automatically set when the Event is added to an EventGroup.)
Returns:
-
float–The onset (start) time.
Raises:
-
ValueError–If the onset time is not set (None).
duration
property
writable
¶
duration: float
Retrieve the duration of the event.
Returns:
-
float–The duration of the event in quarters or seconds.
units_are_seconds
property
¶
units_are_seconds: bool
Check if the times are in seconds.
This event must be in a Score (where _units_are_seconds is stored).
Returns:
-
bool–True iff the event's times are in seconds. If not in a score, False is returned.
units_are_quarters
property
¶
units_are_quarters: bool
Check if the times are in quarters.
This event must be in a Score (where _units_are_seconds is stored).
Returns:
-
bool–True iff the event's times are in quarters. If not in a score, False is returned.
offset
property
writable
¶
offset: float
Retrieve the global offset (stop) time.
Returns:
-
float–The global offset (stop) time.
part
property
¶
part: Optional[Part]
Retrieve the Part containing this event.
Returns:
-
Optional[Part]–The Part containing this event or None if not found.
score
property
¶
score: Optional[Score]
Retrieve the Score containing this event, or self if it is a score.
Returns:
-
Optional[Score]–The Score containing this event or None if not found.
staff
property
¶
staff: Optional[Staff]
Retrieve the Staff containing this event
Returns:
-
Optional[Staff]–The Staff containing this event or None if not found.
measure
property
¶
measure: Optional[Measure]
Retrieve the Measure containing this event
Returns:
-
Optional[Measure]–The Measure containing this event or None if not found.
Functions¶
set
¶
set(property: str, value: Any) -> Event
Set a named property on this Event.
Every event can be extended with additional properties. Although
Python objects are already extensible with new attributes, new
attributes that are not set in __init__ confuse type checkers
and other tools, so every Event has an info attribute as a
dictionary where additional, application-specific information can
be stored. The info attribute is None to save space until the
first property is set, so you should use set and get methods
and avoid writing event.info[property].
Parameters:
-
property(str) –The name of the property to set.
-
value(Any) –The value to assign to the property.
Returns:
-
Event–returns this object (self)
Examples:
>>> note = Note()
>>> note.get("color", "no color")
'no color'
>>> _ = note.set("color", "red").set("harmonicity", 0.2)
>>> (note.has("color"), note.has("shape"))
(True, False)
>>> (note.get("color"), note.get("harmonicity"))
('red', 0.2)
Source code in amads/core/basics.py
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 | |
get
¶
get(property: str, default: Any = None) -> Any
Get the value of a property from this Event.
See set for more details.
Parameters:
-
property(str.) –The name of the property to get.
-
default(Any, default:None) –The default value to return if the property is not found.
Returns:
-
Any–The value of the specified property.
Source code in amads/core/basics.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
has
¶
has(property) -> bool
Check if the Event has a specific property.
See set for more details.
Parameters:
-
property(str) –The name of the property to check.
Returns:
-
bool–True if the property exists, False otherwise.
Source code in amads/core/basics.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
time_shift
¶
time_shift(increment: float, content_only: bool = False) -> EventGroup
Change the onset by an increment, affecting all content.
If content_only is true, preserves EventGroup times (Score, Part, Staff, but not Chord or Measure) and shifts only the content (recursively). Otherwise, shifts this container and all content by the increment. Container onsets (recursively) are not allowed to become negative, so the onset time is set to zero if the increment would make it negative. However, even if the onset is is not shifted by increment, time_shift is still applied recursively to the content, and non-EventGroup events (e.g., Notes) are allowed to have negative onsets, even if this may not be well-defined for all AMADS operations.
Parameters:
-
increment(float) –The time increment (in quarters or seconds).
-
content_only(bool, default:False) –If true, preserves this container's time and shifts only the content.
Returns:
-
Event–The object. This method modifies the
EventGroup.
Raises:
-
ValueError–If the EventGroup has an unknown (None) onset and content_only is false.
Source code in amads/core/basics.py
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 | |
insert_copy_into
¶
insert_copy_into(parent: Optional[EventGroup] = None) -> Event
Make a (mostly) deep copy of the Event and add to a new parent.
Pitch objects are considered immutable and are shared rather
than copied.
Parameters:
-
parent(Optional(EventGroup), default:None) –The copied
Eventwill be a child ofparentif notNone. The parent is modified by this operation.
Returns:
-
Event–A deep copy (except for parent and pitch) of the Event instance.
Source code in amads/core/basics.py
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 | |
ismonophonic
¶
ismonophonic() -> bool
Determine if content is monophonic (non-overlapping notes).
A monophonic list of notes has no overlapping notes (e.g., chords).
Serves as a helper function for ismonophonic and
parts_are_monophonic.
Returns:
-
bool–True if the list of notes is monophonic, False otherwise.
Source code in amads/core/basics.py
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 | |
insert_emptycopy_into
¶
insert_emptycopy_into(
parent: Optional[EventGroup] = None,
) -> EventGroup
Create a deep copy of the EventGroup except for content.
A new parent is provided as an argument and the copy is inserted into this parent. This method is useful for copying an EventGroup without copying its content. See also insert_copy_into to copy an EventGroup with its content into a new parent.
Parameters:
-
parent(Optional[EventGroup], default:None) –The new parent to insert the copied Event into.
Returns:
-
EventGroup–A deep copy of the EventGroup instance with the new parent (if any) and no content.
Source code in amads/core/basics.py
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 | |
expand_chords
¶
expand_chords(parent: Optional[EventGroup] = None) -> EventGroup
Replace chords with the multiple notes they contain.
Returns a deep copy with no parent unless parent is provided.
Normally, you will call score.expand_chords() which returns a deep
copy of Score with notes moved from each chord to the copy of the
chord's parent (a Measure or a Part). The parent parameter is
primarily for internal use when expand_chords is called recursively
on score content.
If a chord has a "rolled" property, after expansion, the notes will each have a "rolled" property.
Parameters:
-
parent(EventGroup, default:None) –The new parent to insert the copied EventGroup into.
Returns:
-
EventGroup–A deep copy of the EventGroup instance with all Chord instances expanded.
Source code in amads/core/basics.py
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 | |
find_all
¶
find_all(
elem_type: Type[Event],
include_tied_to_notes: bool = False,
ignore: list[Note] = [],
) -> Generator[Event, None, None]
Find all instances of a specific type within the EventGroup.
Assumes that objects of type elem_type are not nested within
other objects of the same type. (The first elem_type encountered
in a depth-first enumeration is returned without looking at any
children in its content).
If elem_type is Note and include_tied_to_notes is True, then
all Notes are returned. The default is to omit notes after the first
one in a string of tied notes. In the (rare) case of cross-staff ties
(which are possible to represent), find_all may return a tied-to
note that is encountered before its predecessor.
Parameters:
-
elem_type(Type[Event]) –The type of event to search for.
-
include_tied_to_notes(bool, default:False) –Applies only when
elem_typeis Note, in which case tied groups of notes are all returned wheninclude_tied_to_notesis True, and only the first note of the group is returned when False. Note that thedurationproperty returns the total tied group duration in the case that a Note is tied. -
ignore(list[Note], default:[]) –Do not use this parameter. It is for the implementation to keep track of forward references from tied notes.
Yields:
-
Event–Instances of the specified type found within the EventGroup.
Source code in amads/core/basics.py
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 | |
get_sorted_notes
¶
get_sorted_notes(has_ties: bool = True) -> List[Note]
Return a list of sorted notes with merged ties.
This should generally be called on Parts and Scores since
in all other EventGroups, Events are in time order and
Notes retrieved with find_all() or list_all() are in
time order. However, get_sorted_notes also sorts notes
into increasing pitch (keynum) where note onsets are equal.
Parameters:
-
has_ties(bool, default:True) –If True (default), copy the score, merge the ties, and return a list of these merged copies. If False, assume there are no ties and return a list of original notes.
Raises:
-
ValueError–If has_ties is False, but a tie is encountered.
Returns:
-
list(Note)–a list of sorted notes with merged ties
Source code in amads/core/basics.py
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 | |
has_instanceof
¶
has_instanceof(the_class: Type[Event]) -> bool
Test if EventGroup contains any instances of the_class.
Parameters:
-
the_class(Type[Event]) –The class type to check for.
Returns:
-
bool–True iff the EventGroup contains an instance of the_class.
Source code in amads/core/basics.py
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 | |
has_rests
¶
has_rests() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any Rest objects.
Returns:
-
bool–True iff the EventGroup contains any Rest objects.
Source code in amads/core/basics.py
1970 1971 1972 1973 1974 1975 1976 1977 1978 | |
has_chords
¶
has_chords() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any Chord objects.
Returns:
-
bool–True iff the EventGroup contains any Chord objects.
Source code in amads/core/basics.py
1981 1982 1983 1984 1985 1986 1987 1988 1989 | |
has_ties
¶
has_ties() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any tied notes.
Returns:
-
bool–True iff the EventGroup contains any tied notes.
Source code in amads/core/basics.py
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 | |
has_measures
¶
has_measures() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any Measures.
Returns:
-
bool–True iff the EventGroup contains any Measure objects.
Source code in amads/core/basics.py
2007 2008 2009 2010 2011 2012 2013 2014 2015 | |
inherit_duration
¶
inherit_duration() -> EventGroup
Set the duration of this EventGroup according to maximum offset.
The duration is set to the maximum offset (end) time of the
children. If the EventGroup is empty, the duration is set to 0.
For Notes with ties, use note.onset + note._duration (without ties)
to get the note offset (end) time so that ties across EventGroups
(especially Measures) are not considered for the duration of this
EventGroup. This method modifies this EventGroup instance.
Returns:
-
EventGroup–The EventGroup instance (self) with updated duration.
Source code in amads/core/basics.py
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 | |
insert
¶
insert(event: Event) -> EventGroup
Insert an event.
Sets the parent of event to this EventGroup and makes event
be a member of this EventGroup.content. No changes are made to
event.onset or self.duration. Insert event in content just
before the first element with a greater onset. The method modifies
this object (self).
Parameters:
-
event(Event) –The event to be inserted.
Returns:
-
EventGroup–The EventGroup instance (self) with the event inserted.
Raises:
-
ValueError–If event._onset is None (it must be a number)
Caution Regarding Zero-Duration Event Order
If you have note N with non-zero duration and, at the same time, a grace note G with zero duration, the notes will be ordered according to when they are inserted. So if you insert N and then G, then N will be placed before G. See io.pt_import.py, where this case is dealt with.
Source code in amads/core/basics.py
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 | |
last
¶
last() -> Optional[Event]
Retrieve the last event in the content list.
Because the content list is sorted by onset, the returned
Event is simply the last element of content, but not
necessarily the event with the greatest offset.
Returns:
-
Optional[Event]–The last event in the content list or None if the list is empty.
Source code in amads/core/basics.py
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 | |
list_all
¶
Find all instances of a specific type within the EventGroup.
Assumes that objects of type elem_type are not nested within
other objects of the same type. If elem_type is nested, only
the outer-most object is returned. See also
find_all, which returns
a generator instead of a list.
If elem_type is Note and include_tied_to_notes is True, then
all Notes are listed. The default is to omit notes after the first
one in a string of tied notes. In the (rare) case of cross-staff ties
(which are possible to represent), find_all may list a tied-to
note that is encountered before its predecessor.
Parameters:
-
elem_type(Type[Event]) –The type of event to search for.
-
include_tied_to_notes(bool, default:False) –Applies only when
elem_typeis Note, in which case tied groups of notes are all returned wheninclude_tied_to_notesis True, and only the first note of the group is returned when False. Note that thedurationproperty returns the total tied group duration in the case that a Note is tied.
Returns:
-
list[Event]–A list of all instances of the specified type found within the EventGroup.
Source code in amads/core/basics.py
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 | |
merge_tied_notes
¶
merge_tied_notes(
parent: Optional[EventGroup] = None, ignore: list[Note] = []
) -> EventGroup
Create a new EventGroup with tied notes replaced by single notes.
If ties cross staffs, the replacement is placed in the staff of the
first note in the tied sequence. Insert the new EventGroup into
parent.
Ordinarily, this method is called on a Score with no parameters. The
parameters are used when Score.merge_tied_notes() calls this method
recursively on EventGroups within the Score such as Parts and
Staffs.
Parameters:
-
parent(Optional[EventGroup], default:None) –Where to insert the result.
-
ignore(list[Note], default:[]) –This parameter is used internally. Caller should not use this parameter.
Returns:
-
EventGroup–A copy with tied notes replaced by equivalent single notes.
Source code in amads/core/basics.py
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 | |
pack
¶
pack(onset: float = 0.0, sequential: bool = True) -> float
Adjust the content to be sequential.
The resulting content will begin with the parameter onset
(defaults to 0), and each other object will get an onset equal
to the offset of the previous element. The duration of self is
set to the offset of the last element. This method essentially
arranges the content to eliminate gaps. pack() works recursively
on elements that are EventGroups.
Be careful not to pack Measures (directly or through
recursion) if the Measure's content durations do not add up to
the intended quarters per measure.
To override the sequential behavior, set the sequential
parameter to False. In that case, pack behaves like the
Concurrence.pack() method.
The pack method alters self and its content in place.
Parameters:
-
onset(float, default:0.0) –The onset (start) time for this object.
Returns:
-
float–duration of self
Source code in amads/core/basics.py
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 | |
quantize
¶
quantize(
divisions: int, ignore: Optional[List[Note]] = None
) -> EventGroup
Align onsets and durations to a rhythmic grid.
Assumes time units are quarters. (See Score.convert_to_quarters.)
Modify all times and durations to a multiple of divisions per quarter note, e.g., 4 for sixteenth notes. Onsets and offsets are moved to the nearest quantized time. Any resulting duration change is less than one quantum, but not necessarily less than 0.5 quantum, since the onset and offset can round in opposite directions by up to 0.5 quantum each. Any non-zero duration that would quantize to zero duration gets a duration of one quantum since zero duration is almost certainly going to cause notation and visualization problems.
Special cases for zero duration:
- If the original duration is zero as in metadata or possibly grace notes, we preserve that.
- If a tied note duration quantizes to zero, we remove the tied note entirely provided some other note in the tied sequence has non-zero duration. If all tied notes quantize to zero, we keep the first one and set its duration to one quantum.
This method modifies this EventGroup and all its content in place.
Note that there is no way to specify "sixteenths or eighth triplets" because 6 would not allow sixteenths and 12 would admit sixteenth triplets. Using tuples as in Music21, e.g., (4, 3) for this problem creates another problem: if quantization is to time points 1/4, 1/3, then the difference is 1/12 or a thirty-second triplet. If the quantization is applied to durations, then you could have 1/4 + 1/3 = 7/12, and the remaining duration in a single beat would be 5/12, which is not expressible as sixteenths, eighth triplets or any tied combination.
Parameters:
-
divisions(int) –The number of divisions per quarter note, e.g., 4 for sixteenths, to control quantization.
-
ignore(Optional[List[Note]], default:None) –The list of tied-to notes to ignore during quantization because they have already been quantized. Do not provide this parameter; it is for internal implementation only.
Returns:
-
EventGroup–The EventGroup instance (self) with (modified in place) quantized times.
Source code in amads/core/basics.py
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 | |
remove
¶
remove(element: Event) -> EventGroup
Remove an element from the content list.
The method modifies this object (self).
Parameters:
-
element(Event) –The event to be removed.
Returns:
-
EventGroup–The EventGroup instance (self) with the element removed. The returned value is not a copy.
Source code in amads/core/basics.py
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 | |
remove_rests
¶
remove_rests(parent: Union[EventGroup, None] = None) -> EventGroup
Remove all Rest objects from content.
Returns a deep copy with no parent unless parent is provided.
Parameters:
-
parent(EventGroup, default:None) –The new parent to insert the copied Event into.
Returns:
-
EventGroup–A deep copy of the EventGroup instance with all Rest objects removed.
Source code in amads/core/basics.py
2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 | |
show
¶
show(indent: int = 0, file: Optional[TextIO] = None) -> EventGroup
Print the EventGroup information.
Parameters:
-
indent(int, default:0) –The indentation level for display.
Returns:
-
EventGroup–The EventGroup instance itself.
Source code in amads/core/basics.py
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 | |
measure_times
¶
measure_times(n: int) -> tuple[float, float]
Return the start and end times of measure n.
Parameters:
-
n(int) –The 0-based measure number.
Returns:
-
tuple(float, float)–A tuple containing the start and end times of measure n. Measure 0 is the first measure. Units are the same as the score's time units.
Raises:
-
ValueError–If there is no measure with number n.
Source code in amads/core/basics.py
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 | |
slice
¶
slice(
start: float,
end: float,
units: str = "quarters",
mode: str = "onsets",
truncate: str = "keep",
shift: bool = False,
min_duration: float = 0.05,
) -> EventGroup
Create a new Score containing the content between start and end.
Typically, this method is called on a Score to create a new Score,
but it works on any Sequence as long as the sequence is part of a Score.
If self has Measures, units must be "measures" or "bars", the start and
end parameters are interpreted as 0-based measure numbers, and the
result will therefore contain whole measures. It is assumed that notes
are tied across measure boundaries. The result will contain only the
parts of tied notes that are within the specified measures, excluding
< measure numbered by end.
For any units besides "measures" or "bars", the score must be flat.
Notes that extend beyond the start or end time are included in the
result unless the `truncate` parameter specifies otherwise.
In principle, there is no reason to require a flat score for slicing by
time, but there are many difficult edge cases to consider such as ties
from within one chord to a note in another measure. Therefore, to slice
by time or quarter, you must have a flattened score. Conversely, to
slice a full score, you must slice by measure so that the result gives
you whole measures, which are encoded into the full Score structure.
The entire result can be shifted to start at time 0 by setting the
`shift` parameter to True. Unless `truncate` is "truncate" or
"beginning", the result can include events that start before the start
time, in which case shifting will only shift the earliest onset to 0,
which means the shift amount depends upon the content. (Negative onsets
are currently not allowed.)
The resulting Score, Part, and other content will have onset and offset
times matching start and end, even if some content starts earlier or
ends later.
Parameters
Parameters
start : float
The start time of the slice, inclusive. If units is "measures" or
"bars", start is a 0-based measure number of the first measure.
end : float
The end time of the slice, exclusive. For measures, end is the
0-based measure number of the last measure plus one, so the last
measure is end - 1.
units : str
The time units for start and end. Valid values are "quarters", "s",
"sec", "seconds", "measures", or "bars".
shift: bool
If true, shift the content in the result so that the result starts
time 0.0. If false (default), the content in the result retains
the same time values as in the original Score.
mode : Optional[str]
The slicing mode, ignored if units is "measures" or "bars".
Valid values are:
- `"onsets"` - include events that start within bounds (default)
- `"overlaps"` - include events that overlap (either onset or
offset) is within the bounds. An event ending is considered
to overlap if its offset is greater than start.
- `"offsets"` - include events that end within the bounds
- `"strict"` - include only events that start at or after start
and end at or before end. (End times that are exactly equal
to end are considered to be within bounds, so this is
inclusive of end time.)
truncate: Optional[str]
How to handle events that partially overlap the bounds,
ignored if units is "measures" or "bars". Valid values are:
- `"truncate"` - truncate (clip) events that partially overlap
the bounds. If the event onset is before start, the onset
is increased to start (adjusting duration to maintain the
offset time). Also, if the event offset is after end, the
duration is decreased so that the offset time is end.
- `"keep"` - no modification/truncation (default). Note that
with the default mode="onsets", no event onsets will be
earlier than start, but offsets may extend beyond end.
- `"end"` or "ending" or "dur" or "duration" - truncate (clip)
events that extend beyond end so that all offsets <= end.
- `"beginning"` - events with onsets before start are moved to
make their onsets equal to start, and their duration is
adjusted to maintain the original offset.
min_duration: Optional[float]
Events with duration less than min_duration (after any truncation
is applied) are removed. Ignored if units is "measures" or "bars".
If None or 0.0, no events are removed. The default is 0.05,
independent of time units. Be careful with gracenotes that, when
read from MusicXML, have zero duration.
Returns
Returns
Score
A new Score instance containing the content between start and end.
Raises
Raises
ValueError
If the Sequence is not part of a Score, or if invalid values are
provided for the parameters.
Source code in amads/core/basics.py
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 | |
time_signature
¶
time_signature() -> Optional[TimeSignature]
Retrieve the time signature that applies to this measure.
Returns:
-
Optional[TimeSignature]–The time signature from the score corresponding to the time of this measure, or None if not found.
Raises:
-
ValueError–If there is no Score or no onset time.
Source code in amads/core/basics.py
3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 | |
Staff
¶
Staff(
*args: Event,
parent: Optional[EventGroup] = None,
onset: Optional[float] = 0,
duration: Optional[float] = None,
number: Optional[int] = None
)
Bases: Sequence
A Staff models a musical staff.
This can also model one channel of a standard MIDI file track. A Staff normally contains Measure objects and is an element of a Part.
See Constructor Details.
Parameters:
-
*args(Optional[Event], default:()) –A variable number of Event objects to be added to the group.
-
parent(Optional[EventGroup], default:None) –The containing object or None.
-
onset(Optional[float], default:0) –The onset (start) time. If unknown (None), it will be set when this Staff is added to a parent. Must be passed as a keyword parameter due to
*args. -
duration(Optional[float], default:None) –The duration in quarters or seconds. (If duration is omitted or None, the duration is set so that self.offset ends at the max offset of args, or 0 if there is no content.) Must be passed as a keyword parameter due to
*args. -
number(Optional[int], default:None) –The staff number. Normally, a Staff is given an integer number where 1 is the top staff of the part, 2 is the 2nd, etc. Must be passed as a keyword parameter due to
*args.
Attributes:
-
parent(Optional[EventGroup]) –The containing object or None.
-
_onset(float) –The onset (start) time.
-
duration(float) –The duration in quarters or seconds.
-
content(list[Event]) –Elements contained within this collection.
-
number(Optional[int]) –The staff number. Normally a Staff is given an integer number where 1 is the top staff of the part, 2 is the 2nd, etc.
Source code in amads/core/basics.py
4356 4357 4358 4359 4360 4361 4362 | |
Attributes¶
onset
property
writable
¶
onset: float
Retrieve the onset (start) time.
If the onset is None, raise an exception. (Events can have None onset times, but they must be set before retrieval. onsets that are None are automatically set when the Event is added to an EventGroup.)
Returns:
-
float–The onset (start) time.
Raises:
-
ValueError–If the onset time is not set (None).
duration
property
writable
¶
duration: float
Retrieve the duration of the event.
Returns:
-
float–The duration of the event in quarters or seconds.
units_are_seconds
property
¶
units_are_seconds: bool
Check if the times are in seconds.
This event must be in a Score (where _units_are_seconds is stored).
Returns:
-
bool–True iff the event's times are in seconds. If not in a score, False is returned.
units_are_quarters
property
¶
units_are_quarters: bool
Check if the times are in quarters.
This event must be in a Score (where _units_are_seconds is stored).
Returns:
-
bool–True iff the event's times are in quarters. If not in a score, False is returned.
offset
property
writable
¶
offset: float
Retrieve the global offset (stop) time.
Returns:
-
float–The global offset (stop) time.
part
property
¶
part: Optional[Part]
Retrieve the Part containing this event.
Returns:
-
Optional[Part]–The Part containing this event or None if not found.
score
property
¶
score: Optional[Score]
Retrieve the Score containing this event, or self if it is a score.
Returns:
-
Optional[Score]–The Score containing this event or None if not found.
staff
property
¶
staff: Optional[Staff]
Retrieve the Staff containing this event
Returns:
-
Optional[Staff]–The Staff containing this event or None if not found.
measure
property
¶
measure: Optional[Measure]
Retrieve the Measure containing this event
Returns:
-
Optional[Measure]–The Measure containing this event or None if not found.
Functions¶
set
¶
set(property: str, value: Any) -> Event
Set a named property on this Event.
Every event can be extended with additional properties. Although
Python objects are already extensible with new attributes, new
attributes that are not set in __init__ confuse type checkers
and other tools, so every Event has an info attribute as a
dictionary where additional, application-specific information can
be stored. The info attribute is None to save space until the
first property is set, so you should use set and get methods
and avoid writing event.info[property].
Parameters:
-
property(str) –The name of the property to set.
-
value(Any) –The value to assign to the property.
Returns:
-
Event–returns this object (self)
Examples:
>>> note = Note()
>>> note.get("color", "no color")
'no color'
>>> _ = note.set("color", "red").set("harmonicity", 0.2)
>>> (note.has("color"), note.has("shape"))
(True, False)
>>> (note.get("color"), note.get("harmonicity"))
('red', 0.2)
Source code in amads/core/basics.py
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 | |
get
¶
get(property: str, default: Any = None) -> Any
Get the value of a property from this Event.
See set for more details.
Parameters:
-
property(str.) –The name of the property to get.
-
default(Any, default:None) –The default value to return if the property is not found.
Returns:
-
Any–The value of the specified property.
Source code in amads/core/basics.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
has
¶
has(property) -> bool
Check if the Event has a specific property.
See set for more details.
Parameters:
-
property(str) –The name of the property to check.
Returns:
-
bool–True if the property exists, False otherwise.
Source code in amads/core/basics.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
time_shift
¶
time_shift(increment: float, content_only: bool = False) -> EventGroup
Change the onset by an increment, affecting all content.
If content_only is true, preserves EventGroup times (Score, Part, Staff, but not Chord or Measure) and shifts only the content (recursively). Otherwise, shifts this container and all content by the increment. Container onsets (recursively) are not allowed to become negative, so the onset time is set to zero if the increment would make it negative. However, even if the onset is is not shifted by increment, time_shift is still applied recursively to the content, and non-EventGroup events (e.g., Notes) are allowed to have negative onsets, even if this may not be well-defined for all AMADS operations.
Parameters:
-
increment(float) –The time increment (in quarters or seconds).
-
content_only(bool, default:False) –If true, preserves this container's time and shifts only the content.
Returns:
-
Event–The object. This method modifies the
EventGroup.
Raises:
-
ValueError–If the EventGroup has an unknown (None) onset and content_only is false.
Source code in amads/core/basics.py
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 | |
insert_copy_into
¶
insert_copy_into(parent: Optional[EventGroup] = None) -> Event
Make a (mostly) deep copy of the Event and add to a new parent.
Pitch objects are considered immutable and are shared rather
than copied.
Parameters:
-
parent(Optional(EventGroup), default:None) –The copied
Eventwill be a child ofparentif notNone. The parent is modified by this operation.
Returns:
-
Event–A deep copy (except for parent and pitch) of the Event instance.
Source code in amads/core/basics.py
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 | |
ismonophonic
¶
ismonophonic() -> bool
Determine if content is monophonic (non-overlapping notes).
A monophonic list of notes has no overlapping notes (e.g., chords).
Serves as a helper function for ismonophonic and
parts_are_monophonic.
Returns:
-
bool–True if the list of notes is monophonic, False otherwise.
Source code in amads/core/basics.py
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 | |
insert_emptycopy_into
¶
insert_emptycopy_into(
parent: Optional[EventGroup] = None,
) -> EventGroup
Create a deep copy of the EventGroup except for content.
A new parent is provided as an argument and the copy is inserted into this parent. This method is useful for copying an EventGroup without copying its content. See also insert_copy_into to copy an EventGroup with its content into a new parent.
Parameters:
-
parent(Optional[EventGroup], default:None) –The new parent to insert the copied Event into.
Returns:
-
EventGroup–A deep copy of the EventGroup instance with the new parent (if any) and no content.
Source code in amads/core/basics.py
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 | |
expand_chords
¶
expand_chords(parent: Optional[EventGroup] = None) -> EventGroup
Replace chords with the multiple notes they contain.
Returns a deep copy with no parent unless parent is provided.
Normally, you will call score.expand_chords() which returns a deep
copy of Score with notes moved from each chord to the copy of the
chord's parent (a Measure or a Part). The parent parameter is
primarily for internal use when expand_chords is called recursively
on score content.
If a chord has a "rolled" property, after expansion, the notes will each have a "rolled" property.
Parameters:
-
parent(EventGroup, default:None) –The new parent to insert the copied EventGroup into.
Returns:
-
EventGroup–A deep copy of the EventGroup instance with all Chord instances expanded.
Source code in amads/core/basics.py
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 | |
find_all
¶
find_all(
elem_type: Type[Event],
include_tied_to_notes: bool = False,
ignore: list[Note] = [],
) -> Generator[Event, None, None]
Find all instances of a specific type within the EventGroup.
Assumes that objects of type elem_type are not nested within
other objects of the same type. (The first elem_type encountered
in a depth-first enumeration is returned without looking at any
children in its content).
If elem_type is Note and include_tied_to_notes is True, then
all Notes are returned. The default is to omit notes after the first
one in a string of tied notes. In the (rare) case of cross-staff ties
(which are possible to represent), find_all may return a tied-to
note that is encountered before its predecessor.
Parameters:
-
elem_type(Type[Event]) –The type of event to search for.
-
include_tied_to_notes(bool, default:False) –Applies only when
elem_typeis Note, in which case tied groups of notes are all returned wheninclude_tied_to_notesis True, and only the first note of the group is returned when False. Note that thedurationproperty returns the total tied group duration in the case that a Note is tied. -
ignore(list[Note], default:[]) –Do not use this parameter. It is for the implementation to keep track of forward references from tied notes.
Yields:
-
Event–Instances of the specified type found within the EventGroup.
Source code in amads/core/basics.py
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 | |
get_sorted_notes
¶
get_sorted_notes(has_ties: bool = True) -> List[Note]
Return a list of sorted notes with merged ties.
This should generally be called on Parts and Scores since
in all other EventGroups, Events are in time order and
Notes retrieved with find_all() or list_all() are in
time order. However, get_sorted_notes also sorts notes
into increasing pitch (keynum) where note onsets are equal.
Parameters:
-
has_ties(bool, default:True) –If True (default), copy the score, merge the ties, and return a list of these merged copies. If False, assume there are no ties and return a list of original notes.
Raises:
-
ValueError–If has_ties is False, but a tie is encountered.
Returns:
-
list(Note)–a list of sorted notes with merged ties
Source code in amads/core/basics.py
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 | |
has_instanceof
¶
has_instanceof(the_class: Type[Event]) -> bool
Test if EventGroup contains any instances of the_class.
Parameters:
-
the_class(Type[Event]) –The class type to check for.
Returns:
-
bool–True iff the EventGroup contains an instance of the_class.
Source code in amads/core/basics.py
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 | |
has_rests
¶
has_rests() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any Rest objects.
Returns:
-
bool–True iff the EventGroup contains any Rest objects.
Source code in amads/core/basics.py
1970 1971 1972 1973 1974 1975 1976 1977 1978 | |
has_chords
¶
has_chords() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any Chord objects.
Returns:
-
bool–True iff the EventGroup contains any Chord objects.
Source code in amads/core/basics.py
1981 1982 1983 1984 1985 1986 1987 1988 1989 | |
has_ties
¶
has_ties() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any tied notes.
Returns:
-
bool–True iff the EventGroup contains any tied notes.
Source code in amads/core/basics.py
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 | |
has_measures
¶
has_measures() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any Measures.
Returns:
-
bool–True iff the EventGroup contains any Measure objects.
Source code in amads/core/basics.py
2007 2008 2009 2010 2011 2012 2013 2014 2015 | |
inherit_duration
¶
inherit_duration() -> EventGroup
Set the duration of this EventGroup according to maximum offset.
The duration is set to the maximum offset (end) time of the
children. If the EventGroup is empty, the duration is set to 0.
For Notes with ties, use note.onset + note._duration (without ties)
to get the note offset (end) time so that ties across EventGroups
(especially Measures) are not considered for the duration of this
EventGroup. This method modifies this EventGroup instance.
Returns:
-
EventGroup–The EventGroup instance (self) with updated duration.
Source code in amads/core/basics.py
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 | |
insert
¶
insert(event: Event) -> EventGroup
Insert an event.
Sets the parent of event to this EventGroup and makes event
be a member of this EventGroup.content. No changes are made to
event.onset or self.duration. Insert event in content just
before the first element with a greater onset. The method modifies
this object (self).
Parameters:
-
event(Event) –The event to be inserted.
Returns:
-
EventGroup–The EventGroup instance (self) with the event inserted.
Raises:
-
ValueError–If event._onset is None (it must be a number)
Caution Regarding Zero-Duration Event Order
If you have note N with non-zero duration and, at the same time, a grace note G with zero duration, the notes will be ordered according to when they are inserted. So if you insert N and then G, then N will be placed before G. See io.pt_import.py, where this case is dealt with.
Source code in amads/core/basics.py
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 | |
last
¶
last() -> Optional[Event]
Retrieve the last event in the content list.
Because the content list is sorted by onset, the returned
Event is simply the last element of content, but not
necessarily the event with the greatest offset.
Returns:
-
Optional[Event]–The last event in the content list or None if the list is empty.
Source code in amads/core/basics.py
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 | |
list_all
¶
Find all instances of a specific type within the EventGroup.
Assumes that objects of type elem_type are not nested within
other objects of the same type. If elem_type is nested, only
the outer-most object is returned. See also
find_all, which returns
a generator instead of a list.
If elem_type is Note and include_tied_to_notes is True, then
all Notes are listed. The default is to omit notes after the first
one in a string of tied notes. In the (rare) case of cross-staff ties
(which are possible to represent), find_all may list a tied-to
note that is encountered before its predecessor.
Parameters:
-
elem_type(Type[Event]) –The type of event to search for.
-
include_tied_to_notes(bool, default:False) –Applies only when
elem_typeis Note, in which case tied groups of notes are all returned wheninclude_tied_to_notesis True, and only the first note of the group is returned when False. Note that thedurationproperty returns the total tied group duration in the case that a Note is tied.
Returns:
-
list[Event]–A list of all instances of the specified type found within the EventGroup.
Source code in amads/core/basics.py
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 | |
merge_tied_notes
¶
merge_tied_notes(
parent: Optional[EventGroup] = None, ignore: list[Note] = []
) -> EventGroup
Create a new EventGroup with tied notes replaced by single notes.
If ties cross staffs, the replacement is placed in the staff of the
first note in the tied sequence. Insert the new EventGroup into
parent.
Ordinarily, this method is called on a Score with no parameters. The
parameters are used when Score.merge_tied_notes() calls this method
recursively on EventGroups within the Score such as Parts and
Staffs.
Parameters:
-
parent(Optional[EventGroup], default:None) –Where to insert the result.
-
ignore(list[Note], default:[]) –This parameter is used internally. Caller should not use this parameter.
Returns:
-
EventGroup–A copy with tied notes replaced by equivalent single notes.
Source code in amads/core/basics.py
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 | |
pack
¶
pack(onset: float = 0.0, sequential: bool = True) -> float
Adjust the content to be sequential.
The resulting content will begin with the parameter onset
(defaults to 0), and each other object will get an onset equal
to the offset of the previous element. The duration of self is
set to the offset of the last element. This method essentially
arranges the content to eliminate gaps. pack() works recursively
on elements that are EventGroups.
Be careful not to pack Measures (directly or through
recursion) if the Measure's content durations do not add up to
the intended quarters per measure.
To override the sequential behavior, set the sequential
parameter to False. In that case, pack behaves like the
Concurrence.pack() method.
The pack method alters self and its content in place.
Parameters:
-
onset(float, default:0.0) –The onset (start) time for this object.
Returns:
-
float–duration of self
Source code in amads/core/basics.py
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 | |
quantize
¶
quantize(
divisions: int, ignore: Optional[List[Note]] = None
) -> EventGroup
Align onsets and durations to a rhythmic grid.
Assumes time units are quarters. (See Score.convert_to_quarters.)
Modify all times and durations to a multiple of divisions per quarter note, e.g., 4 for sixteenth notes. Onsets and offsets are moved to the nearest quantized time. Any resulting duration change is less than one quantum, but not necessarily less than 0.5 quantum, since the onset and offset can round in opposite directions by up to 0.5 quantum each. Any non-zero duration that would quantize to zero duration gets a duration of one quantum since zero duration is almost certainly going to cause notation and visualization problems.
Special cases for zero duration:
- If the original duration is zero as in metadata or possibly grace notes, we preserve that.
- If a tied note duration quantizes to zero, we remove the tied note entirely provided some other note in the tied sequence has non-zero duration. If all tied notes quantize to zero, we keep the first one and set its duration to one quantum.
This method modifies this EventGroup and all its content in place.
Note that there is no way to specify "sixteenths or eighth triplets" because 6 would not allow sixteenths and 12 would admit sixteenth triplets. Using tuples as in Music21, e.g., (4, 3) for this problem creates another problem: if quantization is to time points 1/4, 1/3, then the difference is 1/12 or a thirty-second triplet. If the quantization is applied to durations, then you could have 1/4 + 1/3 = 7/12, and the remaining duration in a single beat would be 5/12, which is not expressible as sixteenths, eighth triplets or any tied combination.
Parameters:
-
divisions(int) –The number of divisions per quarter note, e.g., 4 for sixteenths, to control quantization.
-
ignore(Optional[List[Note]], default:None) –The list of tied-to notes to ignore during quantization because they have already been quantized. Do not provide this parameter; it is for internal implementation only.
Returns:
-
EventGroup–The EventGroup instance (self) with (modified in place) quantized times.
Source code in amads/core/basics.py
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 | |
remove
¶
remove(element: Event) -> EventGroup
Remove an element from the content list.
The method modifies this object (self).
Parameters:
-
element(Event) –The event to be removed.
Returns:
-
EventGroup–The EventGroup instance (self) with the element removed. The returned value is not a copy.
Source code in amads/core/basics.py
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 | |
remove_rests
¶
remove_rests(parent: Union[EventGroup, None] = None) -> EventGroup
Remove all Rest objects from content.
Returns a deep copy with no parent unless parent is provided.
Parameters:
-
parent(EventGroup, default:None) –The new parent to insert the copied Event into.
Returns:
-
EventGroup–A deep copy of the EventGroup instance with all Rest objects removed.
Source code in amads/core/basics.py
2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 | |
show
¶
show(indent: int = 0, file: Optional[TextIO] = None) -> EventGroup
Print the EventGroup information.
Parameters:
-
indent(int, default:0) –The indentation level for display.
Returns:
-
EventGroup–The EventGroup instance itself.
Source code in amads/core/basics.py
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 | |
measure_times
¶
measure_times(n: int) -> tuple[float, float]
Return the start and end times of measure n.
Parameters:
-
n(int) –The 0-based measure number.
Returns:
-
tuple(float, float)–A tuple containing the start and end times of measure n. Measure 0 is the first measure. Units are the same as the score's time units.
Raises:
-
ValueError–If there is no measure with number n.
Source code in amads/core/basics.py
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 | |
slice
¶
slice(
start: float,
end: float,
units: str = "quarters",
mode: str = "onsets",
truncate: str = "keep",
shift: bool = False,
min_duration: float = 0.05,
) -> EventGroup
Create a new Score containing the content between start and end.
Typically, this method is called on a Score to create a new Score,
but it works on any Sequence as long as the sequence is part of a Score.
If self has Measures, units must be "measures" or "bars", the start and
end parameters are interpreted as 0-based measure numbers, and the
result will therefore contain whole measures. It is assumed that notes
are tied across measure boundaries. The result will contain only the
parts of tied notes that are within the specified measures, excluding
< measure numbered by end.
For any units besides "measures" or "bars", the score must be flat.
Notes that extend beyond the start or end time are included in the
result unless the `truncate` parameter specifies otherwise.
In principle, there is no reason to require a flat score for slicing by
time, but there are many difficult edge cases to consider such as ties
from within one chord to a note in another measure. Therefore, to slice
by time or quarter, you must have a flattened score. Conversely, to
slice a full score, you must slice by measure so that the result gives
you whole measures, which are encoded into the full Score structure.
The entire result can be shifted to start at time 0 by setting the
`shift` parameter to True. Unless `truncate` is "truncate" or
"beginning", the result can include events that start before the start
time, in which case shifting will only shift the earliest onset to 0,
which means the shift amount depends upon the content. (Negative onsets
are currently not allowed.)
The resulting Score, Part, and other content will have onset and offset
times matching start and end, even if some content starts earlier or
ends later.
Parameters
Parameters
start : float
The start time of the slice, inclusive. If units is "measures" or
"bars", start is a 0-based measure number of the first measure.
end : float
The end time of the slice, exclusive. For measures, end is the
0-based measure number of the last measure plus one, so the last
measure is end - 1.
units : str
The time units for start and end. Valid values are "quarters", "s",
"sec", "seconds", "measures", or "bars".
shift: bool
If true, shift the content in the result so that the result starts
time 0.0. If false (default), the content in the result retains
the same time values as in the original Score.
mode : Optional[str]
The slicing mode, ignored if units is "measures" or "bars".
Valid values are:
- `"onsets"` - include events that start within bounds (default)
- `"overlaps"` - include events that overlap (either onset or
offset) is within the bounds. An event ending is considered
to overlap if its offset is greater than start.
- `"offsets"` - include events that end within the bounds
- `"strict"` - include only events that start at or after start
and end at or before end. (End times that are exactly equal
to end are considered to be within bounds, so this is
inclusive of end time.)
truncate: Optional[str]
How to handle events that partially overlap the bounds,
ignored if units is "measures" or "bars". Valid values are:
- `"truncate"` - truncate (clip) events that partially overlap
the bounds. If the event onset is before start, the onset
is increased to start (adjusting duration to maintain the
offset time). Also, if the event offset is after end, the
duration is decreased so that the offset time is end.
- `"keep"` - no modification/truncation (default). Note that
with the default mode="onsets", no event onsets will be
earlier than start, but offsets may extend beyond end.
- `"end"` or "ending" or "dur" or "duration" - truncate (clip)
events that extend beyond end so that all offsets <= end.
- `"beginning"` - events with onsets before start are moved to
make their onsets equal to start, and their duration is
adjusted to maintain the original offset.
min_duration: Optional[float]
Events with duration less than min_duration (after any truncation
is applied) are removed. Ignored if units is "measures" or "bars".
If None or 0.0, no events are removed. The default is 0.05,
independent of time units. Be careful with gracenotes that, when
read from MusicXML, have zero duration.
Returns
Returns
Score
A new Score instance containing the content between start and end.
Raises
Raises
ValueError
If the Sequence is not part of a Score, or if invalid values are
provided for the parameters.
Source code in amads/core/basics.py
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 | |
remove_measures
¶
remove_measures() -> Staff
Modify Staff by removing all Measures.
Notes are “lifted” from Measures to become direct content of this
Staff. There is no special handling for notes tied to or from another
Staff, so normally this method should be used only on a Staff where
ties have been merged (see merge_tied_notes()).
This method is normally called from remove_measures() in Part,
which insures that this Staff is not shared, so it is safe to
modify it. If called directly, the caller, to avoid unintended
side effects, must ensure that this Staff is not shared data.
Only Note and KeySignature objects are copied from Measures
to the Staff. All other objects are removed.
Returns:
-
Staff–A Staff with all Measures removed.
Source code in amads/core/basics.py
4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 | |
Part
¶
Part(
*args: Event,
parent: Optional[Score] = None,
onset: float = 0.0,
duration: Optional[float] = None,
number: Optional[str] = None,
instrument: Optional[str] = None,
flat: bool = False
)
Bases: EventGroup
A Part models a staff or staff group such as a grand staff.
For that reason, a Part contains one or more Staff objects. It should not contain any other object types. Parts are normally elements of a Score. Note that in a flat score, a Part is a collection of Notes, not Staffs, and it should be organized more sequentially than concurrently, so the default assignment of onset times may not be appropriate.
See Constructor Details.
Part is an EventGroup rather than a Sequence or Concurrence because in flat scores, it acts like a Sequence of notes, but in full scores, it is like a Concurrence of Staff objects.
Parameters:
-
*args(Optional[Event], default:()) –A variable number of Event objects to be added to the group. parent : Optional[EventGroup] The containing object or None. Must be passed as a keyword parameter due to
*args. -
onset(Optional[float], default:0.0) –The onset (start) time. If unknown (None), it will be set when this Part is added to a parent. Must be passed as a keyword parameter due to
*args. -
duration(Optional[float], default:None) –The duration in quarters or seconds. (If duration is omitted or None, the duration is set so that self.offset ends at the max offset of args, or 0 if there is no content.) Must be passed as a keyword parameter due to
*args. -
number(Optional[str], default:None) –A string representing the part number.
-
instrument(Optional[str], default:None) –A string representing the instrument name.
-
flat(bool, default:False) –If true, content in
*argswith onset None are modified to start at the offset of the previous note (or atonsetif this is the first Event in*args, or at 0.0 ifonsetis unspecified). Otherwise, this is assumed to be a Part in a full score,*argsis assumed to containStaffs, and their default onset times are givenonsetby onset (or 0.0 ifonsetis unspecified). This must be passed as a keyword argument due to*args.
Attributes:
-
parent(Optional[EventGroup]) –The containing object or None.
-
_onset(float) –The onset (start) time.
-
duration(float) –The duration in quarters or seconds.
-
content(list[Event]) –Elements contained within this collection.
-
number(Union[str, None]) –A string representing the part number (if any). E.g., "22a".
-
instrument(Union[str, None]) –A string representing the instrument name (if any).
Notes
Standard MIDI File tracks often have text instrument names in
type 4 meta events. These are stored in the `instrument` attribute.
Tracks often contain events for a single MIDI channel and a single
“program” that is another representation of “instrument.” In fact,
the `pretty_midi` library considers MIDI program to be a property
of the track rather than a timed event within the track (many
sequencers use this model as well). Therefore, if there is a
single MIDI program in a track (or an AMADS Part), the program
number (int) is stored in `info` using the key `"midi_program"`.
Source code in amads/core/basics.py
4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 | |
Attributes¶
onset
property
writable
¶
onset: float
Retrieve the onset (start) time.
If the onset is None, raise an exception. (Events can have None onset times, but they must be set before retrieval. onsets that are None are automatically set when the Event is added to an EventGroup.)
Returns:
-
float–The onset (start) time.
Raises:
-
ValueError–If the onset time is not set (None).
duration
property
writable
¶
duration: float
Retrieve the duration of the event.
Returns:
-
float–The duration of the event in quarters or seconds.
units_are_seconds
property
¶
units_are_seconds: bool
Check if the times are in seconds.
This event must be in a Score (where _units_are_seconds is stored).
Returns:
-
bool–True iff the event's times are in seconds. If not in a score, False is returned.
units_are_quarters
property
¶
units_are_quarters: bool
Check if the times are in quarters.
This event must be in a Score (where _units_are_seconds is stored).
Returns:
-
bool–True iff the event's times are in quarters. If not in a score, False is returned.
offset
property
writable
¶
offset: float
Retrieve the global offset (stop) time.
Returns:
-
float–The global offset (stop) time.
part
property
¶
part: Optional[Part]
Retrieve the Part containing this event.
Returns:
-
Optional[Part]–The Part containing this event or None if not found.
score
property
¶
score: Optional[Score]
Retrieve the Score containing this event, or self if it is a score.
Returns:
-
Optional[Score]–The Score containing this event or None if not found.
staff
property
¶
staff: Optional[Staff]
Retrieve the Staff containing this event
Returns:
-
Optional[Staff]–The Staff containing this event or None if not found.
measure
property
¶
measure: Optional[Measure]
Retrieve the Measure containing this event
Returns:
-
Optional[Measure]–The Measure containing this event or None if not found.
Functions¶
set
¶
set(property: str, value: Any) -> Event
Set a named property on this Event.
Every event can be extended with additional properties. Although
Python objects are already extensible with new attributes, new
attributes that are not set in __init__ confuse type checkers
and other tools, so every Event has an info attribute as a
dictionary where additional, application-specific information can
be stored. The info attribute is None to save space until the
first property is set, so you should use set and get methods
and avoid writing event.info[property].
Parameters:
-
property(str) –The name of the property to set.
-
value(Any) –The value to assign to the property.
Returns:
-
Event–returns this object (self)
Examples:
>>> note = Note()
>>> note.get("color", "no color")
'no color'
>>> _ = note.set("color", "red").set("harmonicity", 0.2)
>>> (note.has("color"), note.has("shape"))
(True, False)
>>> (note.get("color"), note.get("harmonicity"))
('red', 0.2)
Source code in amads/core/basics.py
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 | |
get
¶
get(property: str, default: Any = None) -> Any
Get the value of a property from this Event.
See set for more details.
Parameters:
-
property(str.) –The name of the property to get.
-
default(Any, default:None) –The default value to return if the property is not found.
Returns:
-
Any–The value of the specified property.
Source code in amads/core/basics.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
has
¶
has(property) -> bool
Check if the Event has a specific property.
See set for more details.
Parameters:
-
property(str) –The name of the property to check.
Returns:
-
bool–True if the property exists, False otherwise.
Source code in amads/core/basics.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
time_shift
¶
time_shift(increment: float, content_only: bool = False) -> EventGroup
Change the onset by an increment, affecting all content.
If content_only is true, preserves EventGroup times (Score, Part, Staff, but not Chord or Measure) and shifts only the content (recursively). Otherwise, shifts this container and all content by the increment. Container onsets (recursively) are not allowed to become negative, so the onset time is set to zero if the increment would make it negative. However, even if the onset is is not shifted by increment, time_shift is still applied recursively to the content, and non-EventGroup events (e.g., Notes) are allowed to have negative onsets, even if this may not be well-defined for all AMADS operations.
Parameters:
-
increment(float) –The time increment (in quarters or seconds).
-
content_only(bool, default:False) –If true, preserves this container's time and shifts only the content.
Returns:
-
Event–The object. This method modifies the
EventGroup.
Raises:
-
ValueError–If the EventGroup has an unknown (None) onset and content_only is false.
Source code in amads/core/basics.py
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 | |
insert_copy_into
¶
insert_copy_into(parent: Optional[EventGroup] = None) -> Event
Make a (mostly) deep copy of the Event and add to a new parent.
Pitch objects are considered immutable and are shared rather
than copied.
Parameters:
-
parent(Optional(EventGroup), default:None) –The copied
Eventwill be a child ofparentif notNone. The parent is modified by this operation.
Returns:
-
Event–A deep copy (except for parent and pitch) of the Event instance.
Source code in amads/core/basics.py
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 | |
ismonophonic
¶
ismonophonic() -> bool
Determine if content is monophonic (non-overlapping notes).
A monophonic list of notes has no overlapping notes (e.g., chords).
Serves as a helper function for ismonophonic and
parts_are_monophonic.
Returns:
-
bool–True if the list of notes is monophonic, False otherwise.
Source code in amads/core/basics.py
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 | |
insert_emptycopy_into
¶
insert_emptycopy_into(
parent: Optional[EventGroup] = None,
) -> EventGroup
Create a deep copy of the EventGroup except for content.
A new parent is provided as an argument and the copy is inserted into this parent. This method is useful for copying an EventGroup without copying its content. See also insert_copy_into to copy an EventGroup with its content into a new parent.
Parameters:
-
parent(Optional[EventGroup], default:None) –The new parent to insert the copied Event into.
Returns:
-
EventGroup–A deep copy of the EventGroup instance with the new parent (if any) and no content.
Source code in amads/core/basics.py
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 | |
expand_chords
¶
expand_chords(parent: Optional[EventGroup] = None) -> EventGroup
Replace chords with the multiple notes they contain.
Returns a deep copy with no parent unless parent is provided.
Normally, you will call score.expand_chords() which returns a deep
copy of Score with notes moved from each chord to the copy of the
chord's parent (a Measure or a Part). The parent parameter is
primarily for internal use when expand_chords is called recursively
on score content.
If a chord has a "rolled" property, after expansion, the notes will each have a "rolled" property.
Parameters:
-
parent(EventGroup, default:None) –The new parent to insert the copied EventGroup into.
Returns:
-
EventGroup–A deep copy of the EventGroup instance with all Chord instances expanded.
Source code in amads/core/basics.py
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 | |
find_all
¶
find_all(
elem_type: Type[Event],
include_tied_to_notes: bool = False,
ignore: list[Note] = [],
) -> Generator[Event, None, None]
Find all instances of a specific type within the EventGroup.
Assumes that objects of type elem_type are not nested within
other objects of the same type. (The first elem_type encountered
in a depth-first enumeration is returned without looking at any
children in its content).
If elem_type is Note and include_tied_to_notes is True, then
all Notes are returned. The default is to omit notes after the first
one in a string of tied notes. In the (rare) case of cross-staff ties
(which are possible to represent), find_all may return a tied-to
note that is encountered before its predecessor.
Parameters:
-
elem_type(Type[Event]) –The type of event to search for.
-
include_tied_to_notes(bool, default:False) –Applies only when
elem_typeis Note, in which case tied groups of notes are all returned wheninclude_tied_to_notesis True, and only the first note of the group is returned when False. Note that thedurationproperty returns the total tied group duration in the case that a Note is tied. -
ignore(list[Note], default:[]) –Do not use this parameter. It is for the implementation to keep track of forward references from tied notes.
Yields:
-
Event–Instances of the specified type found within the EventGroup.
Source code in amads/core/basics.py
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 | |
get_sorted_notes
¶
get_sorted_notes(has_ties: bool = True) -> List[Note]
Return a list of sorted notes with merged ties.
This should generally be called on Parts and Scores since
in all other EventGroups, Events are in time order and
Notes retrieved with find_all() or list_all() are in
time order. However, get_sorted_notes also sorts notes
into increasing pitch (keynum) where note onsets are equal.
Parameters:
-
has_ties(bool, default:True) –If True (default), copy the score, merge the ties, and return a list of these merged copies. If False, assume there are no ties and return a list of original notes.
Raises:
-
ValueError–If has_ties is False, but a tie is encountered.
Returns:
-
list(Note)–a list of sorted notes with merged ties
Source code in amads/core/basics.py
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 | |
has_instanceof
¶
has_instanceof(the_class: Type[Event]) -> bool
Test if EventGroup contains any instances of the_class.
Parameters:
-
the_class(Type[Event]) –The class type to check for.
Returns:
-
bool–True iff the EventGroup contains an instance of the_class.
Source code in amads/core/basics.py
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 | |
has_rests
¶
has_rests() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any Rest objects.
Returns:
-
bool–True iff the EventGroup contains any Rest objects.
Source code in amads/core/basics.py
1970 1971 1972 1973 1974 1975 1976 1977 1978 | |
has_chords
¶
has_chords() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any Chord objects.
Returns:
-
bool–True iff the EventGroup contains any Chord objects.
Source code in amads/core/basics.py
1981 1982 1983 1984 1985 1986 1987 1988 1989 | |
has_ties
¶
has_ties() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any tied notes.
Returns:
-
bool–True iff the EventGroup contains any tied notes.
Source code in amads/core/basics.py
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 | |
has_measures
¶
has_measures() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any Measures.
Returns:
-
bool–True iff the EventGroup contains any Measure objects.
Source code in amads/core/basics.py
2007 2008 2009 2010 2011 2012 2013 2014 2015 | |
inherit_duration
¶
inherit_duration() -> EventGroup
Set the duration of this EventGroup according to maximum offset.
The duration is set to the maximum offset (end) time of the
children. If the EventGroup is empty, the duration is set to 0.
For Notes with ties, use note.onset + note._duration (without ties)
to get the note offset (end) time so that ties across EventGroups
(especially Measures) are not considered for the duration of this
EventGroup. This method modifies this EventGroup instance.
Returns:
-
EventGroup–The EventGroup instance (self) with updated duration.
Source code in amads/core/basics.py
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 | |
insert
¶
insert(event: Event) -> EventGroup
Insert an event.
Sets the parent of event to this EventGroup and makes event
be a member of this EventGroup.content. No changes are made to
event.onset or self.duration. Insert event in content just
before the first element with a greater onset. The method modifies
this object (self).
Parameters:
-
event(Event) –The event to be inserted.
Returns:
-
EventGroup–The EventGroup instance (self) with the event inserted.
Raises:
-
ValueError–If event._onset is None (it must be a number)
Caution Regarding Zero-Duration Event Order
If you have note N with non-zero duration and, at the same time, a grace note G with zero duration, the notes will be ordered according to when they are inserted. So if you insert N and then G, then N will be placed before G. See io.pt_import.py, where this case is dealt with.
Source code in amads/core/basics.py
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 | |
last
¶
last() -> Optional[Event]
Retrieve the last event in the content list.
Because the content list is sorted by onset, the returned
Event is simply the last element of content, but not
necessarily the event with the greatest offset.
Returns:
-
Optional[Event]–The last event in the content list or None if the list is empty.
Source code in amads/core/basics.py
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 | |
list_all
¶
Find all instances of a specific type within the EventGroup.
Assumes that objects of type elem_type are not nested within
other objects of the same type. If elem_type is nested, only
the outer-most object is returned. See also
find_all, which returns
a generator instead of a list.
If elem_type is Note and include_tied_to_notes is True, then
all Notes are listed. The default is to omit notes after the first
one in a string of tied notes. In the (rare) case of cross-staff ties
(which are possible to represent), find_all may list a tied-to
note that is encountered before its predecessor.
Parameters:
-
elem_type(Type[Event]) –The type of event to search for.
-
include_tied_to_notes(bool, default:False) –Applies only when
elem_typeis Note, in which case tied groups of notes are all returned wheninclude_tied_to_notesis True, and only the first note of the group is returned when False. Note that thedurationproperty returns the total tied group duration in the case that a Note is tied.
Returns:
-
list[Event]–A list of all instances of the specified type found within the EventGroup.
Source code in amads/core/basics.py
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 | |
merge_tied_notes
¶
merge_tied_notes(
parent: Optional[EventGroup] = None, ignore: list[Note] = []
) -> EventGroup
Create a new EventGroup with tied notes replaced by single notes.
If ties cross staffs, the replacement is placed in the staff of the
first note in the tied sequence. Insert the new EventGroup into
parent.
Ordinarily, this method is called on a Score with no parameters. The
parameters are used when Score.merge_tied_notes() calls this method
recursively on EventGroups within the Score such as Parts and
Staffs.
Parameters:
-
parent(Optional[EventGroup], default:None) –Where to insert the result.
-
ignore(list[Note], default:[]) –This parameter is used internally. Caller should not use this parameter.
Returns:
-
EventGroup–A copy with tied notes replaced by equivalent single notes.
Source code in amads/core/basics.py
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 | |
pack
¶
pack(onset: float = 0.0, sequential: bool = False) -> float
Adjust the content to onsets starting with the onset parameter.
By default onsets are set to onset and the duration of self is set to
the maximum duration of the content. pack() works recursively on
elements that are EventGroups. Setting sequential to True implements
sequential packing, where events are placed one after another.
Parameters:
-
onset(float, default:0.0) –The onset (start) time for this object.
Returns:
-
float–duration of self
Source code in amads/core/basics.py
2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 | |
quantize
¶
quantize(
divisions: int, ignore: Optional[List[Note]] = None
) -> EventGroup
Align onsets and durations to a rhythmic grid.
Assumes time units are quarters. (See Score.convert_to_quarters.)
Modify all times and durations to a multiple of divisions per quarter note, e.g., 4 for sixteenth notes. Onsets and offsets are moved to the nearest quantized time. Any resulting duration change is less than one quantum, but not necessarily less than 0.5 quantum, since the onset and offset can round in opposite directions by up to 0.5 quantum each. Any non-zero duration that would quantize to zero duration gets a duration of one quantum since zero duration is almost certainly going to cause notation and visualization problems.
Special cases for zero duration:
- If the original duration is zero as in metadata or possibly grace notes, we preserve that.
- If a tied note duration quantizes to zero, we remove the tied note entirely provided some other note in the tied sequence has non-zero duration. If all tied notes quantize to zero, we keep the first one and set its duration to one quantum.
This method modifies this EventGroup and all its content in place.
Note that there is no way to specify "sixteenths or eighth triplets" because 6 would not allow sixteenths and 12 would admit sixteenth triplets. Using tuples as in Music21, e.g., (4, 3) for this problem creates another problem: if quantization is to time points 1/4, 1/3, then the difference is 1/12 or a thirty-second triplet. If the quantization is applied to durations, then you could have 1/4 + 1/3 = 7/12, and the remaining duration in a single beat would be 5/12, which is not expressible as sixteenths, eighth triplets or any tied combination.
Parameters:
-
divisions(int) –The number of divisions per quarter note, e.g., 4 for sixteenths, to control quantization.
-
ignore(Optional[List[Note]], default:None) –The list of tied-to notes to ignore during quantization because they have already been quantized. Do not provide this parameter; it is for internal implementation only.
Returns:
-
EventGroup–The EventGroup instance (self) with (modified in place) quantized times.
Source code in amads/core/basics.py
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 | |
remove
¶
remove(element: Event) -> EventGroup
Remove an element from the content list.
The method modifies this object (self).
Parameters:
-
element(Event) –The event to be removed.
Returns:
-
EventGroup–The EventGroup instance (self) with the element removed. The returned value is not a copy.
Source code in amads/core/basics.py
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 | |
remove_rests
¶
remove_rests(parent: Union[EventGroup, None] = None) -> EventGroup
Remove all Rest objects from content.
Returns a deep copy with no parent unless parent is provided.
Parameters:
-
parent(EventGroup, default:None) –The new parent to insert the copied Event into.
Returns:
-
EventGroup–A deep copy of the EventGroup instance with all Rest objects removed.
Source code in amads/core/basics.py
2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 | |
show
¶
show(indent: int = 0, file: Optional[TextIO] = None) -> EventGroup
Print the EventGroup information.
Parameters:
-
indent(int, default:0) –The indentation level for display.
Returns:
-
EventGroup–The EventGroup instance itself.
Source code in amads/core/basics.py
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 | |
measure_times
¶
measure_times(n: int) -> tuple[float, float]
Return the start and end times of measure n.
Parameters:
-
n(int) –The 0-based measure number.
Returns:
-
tuple(float, float)–A tuple containing the start and end times of measure n. Measure 0 is the first measure. Units are the same as the score's time units.
Raises:
-
ValueError–If there is no measure with number n.
Source code in amads/core/basics.py
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 | |
slice
¶
slice(
start: float,
end: float,
units: str = "quarters",
mode: str = "onsets",
truncate: str = "keep",
shift: bool = False,
min_duration: float = 0.05,
) -> EventGroup
Create a new Score containing the content between start and end.
Typically, this method is called on a Score to create a new Score,
but it works on any Sequence as long as the sequence is part of a Score.
If self has Measures, units must be "measures" or "bars", the start and
end parameters are interpreted as 0-based measure numbers, and the
result will therefore contain whole measures. It is assumed that notes
are tied across measure boundaries. The result will contain only the
parts of tied notes that are within the specified measures, excluding
< measure numbered by end.
For any units besides "measures" or "bars", the score must be flat.
Notes that extend beyond the start or end time are included in the
result unless the `truncate` parameter specifies otherwise.
In principle, there is no reason to require a flat score for slicing by
time, but there are many difficult edge cases to consider such as ties
from within one chord to a note in another measure. Therefore, to slice
by time or quarter, you must have a flattened score. Conversely, to
slice a full score, you must slice by measure so that the result gives
you whole measures, which are encoded into the full Score structure.
The entire result can be shifted to start at time 0 by setting the
`shift` parameter to True. Unless `truncate` is "truncate" or
"beginning", the result can include events that start before the start
time, in which case shifting will only shift the earliest onset to 0,
which means the shift amount depends upon the content. (Negative onsets
are currently not allowed.)
The resulting Score, Part, and other content will have onset and offset
times matching start and end, even if some content starts earlier or
ends later.
Parameters
Parameters
start : float
The start time of the slice, inclusive. If units is "measures" or
"bars", start is a 0-based measure number of the first measure.
end : float
The end time of the slice, exclusive. For measures, end is the
0-based measure number of the last measure plus one, so the last
measure is end - 1.
units : str
The time units for start and end. Valid values are "quarters", "s",
"sec", "seconds", "measures", or "bars".
shift: bool
If true, shift the content in the result so that the result starts
time 0.0. If false (default), the content in the result retains
the same time values as in the original Score.
mode : Optional[str]
The slicing mode, ignored if units is "measures" or "bars".
Valid values are:
- `"onsets"` - include events that start within bounds (default)
- `"overlaps"` - include events that overlap (either onset or
offset) is within the bounds. An event ending is considered
to overlap if its offset is greater than start.
- `"offsets"` - include events that end within the bounds
- `"strict"` - include only events that start at or after start
and end at or before end. (End times that are exactly equal
to end are considered to be within bounds, so this is
inclusive of end time.)
truncate: Optional[str]
How to handle events that partially overlap the bounds,
ignored if units is "measures" or "bars". Valid values are:
- `"truncate"` - truncate (clip) events that partially overlap
the bounds. If the event onset is before start, the onset
is increased to start (adjusting duration to maintain the
offset time). Also, if the event offset is after end, the
duration is decreased so that the offset time is end.
- `"keep"` - no modification/truncation (default). Note that
with the default mode="onsets", no event onsets will be
earlier than start, but offsets may extend beyond end.
- `"end"` or "ending" or "dur" or "duration" - truncate (clip)
events that extend beyond end so that all offsets <= end.
- `"beginning"` - events with onsets before start are moved to
make their onsets equal to start, and their duration is
adjusted to maintain the original offset.
min_duration: Optional[float]
Events with duration less than min_duration (after any truncation
is applied) are removed. Ignored if units is "measures" or "bars".
If None or 0.0, no events are removed. The default is 0.05,
independent of time units. Be careful with gracenotes that, when
read from MusicXML, have zero duration.
Returns
Returns
Score
A new Score instance containing the content between start and end.
Raises
Raises
ValueError
If the Sequence is not part of a Score, or if invalid values are
provided for the parameters.
Source code in amads/core/basics.py
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 | |
is_well_formed_full_part
¶
is_well_formed_full_part()
Test if Part is measured and well-formed.
Part must conform to a strict hierarchy of: Part-Staff-Measure-(Note or Rest or Chord) and Chord-Note.
Source code in amads/core/basics.py
4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 | |
flatten
¶
flatten(in_place=False)
Build a flat Part where content will consist of notes only.
Parameters:
-
in_place(bool, default:False) –If in_place=True, assume Part already has no ties and can be modified. Otherwise, return a new Part where deep copies of tied notes are merged.
Returns:
-
Part–a new part that has been flattened
Source code in amads/core/basics.py
4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 | |
is_flat
¶
is_flat()
Test if Part is flat (contains only notes without ties).
Returns:
-
bool–True iff the Part is flat
Source code in amads/core/basics.py
4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 | |
remove_measures
¶
Return a Part with all Measures removed.
Preserves Staffs in the hierarchy. Notes are “lifted” from Measures
to become direct content of their Staff. Uses merge_tied_notes()
to copy this Part unless has_ties is False, in which case
there must be no tied notes and this Part is modified. (Note: it is
harmless for has_ties to be True even if there are no ties. This
will simply copy the Part before removing measures.)
Parameters:
-
score(Union[Score, None]) –The Score instance (if any) to which the new Part will be added.
-
has_ties(bool, default:True) –If False, assume this is a copy we are free to modify, there are tied notes, and this Part is already contained by
score. If True, this Part will be copied intoscore.
Returns:
-
Part–A Part with all Measures removed.
Source code in amads/core/basics.py
4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 | |
calc_differences
¶
calc_differences(what: List[str]) -> List[Note]
Calculate inter-onset intervals (IOIs), IOI-ratios and intervals.
This method modifies the Part in place, calculating several
differences between notes. Onset differences (IOIs) are the
time differences between a Note's onset and the onset of the
previous Note in the same Part (regardless of Staff). The IOI
of the first Note in the Part is set to None. The IOI values
are computed when what contains "ioi" or "ioi_ratio" and
stored as "ioi" in the Note's info dictionary.
The IOI-ratio of a Note is the ratio of its IOI to the IOI of
the previous Note. The IOI-ratios of the first two Notes are set
to None. The IOI-ratio values are computed when what contains
"ioi_ratio" and stored as "ioi_ratio" in the Note's info
dictionary.
The pitch interval of a Note is the difference in semitones between
its pitch and the pitch of the previous Note in the same Part. The
interval of the first Note in the Part is set to None. The interval
values are computed when what contains "interval" and stored
as "interval" in the Note's info dictionary.
Note that this method assumes that the Part has no concurrent Notes (IOI == 0) and no ties. In either case, a ValueError is raised.
Parameters:
-
what(list of str) –A list of strings indicating what differences to compute. Valid strings are: 'ioi' (for inter-onset intervals), 'ioi_ratio' (for ratio of successive IOIs), and 'interval' (for pitch intervals in semitones).
Raises:
-
ValueError–If there are tied notes or concurrent notes in the Part or if
whatdoes not contain any of "ioi", "ioi_ratio" or "interval".
Returns:
-
List[Note]–The sorted list of Notes with calculated IOIs and IOI-ratios.
Source code in amads/core/basics.py
4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 | |
Score
¶
Score(
*args: Event,
onset: Optional[float] = 0,
duration: Optional[float] = None,
time_map: Optional[TimeMap] = None,
time_signatures: Optional[List[TimeSignature]] = None
)
Bases: Concurrence
A Score (abstract class) represents a musical work.
Normally, a Score contains Part objects, all with onsets zero, and has no parent.
See Constructor Details.
Additional properties may be assigned, e.g., 'title', 'source_file', 'composer', etc. (See set.)
Parameters:
-
*args(Event, default:()) –A variable number of Event objects to be added to the group.
-
onset(Optional[float], default:0) –The onset (start) time. If unknown (None), onset will be set when the score is added to a parent, but normally, Scores do not have parents, so the default onset is 0. You can override this using keyword parameter (due to
*args). -
duration(Optional[float], default:None) –The duration in quarters or seconds. (If duration is omitted or None, the duration is set so that self.offset ends at the max offset of args, or 0 if there is no content.) Must be passed as a keyword parameter due to
*args. -
time_map(TimeMap, default:None) –A map from quarters to seconds (or seconds to quarters). Must be passed as a keyword parameter due to
*args.
Attributes:
-
parent(Optional[EventGroup]) –The containing object or None.
-
_onset(float) –The onset (start) time.
-
duration(float) –The duration in quarters or seconds.
-
content(list[Event]) –Elements contained within this collection.
-
time_map(TimeMap) –A map from quarters to seconds (or seconds to quarters).
-
time_signatures(list[TimeSignature]) –A list of all time signature changes
-
_units_are_seconds(bool) –True if the units are seconds, False if the units are quarters.
Source code in amads/core/basics.py
3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 | |
Attributes¶
onset
property
writable
¶
onset: float
Retrieve the onset (start) time.
If the onset is None, raise an exception. (Events can have None onset times, but they must be set before retrieval. onsets that are None are automatically set when the Event is added to an EventGroup.)
Returns:
-
float–The onset (start) time.
Raises:
-
ValueError–If the onset time is not set (None).
duration
property
writable
¶
duration: float
Retrieve the duration of the event.
Returns:
-
float–The duration of the event in quarters or seconds.
offset
property
writable
¶
offset: float
Retrieve the global offset (stop) time.
Returns:
-
float–The global offset (stop) time.
measure
property
¶
measure: Optional[Measure]
Retrieve the Measure containing this event
Returns:
-
Optional[Measure]–The Measure containing this event or None if not found.
units_are_quarters
property
¶
units_are_quarters: bool
True if the units are in quarters, False if in seconds.
units_are_seconds
property
¶
units_are_seconds: bool
True if the units are in seconds, False if in quarters.
Functions¶
set
¶
set(property: str, value: Any) -> Event
Set a named property on this Event.
Every event can be extended with additional properties. Although
Python objects are already extensible with new attributes, new
attributes that are not set in __init__ confuse type checkers
and other tools, so every Event has an info attribute as a
dictionary where additional, application-specific information can
be stored. The info attribute is None to save space until the
first property is set, so you should use set and get methods
and avoid writing event.info[property].
Parameters:
-
property(str) –The name of the property to set.
-
value(Any) –The value to assign to the property.
Returns:
-
Event–returns this object (self)
Examples:
>>> note = Note()
>>> note.get("color", "no color")
'no color'
>>> _ = note.set("color", "red").set("harmonicity", 0.2)
>>> (note.has("color"), note.has("shape"))
(True, False)
>>> (note.get("color"), note.get("harmonicity"))
('red', 0.2)
Source code in amads/core/basics.py
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 | |
get
¶
get(property: str, default: Any = None) -> Any
Get the value of a property from this Event.
See set for more details.
Parameters:
-
property(str.) –The name of the property to get.
-
default(Any, default:None) –The default value to return if the property is not found.
Returns:
-
Any–The value of the specified property.
Source code in amads/core/basics.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
has
¶
has(property) -> bool
Check if the Event has a specific property.
See set for more details.
Parameters:
-
property(str) –The name of the property to check.
Returns:
-
bool–True if the property exists, False otherwise.
Source code in amads/core/basics.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |
insert_copy_into
¶
insert_copy_into(parent: Optional[EventGroup] = None) -> Event
Make a (mostly) deep copy of the Event and add to a new parent.
Pitch objects are considered immutable and are shared rather
than copied.
Parameters:
-
parent(Optional(EventGroup), default:None) –The copied
Eventwill be a child ofparentif notNone. The parent is modified by this operation.
Returns:
-
Event–A deep copy (except for parent and pitch) of the Event instance.
Source code in amads/core/basics.py
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 | |
ismonophonic
¶
ismonophonic() -> bool
Determine if content is monophonic (non-overlapping notes).
A monophonic list of notes has no overlapping notes (e.g., chords).
Serves as a helper function for ismonophonic and
parts_are_monophonic.
Returns:
-
bool–True if the list of notes is monophonic, False otherwise.
Source code in amads/core/basics.py
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 | |
insert_emptycopy_into
¶
insert_emptycopy_into(
parent: Optional[EventGroup] = None,
) -> EventGroup
Create a deep copy of the EventGroup except for content.
A new parent is provided as an argument and the copy is inserted into this parent. This method is useful for copying an EventGroup without copying its content. See also insert_copy_into to copy an EventGroup with its content into a new parent.
Parameters:
-
parent(Optional[EventGroup], default:None) –The new parent to insert the copied Event into.
Returns:
-
EventGroup–A deep copy of the EventGroup instance with the new parent (if any) and no content.
Source code in amads/core/basics.py
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 | |
expand_chords
¶
expand_chords(parent: Optional[EventGroup] = None) -> EventGroup
Replace chords with the multiple notes they contain.
Returns a deep copy with no parent unless parent is provided.
Normally, you will call score.expand_chords() which returns a deep
copy of Score with notes moved from each chord to the copy of the
chord's parent (a Measure or a Part). The parent parameter is
primarily for internal use when expand_chords is called recursively
on score content.
If a chord has a "rolled" property, after expansion, the notes will each have a "rolled" property.
Parameters:
-
parent(EventGroup, default:None) –The new parent to insert the copied EventGroup into.
Returns:
-
EventGroup–A deep copy of the EventGroup instance with all Chord instances expanded.
Source code in amads/core/basics.py
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 | |
find_all
¶
find_all(
elem_type: Type[Event],
include_tied_to_notes: bool = False,
ignore: list[Note] = [],
) -> Generator[Event, None, None]
Find all instances of a specific type within the EventGroup.
Assumes that objects of type elem_type are not nested within
other objects of the same type. (The first elem_type encountered
in a depth-first enumeration is returned without looking at any
children in its content).
If elem_type is Note and include_tied_to_notes is True, then
all Notes are returned. The default is to omit notes after the first
one in a string of tied notes. In the (rare) case of cross-staff ties
(which are possible to represent), find_all may return a tied-to
note that is encountered before its predecessor.
Parameters:
-
elem_type(Type[Event]) –The type of event to search for.
-
include_tied_to_notes(bool, default:False) –Applies only when
elem_typeis Note, in which case tied groups of notes are all returned wheninclude_tied_to_notesis True, and only the first note of the group is returned when False. Note that thedurationproperty returns the total tied group duration in the case that a Note is tied. -
ignore(list[Note], default:[]) –Do not use this parameter. It is for the implementation to keep track of forward references from tied notes.
Yields:
-
Event–Instances of the specified type found within the EventGroup.
Source code in amads/core/basics.py
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 | |
get_sorted_notes
¶
get_sorted_notes(has_ties: bool = True) -> List[Note]
Return a list of sorted notes with merged ties.
This should generally be called on Parts and Scores since
in all other EventGroups, Events are in time order and
Notes retrieved with find_all() or list_all() are in
time order. However, get_sorted_notes also sorts notes
into increasing pitch (keynum) where note onsets are equal.
Parameters:
-
has_ties(bool, default:True) –If True (default), copy the score, merge the ties, and return a list of these merged copies. If False, assume there are no ties and return a list of original notes.
Raises:
-
ValueError–If has_ties is False, but a tie is encountered.
Returns:
-
list(Note)–a list of sorted notes with merged ties
Source code in amads/core/basics.py
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 | |
has_instanceof
¶
has_instanceof(the_class: Type[Event]) -> bool
Test if EventGroup contains any instances of the_class.
Parameters:
-
the_class(Type[Event]) –The class type to check for.
Returns:
-
bool–True iff the EventGroup contains an instance of the_class.
Source code in amads/core/basics.py
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 | |
has_rests
¶
has_rests() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any Rest objects.
Returns:
-
bool–True iff the EventGroup contains any Rest objects.
Source code in amads/core/basics.py
1970 1971 1972 1973 1974 1975 1976 1977 1978 | |
has_chords
¶
has_chords() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any Chord objects.
Returns:
-
bool–True iff the EventGroup contains any Chord objects.
Source code in amads/core/basics.py
1981 1982 1983 1984 1985 1986 1987 1988 1989 | |
has_ties
¶
has_ties() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any tied notes.
Returns:
-
bool–True iff the EventGroup contains any tied notes.
Source code in amads/core/basics.py
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 | |
has_measures
¶
has_measures() -> bool
Test if EventGroup (e.g., Score, Part, ...) has any Measures.
Returns:
-
bool–True iff the EventGroup contains any Measure objects.
Source code in amads/core/basics.py
2007 2008 2009 2010 2011 2012 2013 2014 2015 | |
inherit_duration
¶
inherit_duration() -> EventGroup
Set the duration of this EventGroup according to maximum offset.
The duration is set to the maximum offset (end) time of the
children. If the EventGroup is empty, the duration is set to 0.
For Notes with ties, use note.onset + note._duration (without ties)
to get the note offset (end) time so that ties across EventGroups
(especially Measures) are not considered for the duration of this
EventGroup. This method modifies this EventGroup instance.
Returns:
-
EventGroup–The EventGroup instance (self) with updated duration.
Source code in amads/core/basics.py
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 | |
insert
¶
insert(event: Event) -> EventGroup
Insert an event.
Sets the parent of event to this EventGroup and makes event
be a member of this EventGroup.content. No changes are made to
event.onset or self.duration. Insert event in content just
before the first element with a greater onset. The method modifies
this object (self).
Parameters:
-
event(Event) –The event to be inserted.
Returns:
-
EventGroup–The EventGroup instance (self) with the event inserted.
Raises:
-
ValueError–If event._onset is None (it must be a number)
Caution Regarding Zero-Duration Event Order
If you have note N with non-zero duration and, at the same time, a grace note G with zero duration, the notes will be ordered according to when they are inserted. So if you insert N and then G, then N will be placed before G. See io.pt_import.py, where this case is dealt with.
Source code in amads/core/basics.py
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 | |
last
¶
last() -> Optional[Event]
Retrieve the last event in the content list.
Because the content list is sorted by onset, the returned
Event is simply the last element of content, but not
necessarily the event with the greatest offset.
Returns:
-
Optional[Event]–The last event in the content list or None if the list is empty.
Source code in amads/core/basics.py
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 | |
list_all
¶
Find all instances of a specific type within the EventGroup.
Assumes that objects of type elem_type are not nested within
other objects of the same type. If elem_type is nested, only
the outer-most object is returned. See also
find_all, which returns
a generator instead of a list.
If elem_type is Note and include_tied_to_notes is True, then
all Notes are listed. The default is to omit notes after the first
one in a string of tied notes. In the (rare) case of cross-staff ties
(which are possible to represent), find_all may list a tied-to
note that is encountered before its predecessor.
Parameters:
-
elem_type(Type[Event]) –The type of event to search for.
-
include_tied_to_notes(bool, default:False) –Applies only when
elem_typeis Note, in which case tied groups of notes are all returned wheninclude_tied_to_notesis True, and only the first note of the group is returned when False. Note that thedurationproperty returns the total tied group duration in the case that a Note is tied.
Returns:
-
list[Event]–A list of all instances of the specified type found within the EventGroup.
Source code in amads/core/basics.py
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 | |
merge_tied_notes
¶
merge_tied_notes(
parent: Optional[EventGroup] = None, ignore: list[Note] = []
) -> EventGroup
Create a new EventGroup with tied notes replaced by single notes.
If ties cross staffs, the replacement is placed in the staff of the
first note in the tied sequence. Insert the new EventGroup into
parent.
Ordinarily, this method is called on a Score with no parameters. The
parameters are used when Score.merge_tied_notes() calls this method
recursively on EventGroups within the Score such as Parts and
Staffs.
Parameters:
-
parent(Optional[EventGroup], default:None) –Where to insert the result.
-
ignore(list[Note], default:[]) –This parameter is used internally. Caller should not use this parameter.
Returns:
-
EventGroup–A copy with tied notes replaced by equivalent single notes.
Source code in amads/core/basics.py
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 | |
quantize
¶
quantize(
divisions: int, ignore: Optional[List[Note]] = None
) -> EventGroup
Align onsets and durations to a rhythmic grid.
Assumes time units are quarters. (See Score.convert_to_quarters.)
Modify all times and durations to a multiple of divisions per quarter note, e.g., 4 for sixteenth notes. Onsets and offsets are moved to the nearest quantized time. Any resulting duration change is less than one quantum, but not necessarily less than 0.5 quantum, since the onset and offset can round in opposite directions by up to 0.5 quantum each. Any non-zero duration that would quantize to zero duration gets a duration of one quantum since zero duration is almost certainly going to cause notation and visualization problems.
Special cases for zero duration:
- If the original duration is zero as in metadata or possibly grace notes, we preserve that.
- If a tied note duration quantizes to zero, we remove the tied note entirely provided some other note in the tied sequence has non-zero duration. If all tied notes quantize to zero, we keep the first one and set its duration to one quantum.
This method modifies this EventGroup and all its content in place.
Note that there is no way to specify "sixteenths or eighth triplets" because 6 would not allow sixteenths and 12 would admit sixteenth triplets. Using tuples as in Music21, e.g., (4, 3) for this problem creates another problem: if quantization is to time points 1/4, 1/3, then the difference is 1/12 or a thirty-second triplet. If the quantization is applied to durations, then you could have 1/4 + 1/3 = 7/12, and the remaining duration in a single beat would be 5/12, which is not expressible as sixteenths, eighth triplets or any tied combination.
Parameters:
-
divisions(int) –The number of divisions per quarter note, e.g., 4 for sixteenths, to control quantization.
-
ignore(Optional[List[Note]], default:None) –The list of tied-to notes to ignore during quantization because they have already been quantized. Do not provide this parameter; it is for internal implementation only.
Returns:
-
EventGroup–The EventGroup instance (self) with (modified in place) quantized times.
Source code in amads/core/basics.py
2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 | |
remove
¶
remove(element: Event) -> EventGroup
Remove an element from the content list.
The method modifies this object (self).
Parameters:
-
element(Event) –The event to be removed.
Returns:
-
EventGroup–The EventGroup instance (self) with the element removed. The returned value is not a copy.
Source code in amads/core/basics.py
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 | |
remove_rests
¶
remove_rests(parent: Union[EventGroup, None] = None) -> EventGroup
Remove all Rest objects from content.
Returns a deep copy with no parent unless parent is provided.
Parameters:
-
parent(EventGroup, default:None) –The new parent to insert the copied Event into.
Returns:
-
EventGroup–A deep copy of the EventGroup instance with all Rest objects removed.
Source code in amads/core/basics.py
2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 | |
measure_times
¶
measure_times(n: int) -> tuple[float, float]
Return the start and end times of measure n.
Parameters:
-
n(int) –The 0-based measure number.
Returns:
-
tuple(float, float)–A tuple containing the start and end times of measure n. Measure 0 is the first measure. Units are the same as the score's time units.
Raises:
-
ValueError–If there is no measure with number n.
Source code in amads/core/basics.py
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 | |
slice
¶
slice(
start: float,
end: float,
units: str = "quarters",
mode: str = "onsets",
truncate: str = "keep",
shift: bool = False,
min_duration: float = 0.05,
) -> EventGroup
Create a new Score containing the content between start and end.
Typically, this method is called on a Score to create a new Score,
but it works on any Sequence as long as the sequence is part of a Score.
If self has Measures, units must be "measures" or "bars", the start and
end parameters are interpreted as 0-based measure numbers, and the
result will therefore contain whole measures. It is assumed that notes
are tied across measure boundaries. The result will contain only the
parts of tied notes that are within the specified measures, excluding
< measure numbered by end.
For any units besides "measures" or "bars", the score must be flat.
Notes that extend beyond the start or end time are included in the
result unless the `truncate` parameter specifies otherwise.
In principle, there is no reason to require a flat score for slicing by
time, but there are many difficult edge cases to consider such as ties
from within one chord to a note in another measure. Therefore, to slice
by time or quarter, you must have a flattened score. Conversely, to
slice a full score, you must slice by measure so that the result gives
you whole measures, which are encoded into the full Score structure.
The entire result can be shifted to start at time 0 by setting the
`shift` parameter to True. Unless `truncate` is "truncate" or
"beginning", the result can include events that start before the start
time, in which case shifting will only shift the earliest onset to 0,
which means the shift amount depends upon the content. (Negative onsets
are currently not allowed.)
The resulting Score, Part, and other content will have onset and offset
times matching start and end, even if some content starts earlier or
ends later.
Parameters
Parameters
start : float
The start time of the slice, inclusive. If units is "measures" or
"bars", start is a 0-based measure number of the first measure.
end : float
The end time of the slice, exclusive. For measures, end is the
0-based measure number of the last measure plus one, so the last
measure is end - 1.
units : str
The time units for start and end. Valid values are "quarters", "s",
"sec", "seconds", "measures", or "bars".
shift: bool
If true, shift the content in the result so that the result starts
time 0.0. If false (default), the content in the result retains
the same time values as in the original Score.
mode : Optional[str]
The slicing mode, ignored if units is "measures" or "bars".
Valid values are:
- `"onsets"` - include events that start within bounds (default)
- `"overlaps"` - include events that overlap (either onset or
offset) is within the bounds. An event ending is considered
to overlap if its offset is greater than start.
- `"offsets"` - include events that end within the bounds
- `"strict"` - include only events that start at or after start
and end at or before end. (End times that are exactly equal
to end are considered to be within bounds, so this is
inclusive of end time.)
truncate: Optional[str]
How to handle events that partially overlap the bounds,
ignored if units is "measures" or "bars". Valid values are:
- `"truncate"` - truncate (clip) events that partially overlap
the bounds. If the event onset is before start, the onset
is increased to start (adjusting duration to maintain the
offset time). Also, if the event offset is after end, the
duration is decreased so that the offset time is end.
- `"keep"` - no modification/truncation (default). Note that
with the default mode="onsets", no event onsets will be
earlier than start, but offsets may extend beyond end.
- `"end"` or "ending" or "dur" or "duration" - truncate (clip)
events that extend beyond end so that all offsets <= end.
- `"beginning"` - events with onsets before start are moved to
make their onsets equal to start, and their duration is
adjusted to maintain the original offset.
min_duration: Optional[float]
Events with duration less than min_duration (after any truncation
is applied) are removed. Ignored if units is "measures" or "bars".
If None or 0.0, no events are removed. The default is 0.05,
independent of time units. Be careful with gracenotes that, when
read from MusicXML, have zero duration.
Returns
Returns
Score
A new Score instance containing the content between start and end.
Raises
Raises
ValueError
If the Sequence is not part of a Score, or if invalid values are
provided for the parameters.
Source code in amads/core/basics.py
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 | |
from_melody
classmethod
¶
from_melody(
pitches: list[Union[Pitch, int, float, str]],
durations: Union[float, list[float]] = 1.0,
iois: Optional[Union[float, list[float]]] = None,
onsets: Optional[list[float]] = None,
ties: Optional[list[bool]] = None,
) -> Score
Create a Score from a melody specified by pitches and timing.
Parameters:
-
pitches(list of int or list of Pitch) –MIDI note numbers or Pitch objects for each note.
-
durations(float or list of float, default:1.0) –Durations in quarters for each note. If a scalar value, it will be repeated for all notes.
-
iois(float or list of float or None Inter-onset, default:None) –intervals between successive notes. If a scalar value, it will be repeated for all notes. If not provided and onsets is None, takes values from the durations argument, assuming that notes are placed sequentially without overlap.
-
onsets(list of float or None, default:None) –Start times. Cannot be used together with iois. If both are None, defaults to using durations as IOIs.
-
ties(list of bool or None, default:None) –If provided, a list of booleans indicating whether each note is tied to the next note. The last note's tie value is ignored. If None, no ties are created.
Returns:
-
Score–A new (flat) Score object containing the melody. If pitches is empty, returns a score with an empty part.
Examples:
Create a simple C major scale with default timing (sequential quarter notes):
>>> score = Score.from_melody([60, 62, 64, 65, 67, 69, 71, 72])
>>> notes = score.content[0].content
>>> len(notes) # number of notes in first part
8
>>> notes[0].key_num
60
>>> score.duration # last note ends at t=8
8.0
Create three notes with varying durations:
>>> score = Score.from_melody(
... pitches=[60, 62, 64], # C4, D4, E4
... durations=[0.5, 1.0, 2.0],
... )
>>> score.duration # last note ends at t=3.5
3.5
Create three notes with custom IOIs:
>>> score = Score.from_melody(
... pitches=[60, 62, 64], # C4, D4, E4
... durations=1.0, # quarter notes
... iois=2.0, # 2 beats between each note onset
... )
>>> score.duration # last note ends at t=5
5.0
Create three notes with explicit onsets:
>>> score = Score.from_melody(
... pitches=[60, 62, 64], # C4, D4, E4
... durations=1.0, # quarter notes
... onsets=[0.0, 2.0, 4.0], # onset times 2 beats apart
... )
>>> score.duration # last note ends at t=5
5.0
Source code in amads/core/basics.py
3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 | |
copy
¶
copy()
Make a deep copy.
This is equivalent to EventGroup.insert_copy_into, and provided because scores do not normally have a parent and there is nothing to "copy into."
Returns:
-
Score–a copy of the score.
Source code in amads/core/basics.py
3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 | |
emptycopy
¶
emptycopy()
Copy score without content.
Since a Score does not normally have a parent, it is normal for the
parent to be None, so emptycopy() is provided to make code more
readable.
Returns:
-
Score–a copy of the score with no content
Source code in amads/core/basics.py
3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 | |
append_time_signature
¶
append_time_signature(time_signature: TimeSignature) -> None
Append a time signature change to the score.
If there is already a time signature at the given time, it is replaced.
Parameters:
-
time_signature(TimeSignature) –The time signature to append.
Source code in amads/core/basics.py
3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 | |
calc_differences
¶
calc_differences(what: List[str]) -> List[List[Note]]
Calculate inter-onset intervals (IOIs), IOI-ratios and intervals.
This method is a convenience function that calls Part.calc_differences() on each Part of the Score. Since this method requires that Notes have no ties and are not concurrent (IOI == 0), the Score will normally be flat, which means only one Part.
Parameters:
-
what(list of str) –A list of strings indicating what differences to compute. Valid strings are: 'ioi' (for inter-onset intervals), 'ioi_ratio' (for ratio of successive IOIs), and 'interval' (for pitch intervals in semitones).
Returns:
-
list of List[Note]–A list of Notes from each Part with the requested difference properties set.
Source code in amads/core/basics.py
3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 | |
convert_to_seconds
¶
convert_to_seconds() -> None
Convert the score to represent time in seconds.
This function modifies Score without making a copy.
Source code in amads/core/basics.py
3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 | |
convert_to_quarters
¶
convert_to_quarters() -> None
Convert the score to represent time in quarters.
This function modifies Score without making a copy.
Source code in amads/core/basics.py
3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 | |
collapse_parts
¶
collapse_parts(part=None, staff=None, has_ties=True)
Merge the notes of selected Parts and Staffs.
This function is used to extract only selected parts or staffs from a Score and return the data as a flat Score (only one part containing only Notes, with ties merged).
The flatten() method is similar and generally preferred. Use
this collapse_parts() only if you want to select an individual
Staff (e.g., only the left hand when left and right appear as
two staffs) or when you only want to process one Part and avoid
the cost of flattening all Parts with flatten().
If you are calling this method to extract notes separately for each
Staff, it may do extra work. You can call find_all(Note) or
list_all(Note) to get the notes from any Staff, ignoring tied-to
notes, and the duration property of each note includes the duration
of any tied sequence following that note.
Another possibility, if you really want multiple flat scores with selected staffs or parts, is to save some computation by performing a one-time
score = score.merge_tied_notes()
and then call collapse_parts with the parameter has_ties=False.
This pays an up-front cost of merging tied notes, but saves the cost of
merging tied notes multiple times as each part or staff is extracted.
Parameters:
-
part(Union[int, str, list[int], None], default:None) –If part is not None, only notes from the selected part are included:
- part may be an integer to match a part number (
numberis an attribute ofPart), or - part may be a string to match a part instrument, or
- part may be a list with an index, e.g., [3] will select the 4th part (because indexing is zero-based).
- part may be an integer to match a part number (
-
staff(Union[int, List[int], None], default:None) –If staff is given, only the notes from selected staves are included. Note that staff selection requires part selection. Thus, if staff is given without part, an Exception is raised. Also, if staff is given and this is a flat score (no staves), an Exception is raised. Staff selection works as follows:
- staff may be an integer to match a staff number, or
- staff may be a list with an index, e.g., [1] will select the 2nd staff.
-
has_ties(bool, default:True) –Indicates the possibility of tied notes, which must be merged as part of flattening. If the parts are flat already, setting has_ties=False will save some computation.
Raises:
-
ValueError–A ValueError is raised if:
- staff is given without a part specification
- staff is given and this is a flat score (no staves)
Note
The use of lists like [1] for part and staff index notation is not ideal, but parts can be assigned a designated number that is not the same as the index, so we need a way to select by designated number, e.g., 1, and by index, e.g., [1]. Initially, I used tuples, but they are error prone. E.g., part=(0) means part=0, so you would have to write collapse_parts(part=((0))). With [n] notation, you write collapse_parts(part=[0]) to indicate an index. This is prettier and less prone to error.
Source code in amads/core/basics.py
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 | |
flatten
¶
flatten(collapse=False)
Deep copy notes in a score to a flat score.
A flat score consists of only Parts containing Notes (ties are merged).
See collapse_parts to select specific Parts or Staffs and flatten them.
Parameters:
-
collapse(bool, default:False) –If collapse is True, multiple parts are collapsed into a single part, and notes are ordered according to onset times. The resulting score contains one or more Parts, each containing only Notes.
Source code in amads/core/basics.py
3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 | |
is_flat
¶
is_flat()
Test if Score is flat.
a flat Score conforms to strict hierarchy of: Score-Part-Note with no tied notes.
Returns:
-
bool–True iff the score is flat.
Source code in amads/core/basics.py
3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 | |
is_flat_and_collapsed
¶
is_flat_and_collapsed()
Determine if score has been flattened into one part
Returns:
-
bool–True iff the score is flat and has one part.
Source code in amads/core/basics.py
3652 3653 3654 3655 3656 3657 3658 3659 3660 | |
is_well_formed_full_score
¶
is_well_formed_full_score() -> bool
Test if Score is a well-formed full score.
A well-formed full score is measured and conforms to a strict hierarchy of: Score-Part-Staff-Measure-(Note or Rest or Chord) and Chord-Note.
Returns:
-
bool–True iff the Score is a well-formed full Score.
Source code in amads/core/basics.py
3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 | |
note_containers
¶
note_containers()
Returns a list of non-empty note containers.
For full (measured) Scores, these are the Staff objects. For flat Scores, these are the Part objects. This is mainly useful for extracting note sequences where each part or staff represents a separate sequence. This method will retrieve either parts or staffs, whichever applies. This implementation also handles a mix of Parts with and without Staffs, returning a list of whichever is the direct parent of a list of Notes.
Returns:
-
list(EventGroup)–list of (recursively) contained EventGroups that contain Notes
Source code in amads/core/basics.py
3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 | |
pack
¶
pack(onset: float = 0.0, sequential: bool = False) -> float
Adjust onsets to pack events in the entire Score.
This method modifies the Score in place, adjusting onsets
so that events occur sequentially without gaps. By default,
self is assumed to be a full score containing Parts with Staffs
and Measures, so all contained Parts are concurrent, starting
at onset, and Parts are also packed, making all Staffs start
concurrently.
If the Score is flat (Parts contain only Notes), set sequential
to True, which overrides the packing of Parts, making their
content sequential (Notes) instead of concurrent (Staffs).
Note that the direct content of this Score starts concurrently
at onset in either case. Pack is recursive, but it makes
content concurrent in Concurrences like Chords and sequential
is Sequences like Staffs and Measures.
Parameters:
-
onset(float, default:0.0) –The onset time for the Score after packing.
-
sequential(bool, default:False) –If true, Parts are conconcurrently started at
onset, but each Part is packed sequentially, so that the first event in each Part starts atonset, and subsequent events start at the offset of the previous event. Use False for full scores (with Parts and Staffs) and True for flat scores (with Parts containing only Notes).
Returns:
-
Score–The modified Score instance itself.
Source code in amads/core/basics.py
3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 | |
part_count
¶
part_count()
How many parts are in this score?
Returns:
-
int–The number of parts in this score.
Source code in amads/core/basics.py
3768 3769 3770 3771 3772 3773 3774 3775 3776 | |
parts_are_monophonic
¶
parts_are_monophonic() -> bool
Determine if each part of a musical score is monophonic.
A monophonic part has no overlapping notes (e.g., chords).
Returns:
-
bool–True if each part is monophonic, False otherwise.
Source code in amads/core/basics.py
3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 | |
remove_measures
¶
remove_measures() -> Score
Create a new Score with all Measures removed.
Preserves Staffs in the hierarchy. Notes are "lifted" from Measures
to become direct content of their Staff. The result satisfies neither
is_flat() nor is_well_formed_full_score(), but it could be useful
in preserving a separation between staves. See also collapse_parts,
which can be used to extract individual staves from a score. The result
will have ties merged. (If you want to preserve ties and access the
notes in a Staff, consider using find_all(Staff), and then for each staff,
find_all(Note), but note that ties can cross between staves.)
Returns:
-
Score–A new Score instance with all Measures removed.
Source code in amads/core/basics.py
3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 | |
show
¶
show(indent: int = 0, file: Optional[TextIO] = None) -> Score
Print the Score information.
Parameters:
-
indent(int, default:0) –The indentation level for display.
Returns:
-
Score–The Score instance itself.
Source code in amads/core/basics.py
3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 | |
time_shift
¶
time_shift(increment: float, content_only: bool = False) -> Score
Change the onset by an increment, affecting all content.
This is tricky for scores because they have a time_map (tempo specification). If units_are_quarters, time_shift means insert initial quarters at the initial tempo, and shift all tempo changes by increment quarters. If increment is negative, it is assumed that all Events are at or after -increment, so they will shifted to positive times. Quarters are simply removed from the beginning, shifting all time signatures and tempo changes by increment quarters.
If units_are_seconds, we insert increment seconds at the initial tempo (positive increment), or we remove the first -increment seconds (negative increment) but converting increment to quarters and converting this Score to units_are_quarters.
If content_only is true, the time_map and time_signatures are left in place along with the placement of EventGroups including this Score, Parts, Staffs, Measures, and even Chords, but their Event content (Notes, Rests, KeySignatures, Clefs) are shifted. This probably only makes sense for flattened scores without Measures and Chords since Events could be shifted outside of their container boundaries.
Parameters:
-
increment(float) –The time increment (in quarters or seconds).
-
content_only(bool, default:False) –If true, preserves this container's time and shifts only the content.
Returns:
-
Score–The object. This method modifies the
Score.
Source code in amads/core/basics.py
3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 | |
time_stretch
¶
time_stretch(factor: float) -> Score
Scale all timing by a factor, affecting all content.
Stretching works by changing tempos, so the times in quarters are not changed.
Parameters:
-
factor(float) –The scaling factor for timing.
Returns:
-
Score–The object. This method modifies the
Score. The units are not changed.
Source code in amads/core/basics.py
3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 | |