Skip to main content

High-Performance Music Engine - Python bindings for Celeritas

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

celeritas-0.9.0.tar.gz (1.1 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

celeritas-0.9.0-cp311-cp311-win_amd64.whl (1.0 MB view details)

Uploaded CPython 3.11Windows x86-64

celeritas-0.9.0-cp311-cp311-manylinux_2_17_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

celeritas-0.9.0-cp311-cp311-macosx_12_0_universal2.whl (974.8 kB view details)

Uploaded CPython 3.11macOS 12.0+ universal2 (ARM64, x86-64)

File details

Details for the file celeritas-0.9.0.tar.gz.

File metadata

  • Download URL: celeritas-0.9.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for celeritas-0.9.0.tar.gz
Algorithm Hash digest
SHA256 ac302bc603819837db94b656f7058605b18ec85f2fee5e925c939eb8682224ff
MD5 c0912e6c6c037c12b2a13d260f6d4fe0
BLAKE2b-256 109526654acf5b81f8b8b7c1845a91df8a0f37a2873add3e97c1c3b5a89857cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for celeritas-0.9.0.tar.gz:

Publisher: ci.yml on sheinv78/Celeritas

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file celeritas-0.9.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: celeritas-0.9.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 1.0 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for celeritas-0.9.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 18c35bb7377e69ae7ca9ef499f8d44199b49393f866f916d35fc502c145c10d0
MD5 c471a82e091cb1f7d89e64c443c7ff02
BLAKE2b-256 aea11497e01336c80f47177d3d22dac6d268d79250a692681831ad9916e7cf96

See more details on using hashes here.

Provenance

The following attestation bundles were made for celeritas-0.9.0-cp311-cp311-win_amd64.whl:

Publisher: ci.yml on sheinv78/Celeritas

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file celeritas-0.9.0-cp311-cp311-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for celeritas-0.9.0-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 a6fb89e8051982b30e464a9f010ca429b28c85a7d3a1d4674e0e732bbabdef5a
MD5 e46907414c48f663fc04815d35d9ec66
BLAKE2b-256 e37a5d48d3de1ff117d27d6871fa5241c94f3d861a0e486be136f63197fddf2c

See more details on using hashes here.

Provenance

The following attestation bundles were made for celeritas-0.9.0-cp311-cp311-manylinux_2_17_x86_64.whl:

Publisher: ci.yml on sheinv78/Celeritas

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file celeritas-0.9.0-cp311-cp311-macosx_12_0_universal2.whl.

File metadata

File hashes

Hashes for celeritas-0.9.0-cp311-cp311-macosx_12_0_universal2.whl
Algorithm Hash digest
SHA256 e56763226a7979915e51fafdb3711a89bd0c24ce60ec2740f2ce007e16496381
MD5 055982985063093dd14457182be9660d
BLAKE2b-256 7111365cd66443c2dc877e5ad998b4b0e3c8c779f42044a2061fcf97cda2f14c

See more details on using hashes here.

Provenance

The following attestation bundles were made for celeritas-0.9.0-cp311-cp311-macosx_12_0_universal2.whl:

Publisher: ci.yml on sheinv78/Celeritas

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page