Skip to content

utils

Utility helpers, including pitch2freq (pitch name to frequency) and object_name (resolve a box's effective Max object name).

from py2max.utils import pitch2freq

pitch2freq("A4")        # 440.0
pitch2freq("C3")        # 130.81
pitch2freq("A4", A4=442)

Utility functions for py2max.

This module provides helper functions for common Max/MSP operations such as pitch-to-frequency conversion and other musical calculations.

kwds_filter

kwds_filter(
    kwds: Dict[str, Any], **elems: Any
) -> Dict[str, Any]

Return kwds merged with the elems whose value is not None.

Lets a method keep an optional parameter in its signature but omit it from the forwarded **kwds when the caller left it at its None default, so unset options never reach the serialized patch::

def add(self, text, varname=None, **kwds):
    return Box(text, **kwds_filter(kwds, varname=varname))

Only None is treated as "unset"; legitimate falsy values such as 0 or "" are kept. The input kwds is not mutated.

object_name

object_name(box: Any) -> str

Resolve the effective Max object name for a box.

Native-maxclass objects (e.g. live.dial, toggle) carry the name in maxclass. newobj boxes carry it as the first token of their text.

Reading the text property (rather than _kwds directly) means this works for both programmatically-created boxes and boxes loaded from a file, where the text lives in __dict__ instead of _kwds.

pitch2freq

pitch2freq(pitch: str, A4: int = 440) -> float

Convert a pitch name to a frequency in Hz.

Converts standard pitch notation (e.g., "C4", "A#3") to frequency values using equal temperament tuning.

Parameters:

Name Type Description Default
pitch str

Pitch name in format like "C4", "A#3", "Bb2".

required
A4 int

Reference frequency for A4 (default: 440 Hz).

440

Returns:

Type Description
float

Frequency in Hz as a float.

Example

pitch2freq("C3") 130.8127826502993 pitch2freq("A4") 440.0

Note

Based on: https://gist.github.com/CGrassin/26a1fdf4fc5de788da9b376ff717516e