Skip to main content

libcantus

CI PyPI License Python

Music-theory analysis and generation for MIDI note events, from Python: recover the harmony from notes and write new parts against it.

The package calls the libcantus C++ core through ctypes. It exposes the same functions, classes and constants as the TypeScript package @libraz/libcantus, with Python spelling, and returns plain dicts and lists.

Python guide · Differences from pure TypeScript · Repository

Install

Python 3.10 or later.

pip install libcantus

The wheel bundles the native library for its platform, so nothing needs compiling.

Initialisation

There is none to call. The library is loaded on the first call, from the copy in the wheel. In a source checkout it is taken from build-shared/ (make build-shared); set CANTUS_LIB to load a different file.

Quick start

import libcantus

libcantus.detect_chord_best([60, 64, 67])
# {'rootPc': 0, 'quality': 'maj', 'intervals': [0, 4, 7]}

chords = [[48, 60, 64, 67], [41, 60, 65, 69], [43, 59, 62, 65], [48, 60, 64, 67]]
notes = [
    {"pitch": pitch, "startBeat": bar * 4, "durationBeat": 4}
    for bar, pitches in enumerate(chords)
    for pitch in pitches
]
timeline = libcantus.Score.of(notes).timeline()
[entry["roman"] for entry in timeline.roman()]
# ['I', 'IV', 'V7', 'I']

try:
    libcantus.detect_chord([128])
except libcantus.InvalidInputError as error:
    str(error)
    # 'pitches[0] must be an integer in [0, 127]; received 128'
  • Function and method names are the snake_case form of the TypeScript names. An optional TypeScript options object becomes keyword-only arguments, where None means not given; a required one (generate_progression, Composer(...)) is passed as a dict with the TypeScript field names. A positional None, or one inside a list or dict, is sent as JSON null.
  • Model classes, hybrids and handles come back as objects. Release a handle with close() or a with block.
  • Errors derive from CantusError and carry code. InvalidInputError and BudgetExceededError are also ValueError, and NoSolutionError carries at. An exception raised inside a callback you passed is re-raised unchanged.
  • python -m libcantus <op> [ARGS_JSON | @file | -] is the same command line as the cantus tool.

Differences from pure TypeScript

Results, errors and messages are the same. What differs:

  • Unknown keyword arguments raise TypeError, from Python itself; TypeScript ignores unknown option keys.
  • Objects are copied across the boundary. A result is a fresh value on every call, and the per-object caches the TypeScript package keeps do not apply.
  • Handles (Rng, NoteEventIndex, ArrangementSession) are released explicitly. A closed handle raises CantusError with code RELEASED_HANDLE.

The complete list is in C++ core and surface differences, and the shared error table and edge-value behaviour are in Errors and validation.

License

Apache License 2.0.

Metadata

Release files for libcantus 1.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for libcantus 1.4.2
File Interpreter ABI Platform Actions
libcantus-1.4.2-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl Python 3 none Linux glibc 2.28+ x86-64, Linux glibc 2.27+ x86-64 Details for libcantus-1.4.2-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
libcantus-1.4.2-py3-none-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.28+ ARM64, Linux glibc 2.26+ ARM64 Details for libcantus-1.4.2-py3-none-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
libcantus-1.4.2-py3-none-macosx_26_0_arm64.whl Python 3 none macOS 26.0+ ARM64 Details for libcantus-1.4.2-py3-none-macosx_26_0_arm64.whl

Total release size: 9.2 MB

Release files / libcantus-1.4.2-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL libcantus-1.4.2-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 3.6 MB
Tags Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 Python 3
SHA-256 checksum
How to use checksums
58efba582abfcc67d11c841055c5bf700e1e005742dc24de172b3f25235a185f
BLAKE2b-256 checksum
How to use checksums
5ea623c89ad7063c7a2d98bb6a98ea29e75e90d9f8d91a899f3c577384d8cac6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / libcantus-1.4.2-py3-none-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl

Download URL libcantus-1.4.2-py3-none-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Size 3.2 MB
Tags Linux glibc 2.26+ ARM64 Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
c17a5069a2e781033faed89d31641600d770a1485943d4235a56e0638a9ab7f2
BLAKE2b-256 checksum
How to use checksums
e3502b85aeba0ab0e2d9d9c7cde3ab3a5044dac34852bc4b6c20479d48459d0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / libcantus-1.4.2-py3-none-macosx_26_0_arm64.whl

Download URL libcantus-1.4.2-py3-none-macosx_26_0_arm64.whl
Size 2.4 MB
Tags Python 3 macOS 26.0+ ARM64
SHA-256 checksum
How to use checksums
48ee06a50325829bdc7ff1cacbfaa70faa7279f1035e1b1b7712eb7895d4a770
BLAKE2b-256 checksum
How to use checksums
363eb289cdcd9c76be46dd58524fef82c590e8713a53bfd9abdaaf4363b98d1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Security for libcantus 1.4.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 3 files in this release have been uploaded with provenance metadata.

Select from 3 files

Attestation for libcantus-1.4.2-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page