Skip to main content

libcantus

CI PyPI License Python

Detect chords, keys, Roman numerals and cadences in MIDI note events from Python, and write new parts against that same harmonic reading.

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.3

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.3
File Interpreter ABI Platform Actions
libcantus-1.4.3-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.3-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
libcantus-1.4.3-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.3-py3-none-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
libcantus-1.4.3-py3-none-macosx_26_0_arm64.whl Python 3 none macOS 26.0+ ARM64 Details for libcantus-1.4.3-py3-none-macosx_26_0_arm64.whl

Total release size: 9.2 MB

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

Download URL libcantus-1.4.3-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
c4d8926f95a08fbeb5add63513ef1f14b3da0aab981dd34a8b957cf96f035401
BLAKE2b-256 checksum
How to use checksums
3a0a935990f1482b058c0cec73fa3233d4dcba6d29d9cb4aea6b54872bba15d2
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.3-py3-none-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl

Download URL libcantus-1.4.3-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
fa3331abda5efab1c92ad40d4f166f1ee857335bfbfde49c5d3de74eb71e7fee
BLAKE2b-256 checksum
How to use checksums
69016cf638899f72b21a5dcf4894060820dd9ffb2873a5d52421d76dcdd3c151
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.3-py3-none-macosx_26_0_arm64.whl

Download URL libcantus-1.4.3-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
4c423c9c613dd6793660010a492caee8e1bb3f5ca859f21de131ef9386ce59d4
BLAKE2b-256 checksum
How to use checksums
a94e90367c5d7074aa8857d029d2aa610fd4185c5a6f27b59bab122fbc3ea33b
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.3

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.3-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