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

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.0
File Interpreter ABI Platform Actions
libcantus-1.4.0-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl Python 3 none Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 Details for libcantus-1.4.0-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
libcantus-1.4.0-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.0-py3-none-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
libcantus-1.4.0-py3-none-macosx_26_0_arm64.whl Python 3 none macOS 26.0+ ARM64 Details for libcantus-1.4.0-py3-none-macosx_26_0_arm64.whl

Total release size: 9.1 MB

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

Download URL libcantus-1.4.0-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
999cf2b74e37a1b5ddc1939b48d16fc8528669182e46bc80e58add847edea124
BLAKE2b-256 checksum
How to use checksums
1e87deaa21ed2b3e943f7d40ee71c27853c6e2a9295ff355dd2bafccf1fd1183
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 1, 2026.

Transparency log

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

Download URL libcantus-1.4.0-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
dd7a39252bf041c639ee87987a7bc43b7cc763a22c72bfcfb9a0d55146070102
BLAKE2b-256 checksum
How to use checksums
7bd0ce53381f4363ec075600b2222665454a06cdb68e765a7aa8667d5a0da975
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 1, 2026.

Transparency log

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

Download URL libcantus-1.4.0-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
9331a80ec6e55b65c521972de9b3058a1345563abe2be3411847205fb2341da9
BLAKE2b-256 checksum
How to use checksums
13d225e6aa84000f9966eb82385b20f0f4bb5d02f259139729e8c378d8a7039e
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 1, 2026.

Transparency log

Security for libcantus 1.4.0

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.0-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 1, 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.

Release history Release notifications | RSS feed

This release

1.4.0 This release

3 release files Full provenance

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