Skip to main content

ChordSketch

chordsketch

ChordSketch Python bindings — parse and render ChordPro and iReal Pro chord charts from Python via native Rust extensions generated by UniFFI.

Native extension — no pure-Python fallback. The library compiles to a platform wheel; prebuilt wheels for common platforms are published to PyPI so no Rust toolchain is needed to install.

Installation

pip install chordsketch

Requires Python 3.8+ (CPython or PyPy).

On Debian / Ubuntu the system Python refuses pip install outside a virtual environment (externally-managed-environment); create one first: python3 -m venv .venv && . .venv/bin/activate.

Quick Start

import chordsketch

source = """{title: Amazing Grace}
{key: G}

[G]Amazing [G7]grace, how [C]sweet the [G]sound"""

html = chordsketch.parse_and_render_html(source, None, None)
text = chordsketch.parse_and_render_text(source, None, None)
pdf  = chordsketch.parse_and_render_pdf(source, None, None)   # bytes

print(chordsketch.version())

API

The tables below cover every function in crates/ffi/src/chordsketch.udl (UniFFI's namespace chordsketch { ... } block is the authoritative export surface for all language bindings).

Render-function parameters

parse_and_render_* functions all accept the same three arguments:

Parameter Type Description
input str ChordPro source text
config_json str | None Preset name ("guitar", "ukulele") or inline RRJSON; None for defaults
transpose int | None Semitone offset; must fit in i8 (-128..=127). Out-of-range values raise ChordSketchError.InvalidConfig. None defaults to 0.

Basic rendering

Function Returns Description
parse_and_render_text(input, config_json, transpose) str Plain text output
parse_and_render_html(input, config_json, transpose) str Full HTML document
parse_and_render_pdf(input, config_json, transpose) bytes Raw PDF bytes

Body-only HTML and stylesheet

Function Returns Description
parse_and_render_html_body(input, config_json, transpose) str Body-only <div class="song">…</div> HTML fragment with no <!DOCTYPE> / <html> / <head> / <title> / embedded <style> — pair with render_html_css when the host supplies its own document envelope
render_html_css() str Canonical chord-over-lyrics CSS that parse_and_render_html embeds inside <style> (byte-stable; safe to hash for cache-busting)
render_html_css_with_config_json(config_json) str Variant of render_html_css that honours settings.wraplines from the supplied config (when wraplines is false, .line emits flex-wrap: nowrap)

Captured warnings

Function Returns Description
parse_and_render_text_with_warnings(input, config_json, transpose) TextRenderWithWarnings { output: str, warnings: list[str] } Plain text + captured warnings
parse_and_render_html_with_warnings(input, config_json, transpose) TextRenderWithWarnings { output: str, warnings: list[str] } HTML + captured warnings
parse_and_render_pdf_with_warnings(input, config_json, transpose) PdfRenderWithWarnings { output: bytes, warnings: list[str] } PDF + captured warnings
parse_and_render_html_body_with_warnings(input, config_json, transpose) TextRenderWithWarnings { output: str, warnings: list[str] } Body-only HTML fragment + captured warnings (body counterpart to parse_and_render_html_with_warnings)

iReal Pro conversion

Function Returns Description
convert_chordpro_to_irealb(input) ConversionWithWarnings { output: str, warnings: list[str] } Convert ChordPro source to an irealb:// URL (lossy — drops lyrics, fonts, capo)
convert_irealb_to_chordpro_text(input) ConversionWithWarnings { output: str, warnings: list[str] } Convert an irealb:// URL to rendered ChordPro text
render_ireal_svg(input) str (SVG document) Render an irealb:// URL as an iReal Pro-style SVG chart
render_ireal_png(input) bytes (PNG byte stream) Render an irealb:// URL as a PNG image (300 DPI default, A4-equivalent canvas)
render_ireal_pdf(input) bytes (PDF byte stream) Render an irealb:// URL as a single-page A4 PDF document
parse_irealb(input) str (JSON) Parse an irealb:// URL into AST-shaped JSON (mirrors IrealSong)
serialize_irealb(input) str (URL) Serialize an AST-shaped JSON string back into an irealb:// URL (round-trips with parse_irealb)

output of convert_irealb_to_chordpro_text is the rendered text representation of the converted song (chordsketch-render-text output), not raw ChordPro source. Each warnings entry is a "<kind>: <message>" string (kind is lossy-drop, approximated, or unsupported).

result = chordsketch.convert_chordpro_to_irealb("{title: Test}\n[C]Hello")
print(result.output)    # "irealb://..."
print(result.warnings)  # ["lossy-drop: lyrics are dropped", ...]

text = chordsketch.convert_irealb_to_chordpro_text(result.output)
print(text.output)

The *_with_warnings variants return the render warnings (transpose saturation, chorus recall limits, {columns} clamp, etc.) as a list alongside the output instead of forwarding them to sys.stderr / NSLog / System.err / $stderr. Use them when embedding ChordSketch in an app that needs to surface warnings in the UI or aggregate them. See #1827.

Validation

Function Returns Description
validate(input) list[ValidationError] ({ line: int, column: int, message: str }, line / column one-based) Validate ChordPro input and return any parse errors as structured records (empty list if clean). Mirrors the WASM validate shape and the NAPI ValidationError[] interface.
errors = chordsketch.validate(source)  # list[ValidationError] — empty if clean
for e in errors:
    print(f"line {e.line}, column {e.column}: {e.message}")

Chord diagrams

Function Returns Description
chord_diagram_svg(chord, instrument) str | None (SVG markup) Render a chord diagram as inline SVG. instrument is case-insensitive: "guitar", "ukulele" (alias "uke"), "charango", or "piano" (aliases "keyboard", "keys"). Returns None when the chord is not in the built-in voicing database; raises ChordSketchError.InvalidConfig on unknown instrument.
chord_diagram_svg_with_defines(chord, instrument, defines) str | None Same as chord_diagram_svg but consults song-level {define} voicings first. defines is a list of (name, raw) tuples.
chord_diagram_svg_with_orientation(chord, instrument, orientation=None) str | None Orientation-aware variant. orientation: "vertical" (default) or "horizontal" (nut on the left, Japanese tablature convention). Horizontal mode is reader-view only (high pitch on top, matches tablature stave order); see ADR-0026. Unrecognised strings fall back to vertical.
chord_diagram_svg_with_defines_orientation(chord, instrument, defines, orientation=None) str | None Combined surface — accepts both song-level {define} voicings and the orientation knob.
chord_pitches(chord) list[int] | None Constituent pitches of a chord as MIDI note numbers, for driving an audio synth. Returns a block voicing (root, third, fifth, plus any extension / altered / added tones, with a slash bass an octave below); None when the chord is not parseable.
diagram_pitches(chord, instrument, defines) list[int] | None MIDI note numbers sounded by the chord diagram drawn for (chord, instrument) — for auditioning a diagram as exactly the shape it depicts, rather than the name-based block voicing chord_pitches returns. Fretted instruments return one pitch per non-muted string in string order; keyboard instruments return the highlighted keys. defines is the same ChordDefine list chord_diagram_svg_with_defines consults. None when no diagram is available.
chord_staff_notes(chord) list[StaffNote] | None Constituent tones of a chord spelled for staff notation, ascending by pitch (a slash bass sorts first). Each tone is spelled diatonically from the chord's structure so it lands on its conventional staff line (e.g. Ebm7 → E♭ G♭ B♭ D♭, not D♯ F♯ A♯ C♯). None when the chord is not parseable.
key_scale_pitches(key) list[int] | None Ascending one-octave scale of a musical key as MIDI note numbers — the movable-do "do re mi fa sol la ti do". Major keys yield the major scale; minor keys the natural-minor scale. Eight notes; None when the key is not parseable.
key_tonic_triad(key) list[int] | None Tonic triad of a musical key as MIDI note numbers (the "do mi sol" chord). Major / minor per the key; extensions on the spelling are ignored. Three notes; None when the key is not parseable.

StaffNote exposes letter: str ("A"–"G"), accidental: int (signed semitone offset from the letter, -3..=3), octave: int (scientific-pitch-notation octave, middle C = C4), and midi: int (absolute MIDI note number).

Utility

Function Returns Description
version() str Library version string
print(chordsketch.version())  # e.g. "0.3.0"

Options

# Transpose up 2 semitones with the ukulele preset
html = chordsketch.parse_and_render_html(source, "ukulele", 2)

# Inline RRJSON configuration
html = chordsketch.parse_and_render_html(
    source,
    '{"settings": {"notation": "solfege"}}',
    None,
)

Error handling

Functions raise chordsketch.ChordSketchError on invalid configuration:

try:
    html = chordsketch.parse_and_render_html(source, "{ bad json !!!", None)
except chordsketch.ChordSketchError as e:
    print(e)

ChordSketchError has three variants:

  • NoSongsFound — the input produced no parseable songs (rare with lenient parsing)
  • InvalidConfig(reason) — the config_json argument is not a known preset and not valid RRJSON, or transpose is outside the i8 range, or chord_diagram_svg was called with an unsupported instrument
  • ConversionFailed(reason) — a ChordPro ↔ iReal Pro conversion failed (convert_chordpro_to_irealb, convert_irealb_to_chordpro_text, render_ireal_*, parse_irealb, serialize_irealb)

Parse errors in the ChordPro input are not raised — the renderer is lenient and produces a best-effort result. Call validate() to surface diagnostics.

License

MIT

Metadata

Release files for chordsketch 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 chordsketch 0.9.0
File Size Uploaded Provenance Actions
chordsketch-0.9.0.tar.gz 6.4 MB Without provenance Details for chordsketch-0.9.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for chordsketch 0.9.0
File Actions
chordsketch-0.9.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for chordsketch-0.9.0-py3-none-win_amd64.whl
chordsketch-0.9.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl Python 3 none Linux glibc 2.17+ x86-64 Details for chordsketch-0.9.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
chordsketch-0.9.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64 Details for chordsketch-0.9.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
chordsketch-0.9.0-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for chordsketch-0.9.0-py3-none-macosx_11_0_arm64.whl
chordsketch-0.9.0-py3-none-macosx_10_12_x86_64.whl Python 3 none macOS 10.12+ x86-64 Details for chordsketch-0.9.0-py3-none-macosx_10_12_x86_64.whl

Total release size: 44.2 MB

Release files / chordsketch-0.9.0.tar.gz

Download URL chordsketch-0.9.0.tar.gz
Size 6.4 MB
Tags Source
SHA-256 checksum
How to use checksums
39d48bd4b33de10874a5a9a23f330638e2c92f4570e9d18b0da0dee89172986c
BLAKE2b-256 checksum
How to use checksums
fccbb84567e085b41614de54a1e760dd583e77b71d99c5fc6ebd597063e8380e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / chordsketch-0.9.0-py3-none-win_amd64.whl

Download URL chordsketch-0.9.0-py3-none-win_amd64.whl
Size 7.4 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
0d9ce3ec75d52fe8febcbd7dbae55d5f6cba635e1cccf4ae76a9895ec407a9a4
BLAKE2b-256 checksum
How to use checksums
c178eb24d12f8ad61f6165da6865ee528aafbd7edb7f5d22da14b7f35dbd2dd1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / chordsketch-0.9.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL chordsketch-0.9.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 7.6 MB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
6413679ff3948c6c229a3e82cb3691ae454425bc1e86a9a55a6705550ced0ab4
BLAKE2b-256 checksum
How to use checksums
77791035dd461bfb047d32ed1e102923b6635d63bdd9e619dd890f6fdfece797
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / chordsketch-0.9.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL chordsketch-0.9.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 7.8 MB
Tags Linux glibc 2.17+ ARM64 Python 3
SHA-256 checksum
How to use checksums
477c46bbb5cc70ab59dae10bb871553e8ae920f3a0f02847825852526253fb65
BLAKE2b-256 checksum
How to use checksums
14dfcc17f30b35205443bcf92ce06c2a4dc395506df13822c2f18521b3e8fd08
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / chordsketch-0.9.0-py3-none-macosx_11_0_arm64.whl

Download URL chordsketch-0.9.0-py3-none-macosx_11_0_arm64.whl
Size 7.5 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
52b284d34ea8577953527364ba1a5ab1163a0dcd45680d77caa4a18b488c8a5b
BLAKE2b-256 checksum
How to use checksums
5663105a528bf2d441cd638c4a9f5f449fa8df039fa97436f2ed24f779ae3098
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Release files / chordsketch-0.9.0-py3-none-macosx_10_12_x86_64.whl

Download URL chordsketch-0.9.0-py3-none-macosx_10_12_x86_64.whl
Size 7.5 MB
Tags Python 3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
051bc0ffd349de6cb09f3689f8ce12ec06f1d55089cefb975c13c0b75a09ed11
BLAKE2b-256 checksum
How to use checksums
2ab03f42bdc141f44f0c0237dfd4743fdddaa0c262f76c3ad9024487f7b4c515
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via maturin/1.15.0

Security for chordsketch 0.9.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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

0.9.0 This release

6 release files No 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