Package: Score Selection (src.score_selection)

Module: score_selection.main.py

This module facilitates the selection and loading of a music score for further analysis or processing.

The primary function in this module, score_selection, coordinates the process of selecting a score. The user is initially offered a choice between selecting from a list of pre-defined scores or providing their own score via a local file or URL. Depending on their selection, a relevant helper function is invoked to assist the user through the selection process.

Functions

  • select_score_source: Displays a menu that allows the user to choose the source of the score.

  • select_example_score: Shows a menu that lets the user select a predefined score.

  • load_example_score: Loads the selected example score and provides troubleshooting tips if needed.

  • select_own_score: Provides instructions to the user for entering a path or URL to their score.

  • load_own_score: Starts loading the user-provided score and offers troubleshooting advice if needed.

Each function handles user inputs and may return a music21 stream object representing the selected score or guide the user through the score selection process.

In case of any exceptions during the process, they are caught and redirected to the handle_error function for suitable management and resolution.

Example

>>> from score_selection import main
>>> score = main.load_example_score("path/to/example/score.xml")

Notes

This module heavily relies on the music21 library, specifically the music21.converter module to load musical scores from various sources and formats.

See also

music21.converter.parse, music21.stream.Stream, src.utils.error_handling.handle_error

iMaT.src.score_selection.main.load_example_score(score_url)[source]

Helper function to load an example music score.

This function takes as input a URL to an example music score. It first attempts to access the URL, providing troubleshooting guidance to the user if this fails. If the URL is successfully accessed, the function then attempts to parse the score, again providing troubleshooting guidance if this fails. If the score is successfully parsed and contains at least one part, it is returned. Otherwise, the user is prompted to select another score.

See also

display_menu_print_textblock: Function to display a text block and request user input. score_selection: Main function to orchestrate the selection of music scores.

iMaT.src.score_selection.main.load_own_score(filepath)[source]

Helper function to load a user-provided music score.

This function takes as input a path or URL to a music score. It first checks whether the input is a URL. If it is, the function attempts to access the URL, providing troubleshooting guidance to the user if this fails. If the input is not a URL, the function checks whether it corresponds to a valid local file path, again providing troubleshooting guidance if this fails. If the score file is successfully accessed, the function then attempts to parse the score, providing troubleshooting guidance if this fails. If the score is successfully parsed and contains at least one part, it is returned. Otherwise, the user is prompted to select another score.

See also

display_menu_print_textblock: Function to display a text block and request user input. score_selection: Main function to orchestrate the selection of music scores.

iMaT.src.score_selection.main.score_selection()[source]

The main function that orchestrates the selection of music scores.

This function guides the user through the process of selecting a music score. It first asks the user to choose a source for the music score - either from a predefined example or from their own local file or URL. The function then loads the selected score and calls the select_and_name_parts function to allow the user to name the parts of the score. If any exception occurs during this process, it is caught and passed to the handle_error function.

See also

select_score_source: Function to select a source of a music score. select_example_score: Function to select an example music score. select_own_score: Function to select a custom music score.

iMaT.src.score_selection.main.select_example_score()[source]

Helper function to allow the user to select their own music score.

This function presents the user with guidelines for inputting the path or URL to their own score. The function uses the display_menu_print_textblock function to present the guidelines and capture the user’s input. If any exception occurs during this process, it is caught and passed to the handle_error function.

See also

display_menu_print_textblock: Function to display a text block and request user input.

iMaT.src.score_selection.main.select_own_score()[source]

Helper function to allow the user to select their own music score.

This function presents the user with guidelines for inputting the path or URL to their own score. The function uses the display_menu_print_textblock function to present the guidelines and capture the user’s input. If any exception occurs during this process, it is caught and passed to the handle_error function.

See also

display_menu_print_textblock: Function to display a text block and request user input.

iMaT.src.score_selection.main.select_score_source()[source]

Helper function to allow the user to select an example music score.

This function presents the user with a menu of available example scores. The menu entries are constructed based on the entries in the example_scores_dict dictionary. The function uses the display_menu_request_selection function to present the menu and capture the user’s choice. If any exception occurs during this process, it is caught and passed to the handle_error function.

See also

display_menu_request_selection: Function to display a menu and request a selection.

Module: score_selection.name_parts.py

This module provides functionalities to interact with and manipulate music scores using the music21 library. It takes into account predefined names for each part of the score within the files and assigns them to the corresponding music21 stream parts when assigning generic names.

Functions

  • select_and_name_parts: Initializes the selected_score_part_names dictionary and engages with the user to name the parts of the score.

  • rename_parts: Allows the user to rename the parts of the chosen score.

  • view_score: Displays the selected score by using the show method of music21.

Notes

This module uses the music21 library and functions from src.cli.menu_constructors and src.utils.error_handling. The global variable selected_score_part_names associates part names with their corresponding music21 Score or Part objects.

Module-level variable definitions

The module also defines a global variable:

selected_score_part_names: dict This global dictionary associates part names to their corresponding music21 Score or Part objects, enabling user interaction with named parts rather than abstract music21 objects. The key ‘Full Score’ is reserved for the whole Score object. The structure of the dictionary is as follows:

``` selected_score_part_names = {

‘Full Score’: <music21.stream.Score 0x230ce950730>, ‘Part 1’: <music21.stream.Part Soprano>, ‘Part 2’: <music21.stream.Part Alto>, ‘Part 3’: <music21.stream.Part Tenor>, ‘Part 4’: <music21.stream.Part Bass>

iMaT.src.score_selection.name_parts.rename_parts() None[source]

Interactive function that allows the user to rename parts in a musical score. The function operates on a global dictionary selected_score_part_names, where keys are part names and values are part objects.

Here is an example of how the selected_score_part_names dictionary may look before renaming:

``` selected_score_part_names = {

‘Full Score’: <music21.stream.Score 0x230ce950730>, ‘Part 1’: <music21.stream.Part Soprano>, ‘Part 2’: <music21.stream.Part Alto>, ‘Part 3’: <music21.stream.Part Tenor>, ‘Part 4’: <music21.stream.Part Bass>

}

This function displays a list of current part names and prompts the user to enter new names for each part, one at a time. If the user enters a new name that is not already used by another part, the new name will be accepted and used to update the part’s name. If the user tries to enter a name that is already in use, they will be prompted to choose another name.

Once the user has been given the chance to rename all parts, a list of the new (or unchanged) part names is displayed. The user is then asked to confirm whether the new names are correct.

If the user confirms that the new names are correct, the global selected_score_part_names dictionary is updated with the new part names. If the user indicates that the new names are not correct, the renaming process starts over.

The function continues to run until the user confirms that the part names are correct.

The function does not return any value. It directly modifies the selected_score_part_names dictionary.

Raises:

Any exceptions raised by this function are passed to the handle_error function for error handling.

See also

  1. display_menu_print_results(): For displaying a list of current part names and to present new/unchanged names.

  2. display_menu_request_selection(): For requesting the user to select between confirming or rejecting the new names.

  3. handle_error(): For handling exceptions that occur during the renaming process.

iMaT.src.score_selection.name_parts.select_and_name_parts(selected_score: Score) None[source]

Interacts with the user to assign custom or generic names to the parts of a provided music21 Score object.

This function first clears and then repopulates the selected_score_part_names global dictionary, associating names (keys) with the parts of the given Score (values). The ‘Full Score’ key represents the entire Score object.

The function presents a menu to the user to choose between assigning individual names, using generic names, or viewing the selected score. Depending on the user’s choice, it either calls the rename_parts function to assign individual names, prints the selected_score_part_names dictionary if the user chooses generic names, or calls view_score to display the music score.

The function handles exceptions by passing them to the handle_error function.

Parameters:

selected_score (music21.stream.Score) – The Score object for which parts are to be named.

Raises:

Any exceptions occurring during the execution are caught and passed to the handle_error function.

Returns:

None. This function modifies the global ‘selected_score_part_names’ dictionary and doesn’t return a value.

See also

  1. rename_parts(): To assign individual names based on user input.

  2. view_score(): To display the music score.

  3. handle_error(): For handling exceptions occurring during the execution of this function.

iMaT.src.score_selection.name_parts.view_score(selected_score: Score) None[source]

Opens the specified music21 Score object in the default sheet music viewer program.

The music21 library will attempt to open the Score object in the user’s default sheet music viewing program. The specific program used can vary depending on the user’s system configuration.

Before the Score is opened, a notice is displayed to the user. The notice informs the user that the sheet music viewer program likely needs to be closed before they can continue using the tool. The user must press Enter to proceed after reading the notice.

After the user presses Enter, the Score is opened in the sheet music viewer program.

Once the Score is displayed, the user can view the sheet music as needed. When they are done, they need to close the sheet music viewer program and return to the tool.

If any exceptions occur while attempting to display the Score or during user interaction, they are passed to the handle_error() function for error handling.

Parameters:

selected_score (music21.stream.Score) – The Score object to be displayed.

Raises:
  • Any exceptions that occur during the execution of this function are caught and passed to

  • the handle_error() function for error handling.

Returns:

None. This function only performs actions and does not return a value.

See also

  1. display_menu_print_textblock(): For displaying the notice before the Score is opened.

  2. handle_error(): For handling exceptions that occur while attempting to display the Score or during user interaction.

Module: score_selection.select_parts_and_measures.py

This module provides a user interface to facilitate the selection of musical parts and measures from a given musical score using music21, a toolkit for computer-aided musicology.

The main functionalities include: - Selecting the full score or individual parts. - Selecting all measures or a specific range of measures. - Repeating the previous selection.

A global dictionary selected_score_part_names is used to map part names to music21.stream.Score or music21.stream.Part objects.

Functions

  • select_parts_and_measures: Guides the user to select parts and measures from a score. Returns the selected part or score and a tuple of the measure range.

  • select_measure_range: Asks the user to input a measure range for a given score. Returns a tuple with the start and end measures.

Examples

Here is how to use the functions in this module:

>>> from music21 import *
>>> selected_part, selected_range = select_parts_and_measures()
>>> from music21 import corpus
>>> score = corpus.parse('bwv66.6')
>>> start, end = select_measure_range(score)
>>> print(f"Selected range: {start} to {end}")

Notes

This module uses the music21 library, which must be installed and imported for the functions to work correctly. This module also depends on other modules like routines_name_parts and cli.cli_menu_structure for some of its operations.

iMaT.src.score_selection.select_parts_and_measures.select_measure_range(score: Part)[source]

Prompts the user to input a valid measure range in a given musical score.

This function displays a menu to guide the user through the process of selecting a range of measures in the given musical score. The range is inclusive and the measures are 1-indexed. The user first enters a start measure and then an end measure. Both measures should lie within the total number of measures in the score. The end measure should be equal to or larger than the start measure.

The user input is validated according to the following rules: - The input must be a positive integer. - The input must not exceed the total number of measures in the score. - The end measure should be equal to or larger than the start measure.

If the user enters an invalid range, the function will prompt the user again to enter a valid range.

Parameters:

score (music21.stream.Part or music21.stream.Score) – The musical score or part in which the user is selecting a measure range.

Returns:

start, end – The start and end measures of the selected range.

Return type:

tuple of int

Examples

Here is how to use select_measure_range:

>>> from music21 import corpus
>>> score = corpus.parse('bwv66.6')
>>> start, end = select_measure_range(score)
>>> print(f"Selected range: {start} to {end}")
iMaT.src.score_selection.select_parts_and_measures.select_parts_and_measures() -> (<class 'music21.stream.base.Score'>, <class 'str'>, (<class 'int'>, <class 'int'>))[source]

Facilitates the user’s selection of musical parts and measures from a global score.

This function displays a selection menu to the user with several options related to the selection of musical parts and measures. The user is allowed to choose from the full score or individual parts, and either all measures or a specific range of measures. The function also provides an option to repeat the previous selection if one exists.

The function works with a dictionary named selected_score_part_names that should be initialized before calling this function. The dictionary maps part names to music21.stream.Score or music21.stream.Part objects. Here is an example of how the dictionary might look for the score ‘bwv66.6’:

selected_score_part_names = {

‘Full Score’: <music21.stream.Score 0x230ce950730>, ‘Part 1’: <music21.stream.Part Soprano>, ‘Part 2’: <music21.stream.Part Alto>, ‘Part 3’: <music21.stream.Part Tenor>, ‘Part 4’: <music21.stream.Part Bass>

}

Returns:

A tuple containing: - music21.stream.Score or music21.stream.Part: The selected part of the score based on the user’s choice.

The part contains either all measures or a specific range of measures, depending on the user’s choice.

  • str: The name of the selected part.

  • tuple: A tuple representing the start and end measures of the selected range.

Return type:

tuple

Examples

Here is how to use select_parts_and_measures:

>>> from music21 import *
>>> selected_part, selected_part_name, selected_range = select_parts_and_measures()