libcantus
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_caseform of the TypeScript names. An optional TypeScript options object becomes keyword-only arguments, whereNonemeans not given; a required one (generate_progression,Composer(...)) is passed as a dict with the TypeScript field names. A positionalNone, or one inside a list or dict, is sent as JSONnull. - Model classes, hybrids and handles come back as objects. Release a handle with
close()or awithblock. - Errors derive from
CantusErrorand carrycode.InvalidInputErrorandBudgetExceededErrorare alsoValueError, andNoSolutionErrorcarriesat. 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 thecantustool.
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 raisesCantusErrorwith codeRELEASED_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
Metadata
Release files for libcantus 1.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| libcantus-1.4.1-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.1-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
| libcantus-1.4.1-py3-none-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl | Python 3 | none | Linux glibc 2.26+ ARM64, Linux glibc 2.28+ ARM64 | Details for libcantus-1.4.1-py3-none-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl |
| libcantus-1.4.1-py3-none-macosx_26_0_arm64.whl | Python 3 | none | macOS 26.0+ ARM64 | Details for libcantus-1.4.1-py3-none-macosx_26_0_arm64.whl |
Total release size: 9.2 MB
Release files / libcantus-1.4.1-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | libcantus-1.4.1-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 |
82ff81ef2f225ea904cd18a8758719b73a3831272c4b005992d71d354cad2a49
|
|
BLAKE2b-256 checksum How to use checksums |
efb7ef0adb8a52f485dee8ad518b3c91dc8ae7abe59b0b796507df7fc7303638
|
| 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 3, 2026.
Transparency logRelease files / libcantus-1.4.1-py3-none-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
| Download URL | libcantus-1.4.1-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 |
ee4900e937d05381daf32aeabaf767b86914c9c0cc249f7d7cbe48df1e7faa27
|
|
BLAKE2b-256 checksum How to use checksums |
5d4de93b990e9af5bfdbc38c56e507a5d02fe24dd36f40600b5c38e37fe6b0a2
|
| 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 3, 2026.
Transparency logRelease files / libcantus-1.4.1-py3-none-macosx_26_0_arm64.whl
| Download URL | libcantus-1.4.1-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 |
bb870405fc1ba47d9f0a06a0e195c8567d70581d9629874703263b056bda1c98
|
|
BLAKE2b-256 checksum How to use checksums |
45fb8861012db826518062233fa090796db6437b149292b8591f84ca281b7d01
|
| 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 3, 2026.
Transparency logSecurity for libcantus 1.4.1
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.
Attestation for libcantus-1.4.1-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 3, 2026.
Transparency logA 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.