pytakt.utils module

takt_round(x) int

Round x to the nearest integer, the larger if there are two possibilities.

Parameters:

x (float) – the original value

Returns:

Resulting value

takt_roundx(x, mode) int

Rounding function to an integer with various rounding modes.

Parameters:
  • x (float) – original value

  • mode (str or function) –

    rounding mode represented by one of the followings.

    • ’nearestup’: The integer closest to x. If there are two possibilities, the larger one is chosen.

    • ’nearestdown’: The integer closest to x. If there are two possibilities, the smaller one is chosen.

    • ’floor’: The largest integer less than or equal to x.

    • ’ceil’: The smallest integer greater than or equal to x.

    • ’down’: The largest integer less than or equal to (x + 10 -4) (‘floor’ with allowing calculation errors).

    • ’up’: The smallest integer greater than or equal to (x - 10 -4) (‘ceil’ with allowing calculation errors).

    • ’random’: ‘up’ or ‘down’ chosen at random with equal probability.

    • function: the function is called with x as argument, and its return value is the result.

Returns:

Result value

int_preferred(x) Union[int, float, Fraction]

If x has an integer value, convert it to the ‘int’ type, otherwise return it as it is.

Parameters:

x (int, float, or Fraction) – original value

Returns:

Resulting value

std_time_repr(time) str

Converts time to a string with 5 decimal places.

This function is used when converting an Event or EventList to a string with the str function.

Parameters:

time (ticks) – value

frac_time_repr(time) str

Converts time to a string using fractional notation that is as accurate and compact as possible. Unlike the repr function, the result may contain a conversion error up to 1e-8.

This function is the default time-to-string conversion function in showtext() when it is not in the raw mode.

Parameters:

time (ticks) – value

exception TaktWarning

Bases: UserWarning

class NoteDict

Bases: object

A dictionary for finding correspondences between events (typically for NoteOnEvent and NoteOffEvent). By default, it uses (track number, MIDI channel number, MIDI note number) as key to find events with the same key. Unlike normal dict, it allows multiple elements for the same key.


clear()
copy()
push(key, value)
pushuniq(key, value)
pushnote(ev, value)
pop(key, default=<object object>)
popuniq(key, default=<object object>)
popnote(ev, default=<object object>)
items()
keys()
values()
uniquekeys()
popitem()
get_file_type(path, types=('smf', 'json', 'mxl'), guess=True) str

Determines whether the file specified by path is a standard MIDI file, a JSON file, or a MusicXML file. First, the extension in the pathname is examined to determine the file type. If it cannot be determined from the pathname, it is inferred from the file content (only if guess=True). If it still cannot be determined, an exception is raised.

Parameters:
  • path (str) – path name of the file

  • types (tuple of str) – Acceptable file formats

  • guess (bool, optional) – If True, guessed from the file content in addition to the extension in the pathname.

Returns:

‘smf’ for standard MIDI files, ‘json’ for JSON files, or ‘mxl’ for MusicXML files.