Skip to content
librazPublic

Repository files navigation

libcantus

CI npm PyPI codecov License C++17 TypeScript Platform docs

libcantus detects chords, keys, Roman numerals and cadences in MIDI note events, and writes new parts against that same harmonic reading.

It is a music theory library for TypeScript, JavaScript (WebAssembly), Python, Node, C++ and the command line. One specification has a pure TypeScript reference implementation and a C++17 core under it. Every surface gives the same results and the same errors, and the surfaces over the core give the same floating-point bits on every platform.

📖 Documentation  ·  Getting started  ·  Packages and surfaces

From note events to parts, and back

What's inside

  • Harmony from notes — chord and key detection, Roman numerals, cadences, modulation and form from MIDI notes. Analysis
  • Parts against the harmony — progressions, bass, counter-melody, drums and motifs, seeded and reproducible. Generation
  • Theory vocabulary — notes, intervals, scales, chords, voicing, part-writing and counterpoint checks. Harmony
  • Playability — instrument profiles, transposition and range checks. Instruments
  • Evidence in answers — each reading carries its rationale and the readings it turned down; an undecidable question comes back null. Analysis

Surfaces

Surface Package Page
TypeScript (reference) @libraz/libcantus ts/
C++ CMake package cantus C++ core
WebAssembly @libraz/libcantus-wasm bindings/wasm/
Node native @libraz/libcantus-node (built from source) bindings/node/
Python libcantus bindings/python/
Command line cantus Command line

The TypeScript package is synchronous, dependency-free and tree-shakeable. The other surfaces call the C++ core and differ from it only in small, documented ways (C++ core and surface differences).

Installation

yarn add @libraz/libcantus        # TypeScript, Node.js 22 or later
yarn add @libraz/libcantus-wasm   # WebAssembly, ESM only, the same API and types
pip install libcantus             # Python 3.10 or later

The Node native addon builds from a checkout (make build-node), the cantus command-line tool is a release asset on GitHub, and C++ takes CMake 3.24 and a C++17 compiler:

include(FetchContent)
FetchContent_Declare(cantus GIT_REPOSITORY https://github.com/libraz/libcantus.git GIT_TAG main)
FetchContent_MakeAvailable(cantus)
target_link_libraries(app PRIVATE cantus::cantus)

find_package(cantus) against an installed prefix and add_subdirectory work the same way.

Quick start

Each example reads the chord of C, E and G. Runnable versions are in examples/ and run with make examples; the browser example there is a bundler template that make examples does not run.

TypeScript

import { detectChordBest } from '@libraz/libcantus';

detectChordBest([60, 64, 67]); // { rootPc: 0, quality: 'maj', intervals: [0, 4, 7] }

→ Getting started

JavaScript (WASM)

import { detectChordBest } from '@libraz/libcantus-wasm';

detectChordBest([60, 64, 67]);

Code that imports @libraz/libcantus can switch to it without edits, through a package-manager or bundler alias.

→ Switching between pure TypeScript and WebAssembly

Node native

import { detectChordBest } from '@libraz/libcantus-node';

detectChordBest([60, 64, 67]);

→ Packages and surfaces

Python

import libcantus

libcantus.detect_chord_best([60, 64, 67])

→ Python

Command line

cantus detectChordBest '[[60, 64, 67]]'

→ Command line

C++

#include <cantus/cantus.h>

auto matches = cantus::detect_chord({60, 64, 67});

→ C++ core

Build from source

make build         # C++ core, command-line tool and tests in build/
make test-cxx      # run the C++ tests
make wasm          # WebAssembly package (needs Emscripten)
make build-node    # Node-API addon
make build-shared  # shared library for the Python binding
make examples      # build and run the examples in examples/ (not the browser one)

The toolchain pins and every check a change runs through are in CONTRIBUTING.md.

Related projects

  • libsonare — the audio layer below libcantus: analysis, mastering, synthesis and Standard MIDI File I/O for C++, Python, Node.js and WebAssembly (libsonare.libraz.net). The two share no code and neither requires the other.
  • sonare studio — a browser-based DAW whose music theory and composition features come from @libraz/libcantus and whose audio runs on libsonare. A hosted live demo, not a production product. Source: libraz/sonare-studio.
  • midi-sketch — a rule-based pop MIDI generator built on @libraz/libcantus.

Documentation

The documentation is Markdown in docs/en/, with the same pages in Japanese under docs/ja/; the symbol reference is generated at libraz.github.io/libcantus.

Non-goals

  • No I/O, notation or audio. No MIDI file reader or writer, no score rendering, no audio analysis, no playback. Bring your own parser and hand it note events. For the layer below, libsonare covers audio analysis, mastering, synthesis and SMF I/O; the two share no code and neither requires the other.
  • No microtonal analysis. Frequencies, cents and just-intonation ratios live in core, but everything above the pitch layer runs on twelve pitch classes.
  • No rāga or maqām-style modal grammars. WORLD_SCALES records the pitch material a tradition names, not the grammar built on it.
  • No corpus. Nothing is bundled to run statistics over.

License

Apache-2.0

Releases

Packages

Contributors

Languages