Input and Output
Input¶
The main function for input is
readscore.read_score described below.
It calls upon various file readers to read Standard MIDI Files and Music XML
files. Much of the work is done by various subsystems including Music21,
Partitura, and pretty_midi. Use read_score to get the recommended
implementation automatically.
readscore
¶
Functions for music data input.
Classes¶
Functions¶
set_preferred_midi_reader
¶
set_preferred_midi_reader(reader: str) -> str
Set a (new) preferred MIDI reader.
Returns the previous reader preference. The current preference is stored
in amads.io.reader.preferred_midi_reader.
Parameters:
-
reader(str) –The name of the preferred MIDI reader; "music21" or "pretty_midi".
Returns:
-
str–The previous name of the preferred MIDI reader.
Raises:
-
ValueError–If an invalid reader is provided.
Source code in amads/io/readscore.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
set_preferred_xml_reader
¶
set_preferred_xml_reader(reader: str) -> str
Set a (new) preferred XML reader.
Returns the previous reader preference. The current preference is stored
in amads.io.reader.preferred_xml_reader.
Parameters:
-
reader(str) –The name of the preferred XML reader. Can be "music21" or "partitura".
Returns:
-
str–The previous name of the preferred XML reader.
Raises:
-
ValueError–If an invalid reader is provided.
Source code in amads/io/readscore.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
set_preferred_kern_reader
¶
set_preferred_kern_reader(reader: str) -> str
Set a (new) preferred Kern reader.
Returns the previous reader preference. The current preference is stored
in amads.io.reader.preferred_kern_reader.
Parameters:
-
reader(str) –The name of the preferred Kern reader. Can be "music21" or "partitura".
Returns:
-
str–The previous name of the preferred Kern reader.
Raises:
-
ValueError–If an invalid reader is provided.
Source code in amads/io/readscore.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
set_preferred_mei_reader
¶
set_preferred_mei_reader(reader: str) -> str
Set a (new) preferred MEI reader.
Returns the previous reader preference. The current preference is stored
in amads.io.reader.preferred_mei_reader.
Parameters:
-
reader(str) –The name of the preferred MEI reader. Can be "music21" or "partitura".
Returns:
-
str–The previous name of the preferred MEI reader.
Raises:
-
ValueError–If an invalid reader is provided.
Source code in amads/io/readscore.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
set_reader_warning_level
¶
set_reader_warning_level(level: str) -> str
Set the warning level for readscore functions.
The translation from music data files to AMADS is not always well-defined and may involve intermediate representations using Music21, Partitura or others. Usually, warnings are produced when there is possible data loss or ambiguity, but these can be more annoying than informative. The warning level can be controlled using this function, which applies to all file formats.
The current warning level is stored in
amads.io.reader.reader_warning_level.
Parameters:
-
level(str) –The warning level to set. Options are "none", "low", "default", "high".
- "none" will suppress all warnings during
read_score()and also suppresses notice of reader subsystem and file name. - "low" will print one notice if there are any warnings.
- "default" will obey environment settings to control warnings.
- "high" will print all warnings during
read_score(), overriding environment settings.
- "none" will suppress all warnings during
Returns:
-
str–Previous warning level.
Raises:
-
ValueError–If an invalid warning level is provided.
Source code in amads/io/readscore.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
_check_for_subsystem
¶
_check_for_subsystem(
format: str,
) -> tuple[
Optional[Callable[[str, str, bool, bool, bool, bool], Score]],
Optional[str],
]
Check if the preferred reader is available.
We support:
music21 for midi and xml,
partitura for xml,
and
PrettyMIDI for midi.
Partitura has basic MIDI import functionality, but is unsupported here because when it reads in a score it has no MIDI velocity and when it reads in a performance it has no tempo track, key signature, etc.
Parameters:
-
format(str) –The type of file to read: 'midi', 'musicxml', 'kern', or 'mei'.
Returns:
-
tuple[Optional[Callable], Optional[str]]–The import function if available, None otherwise.
Source code in amads/io/readscore.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
_import_score
¶
_import_score(
filename: str,
format: str,
flatten: bool = False,
collapse: bool = False,
show: bool = False,
group_by_instrument: bool = True,
) -> Score
Import a score file
Author: Roger B. Dannenberg
Source code in amads/io/readscore.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
read_score
¶
read_score(
filename: str,
flatten: bool = False,
collapse: bool = False,
show: bool = False,
format: Optional[str] = None,
group_by_instrument: bool = True,
) -> Score
Read a file with the given format, 'musicxml', 'midi', 'kern', 'mei'.
If format is None (default), the format is based on the filename
extension, which can be 'musicxml', 'mid', 'midi', 'smf', 'kern',
or 'mei'. (Valid extensions are in
amads.io.readscore.valid_score_extensions.)
Author: Roger B. Dannenberg
Parameters:
-
filename(str) –The path (relative or absolute) to the music file. Can also be an URL.
-
flatten(bool, default:False) –The returned score will be flat (Score, Parts, Notes).
-
collapse(bool, default:False) –If collapse and flatten, the parts will be merged into one.
-
show(bool, default:False) –Print a text representation of the data.
-
format(Optional[str], default:None) –One from among limited standard options (e.g.,
'musicxml','midi','kern','mei') -
group_by_instrument(bool, default:True) –If True (default), when the underlying reader (e.g. for "pretty_midi", "music21" or "partitura") reads Parts with the same instrument, their content will be grouped into a single part. This means that if
flatten, then parts with the same instrument will be merged into a single part. Ifflattenis False, then the staffs of parts with the same instrument will be grouped within a single part. Ifgroup_by_instrumentis False, the parts read in by the underlying reader will be preserved as separate parts.group_by_instrumentis True by default so that when reading Piano scores with separate treble and bass staffs, the resulting AMADS Score will generally have a single Piano part with two staffs. A score for Piano and Violin will generally have two parts, one for Piano and one for Violin, as opposed to three parts (Piano-Treble, Piano-Bass, Violin). On the other hand, a score for two Violins might be represented a one part with two staffs by default, but settinggroup_by_instrumentto False will more likely keep the two Violin parts separate. Unfortunately, exact behavior depends on the underlying reader, MIDI track names, and/or MusicXML structure and naming.
Returns:
-
Score–The imported score
Raises:
-
ValueError–If the format is unknown or not implemented.
Note on Incomplete First Measure
In Music21, the first measure may be a partial measure containing an anacrusis (“pickup”). This is somewhat ambiguous and does not translate well to MIDI which is less expressive than MusicXML.
Therefore, if the first measure read with Music21 is not a full measure, a rest is inserted and the remainder is shifted to form a full measure according to its time signature. Remaining measures are shifted in time accordingly and Score, Part and Staff durations are adjusted accordingly.
General MIDI Import Notes
Each Standard MIDI File track corresponds to a Staff when
creating a full AMADS Score. Everything is combined into one
part when flatten and collapse are specified.
AMADS assumes that instruments (midi program numbers) are fixed for each Staff (or Part in flat scores), and MIDI channels are not represented. The use of program change messages within a track to change the program are ignored, but may generate warnings.
In general, AMADS instrument name corresponds to the MIDI track
name, and MIDI program numbers are stored as "midi_program"
in the info attribute of the Staff or Part corresponding to
the track.
MIDI files do not have a Part/Staff structure, but you can
write multiple tracks with the same name. Both the "music21"
and "pretty_midi" readers will group tracks with matching
names as Staffs in a single Part. This may result in an
unexpected Part/Staff hierarchy if tracks are not named or
if tracks are named something like "Piano-Treble" and
"Piano-Bass", which would produce two Parts as different
instruments as opposed to one Part with two Staffs.
Unless flatten or collapse, the MIDI file time signature
information will be used to form Measures with Staffs, and
Notes will be broken where they cross measure boundaries and
then tied. The default time signature is 4/4.
Pretty MIDI Import Notes
If there is no program change in a file, the "pretty_midi"
reader will use 0, and 0 will be stored as "midi_program"
in the Part or Staff's info (see
get and
set).
If there is no track name, the Part.instrument is derived
from the track program number (defaults to zero).
If the MIDI file track name is "Unknown", the Part.instrument
is set to None. This is because when the "pretty_midi" writer
writes a part where Part.instrument is None, the name "Unknown"
is used instead. Therefore, the reader will recreate the AMADS
Part where Part.instrument is None.
Pretty MIDI will not insert any KeySignature unless key signature meta-events are found.
Music21 MIDI Import Notes
Music21 may infer a Clef and KeySignature even though MIDI does not even have a meta-event for clefs, and even if the MIDI file has no key signature meta-event.
Source code in amads/io/readscore.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | |
last_used_reader
¶
last_used_reader() -> Optional[str]
Return the name of the last used reader function.
Returns:
-
Optional[str]–The name of the actual function used in the last call to
read_score, or None if no reader has been used yet.
Source code in amads/io/readscore.py
481 482 483 484 485 486 487 488 489 490 491 492 | |
Output¶
Similar to input functions, you should use writescore.write_score described below to write an AMADS Score to a file.
writescore
¶
functions for file output
Classes¶
Functions¶
set_preferred_midi_writer
¶
set_preferred_midi_writer(writer: str) -> str
Set a (new) preferred MIDI writer.
Returns the previous writer preference. The current preference is stored
in amads.io.writer.preferred_midi_writer.
Parameters:
-
writer(str) –The name of the preferred MIDI writer. Can be "music21" or "pretty_midi".
Returns:
-
str–The previous name of the preferred MIDI writer.
Raises:
-
ValueError–If an invalid writer is provided.
Source code in amads/io/writescore.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
set_preferred_xml_writer
¶
set_preferred_xml_writer(writer: str) -> str
Set a (new) preferred XML writer.
Returns the previous writer preference. The current preference is stored
in amads.io.writer.preferred_xml_writer.
Parameters:
-
writer(str) –The name of the preferred XML writer. Can be "music21" or "partitura".
Returns:
-
str–The previous name of the preferred XML writer.
Raises:
-
ValueError–If an invalid writer is provided.
Source code in amads/io/writescore.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
set_preferred_kern_writer
¶
set_preferred_kern_writer(writer: str) -> str
Set a (new) preferred Kern writer.
Returns the previous writer preference. The current preference is stored
in amads.io.writer.preferred_kern_writer.
Parameters:
-
writer(str) –The name of the preferred Kern writer. Can be "music21".
Returns:
-
str–The previous name of the preferred Kern writer.
Raises:
-
ValueError–If an invalid writer is provided.
Source code in amads/io/writescore.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
set_preferred_mei_writer
¶
set_preferred_mei_writer(writer: str) -> str
Set a (new) preferred MEI writer.
Returns the previous writer preference. The current preference is stored
in amads.io.writer.preferred_mei_writer.
Parameters:
-
writer(str) –The name of the preferred MEI writer. Can be "music21".
Returns:
-
str–The previous name of the preferred MEI writer.
Raises:
-
ValueError–If an invalid writer is provided.
Source code in amads/io/writescore.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
set_preferred_pdf_writer
¶
set_preferred_pdf_writer(writer: str) -> str
Set a (new) preferred PDF writer.
Returns the previous writer preference. The current preference is stored
in amads.io.writescore.preferred_pdf_writer. Preferences are:
- "music21-lilypond" - use music21 to create a LilyPond file, then use
LilyPond to create a PDF.
- "music21-xml-lilypond" - use music21 to create a MusicXML file, then run
the program musicxml2ly to convert XML to LilyPond, then run LilyPond to
create a PDF.
- "partitura-xml-lilypond" - use partitura to create a MusicXML file, then
run the program musicxml2ly to convert XML to LilyPond, then run LilyPond
to create a PDF.
Parameters:
-
writer(str) –The name of the preferred PDF writer. Can be "music21-lilypond", "music21-xml-lilypond", or "partitura-xml-lilypond".
Returns:
-
str–The previous name of the preferred PDF writer.
Raises:
-
ValueError–If an invalid writer is provided.
Source code in amads/io/writescore.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
set_writer_warning_level
¶
set_writer_warning_level(level: str) -> str
Set the warning level for writescore functions.
The translation from AMADS to music data files is not always well-defined and may involve intermediate representations using Music21, Partitura or others. Usually, warnings are produced when there is possible data loss or ambiguity, but these can be more annoying than informative. The warning level can be controlled using this function, which applies to all file formats.
The current warning level is stored in
amads.io.writer.writer_warning_level.
Parameters:
-
level(str) –The warning level to set. Can be "none", "low", "default", "high".
- "none" - will suppress all warnings during write_score().
- "low" - will show print one notice if there are any warnings.
- "default" - will obey environment settings to control warnings.
- "high" - will print all warnings during write_score(), overriding environment settings.
Returns:
-
str–Previous warning level.
Raises:
-
ValueError–If an invalid warning level is provided.
Source code in amads/io/writescore.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | |
_check_for_subsystem
¶
_check_for_subsystem(
format: str,
) -> tuple[
Optional[
Callable[
[Score, Optional[str], Optional[str], bool, bool], None
]
],
Optional[str],
]
Check if the preferred subsystem is available.
Parameters:
-
format(str) –The format of the file to write, either 'midi', 'musicxml', or 'pdf'.
Returns:
-
tuple[Optional[Callable], Optional[str]]–The export function if available, None otherwise, and the name of the subsystem used.
Source code in amads/io/writescore.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |
_export_score
¶
_export_score(
score: Score,
filename: Optional[str],
format: str,
show: bool = False,
display: bool = False,
) -> None
Use Partitura or music21 to export a MusicXML file.
Author: Roger B. Dannenberg
Source code in amads/io/writescore.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | |
write_score
¶
write_score(
score: Score,
filename: str,
show: bool = False,
format: Optional[str] = None,
) -> None
Write a file with the given format.
If format is None (default), the format is based on the filename
extension, which can be one of writescore.valid_score_extensions
('xml', 'musicxml', 'mxl', 'mid', 'midi', 'smf', 'krn', 'kern',
'mei', 'pdf', or 'ly').
Author: Roger B. Dannenberg
Parameters:
-
score(Score) –the score to write
-
filename(Optional[str]) –the path (relative or absolute) to the music file. Optional only when display is True, but for display, you should call
display_scoreinstead. -
show(bool, default:False) –print a text representation of the data
-
format(Optional[string], default:None) –one of
'musicxml','midi','kern','mei','pdf','lilypond'. Defaults to the format implied byfilename.
Raises:
-
ValueError–if format is unknown
Notes
AMADS assumes that instruments (midi program numbers) are fixed
for each Staff (or Part in flat scores), and MIDI channels are
not represented. This corresponds to some DAWs such as LogicPro,
which represents channels but ignores them when tracks are
synthesized in software by a single instrument. The MIDI program
is stored as info (see get and
set) under key "midi_program"
on the Staff, or if there is no Staff or no "midi_program" on
the Staff, under key "midi_program" on the Part.
Parts also have an instrument attribute, which is stored as
the MIDI track name. (Therefore, if a Part has two Staffs, there
will be two tracks with the same name.) If there is no MIDI
program for the track, the 'pretty_midi' writer will use
pretty_midi.instrument_name_to_program to determine a program
number since a program number is required. (As opposed to Standard
MIDI Files, which need not have any MIDI program message at all.)
If pretty_midi.instrument_name_to_program fails, the program is
set to 0 (“Acoustic Grand Piano”).
Partitura does not seem to support per-staff key signatures, so key signatures from AMADS are simply added to Partitura parts. When there are multiple staffs, there could be duplicate key signatures (to be tested).
Pretty MIDI also requires an instrument name. If the AMADS Part
instrument attribute is None, then "Unknown" is used. The
Pretty MIDI reader will convert "Unknown" back to None.
Source code in amads/io/writescore.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
_write_or_display_score
¶
_write_or_display_score(
score: Score,
filename: Optional[str],
show: bool = False,
format: Optional[str] = None,
display: bool = False,
) -> None
Write or display a Score.
If format is None (default), the format is based on the filename
extension, which can be one of writescore.valid_score_extensions
('xml', 'musicxml', 'mxl', 'mid', 'midi', 'smf', 'krn', 'kern',
'mei', 'pdf', or 'ly').
If display is True, the goal is to display the file, so the filename is optional, and a temporary file will be used if needed.
display is suppressed by setting AMADS_NO_OPEN=1 in the environment, which is used in testing with pytest so the user does not have to close a bunch of windows opened by demos that are run as part of testing.
Author: Roger B. Dannenberg
Parameters:
-
score(Score) –the score to write
-
filename(Optional[str]) –the path (relative or absolute) to the music file. Optional only when display is True, but for display, you should call
display_scoreinstead. -
show(bool, default:False) –print a text representation of the data
-
format(Optional[string], default:None) –one of 'musicxml', 'midi', 'kern', 'mei', 'pdf', 'lilypond'. Defaults to the format implied by
filename. -
display(bool, default:False) –If True and format is 'pdf', the created PDF file is displayed.
Raises:
-
ValueError–if format is unknown
Source code in amads/io/writescore.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | |
Display¶
Similar to output functions, you should use displayscore.display_score described below to display an AMADS Score. You can use Music21 to write directly to a LilyPond file and use LilyPond to render the file as a PDF, you can use Music21 or Partitura to write a musicxml file, convert that with musicxml2ly and render with LilyPond, or you can writea musicxml file and open it with MuseScore.
You will need to install either LilyPond or MuseScore, and either Music21 or Partitura to display a score.
displayscore
¶
functions for score display
Classes¶
Functions¶
set_preferred_display_method
¶
set_preferred_display_method(method: str) -> str
Set a (new) preferred display method.
Returns the previous preference. The current preference is stored in `amads.io.writer.preferred_display_method
Parameters:
-
method(str) –The name of the preferred method. Can be "pdf", "musescore" or "pianoroll". Note that if the method is "pdf", then
io.writescore.preferred_pdf_writeris used to create a PDF to display.
Returns:
-
str–The previous name of the preferred method.
Raises:
-
ValueError–If an invalid method is provided.
Source code in amads/io/displayscore.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
display_score
¶
display_score(score: Score, show: bool = False) -> None
Display a score.
Author: Roger B. Dannenberg
Parameters:
-
score(Score) –the score to write
-
show(bool, default:False) –show text representation of converted score for debugging
Source code in amads/io/displayscore.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
Piano Roll Display¶
pianoroll
¶
Ports pianoroll Function
Original Doc: https://citeseerx.ist.psu.edu/document?repid=rep1&type=pdf&doi=6e06906ca1ba0bf0ac8f2cb1a929f3be95eeadfa#page=82
Classes¶
Functions¶
pianoroll
¶
pianoroll(
score: Score,
title: str = "Piano Roll",
y_label: str = "name",
x_label: str = "quarter",
color: str = "skyblue",
accidental: str = "sharp",
show: bool = True,
) -> Figure
Converts a Score to a piano roll display of a musical score.
Parameters:
-
score(Score) –The musical score to display
-
title(str, default:'Piano Roll') –The title of the plot. Defaults to "Piano Roll".
-
y_label(str, default:'name') –Determines whether the y-axis is labeled with note names or MIDI numbers. Valid Input: 'name' (default) or 'num'.
-
x_label(str, default:'quarter') –Determines whether the x-axis is labeled with quarters or seconds. Valid input: 'quarter' (default) or 'sec'.
-
color(str, default:'skyblue') –The color of the note rectangles. Defaults to 'skyblue'.
-
accidental(str, default:'sharp') –Determines whether the y-axis is labeled with sharps or flats. Only useful if argument y_label is 'name'. Raises exception on inputs that's not 'sharp', 'flat', or 'both'. Defaults to 'sharp', which is what is done in miditoolbox. 'both' means use AMADS defaults which are C#, Eb, F#, G#, Bb.
-
show(bool, default:True) –If True (default), the plot is displayed.
Returns:
-
Figure–A matplotlib.figure.Figure of a pianoroll diagram.
Raises:
-
ValueError–If there are invalid input arguments
Source code in amads/io/pianoroll.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
Low-Level Input Functions¶
pretty_midi_import
¶
pretty_midi_import(
filename: str,
format: str,
flatten: bool = False,
collapse: bool = False,
show: bool = False,
group_by_instrument: bool = True,
) -> Score
Use PrettyMIDI to import a MIDI file and convert it to a Score.
Author: Roger B. Dannenberg
Parameters:
-
filename(Union(str, PosixPath)) –The path to the MIDI file to import.
-
format(str) –The format of the MIDI file. Must be "midi".
-
flatten(bool, default:False) –If True, create a flat score where notes are direct children of parts. Defaults to collapse, which defaults to False.
-
collapse(bool, default:False) –If True, merge all parts into a single part. Implies flatten=True. Defaults to False.
-
show(bool, default:False) –If True, print the PrettyMIDI score structure for debugging. Defaults to False.
-
group_by_instrument(bool, default:True) –If True, group parts by instrument name into staffs. Defaults to True. See read_midi() for more details.
Returns:
-
Score–The converted Score object containing the imported MIDI data.
Examples:
>>> from amads.io.pm_midi_import import pretty_midi_import
>>> from amads.music import example
>>> score = pretty_midi_import( example.fullpath("midi/sarabande.mid"), "midi", flatten=True) # show=True to see PrettyMIDI data
Source code in amads/io/pm_midi_import.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
music21_import
¶
music21_import(
filename: str,
format: str,
flatten: bool = False,
collapse: bool = False,
show: bool = False,
group_by_instrument: bool = True,
) -> Score
Use music21 to import a file and convert it to a Score.
Parameters:
-
filename(str) –The path to the music file.
-
format(str) –The file format: 'musicxml', 'kern', 'mei'
-
flatten(bool, default:False) –If True, flatten the score structure.
-
collapse(bool, default:False) –If True and flatten is true, also collapse parts.
-
show(bool, default:False) –If True, print the music21 score structure for debugging.
-
group_by_instrument(bool, default:True) –If True, group parts by instrument name into staffs. Defaults to True. See music21_to_score() for more details.
Returns:
-
Score–The converted AMADS Score object.
Source code in amads/io/m21_import.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
partitura_import
¶
partitura_import(
filename: Path | str,
format: str,
flatten: bool = False,
collapse: bool = False,
show: bool = False,
group_by_instrument: bool = True,
) -> Score
Use Partitura to import a MusicXML file.
Parameters:
-
filename(Path | str) –The path (relative or absolute) to the music file.
-
format(str) –The format of the file: 'musicxml', 'mei', 'kern'
-
flatten(bool = False, default:False) –Returns a flat score (see
amads.core.basics.Part.flatten) -
collapse(bool = False, default:False) –If
flattenis True, all Notes are merged (collapsed) into a single Part in the resulting Score. -
show(bool = False, default:False) –Print a text representation of the data.
-
group_by_instrument(bool = True, default:True) –This parameter is ignored by Partitura, which automatically produces parts with multiple staffs. The Partitura grouping is respected, and
group_by_instrumentis ignored.
Returns:
-
Score–The imported Score
-
<small>**Author**: Roger B. Dannenberg</small>–
Source code in amads/io/pt_import.py
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 | |
Low-Level Output Functions¶
pretty_midi_export
¶
pretty_midi_export(
score: Score,
filename: str,
format: str,
show: bool = False,
display: bool = False,
) -> None
Export a Score as a standard MIDI file using PrettyMIDI library.
Parameters:
-
score(Score) –The Score object to export.
-
filename(str) –The path to the output MIDI file.
-
format(str) –The export format, should be "midi" for this function.
-
show(bool, default:False) –Print a text representation of the data.
-
display(bool, default:False) –Display the MIDI file (always False for MIDI export, included for consistency with other export functions).
Source code in amads/io/pm_midi_export.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
music21_export
¶
music21_export(
score: Score,
filename: Path | str,
format: str,
show: bool = False,
display: bool = False,
) -> None
Save a Score to a file in musicxml format using music21.
Author: Roger B. Dannenberg
Parameters:
-
score(Score) –The Score to export.
-
filename(Path | str) –The name or path of the file to save the MusicXML data.
-
format(str) –The format to export. Must be "musicxml", "midi", or "kern".
-
show(bool, default:False) –If True, print the music21 score structure for debugging.
-
display(bool, default:False) –If True, open the generated PDF in the default viewer. Only relevant for PDF export (see
music21_xml_lilypond_export).
Source code in amads/io/m21_export.py
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | |
partitura_export
¶
partitura_export(
score: Score,
filename: Path | str,
format: str,
show: bool = False,
display: bool = False,
) -> None
Save a Score to a file in musicxml format using Partitura.
Author: Roger B. Dannenberg
Parameters:
-
score(Score) –The Score to export.
-
filename(Path | str) –The name or path of the file to save the MusicXML data.
-
format(str) –The format of the file to save. Must be "musicxml" for this function.
-
show(bool, default:False) –If True, print the partitura score structure for debugging.
-
display(bool, default:False) –If True, open the generated PDF in the default viewer. Only relevant for PDF export (see
music21_xml_lilypond_export).
Source code in amads/io/pt_export.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
music21_pdf_export
¶
music21_pdf_export(
score: Score,
filename: str,
show: bool = False,
lilypond: bool = False,
display: bool = False,
) -> None
Save a Score to a file in PDF format using music21 and LilyPond.
There are three main modes of operation, controlled by the lilypond
and display flags:
- To save a PDF file without opening it, set
lilypond=Falseand provide afilenameending in.pdf. - To save a LilyPond file without converting to PDF, set
lilypond=Trueand provide afilenameending in.ly. - To save and display a PDF file, set
display=Trueand optionally provide afilenameending in.pdf.
Temporary files are created as needed for intermediate LilyPond and PDF output.
Author: Roger B. Dannenberg
Parameters:
-
score(Score) –The Score to export.
-
filename(str) –The name of the file to save the LilyPond or PDF data. Must be provided if
displayis False, and must end if.lyiflilypondis True, or.pdfiflilypondis False. Ifdisplayis True, filename is optional and names the .pdf file to be written. In all cases, a LilyPond (.ly) file is written, either tofilenameor to a temporary directory whendisplayis True orlilypondis False. -
show(bool, default:False) –If True, print the music21 score structure for debugging.
-
lilypond(bool, default:False) –If True, also save the intermediate LilyPond file rather than PDF.
-
display(bool, default:False) –If True, open the generated PDF in the default viewer.
Raises:
-
ValueError–If
filenameis not provided whendisplayis False, or iffilenameextension is not the expected.pdfor.ly.
Source code in amads/io/m21_pdf_export.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
music21_xml_pdf_export
¶
music21_xml_pdf_export(
score: Score,
filename: str,
show: bool = False,
lilypond: bool = False,
display: bool = False,
) -> None
Write Score as PDF file using music21, musicxml2ly and LilyPond.
There are three main modes of operation, controlled by the lilypond
and display flags:
- To save a PDF file without opening it, set
lilypond=Falseand provide afilenameending in.pdf. - To save a LilyPond file without converting to PDF, set
lilypond=Trueand provide afilenameending in.ly. - To save and display a PDF file, set
display=Trueand optionally provide afilenameending in.pdf.
Temporary files are created as needed for intermediate XML, LilyPond, and PDF output.
Author: Roger B. Dannenberg
Parameters:
-
score(Score) –The Score to export.
-
filename(str) –The name of the file to save the LilyPond or PDF data. Must be provided if
displayis False, and must end if.lyiflilypondis True, or.pdfiflilypondis False. Ifdisplayis True, filename is optional and names the .pdf file to be written. In all cases, a LilyPond (.ly) file is written, either tofilenameor to a temporary directory whendisplayis True orlilypondis False. -
show(bool, default:False) –If True, print the music21 score structure for debugging.
-
lilypond(bool, default:False) –If True, also save the intermediate LilyPond file rather than PDF.
-
display(bool, default:False) –If True, open the generated PDF in the default viewer.
Raises:
-
ValueError–If
filenameis not provided whendisplayis False, or iffilenameextension is not the expected.pdfor.ly, or if lilypond2ly or lilypond executables are not found or fail.
Source code in amads/io/m21_pdf_export.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
partitura_xml_pdf_export
¶
partitura_xml_pdf_export(
score: Score,
filename: Path | str,
show: bool = False,
lilypond: bool = False,
display: bool = False,
) -> None
Save a Score to a file in PDF format using Partitura and LilyPond.
There are three main modes of operation, controlled by the lilypond
and display flags:
- To save a PDF file without opening it, set
lilypond=Falseand provide afilenameending in.pdf. - To save a LilyPond file without converting to PDF, set
lilypond=Trueand provide afilenameending in.ly. - To save and display a PDF file, set
display=Trueand optionally provide afilenameending in.pdf.
Temporary files are created as needed for intermediate XML, LilyPond, and PDF output.
Author: Roger B. Dannenberg
Parameters:
-
score(Score) –The Score to export.
-
filename(Path | str) –The name or path of the file to save the LilyPond or PDF data. Must be provided if
displayis False, and must end if.lyiflilypondis True, or.pdfiflilypondis False. Ifdisplayis True, filename is optional and names the .pdf file to be written. In all cases, a LilyPond (.ly) file is written, either tofilenameor to a temporary directory whendisplayis True orlilypondis False. -
show(bool, default:False) –If True, print the partitura score structure for debugging.
-
lilypond(bool, default:False) –If True, also save the intermediate LilyPond file rather than PDF.
-
display(bool, default:False) –If True, open the generated PDF in the default viewer.
Raises:
-
ValueError–If
filenameis not provided whendisplayis False, or iffilenameextension is not the expected.pdfor.ly.
Source code in amads/io/pt_pdf_export.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
Built-In Scores¶
example
¶
Functions¶
fullpath
¶
fullpath(example: str) -> str
Construct a full path name for an example file.
For example, fullpath("midi/sarabande.mid") returns a path to a readable file from this package. This uses importlib so that we can read files even from compressed packages (we hope).
Parameters:
-
example(str) –The relative path to the example file, starting from the "music" directory. For example, "midi/sarabande.mid" or "musicxml/ex2.xml".
Returns:
-
str–The full path to the example file.
Raises:
-
FileNotFoundError–If the example file is not found or is not readable.
Source code in amads/music/example.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |