Skip to main content

Celeritas Python Bindings

Python bindings for the Celeritas high-performance music engine.

Installation

# From this repo (recommended for now)
pip install -e ./bindings/python

# Or, if/when published to PyPI
pip install celeritas

Quick Start

from celeritas import parse_note, transpose, identify_chord, detect_key, parse_chord_symbol, Trill

# Parse a note
note = parse_note("C4")
print(f"Pitch: {note.pitch}, Time: {note.time}, Duration: {note.duration}")

# Transpose pitches (uses SIMD acceleration)
pitches = [60, 64, 67]  # C, E, G
transposed = transpose(pitches, 2)  # Up 2 semitones
print(f"Transposed: {transposed}")  # [62, 66, 69] = D, F#, A

# Identify chord
chord = identify_chord([60, 64, 67])
print(f"Chord: {chord}")  # "Cmaj"

# Parse chord symbols (ANTLR-backed)
print(parse_chord_symbol("C7(b9,#11)"))
print(parse_chord_symbol("C/E"))
print(parse_chord_symbol("C|G"))

# Detect key
key_name, is_major = detect_key([60, 62, 64, 65, 67, 69, 71])
print(f"Key: {key_name} {'Major' if is_major else 'Minor'}")

# Create ornaments
note = parse_note("C4")
trill = Trill(note, interval=2, speed=8)
expanded_notes = trill.expand()
print(f"Trill expanded to {len(expanded_notes)} notes")

Features

  • SIMD-Accelerated Operations - Fast pitch transposition via native SIMD
  • Basic Note Notation - Parse single notes like C4, F#5, Bb3
  • Chord Identification - Identify a chord from MIDI pitches
  • Key Detection - Detect a key from a pitch sequence
  • Pitch Name Conversion - Convert MIDI pitches to note names (e.g. 60 -> C4)
  • Ornaments - Trills and mordents (expand to note sequences)

Chord-symbol parsing is implemented in the native library and exposed in Python via parse_chord_symbol.

API Reference

The Python bindings currently expose a focused subset of the Celeritas engine. This section lists the complete public API available from:

import celeritas

Types

  • NoteEvent(pitch, time_numerator, time_denominator, duration_numerator, duration_denominator, velocity=80)
    • Convenience properties: note.time and note.duration (floats)
  • ChordQuality (enum)
  • MordentType (enum): UPPER, LOWER
  • TurnType (enum): NORMAL, INVERTED

Functions

  • parse_note(notation: str) -> Optional[NoteEvent]
    • Parses a single note like C4, F#5, Bb3
  • transpose(pitches: List[int], semitones: int) -> List[int]
    • SIMD-accelerated transposition
  • midi_to_note_name(pitch: int, prefer_flats: bool = False) -> str
    • Converts MIDI pitch (0..127) to scientific pitch notation
  • identify_chord(pitches: List[int]) -> str
    • Returns chord symbol like Cmaj, Dm7, G7
  • detect_key(pitches: List[int]) -> Tuple[str, bool]
    • Returns (key_name, is_major)
  • parse_chord_symbol(symbol: str, max_pitches: int = 32) -> Optional[List[int]]
    • Parses chord symbols like C7(b9,#11), C/E, C|G

Ornaments

  • Trill(base_note: NoteEvent, interval: int = 2, speed: int = 8, start_with_upper: bool = False, end_with_turn: bool = False)
    • expand() -> List[NoteEvent]
  • Mordent(base_note: NoteEvent, mordent_type: MordentType = MordentType.UPPER, interval: int = 2, alternations: int = 1)
    • expand() -> List[NoteEvent]

If you want the Python bindings to cover more of the C# API, the usual path is:

  1. add new NativeAOT exports in src/Celeritas.Native, then
  2. expose them via ctypes in bindings/python/celeritas/celeritas.py.

Full .NET API (Complete Coverage)

If you need the entire Celeritas .NET API surface in Python, the bindings include an opt-in bridge powered by pythonnet.

This keeps the current NativeAOT bindings (fast + no extra deps) and additionally enables calling any public .NET type/method directly.

Install

pip install pythonnet

Build the managed assembly

From the repo root:

dotnet build src/Celeritas/Celeritas.csproj -c Release

Use

from celeritas import load_celeritas

result = load_celeritas()  # loads Celeritas.dll via pythonnet
Celeritas = result.namespace

print("Loaded:", result.assembly_path)

# From here you can use the full .NET API under the Celeritas namespace.

You can also override the assembly location:

export CELERITAS_DOTNET_ASSEMBLY=/abs/path/to/Celeritas.dll

Requirements

  • Python 3.8+
  • Celeritas native library (included in package)

Performance

Python bindings maintain near-native performance for core operations:

import time
from celeritas import transpose

# Benchmark: transpose 1 million notes
pitches = list(range(60, 72)) * 100000  # 1.2M pitches
start = time.time()
transposed = transpose(pitches, 5)
elapsed = time.time() - start
print(f"Transposed {len(pitches)} pitches in {elapsed*1000:.2f}ms")
# Note: timings vary widely by CPU, build flags, and Python overhead.

License

Licensed under the Business Source License 1.1 (BSL-1.1).

  • ✅ Free for non-commercial use
  • ✅ Free for open source projects
  • ⚠️ Commercial use requires a license

For commercial licensing, contact: sheinv78@gmail.com

Testing

Run the test suite:

# Using unittest (standard library)
python test_celeritas.py

# Or with pytest
pip install pytest
pytest test_celeritas.py -v

# With coverage
pip install pytest-cov
pytest test_celeritas.py --cov=celeritas --cov-report=html

Test Coverage: 35 tests (100% passing) covering NoteEvent, parsing, transpose (SIMD), chord/key detection, ornaments (trills, mordents), and integration scenarios.

Building Native Library

The Python bindings require a native library built with NativeAOT:

Quick path (recommended):

# From repo root
pwsh ./scripts/build-python-native.ps1 -Configuration Release -Runtime win-x64
# Build for Windows
dotnet publish ../../src/Celeritas.Native/Celeritas.Native.csproj -c Release -r win-x64

# Build for Linux
dotnet publish ../../src/Celeritas.Native/Celeritas.Native.csproj -c Release -r linux-x64

# Build for macOS
dotnet publish ../../src/Celeritas.Native/Celeritas.Native.csproj -c Release -r osx-x64

# Copy to package folder (Windows example)
cp ../../src/Celeritas.Native/bin/Release/net10.0/win-x64/publish/Celeritas.Native.dll celeritas/native/

Documentation

Full documentation available at: https://github.com/sheinv78/Celeritas

Examples

Working with Notes

from celeritas import NoteEvent

note = NoteEvent(
    pitch=60,  # Middle C
    time_numerator=0,
    time_denominator=1,
    duration_numerator=1,
    duration_denominator=4,  # Quarter note
    velocity=80
)

Ornaments

from celeritas import Trill, Mordent, MordentType

# Create a trill
note = parse_note("E5")
trill = Trill(note, interval=2, speed=8, end_with_turn=True)
notes = trill.expand()

# Create an upper mordent
mordent = Mordent(note, mordent_type=MordentType.UPPER)
notes = mordent.expand()

Support

Release files for celeritas 0.9.0

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

Source distribution (sdist)

Source distribution for celeritas 0.9.0
File Size Uploaded
celeritas-0.9.0.tar.gz 1.1 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for celeritas 0.9.0
File Interpreter ABI Platform
celeritas-0.9.0-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
celeritas-0.9.0-cp311-cp311-manylinux_2_17_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details
celeritas-0.9.0-cp311-cp311-macosx_12_0_universal2.whl CPython 3.11 CPython 3.11 macOS 12.0+ universal2 (ARM64, x86-64) Details

Total release size: 4.2 MB

Release files / celeritas-0.9.0.tar.gz

Download URL celeritas-0.9.0.tar.gz
Size 1.1 MB
Tags Source
SHA-256 checksum
How to use checksums
ac302bc603819837db94b656f7058605b18ec85f2fee5e925c939eb8682224ff
BLAKE2b-256 checksum
How to use checksums
109526654acf5b81f8b8b7c1845a91df8a0f37a2873add3e97c1c3b5a89857cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Dec 30, 2025.

Transparency log

Release files / celeritas-0.9.0-cp311-cp311-win_amd64.whl

Download URL celeritas-0.9.0-cp311-cp311-win_amd64.whl
Size 1.0 MB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
18c35bb7377e69ae7ca9ef499f8d44199b49393f866f916d35fc502c145c10d0
BLAKE2b-256 checksum
How to use checksums
aea11497e01336c80f47177d3d22dac6d268d79250a692681831ad9916e7cf96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Dec 30, 2025.

Transparency log

Release files / celeritas-0.9.0-cp311-cp311-manylinux_2_17_x86_64.whl

Download URL celeritas-0.9.0-cp311-cp311-manylinux_2_17_x86_64.whl
Size 1.1 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
a6fb89e8051982b30e464a9f010ca429b28c85a7d3a1d4674e0e732bbabdef5a
BLAKE2b-256 checksum
How to use checksums
e37a5d48d3de1ff117d27d6871fa5241c94f3d861a0e486be136f63197fddf2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Dec 30, 2025.

Transparency log

Release files / celeritas-0.9.0-cp311-cp311-macosx_12_0_universal2.whl

Download URL celeritas-0.9.0-cp311-cp311-macosx_12_0_universal2.whl
Size 974.8 kB
Tags CPython 3.11 macOS 12.0+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
e56763226a7979915e51fafdb3711a89bd0c24ce60ec2740f2ce007e16496381
BLAKE2b-256 checksum
How to use checksums
7111365cd66443c2dc877e5ad998b4b0e3c8c779f42044a2061fcf97cda2f14c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Dec 30, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.9.0 This release

4 release files

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