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: Pd's m_pd.h (found in /usr/include/pd, a macOS Pd*.app, or $PD_INCLUDE).

  • For SuperCollider UGens: the SuperCollider plugin headers (/usr/include/SuperCollider, or $SC_INCLUDE).

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, ...

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 lint type-check
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/shims/          driver, Pd, SuperCollider, benchmark and flush-to-zero templates
tests/                     fixtures and the differential, host, diagnostics and codegen tests
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.

Metadata

Release files for cignal 0.0.1

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.1
File Size Uploaded Provenance Actions
cignal-0.0.1.tar.gz 62.5 kB Without provenance Details for cignal-0.0.1.tar.gz

Built distribution (wheel)

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

Total release size: 109.3 kB

Release files / cignal-0.0.1.tar.gz

Download URL cignal-0.0.1.tar.gz
Size 62.5 kB
Tags Source
SHA-256 checksum
How to use checksums
fc8c7ad8f5e2c1eae9766a76276dfa53e0d7a551ca557c989a933c99e026cfaf
BLAKE2b-256 checksum
How to use checksums
072494f39417a7a91e0e186ceed1a747afd99ff99773848d79c7fd8b1e1797cb
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.1-py3-none-any.whl

Download URL cignal-0.0.1-py3-none-any.whl
Size 46.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
123a8b131cb6c72940874c6db3467790384d55f5921656e371f93f0cf7d9a86b
BLAKE2b-256 checksum
How to use checksums
724185c3c11775d28ddbdc6563d8bba74fdd334284d43c918b9f9663d25634a8
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.1

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