Skip to main content

cignal

cignal compiles DSP kernels written in a static subset of Python to C99, for real-time audio hosts. The same source runs unchanged under CPython, as the reference model the C is tested against.

  • One C99 kernel ABI: a state struct, init, process, set_param, reset. No allocation, locks or I/O in the processing path.

  • Host shims for Pd externals and SuperCollider UGens, plus a standalone test driver.

  • Compiled with double in place of f32, the C matches CPython bit for bit. The test suite checks this for every fixture on macOS (clang) and Linux (GCC, glibc).

The design follows Vult. The full specification, with the reasons for each choice, is in docs/dev/spec.md.

Status: pre-release. Nothing is published to PyPI yet.

Requirements

  • Python 3.12 or later, and uv.

  • A C99 compiler (cc, or $CC). The SuperCollider shim also needs a C++17 compiler (c++, or $CXX).

  • For Pd externals: m_pd.h, from $PD_INCLUDE, /usr/include/pd or a macOS Pd*.app.

  • For SuperCollider UGens: the plugin headers, from $SC_INCLUDE or /usr/include/SuperCollider. The macOS app does not include them.

  • Without either, cignal fetch-headers downloads m_pd.h from Pd 0.57.0 and the SuperCollider 3.14.1 headers into the user cache, verified against pinned SHA-256 hashes. cignal does not ship them: the SuperCollider headers are GPL-2.0-or-later.

uv sync

A kernel

import math

from cignal import Kernel, f32, param


class Section(Kernel):
    b0: f32 = 1.0
    b1: f32 = 0.0
    b2: f32 = 0.0
    a1: f32 = 0.0
    a2: f32 = 0.0
    z1: f32 = 0.0
    z2: f32 = 0.0

    def tick(self, x: f32) -> f32:
        y = self.b0 * x + self.z1
        self.z1 = self.b1 * x - self.a1 * y + self.z2
        self.z2 = self.b2 * x - self.a2 * y
        return y


class Lowpass(Kernel):
    cutoff: f32 = param(1000.0, lo=20.0, hi=20000.0)
    q: f32 = param(0.707, lo=0.1, hi=20.0)
    section: Section  # a sub-kernel: its state is part of Lowpass's struct

    def control(self) -> None:  # once per block
        w = 2.0 * math.pi * self.cutoff / self.sample_rate
        alpha = math.sin(w) / (2.0 * self.q)
        c = math.cos(w)
        a0 = 1.0 + alpha
        self.section.b0 = (1.0 - c) / 2.0 / a0
        self.section.b1 = (1.0 - c) / a0
        self.section.b2 = self.section.b0
        self.section.a1 = -2.0 * c / a0
        self.section.a2 = (1.0 - alpha) / a0

    def tick(self, x: f32) -> f32:  # once per sample
        return self.section.tick(x)

As Python:

lp = Lowpass(sample_rate=48000)
lp.set_param("cutoff", 500.0)
out = lp.process([[1.0] + [0.0] * 63], 64)  # one list per input channel

Fields are state. tick sets the inputs and outputs: its parameters are input samples, and it returns one sample or a tuple. init runs once, and again on reset.

Build

uv run cignal check lowpass.py                       # diagnostics only
uv run cignal build lowpass.py -o out                # out/lowpass.h, out/lowpass.c
uv run cignal build lowpass.py -o out --shim pd      # out/lowpass~.pd_darwin (or .pd_linux)
uv run cignal build lowpass.py -o out --shim sc      # out/CignalLowpass.so (.scx) and CignalLowpass.sc
uv run cignal build lowpass.py -o out --prefix fx_   # fx_Lowpass, fx_Section, ...
uv run cignal fetch-headers                          # or `pd` / `sc`: host headers into the user cache

A file with several kernels needs --kernel NAME, unless only one of them is not a field of another.

  • Pd. Inlets are the signal inputs, then one float inlet per parameter. Creation arguments set the parameters in order, and a reset message clears state. Pick a name that is not a Pd built-in with --pd-name: biquad~ would load Pd's own object, without an error.

  • SuperCollider. Copy the plugin and the .sc class file into an extensions folder. UGen inputs are the signal inputs, then one input per parameter: CignalLowpass.ar(sig, 800, 2). Parameters are read once per block. /u_cmd node ugen "reset" clears state.

Both shims flush denormals to zero while the kernel runs, and restore the host's mode afterwards.

Things that differ from plain Python

  • Types are explicit. f32, f64, i32, bool. A bare float or int annotation is an error. Mixing types without a cast (f32(x)) is an error; literals adapt.

  • Python semantics win where C differs. // and % floor, as in Python; -7 // 2 is -4. >> floors on negative values.

  • i32 wraps at 32 bits in C. CPython ints do not, so a value that leaves i32 range is outside the contract, unless it is masked back into range.

  • Sizes are build-time constants. Array[f32, 16], Delay[f32, MAX], @table(size=512, ...). Module-level constants and math expressions on them are folded in CPython.

  • Array indices must be provably in range. Use a constant, a range loop variable with constant bounds, or e % N. Anything else is a compile error, and there are no runtime bounds checks.

  • Delay.read(d) counts from 1. read(1) is the newest sample, so read(D) before write(x) delays by exactly D samples.

  • Smoothed parameters (param(..., smooth=0.01)) glide per sample. control may not read them, because the value it saw would depend on block size.

  • Tables (@table(size=..., lo=..., hi=...) on a module-level function) are sampled in CPython at build time and interpolated linearly. The function body can use anything, SciPy included.

  • Module-level functions called from kernels are compiled; others, such as build-time filter design, are left alone.

  • while compiles, with a warning: its termination is not checked against the audio deadline.

Performance

K_process matches hand-written C for a biquad on clang and GCC, and beats it for a 16-tap FIR. make bench prints ns per sample for every fixture. Float % inside a recursion is slow on clang. For a phase accumulator whose increment is in [0, 1), use phase += inc; if phase >= 1.0: phase -= 1.0. See spec section 9.5.

Tests

make test                     # pytest; Pd and SuperCollider tests skip when not installed
make qa                       # lint, format and type checks, then tests, as in CI
make linux-image test-linux   # Debian in an Apple `container` VM: GCC, glibc, Pd 0.55, SuperCollider 3.13
make linux-image test-linux ARCH=amd64
make bench

Each fixture in tests/fixtures/ is compiled and checked four ways:

  • the double build against CPython, bit for bit;

  • the float build against the double build, SNR >= 100 dB;

  • six block-size patterns, which must give identical output;

  • in Pd and in SuperCollider, against the driver, bit for bit, with mid-stream parameter changes and resets.

Layout

src/cignal/kernel.py       the Python runtime: Kernel, param, Array, Delay, table
src/cignal/compiler.py     source -> typed walk -> C
src/cignal/build.py        writing C, rendering shims, invoking the compiler
src/cignal/headers.py      `cignal fetch-headers`: pinned host header downloads
src/cignal/shims/          driver, Pd, SuperCollider, benchmark and flush-to-zero templates
tests/                     fixtures and the differential, host, diagnostics and codegen tests
thirdparty/                Pd and SuperCollider headers for the tests, not packaged
bench/                     the benchmark runner and hand-written C references
ci/linux.Containerfile     the Linux test image
docs/dev/spec.md           the specification

License

MIT. See LICENSE. The headers in thirdparty/ keep their own licenses: Pd (BSD-3-Clause) and SuperCollider (GPL-2.0-or-later).

Metadata

Release files for cignal 0.0.2

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

Source distribution (sdist)

Source distribution for cignal 0.0.2
File Size Uploaded Provenance Actions
cignal-0.0.2.tar.gz 71.0 kB Without provenance Details for cignal-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cignal 0.0.2
File Interpreter ABI Platform Actions
cignal-0.0.2-py3-none-any.whl Python 3 none any Details for cignal-0.0.2-py3-none-any.whl

Total release size: 122.9 kB

Release files / cignal-0.0.2.tar.gz

Download URL cignal-0.0.2.tar.gz
Size 71.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d64e09e051d898eebd51998d280433bff2bc707a458f53f0ffb4eddf81806bf7
BLAKE2b-256 checksum
How to use checksums
6c778d6287e096f07fdb3d32a0b7d2665b7a8845cd8fb8ca0e394fb6fec2f28f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / cignal-0.0.2-py3-none-any.whl

Download URL cignal-0.0.2-py3-none-any.whl
Size 52.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc8a3b42f32852db8c972d238f2f71f9c3c4c966438c92fd813b421604983075
BLAKE2b-256 checksum
How to use checksums
316eeb72bb2e72e45479b9701c8d11764dd83837c6748453413f5bda0ad28623
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for cignal 0.0.2

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.

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