Utilities

Pitch parsing, duration values, and font size management.

Pitch utilities

These functions convert between pitch string notation and internal step values used for vertical positioning on the staff.

parse_pitch(pitch_str: str) → tuple[str, str, int]

Decompose a scientific pitch string into (step, accidental, octave).

from pylute import parse_pitch

parse_pitch("C4")   # ("C", "", 4)
parse_pitch("F#4")  # ("F", "#", 4)
parse_pitch("Bb3")  # ("B", "b", 3)
parse_pitch("Eb5")  # ("E", "b", 5)
pitch_to_step(pitch_str: str, clef="treble") → int

Convert a pitch string to a staff step integer used for vertical note positioning. Step 0 = bottom staff line, step 1 = first space, step 4 = top line. Steps below 0 or above 8 produce ledger lines.

from pylute import pitch_to_step

pitch_to_step("E4", "treble")  # 0  (bottom line in treble clef)
pitch_to_step("G4", "treble")  # 1  (first space)
pitch_to_step("B4", "treble")  # 2  (second line)
pitch_to_step("G2", "bass")    # 0  (bottom line in bass clef)
parse_key(key: str | int) → int

Convert a key name to a signed accidental count. Negative = flats, positive = sharps. Integers pass through unchanged.

from pylute import parse_key

parse_key("C")   # 0
parse_key("G")   # 1
parse_key("Bb")  # -2
parse_key("F#")  # 6
parse_key(-3)    # -3 (pass-through)

Durations

The VALUES dictionary maps duration type strings to their fractional beat value, where 1.0 = one whole note. A 4/4 bar has capacity 1.0; a 3/4 bar has capacity 0.75.

Duration typeBeat valueFills a 4/4 bar
whole1.0Exactly
half0.52 per bar
quarter0.254 per bar
eighth0.1258 per bar
sixteenth0.062516 per bar
dotted_half0.751 per 3/4 bar
dotted_quarter0.375—
dotted_eighth0.1875—
from pylute import VALUES, get_value

get_value("quarter")       # 0.25
get_value("dotted_half")   # 0.75

# You can also access the dict directly:
VALUES["eighth"]           # 0.125

Font management

PyLute uses the Bravura SMuFL music font for all glyphs. Font size is global and controls the visual scale of all notation elements.

FunctionDescription
get_font_size() → intCurrent global font size in points. Default: 70.
set_font_size(size: int) → NoneSet the global font size. Clamped to a minimum of 28. Affects the rendered size of all glyphs and staff spacing. Call before creating any Score.
get_font() → pygame.freetype.FontReturns the cached Bravura font instance at the current size.
make_font(size: int) → pygame.freetype.FontCreate a new font instance at a specific size, independent of the global setting.
from pylute.font import set_font_size, get_font_size

# Smaller notation (good for dense scores or small windows)
set_font_size(50)
print(get_font_size())  # 50

# Back to default
set_font_size(70)
Call set_font_size() before creating any Score. Changing the font size after scores are created does not retroactively update their layout — create a new Score after changing font size.