Melsim

Python wrapper around the R package melsim. See the user guide Melsim for setup, measures, transformations, and examples.

install_dependencies()[source]

Install all required R packages.

check_r_packages_installed(install_missing=False, n_retries=3)[source]

Check if required R packages are installed.

Parameters:
  • install_missing (bool)

  • n_retries (int)

get_similarity_from_midi(midi_path1, midi_path2=None, method='opti3', transformation=None, output_file=None, n_cores=None, batch_size=100, r_timeout=300)[source]

Calculate similarity between MIDI files using melsim.

This function directly calls melsim’s read_midi and similarity functions for consistency.

Parameters:
  • midi_path1 (Union[str, Path, List[Union[str, Path]]]) – Path to first MIDI file, directory containing MIDI files, or list of MIDI file paths

  • midi_path2 (Union[str, Path], optional) – Path to second MIDI file. Ignored if midi_path1 is a directory or list

  • method (Union[str, List[str]], default="opti3") – Name of the similarity method(s) to use. Can be a single method or a list of methods. Use built-in measures like “opti3”, “Jaccard”, “edit_sim”, etc.

  • transformation (Union[str, List[str]], optional) – Transformation to apply (only used for non-opti3 measures). Options: “pitch”, “int”, “fuzzy_int”, “parsons”, “pc”, etc.

  • output_file (Union[str, Path], optional) – If provided and doing pairwise comparisons, save results to this file.

  • n_cores (int, optional) – Number of CPU cores to use for parallel processing. Defaults to all available cores.

  • batch_size (int, default=100) – Number of comparisons to process in each R batch call

  • r_timeout (int, default=300) – Seconds before an R subprocess is killed and a RuntimeError is raised. Guards against indefinite hangs.

Returns:

If comparing two files, returns similarity value. If comparing multiple files, returns dictionary mapping tuples of (file1, file2, method, transformation) to their similarity values

Return type:

Union[float, Dict[Tuple[str, str, str, str], float]]

get_similarity(melody1_pitches, melody1_starts, melody1_ends, melody2_pitches, melody2_starts, melody2_ends, method, transformation='pitch')[source]

Calculate similarity between two melodies using the specified method.

This is a legacy function that accepts pre-parsed melody data. For better performance, use get_similarity_from_midi() which uses melsim’s built-in MIDI reader.

Parameters:
Return type:

float

load_midi_file(file_path)[source]

Load MIDI file and extract melody attributes.

Parameters:

file_path (Union[str, Path]) – Path to MIDI file

Returns:

Tuple of (pitches, start_times, end_times)

Return type:

Tuple[List[int], List[float], List[float]]

BUILTIN_SIMILARITY_MEASURES

set() -> new empty set object set(iterable) -> new set object

Build an unordered collection of unique elements.